Skip to content

Commit f0f5e34

Browse files
committedJul 9, 2024·
tests: Move utility functions into own source file
1 parent fa92d68 commit f0f5e34

File tree

2 files changed

+175
-164
lines changed

2 files changed

+175
-164
lines changed
 

‎controllers/objecttemplate_controller_test.go

-164
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package controllers
1919
import (
2020
"fmt"
2121
v1 "k8s.io/api/core/v1"
22-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23-
"k8s.io/apimachinery/pkg/runtime"
2422
"math/rand/v2"
2523
"sigs.k8s.io/controller-runtime/pkg/client"
2624
"time"
@@ -31,168 +29,6 @@ import (
3129
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3230
)
3331

34-
func toUnstructured(o client.Object) *unstructured.Unstructured {
35-
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(o)
36-
Expect(err).To(Succeed())
37-
return &unstructured.Unstructured{
38-
Object: m,
39-
}
40-
}
41-
42-
func buildTestConfigMap(name string, namespace string, data map[string]string) *unstructured.Unstructured {
43-
return toUnstructured(&v1.ConfigMap{
44-
TypeMeta: metav1.TypeMeta{
45-
APIVersion: "v1",
46-
Kind: "ConfigMap",
47-
},
48-
ObjectMeta: metav1.ObjectMeta{
49-
Name: name,
50-
Namespace: namespace,
51-
},
52-
Data: data,
53-
})
54-
}
55-
56-
func buildTestSecret(name string, namespace string, data map[string]string) *unstructured.Unstructured {
57-
return toUnstructured(&v1.Secret{
58-
TypeMeta: metav1.TypeMeta{
59-
APIVersion: "v1",
60-
Kind: "Secret",
61-
},
62-
ObjectMeta: metav1.ObjectMeta{
63-
Name: name,
64-
Namespace: namespace,
65-
},
66-
StringData: data,
67-
})
68-
}
69-
70-
func buildObjectTemplate(name string, namespace string, matrixEntries []templatesv1alpha1.MatrixEntry, templates []templatesv1alpha1.Template) *templatesv1alpha1.ObjectTemplate {
71-
t := &templatesv1alpha1.ObjectTemplate{
72-
TypeMeta: metav1.TypeMeta{
73-
APIVersion: templatesv1alpha1.GroupVersion.String(),
74-
Kind: "ObjectTemplate",
75-
},
76-
ObjectMeta: metav1.ObjectMeta{
77-
Name: name,
78-
Namespace: namespace,
79-
},
80-
Spec: templatesv1alpha1.ObjectTemplateSpec{
81-
Interval: metav1.Duration{Duration: time.Second},
82-
Matrix: matrixEntries,
83-
Templates: templates,
84-
},
85-
}
86-
return t
87-
}
88-
89-
func buildMatrixListEntry(name string) templatesv1alpha1.MatrixEntry {
90-
return templatesv1alpha1.MatrixEntry{
91-
Name: name,
92-
List: []runtime.RawExtension{
93-
{
94-
Raw: []byte(`{"k1": 1, "k2": 2}`),
95-
},
96-
},
97-
}
98-
}
99-
100-
func buildMatrixObjectEntry(name string, objName string, objNamespace string, objKind string, jsonPath string, expandLists bool) templatesv1alpha1.MatrixEntry {
101-
var jsonPathPtr *string
102-
if jsonPath != "" {
103-
jsonPathPtr = &jsonPath
104-
}
105-
return templatesv1alpha1.MatrixEntry{
106-
Name: name,
107-
Object: &templatesv1alpha1.MatrixEntryObject{
108-
Ref: templatesv1alpha1.ObjectRef{
109-
APIVersion: "v1",
110-
Kind: objKind,
111-
Name: objName,
112-
Namespace: objNamespace,
113-
},
114-
JsonPath: jsonPathPtr,
115-
ExpandLists: expandLists,
116-
},
117-
}
118-
}
119-
120-
func updateObjectTemplate(key client.ObjectKey, fn func(t *templatesv1alpha1.ObjectTemplate)) {
121-
t := getObjectTemplate(key)
122-
fn(t)
123-
err := k8sClient.Update(ctx, t, client.FieldOwner("tests"))
124-
Expect(err).To(Succeed())
125-
}
126-
127-
func triggerReconcile(key client.ObjectKey) {
128-
updateObjectTemplate(key, func(t *templatesv1alpha1.ObjectTemplate) {
129-
t.Spec.Interval.Duration += time.Millisecond
130-
})
131-
}
132-
133-
func waitUntiReconciled(key client.ObjectKey, timeout time.Duration) {
134-
Eventually(func() bool {
135-
t := getObjectTemplate(key)
136-
if t == nil {
137-
return false
138-
}
139-
c := getReadyCondition(t.GetConditions())
140-
if c == nil {
141-
return false
142-
}
143-
return c.ObservedGeneration == t.Generation
144-
}, timeout, time.Millisecond*250).Should(BeTrue())
145-
}
146-
147-
func getObjectTemplate(key client.ObjectKey) *templatesv1alpha1.ObjectTemplate {
148-
var t templatesv1alpha1.ObjectTemplate
149-
err := k8sClient.Get(ctx, key, &t)
150-
if err != nil {
151-
return nil
152-
}
153-
return &t
154-
}
155-
156-
func assertAppliedConfigMaps(key client.ObjectKey, keys ...client.ObjectKey) {
157-
t := getObjectTemplate(key)
158-
Expect(t).ToNot(BeNil())
159-
160-
var found []client.ObjectKey
161-
for _, as := range t.Status.AppliedResources {
162-
if as.Success {
163-
found = append(found, client.ObjectKey{Name: as.Ref.Name, Namespace: as.Ref.Namespace})
164-
}
165-
}
166-
167-
Expect(found).To(ConsistOf(keys))
168-
}
169-
170-
func assertFailedConfigMaps(key client.ObjectKey, keys ...client.ObjectKey) {
171-
t := getObjectTemplate(key)
172-
Expect(t).ToNot(BeNil())
173-
174-
var found []client.ObjectKey
175-
for _, as := range t.Status.AppliedResources {
176-
if !as.Success {
177-
found = append(found, client.ObjectKey{Name: as.Ref.Name, Namespace: as.Ref.Namespace})
178-
}
179-
}
180-
181-
Expect(found).To(ConsistOf(keys))
182-
}
183-
184-
func assertFailedConfigMap(key client.ObjectKey, cmKey client.ObjectKey, errStr string) {
185-
t := getObjectTemplate(key)
186-
Expect(t).ToNot(BeNil())
187-
for _, as := range t.Status.AppliedResources {
188-
if !as.Success && as.Ref.Name == cmKey.Name && as.Ref.Namespace == cmKey.Namespace {
189-
Expect(as.Error).To(ContainSubstring(errStr))
190-
return
191-
}
192-
}
193-
Expect(false).To(BeTrue())
194-
}
195-
19632
var _ = Describe("ObjectTemplate controller", func() {
19733
const (
19834
timeout = time.Second * 1000

‎controllers/utils_test.go

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package controllers
2+
3+
import (
4+
templatesv1alpha1 "github.com/kluctl/template-controller/api/v1alpha1"
5+
v1 "k8s.io/api/core/v1"
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
"sigs.k8s.io/controller-runtime/pkg/client"
10+
"time"
11+
12+
. "github.com/onsi/gomega"
13+
)
14+
15+
func toUnstructured(o client.Object) *unstructured.Unstructured {
16+
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(o)
17+
Expect(err).To(Succeed())
18+
return &unstructured.Unstructured{
19+
Object: m,
20+
}
21+
}
22+
23+
func buildTestConfigMap(name string, namespace string, data map[string]string) *unstructured.Unstructured {
24+
return toUnstructured(&v1.ConfigMap{
25+
TypeMeta: metav1.TypeMeta{
26+
APIVersion: "v1",
27+
Kind: "ConfigMap",
28+
},
29+
ObjectMeta: metav1.ObjectMeta{
30+
Name: name,
31+
Namespace: namespace,
32+
},
33+
Data: data,
34+
})
35+
}
36+
37+
func buildTestSecret(name string, namespace string, data map[string]string) *unstructured.Unstructured {
38+
return toUnstructured(&v1.Secret{
39+
TypeMeta: metav1.TypeMeta{
40+
APIVersion: "v1",
41+
Kind: "Secret",
42+
},
43+
ObjectMeta: metav1.ObjectMeta{
44+
Name: name,
45+
Namespace: namespace,
46+
},
47+
StringData: data,
48+
})
49+
}
50+
51+
func buildObjectTemplate(name string, namespace string, matrixEntries []templatesv1alpha1.MatrixEntry, templates []templatesv1alpha1.Template) *templatesv1alpha1.ObjectTemplate {
52+
t := &templatesv1alpha1.ObjectTemplate{
53+
TypeMeta: metav1.TypeMeta{
54+
APIVersion: templatesv1alpha1.GroupVersion.String(),
55+
Kind: "ObjectTemplate",
56+
},
57+
ObjectMeta: metav1.ObjectMeta{
58+
Name: name,
59+
Namespace: namespace,
60+
},
61+
Spec: templatesv1alpha1.ObjectTemplateSpec{
62+
Interval: metav1.Duration{Duration: time.Second},
63+
Matrix: matrixEntries,
64+
Templates: templates,
65+
},
66+
}
67+
return t
68+
}
69+
70+
func buildMatrixListEntry(name string) templatesv1alpha1.MatrixEntry {
71+
return templatesv1alpha1.MatrixEntry{
72+
Name: name,
73+
List: []runtime.RawExtension{
74+
{
75+
Raw: []byte(`{"k1": 1, "k2": 2}`),
76+
},
77+
},
78+
}
79+
}
80+
81+
func buildMatrixObjectEntry(name string, objName string, objNamespace string, objKind string, jsonPath string, expandLists bool) templatesv1alpha1.MatrixEntry {
82+
var jsonPathPtr *string
83+
if jsonPath != "" {
84+
jsonPathPtr = &jsonPath
85+
}
86+
return templatesv1alpha1.MatrixEntry{
87+
Name: name,
88+
Object: &templatesv1alpha1.MatrixEntryObject{
89+
Ref: templatesv1alpha1.ObjectRef{
90+
APIVersion: "v1",
91+
Kind: objKind,
92+
Name: objName,
93+
Namespace: objNamespace,
94+
},
95+
JsonPath: jsonPathPtr,
96+
ExpandLists: expandLists,
97+
},
98+
}
99+
}
100+
101+
func updateObjectTemplate(key client.ObjectKey, fn func(t *templatesv1alpha1.ObjectTemplate)) {
102+
t := getObjectTemplate(key)
103+
fn(t)
104+
err := k8sClient.Update(ctx, t, client.FieldOwner("tests"))
105+
Expect(err).To(Succeed())
106+
}
107+
108+
func triggerReconcile(key client.ObjectKey) {
109+
updateObjectTemplate(key, func(t *templatesv1alpha1.ObjectTemplate) {
110+
t.Spec.Interval.Duration += time.Millisecond
111+
})
112+
}
113+
114+
func waitUntiReconciled(key client.ObjectKey, timeout time.Duration) {
115+
Eventually(func() bool {
116+
t := getObjectTemplate(key)
117+
if t == nil {
118+
return false
119+
}
120+
c := getReadyCondition(t.GetConditions())
121+
if c == nil {
122+
return false
123+
}
124+
return c.ObservedGeneration == t.Generation
125+
}, timeout, time.Millisecond*250).Should(BeTrue())
126+
}
127+
128+
func getObjectTemplate(key client.ObjectKey) *templatesv1alpha1.ObjectTemplate {
129+
var t templatesv1alpha1.ObjectTemplate
130+
err := k8sClient.Get(ctx, key, &t)
131+
if err != nil {
132+
return nil
133+
}
134+
return &t
135+
}
136+
137+
func assertAppliedConfigMaps(key client.ObjectKey, keys ...client.ObjectKey) {
138+
t := getObjectTemplate(key)
139+
Expect(t).ToNot(BeNil())
140+
141+
var found []client.ObjectKey
142+
for _, as := range t.Status.AppliedResources {
143+
if as.Success {
144+
found = append(found, client.ObjectKey{Name: as.Ref.Name, Namespace: as.Ref.Namespace})
145+
}
146+
}
147+
148+
Expect(found).To(ConsistOf(keys))
149+
}
150+
151+
func assertFailedConfigMaps(key client.ObjectKey, keys ...client.ObjectKey) {
152+
t := getObjectTemplate(key)
153+
Expect(t).ToNot(BeNil())
154+
155+
var found []client.ObjectKey
156+
for _, as := range t.Status.AppliedResources {
157+
if !as.Success {
158+
found = append(found, client.ObjectKey{Name: as.Ref.Name, Namespace: as.Ref.Namespace})
159+
}
160+
}
161+
162+
Expect(found).To(ConsistOf(keys))
163+
}
164+
165+
func assertFailedConfigMap(key client.ObjectKey, cmKey client.ObjectKey, errStr string) {
166+
t := getObjectTemplate(key)
167+
Expect(t).ToNot(BeNil())
168+
for _, as := range t.Status.AppliedResources {
169+
if !as.Success && as.Ref.Name == cmKey.Name && as.Ref.Namespace == cmKey.Namespace {
170+
Expect(as.Error).To(ContainSubstring(errStr))
171+
return
172+
}
173+
}
174+
Expect(false).To(BeTrue())
175+
}

0 commit comments

Comments
 (0)
Please sign in to comment.