Skip to content

Commit 03d6487

Browse files
authored
Merge pull request #5987 from killianmuldoon/fix/builder-test
🐛 inline builders in test for MHC reconcilation
2 parents 4c4b47d + a55b48b commit 03d6487

File tree

5 files changed

+371
-61
lines changed

5 files changed

+371
-61
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ generate-go-deepcopy-core: $(CONTROLLER_GEN) ## Generate deepcopy go code for co
206206
paths=./api/... \
207207
paths=./$(EXP_DIR)/api/... \
208208
paths=./$(EXP_DIR)/addons/api/... \
209-
paths=./cmd/clusterctl/...
209+
paths=./cmd/clusterctl/... \
210+
paths=./internal/test/builder/...
210211

211212
.PHONY: generate-go-deepcopy-kubeadm-bootstrap
212213
generate-go-deepcopy-kubeadm-bootstrap: $(CONTROLLER_GEN) ## Generate deepcopy go code for kubeadm bootstrap

internal/controllers/topology/cluster/reconcile_state_test.go

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,7 +1775,6 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) {
17751775
Build()
17761776

17771777
maxUnhealthy := intstr.Parse("45%")
1778-
// TODO: (killianmuldoon) This builder should be copied and not just passed around.
17791778
mhcBuilder := builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
17801779
WithSelector(*selectorForMachineDeploymentMHC(md)).
17811780
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
@@ -1802,12 +1801,10 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) {
18021801
current: nil,
18031802
desired: []*scope.MachineDeploymentState{
18041803
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1805-
mhcBuilder.Build()),
1804+
mhcBuilder.DeepCopy().Build()),
18061805
},
18071806
want: []*clusterv1.MachineHealthCheck{
1808-
mhcBuilder.
1809-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1810-
Build()},
1807+
mhcBuilder.DeepCopy().Build()},
18111808
},
18121809
{
18131810
name: "Create a new MachineHealthCheck if the MachineDeployment is modified to include one",
@@ -1817,82 +1814,52 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) {
18171814
// MHC is added in the desired state of the MachineDeployment
18181815
desired: []*scope.MachineDeploymentState{
18191816
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1820-
mhcBuilder.Build()),
1817+
mhcBuilder.DeepCopy().Build()),
18211818
},
18221819
want: []*clusterv1.MachineHealthCheck{
1823-
mhcBuilder.
1824-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1825-
Build()}},
1820+
mhcBuilder.DeepCopy().Build()}},
18261821
{
18271822
name: "Update MachineHealthCheck spec adding a field if the spec adds a field",
18281823
current: []*scope.MachineDeploymentState{
1829-
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, mhcBuilder.Build()),
1824+
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1825+
mhcBuilder.DeepCopy().Build()),
18301826
},
18311827
desired: []*scope.MachineDeploymentState{
18321828
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1833-
mhcBuilder.WithMaxUnhealthy(&maxUnhealthy).Build())},
1829+
mhcBuilder.DeepCopy().WithMaxUnhealthy(&maxUnhealthy).Build())},
18341830
want: []*clusterv1.MachineHealthCheck{
1835-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1836-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1837-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1838-
{
1839-
Type: corev1.NodeReady,
1840-
Status: corev1.ConditionUnknown,
1841-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1842-
},
1843-
}).
1831+
mhcBuilder.DeepCopy().
18441832
WithMaxUnhealthy(&maxUnhealthy).
1845-
WithClusterName("cluster1").
1846-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
18471833
Build()},
18481834
},
18491835
{
18501836
name: "Update MachineHealthCheck spec removing a field if the spec removes a field",
18511837
current: []*scope.MachineDeploymentState{
18521838
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1853-
mhcBuilder.WithMaxUnhealthy(&maxUnhealthy).Build()),
1839+
mhcBuilder.DeepCopy().WithMaxUnhealthy(&maxUnhealthy).Build()),
18541840
},
18551841
desired: []*scope.MachineDeploymentState{
18561842
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1857-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1858-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1859-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1860-
{
1861-
Type: corev1.NodeReady,
1862-
Status: corev1.ConditionUnknown,
1863-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1864-
},
1865-
}).
1866-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1867-
WithClusterName("cluster1").
1868-
Build()),
1843+
mhcBuilder.DeepCopy().Build()),
18691844
},
18701845
want: []*clusterv1.MachineHealthCheck{
1871-
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
1872-
WithSelector(*selectorForMachineDeploymentMHC(md)).
1873-
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1874-
{
1875-
Type: corev1.NodeReady,
1876-
Status: corev1.ConditionUnknown,
1877-
Timeout: metav1.Duration{Duration: 5 * time.Minute},
1878-
},
1879-
}).
1880-
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
1881-
WithClusterName("cluster1").
1882-
Build()},
1846+
mhcBuilder.DeepCopy().Build(),
1847+
},
18831848
},
18841849
{
18851850
name: "Delete MachineHealthCheck spec if the MachineDeployment is modified to remove an existing one",
18861851
current: []*scope.MachineDeploymentState{
1887-
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, mhcBuilder.Build()),
1852+
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1853+
mhcBuilder.DeepCopy().Build()),
18881854
},
18891855
desired: []*scope.MachineDeploymentState{newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, nil)},
18901856
want: []*clusterv1.MachineHealthCheck{},
18911857
},
18921858
{
18931859
name: "Delete MachineHealthCheck spec if the MachineDeployment is deleted",
18941860
current: []*scope.MachineDeploymentState{
1895-
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, mhcBuilder.Build()),
1861+
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
1862+
mhcBuilder.DeepCopy().Build()),
18961863
},
18971864
desired: []*scope.MachineDeploymentState{},
18981865
want: []*clusterv1.MachineHealthCheck{},
@@ -1994,36 +1961,36 @@ func TestReconciler_reconcileMachineHealthCheck(t *testing.T) {
19941961
{
19951962
name: "Create a MachineHealthCheck",
19961963
current: nil,
1997-
desired: mhcBuilder.Build(),
1998-
want: mhcBuilder.Build(),
1964+
desired: mhcBuilder.DeepCopy().Build(),
1965+
want: mhcBuilder.DeepCopy().Build(),
19991966
},
20001967
{
20011968
name: "Successfully create a valid Ownerreference on the MachineHealthCheck",
20021969
current: nil,
20031970
// update the unhealthy conditions in the MachineHealthCheck
2004-
desired: mhcBuilder.
1971+
desired: mhcBuilder.DeepCopy().
20051972
// Desired object has an incomplete owner reference which has no UID.
20061973
WithOwnerReferences([]metav1.OwnerReference{{Name: cp.GetName(), Kind: cp.GetKind(), APIVersion: cp.GetAPIVersion()}}).
20071974
Build(),
20081975
// Want a reconciled object with a full ownerReference including UID
2009-
want: mhcBuilder.
1976+
want: mhcBuilder.DeepCopy().
20101977
WithOwnerReferences([]metav1.OwnerReference{{Name: cp.GetName(), Kind: cp.GetKind(), APIVersion: cp.GetAPIVersion(), UID: cp.GetUID()}}).
20111978
Build(),
20121979
wantErr: false,
20131980
},
20141981

20151982
{
20161983
name: "Update a MachineHealthCheck with changes",
2017-
current: mhcBuilder.Build(),
1984+
current: mhcBuilder.DeepCopy().Build(),
20181985
// update the unhealthy conditions in the MachineHealthCheck
2019-
desired: mhcBuilder.WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1986+
desired: mhcBuilder.DeepCopy().WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
20201987
{
20211988
Type: corev1.NodeReady,
20221989
Status: corev1.ConditionUnknown,
20231990
Timeout: metav1.Duration{Duration: 1000 * time.Minute},
20241991
},
20251992
}).Build(),
2026-
want: mhcBuilder.WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
1993+
want: mhcBuilder.DeepCopy().WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
20271994
{
20281995
Type: corev1.NodeReady,
20291996
Status: corev1.ConditionUnknown,
@@ -2033,14 +2000,14 @@ func TestReconciler_reconcileMachineHealthCheck(t *testing.T) {
20332000
},
20342001
{
20352002
name: "Don't change a MachineHealthCheck with no difference between desired and current",
2036-
current: mhcBuilder.Build(),
2003+
current: mhcBuilder.DeepCopy().Build(),
20372004
// update the unhealthy conditions in the MachineHealthCheck
2038-
desired: mhcBuilder.Build(),
2039-
want: mhcBuilder.Build(),
2005+
desired: mhcBuilder.DeepCopy().Build(),
2006+
want: mhcBuilder.DeepCopy().Build(),
20402007
},
20412008
{
20422009
name: "Delete a MachineHealthCheck",
2043-
current: mhcBuilder.Build(),
2010+
current: mhcBuilder.DeepCopy().Build(),
20442011
// update the unhealthy conditions in the MachineHealthCheck
20452012
desired: nil,
20462013
want: nil,

internal/test/builder/builders.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ func (m *MachineDeploymentClassBuilder) Build() *clusterv1.MachineDeploymentClas
394394
}
395395

396396
// InfrastructureMachineTemplateBuilder holds the variables and objects needed to build an InfrastructureMachineTemplate.
397+
// +kubebuilder:object:generate=false
397398
type InfrastructureMachineTemplateBuilder struct {
398399
namespace string
399400
name string
@@ -437,6 +438,7 @@ func (i *InfrastructureMachineTemplateBuilder) Build() *unstructured.Unstructure
437438
}
438439

439440
// BootstrapTemplateBuilder holds the variables needed to build a generic BootstrapTemplate.
441+
// +kubebuilder:object:generate=false
440442
type BootstrapTemplateBuilder struct {
441443
namespace string
442444
name string
@@ -473,6 +475,7 @@ func (b *BootstrapTemplateBuilder) Build() *unstructured.Unstructured {
473475
}
474476

475477
// InfrastructureClusterTemplateBuilder holds the variables needed to build a generic InfrastructureClusterTemplate.
478+
// +kubebuilder:object:generate=false
476479
type InfrastructureClusterTemplateBuilder struct {
477480
namespace string
478481
name string
@@ -517,6 +520,7 @@ func (i *InfrastructureClusterTemplateBuilder) Build() *unstructured.Unstructure
517520
}
518521

519522
// ControlPlaneTemplateBuilder holds the variables and objects needed to build a generic ControlPlane template.
523+
// +kubebuilder:object:generate=false
520524
type ControlPlaneTemplateBuilder struct {
521525
namespace string
522526
name string
@@ -573,6 +577,7 @@ func (c *ControlPlaneTemplateBuilder) Build() *unstructured.Unstructured {
573577
}
574578

575579
// InfrastructureClusterBuilder holds the variables and objects needed to build a generic InfrastructureCluster.
580+
// +kubebuilder:object:generate=false
576581
type InfrastructureClusterBuilder struct {
577582
namespace string
578583
name string
@@ -613,6 +618,7 @@ func (i *InfrastructureClusterBuilder) Build() *unstructured.Unstructured {
613618
}
614619

615620
// ControlPlaneBuilder holds the variables and objects needed to build a generic object for cluster.spec.controlPlaneRef.
621+
// +kubebuilder:object:generate=false
616622
type ControlPlaneBuilder struct {
617623
namespace string
618624
name string

internal/test/builder/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ limitations under the License.
1515
*/
1616

1717
// Package builder implements builder and CRDs for creating API objects for testing.
18+
// +kubebuilder:object:generate=true
1819
package builder

0 commit comments

Comments
 (0)