|
| 1 | +package clusterpath |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "testing" |
| 6 | + |
| 7 | + kcpcore "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + "k8s.io/client-go/rest" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 13 | +) |
| 14 | + |
| 15 | +func TestResolver(t *testing.T) { |
| 16 | + tests := map[string]struct { |
| 17 | + baseConfig *rest.Config |
| 18 | + clusterName string |
| 19 | + expectErr bool |
| 20 | + }{ |
| 21 | + "valid_cluster": {baseConfig: &rest.Config{}, clusterName: "test-cluster", expectErr: false}, |
| 22 | + "nil_base_config": {baseConfig: nil, clusterName: "test-cluster", expectErr: true}, |
| 23 | + } |
| 24 | + |
| 25 | + for name, tc := range tests { |
| 26 | + t.Run(name, func(t *testing.T) { |
| 27 | + resolver := &Resolver{ |
| 28 | + Scheme: runtime.NewScheme(), |
| 29 | + Config: tc.baseConfig, |
| 30 | + clientFactory: func(config *rest.Config, options client.Options) (client.Client, error) { |
| 31 | + return fake.NewClientBuilder().WithScheme(options.Scheme).Build(), nil |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + client, err := resolver.ClientForCluster(tc.clusterName) |
| 36 | + if tc.expectErr { |
| 37 | + assert.Error(t, err) |
| 38 | + return |
| 39 | + } |
| 40 | + assert.NoError(t, err) |
| 41 | + assert.NotNil(t, client) |
| 42 | + |
| 43 | + }) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestPathForCluster(t *testing.T) { |
| 48 | + scheme := runtime.NewScheme() |
| 49 | + err := kcpcore.AddToScheme(scheme) |
| 50 | + assert.NoError(t, err) |
| 51 | + tests := map[string]struct { |
| 52 | + clusterName string |
| 53 | + annotations map[string]string |
| 54 | + expectErr bool |
| 55 | + expectedPath string |
| 56 | + }{ |
| 57 | + "root_cluster": { |
| 58 | + clusterName: "root", |
| 59 | + annotations: nil, |
| 60 | + expectErr: false, |
| 61 | + expectedPath: "root", |
| 62 | + }, |
| 63 | + "valid_cluster_with_1st_level_path": { |
| 64 | + clusterName: "sap", |
| 65 | + annotations: map[string]string{"kcp.io/path": "root:sap"}, |
| 66 | + expectErr: false, |
| 67 | + expectedPath: "root:sap", |
| 68 | + }, |
| 69 | + "valid_cluster_with_2nd_level_path": { |
| 70 | + clusterName: "openmfp", |
| 71 | + annotations: map[string]string{"kcp.io/path": "root:sap:openmfp"}, |
| 72 | + expectErr: false, |
| 73 | + expectedPath: "root:sap:openmfp", |
| 74 | + }, |
| 75 | + "missing_annotation": { |
| 76 | + clusterName: "test-cluster", |
| 77 | + annotations: map[string]string{}, |
| 78 | + expectErr: true, |
| 79 | + }, |
| 80 | + "nil_annotation": { |
| 81 | + clusterName: "test-cluster", |
| 82 | + annotations: nil, |
| 83 | + expectErr: true, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + for name, tc := range tests { |
| 88 | + t.Run(name, func(t *testing.T) { |
| 89 | + builder := fake.NewClientBuilder().WithScheme(scheme) |
| 90 | + if tc.annotations != nil { |
| 91 | + lc := &kcpcore.LogicalCluster{} |
| 92 | + lc.SetName("cluster") |
| 93 | + lc.SetAnnotations(tc.annotations) |
| 94 | + builder = builder.WithObjects(lc) |
| 95 | + } |
| 96 | + clt := builder.Build() |
| 97 | + |
| 98 | + path, err := PathForCluster(tc.clusterName, clt) |
| 99 | + if tc.expectErr { |
| 100 | + assert.Error(t, err) |
| 101 | + assert.Empty(t, path) |
| 102 | + return |
| 103 | + } |
| 104 | + assert.NoError(t, err) |
| 105 | + assert.Equal(t, tc.expectedPath, path) |
| 106 | + |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestGetClusterConfig(t *testing.T) { |
| 112 | + tests := map[string]struct { |
| 113 | + cfg *rest.Config |
| 114 | + cluster string |
| 115 | + expect *rest.Config |
| 116 | + expectErr bool |
| 117 | + }{ |
| 118 | + "nil_config": { |
| 119 | + cfg: nil, |
| 120 | + cluster: "openmfp", |
| 121 | + expect: nil, |
| 122 | + expectErr: true, |
| 123 | + }, |
| 124 | + "valid_config": { |
| 125 | + cfg: &rest.Config{Host: "https://127.0.0.1:56120/clusters/root"}, |
| 126 | + cluster: "openmfp", |
| 127 | + expect: &rest.Config{Host: "https://127.0.0.1:56120/clusters/openmfp"}, |
| 128 | + expectErr: false, |
| 129 | + }, |
| 130 | + "invalid_URL": { |
| 131 | + cfg: &rest.Config{Host: ":://bad-url"}, |
| 132 | + cluster: "openmfp", |
| 133 | + expect: nil, |
| 134 | + expectErr: true, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + for name, tc := range tests { |
| 139 | + t.Run(name, func(t *testing.T) { |
| 140 | + got, err := getClusterConfig(tc.cluster, tc.cfg) |
| 141 | + if tc.expectErr { |
| 142 | + assert.Error(t, err) |
| 143 | + return |
| 144 | + } |
| 145 | + assert.NoError(t, err) |
| 146 | + assert.NotNil(t, got) |
| 147 | + assert.Equal(t, tc.expect.Host, got.Host) |
| 148 | + parsedURL, err1 := url.Parse(got.Host) |
| 149 | + assert.NoError(t, err1) |
| 150 | + assert.NotEmpty(t, parsedURL) |
| 151 | + expectedURL, err2 := url.Parse(tc.expect.Host) |
| 152 | + assert.NoError(t, err2) |
| 153 | + assert.NotEmpty(t, expectedURL) |
| 154 | + assert.Equal(t, expectedURL, parsedURL) |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
0 commit comments