Skip to content

Commit 0095025

Browse files
committed
Add tests
Signed-off-by: Marko Lukša <[email protected]>
1 parent 9573bec commit 0095025

File tree

7 files changed

+580
-28
lines changed

7 files changed

+580
-28
lines changed

Makefile.core.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ IMAGE ?= ${HUB}/${IMAGE_BASE}:${TAG}
7676
# Namespace to deploy the controller in
7777
NAMESPACE ?= sail-operator
7878
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
79-
ENVTEST_K8S_VERSION ?= 1.29.0
79+
ENVTEST_K8S_VERSION ?= 1.30.0
8080

8181
# Set DOCKER_BUILD_FLAGS to specify flags to pass to 'docker build', default to empty. Example: --platform=linux/arm64
8282
DOCKER_BUILD_FLAGS ?= "--platform=$(TARGET_OS)/$(TARGET_ARCH)"

api/v1/istiorevision_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ const (
205205
// Users shouldn't create IstioRevision objects directly. Instead, they should
206206
// create an Istio object and allow the operator to create the underlying
207207
// IstioRevision object(s).
208-
// +kubebuilder:validation:XValidation:rule="self.metadata.name == 'default' ? (!has(self.spec.values.revision) || size(self.spec.values.revision) == 0) : self.spec.values.revision == self.metadata.name",message="spec.values.revision must match metadata.name"
208+
// +kubebuilder:validation:XValidation:rule="self.metadata.name == 'default' ? (!has(self.spec.values.revision) || size(self.spec.values.revision) == 0) : self.spec.values.revision == self.metadata.name",message="spec.values.revision must match metadata.name or be empty when the name is 'default'"
209209
type IstioRevision struct {
210210
metav1.TypeMeta `json:",inline"`
211211
// +optional

bundle/manifests/sailoperator.io_istiorevisions.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9601,7 +9601,8 @@ spec:
96019601
type: object
96029602
type: object
96039603
x-kubernetes-validations:
9604-
- message: spec.values.revision must match metadata.name
9604+
- message: spec.values.revision must match metadata.name or be empty when
9605+
the name is 'default'
96059606
rule: 'self.metadata.name == ''default'' ? (!has(self.spec.values.revision)
96069607
|| size(self.spec.values.revision) == 0) : self.spec.values.revision ==
96079608
self.metadata.name'

chart/crds/sailoperator.io_istiorevisions.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9601,7 +9601,8 @@ spec:
96019601
type: object
96029602
type: object
96039603
x-kubernetes-validations:
9604-
- message: spec.values.revision must match metadata.name
9604+
- message: spec.values.revision must match metadata.name or be empty when
9605+
the name is 'default'
96059606
rule: 'self.metadata.name == ''default'' ? (!has(self.spec.values.revision)
96069607
|| size(self.spec.values.revision) == 0) : self.spec.values.revision ==
96079608
self.metadata.name'

pkg/helm/postrenderer_test.go

Lines changed: 166 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,153 @@ import (
2323
)
2424

2525
func TestHelmPostRenderer(t *testing.T) {
26-
postRenderer := HelmPostRenderer{
27-
ownerReference: &metav1.OwnerReference{
28-
APIVersion: "sailoperator.io/v1",
29-
Kind: "Istio",
30-
Name: "my-istio",
31-
UID: "123",
26+
testCases := []struct {
27+
name string
28+
ownerReference *metav1.OwnerReference
29+
ownerNamespace string
30+
input string
31+
expected string
32+
}{
33+
{
34+
name: "cluster-scoped owner",
35+
ownerReference: &metav1.OwnerReference{
36+
APIVersion: "sailoperator.io/v1",
37+
Kind: "Istio",
38+
Name: "my-istio",
39+
UID: "123",
40+
},
41+
ownerNamespace: "istio-system",
42+
input: `apiVersion: v1
43+
kind: Deployment
44+
metadata:
45+
name: istiod
46+
namespace: istio-system
47+
spec:
48+
replicas: 1
49+
`,
50+
expected: `apiVersion: v1
51+
kind: Deployment
52+
metadata:
53+
labels:
54+
managed-by: sail-operator
55+
name: istiod
56+
namespace: istio-system
57+
ownerReferences:
58+
- apiVersion: sailoperator.io/v1
59+
kind: Istio
60+
name: my-istio
61+
uid: "123"
62+
spec:
63+
replicas: 1
64+
`,
3265
},
33-
ownerNamespace: "istio-system",
34-
}
35-
36-
input := `---
66+
{
67+
name: "namespace-scoped owner in same namespace",
68+
ownerReference: &metav1.OwnerReference{
69+
APIVersion: "v1",
70+
Kind: "ConfigMap",
71+
Name: "my-configmap",
72+
UID: "123",
73+
},
74+
ownerNamespace: "istio-system",
75+
input: `apiVersion: v1
76+
kind: Deployment
77+
metadata:
78+
name: istiod
79+
namespace: istio-system
80+
spec:
81+
replicas: 1
82+
`,
83+
expected: `apiVersion: v1
84+
kind: Deployment
85+
metadata:
86+
labels:
87+
managed-by: sail-operator
88+
name: istiod
89+
namespace: istio-system
90+
ownerReferences:
91+
- apiVersion: v1
92+
kind: ConfigMap
93+
name: my-configmap
94+
uid: "123"
95+
spec:
96+
replicas: 1
97+
`,
98+
},
99+
{
100+
name: "namespace-scoped owner in different namespace",
101+
ownerReference: &metav1.OwnerReference{
102+
APIVersion: "v1",
103+
Kind: "ConfigMap",
104+
Name: "my-configmap",
105+
UID: "123",
106+
},
107+
ownerNamespace: "istio-system",
108+
input: `apiVersion: v1
109+
kind: Service
110+
metadata:
111+
name: some-service
112+
namespace: other-namespace
113+
labels:
114+
foo: bar
115+
spec:
116+
ports:
117+
- port: 80
118+
`,
119+
expected: `apiVersion: v1
120+
kind: Service
121+
metadata:
122+
annotations:
123+
operator-sdk/primary-resource: istio-system/my-configmap
124+
operator-sdk/primary-resource-type: ConfigMap.v1
125+
labels:
126+
foo: bar
127+
managed-by: sail-operator
128+
name: some-service
129+
namespace: other-namespace
130+
spec:
131+
ports:
132+
- port: 80
133+
`,
134+
},
135+
{
136+
name: "no owner reference",
137+
ownerReference: nil,
138+
ownerNamespace: "",
139+
input: `apiVersion: v1
140+
kind: Deployment
141+
metadata:
142+
name: istiod
143+
namespace: istio-system
144+
spec:
145+
replicas: 1
146+
`,
147+
expected: `apiVersion: v1
148+
kind: Deployment
149+
metadata:
150+
labels:
151+
managed-by: sail-operator
152+
name: istiod
153+
namespace: istio-system
154+
spec:
155+
replicas: 1
156+
`,
157+
},
158+
{
159+
name: "multiple manifests",
160+
ownerReference: &metav1.OwnerReference{
161+
APIVersion: "sailoperator.io/v1",
162+
Kind: "Istio",
163+
Name: "my-istio",
164+
UID: "123",
165+
},
166+
ownerNamespace: "istio-system",
167+
input: `
168+
---
37169
apiVersion: v1
38170
kind: Deployment
39171
metadata:
40-
name: deployment-in-same-namespace
172+
name: istiod
41173
namespace: istio-system
42174
spec:
43175
replicas: 1
@@ -48,21 +180,20 @@ spec:
48180
apiVersion: v1
49181
kind: Service
50182
metadata:
51-
name: service-in-different-namespace
183+
name: some-service
52184
namespace: other-namespace
53185
labels:
54186
foo: bar
55187
spec:
56188
ports:
57189
- port: 80
58-
`
59-
60-
expected := `apiVersion: v1
190+
`,
191+
expected: `apiVersion: v1
61192
kind: Deployment
62193
metadata:
63194
labels:
64195
managed-by: sail-operator
65-
name: deployment-in-same-namespace
196+
name: istiod
66197
namespace: istio-system
67198
ownerReferences:
68199
- apiVersion: sailoperator.io/v1
@@ -81,19 +212,30 @@ metadata:
81212
labels:
82213
foo: bar
83214
managed-by: sail-operator
84-
name: service-in-different-namespace
215+
name: some-service
85216
namespace: other-namespace
86217
spec:
87218
ports:
88219
- port: 80
89-
`
90-
91-
actual, err := postRenderer.Run(bytes.NewBufferString(input))
92-
if err != nil {
93-
t.Fatal(err)
220+
`,
221+
},
94222
}
95223

96-
if diff := cmp.Diff(expected, actual.String()); diff != "" {
97-
t.Errorf("ownerReference or managed-by label wasn't added properly; diff (-expected, +actual):\n%v", diff)
224+
for _, tc := range testCases {
225+
t.Run(tc.name, func(t *testing.T) {
226+
postRenderer := HelmPostRenderer{
227+
ownerReference: tc.ownerReference,
228+
ownerNamespace: tc.ownerNamespace,
229+
}
230+
231+
actual, err := postRenderer.Run(bytes.NewBufferString(tc.input))
232+
if err != nil {
233+
t.Fatal(err)
234+
}
235+
236+
if diff := cmp.Diff(tc.expected, actual.String()); diff != "" {
237+
t.Errorf("ownerReference or managed-by label wasn't added properly; diff (-expected, +actual):\n%v", diff)
238+
}
239+
})
98240
}
99241
}

0 commit comments

Comments
 (0)