-
Notifications
You must be signed in to change notification settings - Fork 24
Simplify nstemplatetiers.GenerateTiers #486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ import ( | |||||
| "fmt" | ||||||
| "io/fs" | ||||||
| "path/filepath" | ||||||
| "reflect" | ||||||
| "regexp" | ||||||
| "testing" | ||||||
| texttemplate "text/template" | ||||||
|
|
@@ -17,8 +18,6 @@ import ( | |||||
| commonclient "github.com/codeready-toolchain/toolchain-common/pkg/client" | ||||||
| "github.com/codeready-toolchain/toolchain-common/pkg/test" | ||||||
| "github.com/pkg/errors" | ||||||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||||||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||
| "k8s.io/apimachinery/pkg/types" | ||||||
| runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" | ||||||
|
|
||||||
|
|
@@ -141,12 +140,10 @@ func getTestTemplates(t *testing.T) map[string][]byte { | |||||
| } | ||||||
|
|
||||||
| func TestGenerateTiers(t *testing.T) { | ||||||
|
|
||||||
| s := addToScheme(t) | ||||||
| logf.SetLogger(zap.New(zap.UseDevMode(true))) | ||||||
|
|
||||||
| t.Run("ok", func(t *testing.T) { | ||||||
|
|
||||||
| expectedTemplateRefs := map[string]map[string]interface{}{ | ||||||
| "advanced": { | ||||||
| "clusterresources": "advanced-clusterresources-abcd123-654321a", | ||||||
|
|
@@ -280,19 +277,29 @@ func TestGenerateTiers(t *testing.T) { | |||||
| // when | ||||||
| err := GenerateTiers(s, ensureObjectFuncForClient(clt), namespace, getTestMetadata(), getTestTemplates(t)) | ||||||
| require.NoError(t, err) | ||||||
| // here we're just capturing the state after the first call so that we can compare with the state after the second call | ||||||
| tierTmpls := toolchainv1alpha1.TierTemplateList{} | ||||||
| err = clt.List(context.TODO(), &tierTmpls, runtimeclient.InNamespace(namespace)) | ||||||
| require.NoError(t, err) | ||||||
| require.Len(t, tierTmpls.Items, 16) // 4 items for advanced and base tiers + 3 for nocluster tier + 4 for appstudio | ||||||
| origTemplates := map[string]*toolchainv1alpha1.TierTemplate{} | ||||||
| for _, tmpl := range tierTmpls.Items { | ||||||
| origTemplates[tmpl.Name] = &tmpl | ||||||
| } | ||||||
|
|
||||||
| // when calling CreateOrUpdateResources a second time | ||||||
| err = GenerateTiers(s, ensureObjectFuncForClient(clt), namespace, getTestMetadata(), getTestTemplates(t)) | ||||||
|
|
||||||
| // then | ||||||
| require.NoError(t, err) | ||||||
| // verify that all TierTemplate CRs were updated | ||||||
| tierTmpls := toolchainv1alpha1.TierTemplateList{} | ||||||
| tierTmpls = toolchainv1alpha1.TierTemplateList{} | ||||||
| err = clt.List(context.TODO(), &tierTmpls, runtimeclient.InNamespace(namespace)) | ||||||
| require.NoError(t, err) | ||||||
| require.Len(t, tierTmpls.Items, 16) // 4 items for advanced and base tiers + 3 for nocluster tier + 4 for appstudio | ||||||
| // check that the tier templates are unchanged | ||||||
| for _, tierTmpl := range tierTmpls.Items { | ||||||
| assert.Equal(t, int64(1), tierTmpl.Generation) // unchanged | ||||||
| assert.True(t, reflect.DeepEqual(origTemplates[tierTmpl.Name].Spec, tierTmpl.Spec)) | ||||||
|
||||||
| assert.True(t, reflect.DeepEqual(origTemplates[tierTmpl.Name].Spec, tierTmpl.Spec)) | |
| assert.Equal(t, origTemplates[tierTmpl.Name].Spec, tierTmpl.Spec) |
instead, right?
Or why would the generation change here? The tier is not going to be updated for the same template, right? Basically why you need to change the test here at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, this led me down a rabbit hole. I changed this because the generation was being changed during a single patch call, so I was suspecting our test client doing something weird.
And it indeed was weird. The actual "error" because of which the generation was changed to 2 was caused by our implementation of test.Patch (the default patching behavior on top of the fake client).
We want determine whether to increase the generation there by reading the object from the cluster, dry-running the apply on a copy of the object and then comparing the two.
Now, the "original" object comes directly from the filesystem and contains the template as "raw extension", that is read as just a []byte. When you save it to the cluster, the raw extension gets serialized for some reason.
Now, because some of the templates explicitly declared a zero value of some properties (namely ClusterResourceQuota.spec.selector.annotation) the representation didn't match what came deserialized back from the cluster (which didn't contain the zero value anymore, understandably).
To capture this in the future, I added a comment and a second check on the same place - one check is using my method of deep equal and the other is the original generation change check. If we see just one of them failing, we know where to look the next time.
Thanks for pointing this out. Took me a bit to figure this out but the test is now better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice investigation!
Uh oh!
There was an error while loading. Please reload this page.