Skip to content

Commit 11cfb37

Browse files
committed
be clever about detecting the generation change and try to detect possible test problems with the test templates
1 parent cf062f9 commit 11cfb37

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

pkg/template/nstemplatetiers/nstemplatetier_generator_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func TestGenerateTiers(t *testing.T) {
284284
require.Len(t, tierTmpls.Items, 16) // 4 items for advanced and base tiers + 3 for nocluster tier + 4 for appstudio
285285
origTemplates := map[string]*toolchainv1alpha1.TierTemplate{}
286286
for _, tmpl := range tierTmpls.Items {
287-
origTemplates[tmpl.Name] = &tmpl
287+
origTemplates[tmpl.Name] = tmpl.DeepCopy()
288288
}
289289

290290
// when calling CreateOrUpdateResources a second time
@@ -299,10 +299,13 @@ func TestGenerateTiers(t *testing.T) {
299299
require.Len(t, tierTmpls.Items, 16) // 4 items for advanced and base tiers + 3 for nocluster tier + 4 for appstudio
300300
// check that the tier templates are unchanged
301301
for _, tierTmpl := range tierTmpls.Items {
302-
assert.True(t, reflect.DeepEqual(origTemplates[tierTmpl.Name].Spec, tierTmpl.Spec))
303-
}
304-
305-
// verify that 4 NSTemplateTier CRs were created:
302+
// these two should always mean the same thing. If they don't, it means there's an issue with serde of
303+
// the templates where template json -> object in cluster -> template json doesn't yield the same thing.
304+
// (the json reprentation is used to detect generation change in our test.Client).
305+
// This is usually caused by e.g explicitly using a zero value of some property in the template file.
306+
assert.True(t, reflect.DeepEqual(origTemplates[tierTmpl.Name].Spec, tierTmpl.Spec), "deep equal different on %T %s", tierTmpl, tierTmpl.Name)
307+
assert.Equal(t, int64(1), tierTmpl.Generation, "generation different on %T %s", tierTmpl, tierTmpl.Name) // unchanged
308+
} // verify that 4 NSTemplateTier CRs were created:
306309
for _, tierName := range []string{"advanced", "base", "nocluster", "appstudio"} {
307310
tier := toolchainv1alpha1.NSTemplateTier{}
308311
err = clt.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: tierName}, &tier)

pkg/template/nstemplatetiers/testdata/nstemplatetiers/appstudio/cluster.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ objects:
1919
requests.ephemeral-storage: 7Gi
2020
count/persistentvolumeclaims: "5"
2121
selector:
22-
annotations: null
2322
labels:
2423
matchLabels:
2524
toolchain.dev.openshift.com/space: ${SPACE_NAME}

pkg/template/nstemplatetiers/testdata/nstemplatetiers/base/cluster.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ objects:
1717
requests.storage: 7Gi
1818
persistentvolumeclaims: "5"
1919
selector:
20-
annotations: null
2120
labels:
2221
matchLabels:
2322
toolchain.dev.openshift.com/space: ${SPACE_NAME}
2423
parameters:
2524
- name: SPACE_NAME
2625
required: true
2726
- name: CPU_LIMIT
28-
value: 4000m
27+
value: 4000m

pkg/test/client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"k8s.io/client-go/kubernetes/scheme"
1616
"sigs.k8s.io/controller-runtime/pkg/client"
1717
"sigs.k8s.io/controller-runtime/pkg/client/fake" //nolint: staticcheck // not deprecated anymore: see https://github.com/kubernetes-sigs/controller-runtime/pull/1101
18+
kjson "sigs.k8s.io/json"
1819
)
1920

2021
// NewFakeClient creates a fake K8s client with ability to override specific Get/List/Create/Update/StatusUpdate/Delete functions
@@ -201,7 +202,10 @@ func toMap(obj runtime.Object) (map[string]interface{}, error) {
201202
return nil, err
202203
}
203204
m := map[string]interface{}{}
204-
if err := json.Unmarshal(content, &m); err != nil {
205+
if serr, err := kjson.UnmarshalStrict(content, &m); err != nil || len(serr) > 0 {
206+
if err == nil {
207+
err = fmt.Errorf("strict unmarshalling errors: %v", serr)
208+
}
205209
return nil, err
206210
}
207211

0 commit comments

Comments
 (0)