Skip to content

Commit 39d3a88

Browse files
authored
Merge pull request #6735 from sayantani11/TestInfrastructureMachineTemplateBuilder
🌱 Make TestInfrastructureMachineTemplateBuilder deepcopy-able
2 parents 7809ffb + 0a1972e commit 39d3a88

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

internal/test/builder/builders.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -445,18 +445,26 @@ func (i *InfrastructureMachineTemplateBuilder) Build() *unstructured.Unstructure
445445
}
446446

447447
// TestInfrastructureMachineTemplateBuilder holds the variables and objects needed to build an TestInfrastructureMachineTemplate.
448-
// +kubebuilder:object:generate=false
449448
type TestInfrastructureMachineTemplateBuilder struct {
450-
namespace string
451-
name string
452-
specFields map[string]interface{}
449+
obj *unstructured.Unstructured
453450
}
454451

455452
// TestInfrastructureMachineTemplate creates an TestInfrastructureMachineTemplateBuilder with the given name and namespace.
456453
func TestInfrastructureMachineTemplate(namespace, name string) *TestInfrastructureMachineTemplateBuilder {
454+
obj := &unstructured.Unstructured{}
455+
obj.SetName(name)
456+
obj.SetNamespace(namespace)
457+
obj.SetAPIVersion(InfrastructureGroupVersion.String())
458+
obj.SetKind(TestInfrastructureMachineTemplateKind)
459+
// Set the mandatory spec fields for the object.
460+
if err := unstructured.SetNestedField(obj.Object, map[string]interface{}{}, "spec"); err != nil {
461+
panic(err)
462+
}
463+
if err := unstructured.SetNestedField(obj.Object, map[string]interface{}{}, "spec", "template", "spec"); err != nil {
464+
panic(err)
465+
}
457466
return &TestInfrastructureMachineTemplateBuilder{
458-
namespace: namespace,
459-
name: name,
467+
obj,
460468
}
461469
}
462470

@@ -469,24 +477,13 @@ func TestInfrastructureMachineTemplate(namespace, name string) *TestInfrastructu
469477
// "spec.version": "v1.2.3",
470478
// }.
471479
func (i *TestInfrastructureMachineTemplateBuilder) WithSpecFields(fields map[string]interface{}) *TestInfrastructureMachineTemplateBuilder {
472-
i.specFields = fields
480+
setSpecFields(i.obj, fields)
473481
return i
474482
}
475483

476484
// Build takes the objects and variables in the InfrastructureMachineTemplateBuilder and generates an unstructured object.
477485
func (i *TestInfrastructureMachineTemplateBuilder) Build() *unstructured.Unstructured {
478-
obj := &unstructured.Unstructured{}
479-
obj.SetAPIVersion(InfrastructureGroupVersion.String())
480-
obj.SetKind(TestInfrastructureMachineTemplateKind)
481-
obj.SetNamespace(i.namespace)
482-
obj.SetName(i.name)
483-
484-
// Initialize the spec.template.spec to make the object valid in reconciliation.
485-
setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}})
486-
487-
setSpecFields(obj, i.specFields)
488-
489-
return obj
486+
return i.obj
490487
}
491488

492489
// BootstrapTemplateBuilder holds the variables needed to build a generic BootstrapTemplate.

internal/test/builder/zz_generated.deepcopy.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)