|
| 1 | +/* |
| 2 | +Copyright 2023 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "testing" |
| 24 | + |
| 25 | + . "github.com/onsi/gomega" |
| 26 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 29 | + |
| 30 | + kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" |
| 31 | + "github.com/fluxcd/pkg/ssa" |
| 32 | +) |
| 33 | + |
| 34 | +func Test_getFluxClusterInfo(t *testing.T) { |
| 35 | + g := NewWithT(t) |
| 36 | + f, err := os.Open("./testdata/cluster_info/gitrepositories.yaml") |
| 37 | + g.Expect(err).To(BeNil()) |
| 38 | + |
| 39 | + objs, err := ssa.ReadObjects(f) |
| 40 | + g.Expect(err).To(Not(HaveOccurred())) |
| 41 | + gitrepo := objs[0] |
| 42 | + |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + labels map[string]string |
| 46 | + wantErr bool |
| 47 | + wantBool bool |
| 48 | + wantInfo fluxClusterInfo |
| 49 | + }{ |
| 50 | + { |
| 51 | + name: "no git repository CRD present", |
| 52 | + wantBool: false, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "CRD with kustomize-controller labels", |
| 56 | + labels: map[string]string{ |
| 57 | + fmt.Sprintf("%s/name", kustomizev1.GroupVersion.Group): "flux-system", |
| 58 | + fmt.Sprintf("%s/namespace", kustomizev1.GroupVersion.Group): "flux-system", |
| 59 | + "app.kubernetes.io/version": "v2.1.0", |
| 60 | + }, |
| 61 | + wantBool: true, |
| 62 | + wantInfo: fluxClusterInfo{ |
| 63 | + version: "v2.1.0", |
| 64 | + bootstrapped: true, |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "CRD with kustomize-controller labels and managed-by label", |
| 69 | + labels: map[string]string{ |
| 70 | + fmt.Sprintf("%s/name", kustomizev1.GroupVersion.Group): "flux-system", |
| 71 | + fmt.Sprintf("%s/namespace", kustomizev1.GroupVersion.Group): "flux-system", |
| 72 | + "app.kubernetes.io/version": "v2.1.0", |
| 73 | + "app.kubernetes.io/managed-by": "flux", |
| 74 | + }, |
| 75 | + wantBool: true, |
| 76 | + wantInfo: fluxClusterInfo{ |
| 77 | + version: "v2.1.0", |
| 78 | + bootstrapped: true, |
| 79 | + managedBy: "flux", |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "CRD with only managed-by label", |
| 84 | + labels: map[string]string{ |
| 85 | + "app.kubernetes.io/version": "v2.1.0", |
| 86 | + "app.kubernetes.io/managed-by": "helm", |
| 87 | + }, |
| 88 | + wantBool: true, |
| 89 | + wantInfo: fluxClusterInfo{ |
| 90 | + version: "v2.1.0", |
| 91 | + managedBy: "helm", |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "CRD with no labels", |
| 96 | + labels: map[string]string{}, |
| 97 | + wantBool: true, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "CRD with only version label", |
| 101 | + labels: map[string]string{ |
| 102 | + "app.kubernetes.io/version": "v2.1.0", |
| 103 | + }, |
| 104 | + wantBool: true, |
| 105 | + wantInfo: fluxClusterInfo{ |
| 106 | + version: "v2.1.0", |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tt := range tests { |
| 112 | + t.Run(tt.name, func(t *testing.T) { |
| 113 | + g := NewWithT(t) |
| 114 | + newscheme := runtime.NewScheme() |
| 115 | + apiextensionsv1.AddToScheme(newscheme) |
| 116 | + builder := fake.NewClientBuilder().WithScheme(newscheme) |
| 117 | + if tt.labels != nil { |
| 118 | + gitrepo.SetLabels(tt.labels) |
| 119 | + builder = builder.WithRuntimeObjects(gitrepo) |
| 120 | + } |
| 121 | + |
| 122 | + client := builder.Build() |
| 123 | + info, present, err := getFluxClusterInfo(context.Background(), client) |
| 124 | + if tt.wantErr { |
| 125 | + g.Expect(err).To(HaveOccurred()) |
| 126 | + } |
| 127 | + |
| 128 | + g.Expect(present).To(Equal(tt.wantBool)) |
| 129 | + g.Expect(info).To(BeEquivalentTo(tt.wantInfo)) |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
0 commit comments