77 "fmt"
88 "io/fs"
99 "path/filepath"
10+ "reflect"
1011 "regexp"
1112 "testing"
1213 texttemplate "text/template"
@@ -17,8 +18,6 @@ import (
1718 commonclient "github.com/codeready-toolchain/toolchain-common/pkg/client"
1819 "github.com/codeready-toolchain/toolchain-common/pkg/test"
1920 "github.com/pkg/errors"
20- apierrors "k8s.io/apimachinery/pkg/api/errors"
21- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2221 "k8s.io/apimachinery/pkg/types"
2322 runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
2423
@@ -141,12 +140,10 @@ func getTestTemplates(t *testing.T) map[string][]byte {
141140}
142141
143142func TestGenerateTiers (t * testing.T ) {
144-
145143 s := addToScheme (t )
146144 logf .SetLogger (zap .New (zap .UseDevMode (true )))
147145
148146 t .Run ("ok" , func (t * testing.T ) {
149-
150147 expectedTemplateRefs := map [string ]map [string ]interface {}{
151148 "advanced" : {
152149 "clusterresources" : "advanced-clusterresources-abcd123-654321a" ,
@@ -280,19 +277,29 @@ func TestGenerateTiers(t *testing.T) {
280277 // when
281278 err := GenerateTiers (s , ensureObjectFuncForClient (clt ), namespace , getTestMetadata (), getTestTemplates (t ))
282279 require .NoError (t , err )
280+ // here we're just capturing the state after the first call so that we can compare with the state after the second call
281+ tierTmpls := toolchainv1alpha1.TierTemplateList {}
282+ err = clt .List (context .TODO (), & tierTmpls , runtimeclient .InNamespace (namespace ))
283+ require .NoError (t , err )
284+ require .Len (t , tierTmpls .Items , 16 ) // 4 items for advanced and base tiers + 3 for nocluster tier + 4 for appstudio
285+ origTemplates := map [string ]* toolchainv1alpha1.TierTemplate {}
286+ for _ , tmpl := range tierTmpls .Items {
287+ origTemplates [tmpl .Name ] = & tmpl
288+ }
283289
284290 // when calling CreateOrUpdateResources a second time
285291 err = GenerateTiers (s , ensureObjectFuncForClient (clt ), namespace , getTestMetadata (), getTestTemplates (t ))
286292
287293 // then
288294 require .NoError (t , err )
289295 // verify that all TierTemplate CRs were updated
290- tierTmpls : = toolchainv1alpha1.TierTemplateList {}
296+ tierTmpls = toolchainv1alpha1.TierTemplateList {}
291297 err = clt .List (context .TODO (), & tierTmpls , runtimeclient .InNamespace (namespace ))
292298 require .NoError (t , err )
293299 require .Len (t , tierTmpls .Items , 16 ) // 4 items for advanced and base tiers + 3 for nocluster tier + 4 for appstudio
300+ // check that the tier templates are unchanged
294301 for _ , tierTmpl := range tierTmpls .Items {
295- assert .Equal (t , int64 ( 1 ) , tierTmpl .Generation ) // unchanged
302+ assert .True (t , reflect . DeepEqual ( origTemplates [ tierTmpl . Name ]. Spec , tierTmpl .Spec ))
296303 }
297304
298305 // verify that 4 NSTemplateTier CRs were created:
@@ -446,27 +453,25 @@ func TestGenerateTiers(t *testing.T) {
446453 })
447454
448455 t .Run ("failures" , func (t * testing.T ) {
449-
450456 namespace := "host-operator" + uuid .NewString ()[:7 ]
451457
452458 t .Run ("nstemplatetiers" , func (t * testing.T ) {
453-
454- t .Run ("failed to create nstemplatetiers" , func (t * testing.T ) {
459+ t .Run ("failed to patch nstemplatetiers" , func (t * testing.T ) {
455460 // given
456461 clt := test .NewFakeClient (t )
457- clt .MockCreate = func (ctx context.Context , obj runtimeclient.Object , opts ... runtimeclient.CreateOption ) error {
458- if obj .GetObjectKind (). GroupVersionKind (). Kind == "NSTemplateTier" {
462+ clt .MockPatch = func (ctx context.Context , obj runtimeclient.Object , patch runtimeclient. Patch , opts ... runtimeclient.PatchOption ) error {
463+ if _ , ok := obj .( * toolchainv1alpha1. NSTemplateTier ); ok {
459464 // simulate a client/server error
460465 return errors .Errorf ("an error" )
461466 }
462- return clt . Client . Create (ctx , obj , opts ... )
467+ return test . Patch (ctx , clt , obj , patch , opts ... )
463468 }
464469
465470 // when
466471 err := GenerateTiers (s , ensureObjectFuncForClient (clt ), namespace , getTestMetadata (), getTestTemplates (t ))
467472 // then
468473 require .Error (t , err )
469- assert .Regexp (t , "unable to create or update the '\\ w+' NSTemplateTier: unable to create resource of kind: NSTemplateTier, version: v1alpha1 : an error" , err .Error ())
474+ assert .Regexp (t , "unable to create NSTemplateTiers: unable to create or update the '\\ w+' NSTemplateTier: unable to patch 'toolchain.dev.openshift.com/v1alpha1, Kind=NSTemplateTier' called ' \\ w+' in namespace '[a-zA-Z0-9-]+' : an error" , err .Error ())
470475 })
471476
472477 t .Run ("missing tier.yaml file" , func (t * testing.T ) {
@@ -481,40 +486,15 @@ func TestGenerateTiers(t *testing.T) {
481486 require .EqualError (t , err , "unable to init NSTemplateTier generator: tier appstudio is missing a tier.yaml file" )
482487 })
483488
484- t .Run ("failed to update nstemplatetiers" , func (t * testing.T ) {
485- // given
486- // initialize the client with an existing `advanced` NSTemplatetier
487- clt := test .NewFakeClient (t , & toolchainv1alpha1.NSTemplateTier {
488- ObjectMeta : metav1.ObjectMeta {
489- Namespace : namespace ,
490- Name : "advanced" ,
491- },
492- })
493- clt .MockUpdate = func (ctx context.Context , obj runtimeclient.Object , opts ... runtimeclient.UpdateOption ) error {
494- if obj .GetObjectKind ().GroupVersionKind ().Kind == "NSTemplateTier" {
495- // simulate a client/server error
496- return errors .Errorf ("an error" )
497- }
498- return clt .Client .Update (ctx , obj , opts ... )
499- }
500-
501- // when
502- err := GenerateTiers (s , ensureObjectFuncForClient (clt ), namespace , getTestMetadata (), getTestTemplates (t ))
503-
504- // then
505- require .Error (t , err )
506- assert .Contains (t , err .Error (), "unable to create NSTemplateTiers: unable to create or update the 'advanced' NSTemplateTier: unable to create resource of kind: NSTemplateTier, version: v1alpha1: unable to update the resource" )
507- })
508-
509- t .Run ("failed to create nstemplatetiers" , func (t * testing.T ) {
489+ t .Run ("failed to patch nstemplatetiers" , func (t * testing.T ) {
510490 // given
511491 clt := test .NewFakeClient (t )
512- clt .MockCreate = func (ctx context.Context , obj runtimeclient.Object , opts ... runtimeclient.CreateOption ) error {
492+ clt .MockPatch = func (ctx context.Context , obj runtimeclient.Object , patch runtimeclient. Patch , opts ... runtimeclient.PatchOption ) error {
513493 if _ , ok := obj .(* toolchainv1alpha1.TierTemplate ); ok {
514494 // simulate a client/server error
515495 return errors .Errorf ("an error" )
516496 }
517- return clt . Client . Create (ctx , obj , opts ... )
497+ return test . Patch (ctx , clt , obj , patch , opts ... )
518498 }
519499
520500 // when
@@ -529,7 +509,6 @@ func TestGenerateTiers(t *testing.T) {
529509}
530510
531511func TestLoadTemplatesByTiers (t * testing.T ) {
532-
533512 logf .SetLogger (zap .New (zap .UseDevMode (true )))
534513
535514 t .Run ("ok" , func (t * testing.T ) {
@@ -591,7 +570,6 @@ func TestLoadTemplatesByTiers(t *testing.T) {
591570 })
592571
593572 t .Run ("failures" , func (t * testing.T ) {
594-
595573 t .Run ("unparseable content" , func (t * testing.T ) {
596574 // given
597575 testTemplates := getTestTemplates (t )
@@ -663,13 +641,11 @@ func TestLoadTemplatesByTiers(t *testing.T) {
663641}
664642
665643func TestNewNSTemplateTier (t * testing.T ) {
666-
667644 s := scheme .Scheme
668645 err := toolchainv1alpha1 .AddToScheme (s )
669646 require .NoError (t , err )
670647
671648 t .Run ("ok" , func (t * testing.T ) {
672-
673649 t .Run ("with test assets" , func (t * testing.T ) {
674650 // given
675651 namespace := "host-operator-" + uuid .NewString ()[:7 ]
@@ -740,7 +716,6 @@ func TestNewTierTemplate(t *testing.T) {
740716 namespace := "host-operator-" + uuid .NewString ()[:7 ]
741717
742718 t .Run ("ok" , func (t * testing.T ) {
743-
744719 t .Run ("with test assets" , func (t * testing.T ) {
745720 // given
746721
@@ -780,7 +755,6 @@ func TestNewTierTemplate(t *testing.T) {
780755 })
781756
782757 t .Run ("failures" , func (t * testing.T ) {
783-
784758 t .Run ("invalid template" , func (t * testing.T ) {
785759 // given
786760 testTemplates := getTestTemplates (t )
@@ -796,15 +770,9 @@ func TestNewTierTemplate(t *testing.T) {
796770}
797771
798772func ensureObjectFuncForClient (cl runtimeclient.Client ) EnsureObject {
799- return func (toEnsure runtimeclient.Object , canUpdate bool , _ string ) (bool , error ) {
800- if ! canUpdate {
801- if err := cl .Create (context .TODO (), toEnsure ); err != nil && ! apierrors .IsAlreadyExists (err ) {
802- return false , err
803- }
804- return true , nil
805- }
806- applyCl := commonclient .NewApplyClient (cl )
807- return applyCl .ApplyObject (context .TODO (), toEnsure , commonclient .ForceUpdate (true ))
773+ return func (toEnsure runtimeclient.Object , _ string ) error {
774+ applyCl := commonclient .NewSSAApplyClient (cl , "testFieldManager" )
775+ return applyCl .ApplyObject (context .TODO (), toEnsure )
808776 }
809777}
810778
@@ -862,7 +830,6 @@ func expectedTemplateFromBasedOnTierConfig(t *testing.T, tier, templateFileName
862830}
863831
864832func TestNewNSTemplateTiers (t * testing.T ) {
865-
866833 // given
867834 s := addToScheme (t )
868835
0 commit comments