@@ -85,24 +85,40 @@ func TestReconcileNewClusterClass(t *testing.T) {
8585 targetNamespaces , err := createTargetNamespaces (3 )
8686 g .Expect (err ).ToNot (HaveOccurred ())
8787
88- sourceClusterClassName , _ , cleanup , err := createUniqueClusterClassAndTemplates (
89- sourceClusterClassNamespace ,
90- )
91- g .Expect (err ).ToNot (HaveOccurred ())
92- defer func () {
93- g .Expect (cleanup ()).To (Succeed ())
94- }()
95-
96- for _ , targetNamespace := range targetNamespaces {
97- g .Eventually (func () error {
98- return verifyClusterClassAndTemplates (
99- env .Client ,
100- sourceClusterClassName ,
101- targetNamespace .Name ,
102- )
88+ testCases := []struct {
89+ name string
90+ create func (namespace string ) (string , []client.Object , func () error , error )
91+ }{
92+ {
93+ name : "cluster class" ,
94+ create : createUniqueClusterClassAndTemplates ,
10395 },
104- timeout ,
105- ).Should (Succeed ())
96+ {
97+ name : "managed cluster class" ,
98+ create : createUniqueManagedClusterClassAndTemplates ,
99+ },
100+ }
101+
102+ for _ , tc := range testCases {
103+ t .Run (tc .name , func (t * testing.T ) {
104+ g := NewWithT (t )
105+ sourceClusterClassName , _ , cleanup , err := tc .create (sourceClusterClassNamespace )
106+ g .Expect (err ).ToNot (HaveOccurred ())
107+ defer func () {
108+ g .Expect (cleanup ()).To (Succeed ())
109+ }()
110+ for _ , targetNamespace := range targetNamespaces {
111+ g .Eventually (func () error {
112+ return verifyClusterClassAndTemplates (
113+ env .Client ,
114+ sourceClusterClassName ,
115+ targetNamespace .Name ,
116+ )
117+ },
118+ timeout ,
119+ ).Should (Succeed ())
120+ }
121+ })
106122 }
107123}
108124
@@ -292,6 +308,89 @@ func createClusterClassAndTemplates(
292308 return clusterClass .Name , templates , cleanup , nil
293309}
294310
311+ func createUniqueManagedClusterClassAndTemplates (namespace string ) (
312+ clusterClassName string ,
313+ templates []client.Object ,
314+ cleanup func () error ,
315+ err error ,
316+ ) {
317+ return createManagedClusterClassAndTemplates (
318+ names .SimpleNameGenerator .GenerateName ("test-managed-" ),
319+ namespace ,
320+ )
321+ }
322+
323+ // createManagedClusterClassAndTemplates creates a ClusterClass with a ControlPlane that does not have a MachineInfrastructure reference.
324+ func createManagedClusterClassAndTemplates (
325+ prefix ,
326+ namespace string ,
327+ ) (
328+ clusterClassName string ,
329+ templates []client.Object ,
330+ cleanup func () error ,
331+ err error ,
332+ ) {
333+ // The below objects are created in order to feed the reconcile loop all the information it needs to create a
334+ // full tree of ClusterClass objects (the objects should have owner references to the ClusterClass).
335+
336+ // Bootstrap templates for the workers.
337+ bootstrapTemplate := builder .BootstrapTemplate (namespace , prefix ).Build ()
338+
339+ // InfraMachineTemplates for the workers
340+ infraMachineTemplateWorker := builder .InfrastructureMachineTemplate (
341+ namespace ,
342+ fmt .Sprintf ("%s-worker" , prefix ),
343+ ).Build ()
344+
345+ // Control plane template.
346+ controlPlaneTemplate := builder .ControlPlaneTemplate (namespace , prefix ).Build ()
347+
348+ // InfraClusterTemplate.
349+ infraClusterTemplate := builder .InfrastructureClusterTemplate (namespace , prefix ).Build ()
350+
351+ // MachineDeploymentClasses that will be part of the ClusterClass.
352+ machineDeploymentClass := builder .MachineDeploymentClass (fmt .Sprintf ("%s-worker" , prefix )).
353+ WithBootstrapTemplate (bootstrapTemplate ).
354+ WithInfrastructureTemplate (infraMachineTemplateWorker ).
355+ Build ()
356+
357+ // ClusterClass.
358+ clusterClass := builder .ClusterClass (namespace , prefix ).
359+ WithInfrastructureClusterTemplate (infraClusterTemplate ).
360+ WithControlPlaneTemplate (controlPlaneTemplate ).
361+ WithWorkerMachineDeploymentClasses (* machineDeploymentClass ).
362+ Build ()
363+
364+ // Create the set of initObjects from the objects above to add to the API server when the test environment starts.
365+
366+ templates = []client.Object {
367+ bootstrapTemplate ,
368+ infraMachineTemplateWorker ,
369+ controlPlaneTemplate ,
370+ infraClusterTemplate ,
371+ }
372+
373+ for _ , obj := range templates {
374+ if err := env .CreateAndWait (ctx , obj ); err != nil {
375+ return "" , nil , nil , err
376+ }
377+ }
378+ if err := env .CreateAndWait (ctx , clusterClass ); err != nil {
379+ return "" , nil , nil , err
380+ }
381+
382+ cleanup = func () error {
383+ for _ , obj := range templates {
384+ if err := env .CleanupAndWait (ctx , obj ); err != nil {
385+ return err
386+ }
387+ }
388+ return env .CleanupAndWait (ctx , clusterClass )
389+ }
390+
391+ return clusterClass .Name , templates , cleanup , nil
392+ }
393+
295394func createTargetNamespaces (number int ) ([]* corev1.Namespace , error ) {
296395 targetNamespaces := []* corev1.Namespace {}
297396 for i := 0 ; i < number ; i ++ {
0 commit comments