Skip to content

Commit

Permalink
Merge pull request #5987 from killianmuldoon/fix/builder-test
Browse files Browse the repository at this point in the history
🐛  inline builders in test for MHC reconcilation
  • Loading branch information
k8s-ci-robot authored Jan 28, 2022
2 parents 4c4b47d + a55b48b commit 03d6487
Show file tree
Hide file tree
Showing 5 changed files with 371 additions and 61 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ generate-go-deepcopy-core: $(CONTROLLER_GEN) ## Generate deepcopy go code for co
paths=./api/... \
paths=./$(EXP_DIR)/api/... \
paths=./$(EXP_DIR)/addons/api/... \
paths=./cmd/clusterctl/...
paths=./cmd/clusterctl/... \
paths=./internal/test/builder/...

.PHONY: generate-go-deepcopy-kubeadm-bootstrap
generate-go-deepcopy-kubeadm-bootstrap: $(CONTROLLER_GEN) ## Generate deepcopy go code for kubeadm bootstrap
Expand Down
87 changes: 27 additions & 60 deletions internal/controllers/topology/cluster/reconcile_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,6 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) {
Build()

maxUnhealthy := intstr.Parse("45%")
// TODO: (killianmuldoon) This builder should be copied and not just passed around.
mhcBuilder := builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
WithSelector(*selectorForMachineDeploymentMHC(md)).
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
Expand All @@ -1802,12 +1801,10 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) {
current: nil,
desired: []*scope.MachineDeploymentState{
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
mhcBuilder.Build()),
mhcBuilder.DeepCopy().Build()),
},
want: []*clusterv1.MachineHealthCheck{
mhcBuilder.
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
Build()},
mhcBuilder.DeepCopy().Build()},
},
{
name: "Create a new MachineHealthCheck if the MachineDeployment is modified to include one",
Expand All @@ -1817,82 +1814,52 @@ func TestReconcileMachineDeploymentMachineHealthCheck(t *testing.T) {
// MHC is added in the desired state of the MachineDeployment
desired: []*scope.MachineDeploymentState{
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
mhcBuilder.Build()),
mhcBuilder.DeepCopy().Build()),
},
want: []*clusterv1.MachineHealthCheck{
mhcBuilder.
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
Build()}},
mhcBuilder.DeepCopy().Build()}},
{
name: "Update MachineHealthCheck spec adding a field if the spec adds a field",
current: []*scope.MachineDeploymentState{
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, mhcBuilder.Build()),
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
mhcBuilder.DeepCopy().Build()),
},
desired: []*scope.MachineDeploymentState{
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
mhcBuilder.WithMaxUnhealthy(&maxUnhealthy).Build())},
mhcBuilder.DeepCopy().WithMaxUnhealthy(&maxUnhealthy).Build())},
want: []*clusterv1.MachineHealthCheck{
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
WithSelector(*selectorForMachineDeploymentMHC(md)).
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionUnknown,
Timeout: metav1.Duration{Duration: 5 * time.Minute},
},
}).
mhcBuilder.DeepCopy().
WithMaxUnhealthy(&maxUnhealthy).
WithClusterName("cluster1").
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
Build()},
},
{
name: "Update MachineHealthCheck spec removing a field if the spec removes a field",
current: []*scope.MachineDeploymentState{
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
mhcBuilder.WithMaxUnhealthy(&maxUnhealthy).Build()),
mhcBuilder.DeepCopy().WithMaxUnhealthy(&maxUnhealthy).Build()),
},
desired: []*scope.MachineDeploymentState{
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
WithSelector(*selectorForMachineDeploymentMHC(md)).
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionUnknown,
Timeout: metav1.Duration{Duration: 5 * time.Minute},
},
}).
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
WithClusterName("cluster1").
Build()),
mhcBuilder.DeepCopy().Build()),
},
want: []*clusterv1.MachineHealthCheck{
builder.MachineHealthCheck(metav1.NamespaceDefault, "md-1").
WithSelector(*selectorForMachineDeploymentMHC(md)).
WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionUnknown,
Timeout: metav1.Duration{Duration: 5 * time.Minute},
},
}).
WithOwnerReferences([]metav1.OwnerReference{*ownerReferenceTo(md)}).
WithClusterName("cluster1").
Build()},
mhcBuilder.DeepCopy().Build(),
},
},
{
name: "Delete MachineHealthCheck spec if the MachineDeployment is modified to remove an existing one",
current: []*scope.MachineDeploymentState{
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, mhcBuilder.Build()),
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
mhcBuilder.DeepCopy().Build()),
},
desired: []*scope.MachineDeploymentState{newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, nil)},
want: []*clusterv1.MachineHealthCheck{},
},
{
name: "Delete MachineHealthCheck spec if the MachineDeployment is deleted",
current: []*scope.MachineDeploymentState{
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate, mhcBuilder.Build()),
newFakeMachineDeploymentTopologyState("md-1", infrastructureMachineTemplate, bootstrapTemplate,
mhcBuilder.DeepCopy().Build()),
},
desired: []*scope.MachineDeploymentState{},
want: []*clusterv1.MachineHealthCheck{},
Expand Down Expand Up @@ -1994,36 +1961,36 @@ func TestReconciler_reconcileMachineHealthCheck(t *testing.T) {
{
name: "Create a MachineHealthCheck",
current: nil,
desired: mhcBuilder.Build(),
want: mhcBuilder.Build(),
desired: mhcBuilder.DeepCopy().Build(),
want: mhcBuilder.DeepCopy().Build(),
},
{
name: "Successfully create a valid Ownerreference on the MachineHealthCheck",
current: nil,
// update the unhealthy conditions in the MachineHealthCheck
desired: mhcBuilder.
desired: mhcBuilder.DeepCopy().
// Desired object has an incomplete owner reference which has no UID.
WithOwnerReferences([]metav1.OwnerReference{{Name: cp.GetName(), Kind: cp.GetKind(), APIVersion: cp.GetAPIVersion()}}).
Build(),
// Want a reconciled object with a full ownerReference including UID
want: mhcBuilder.
want: mhcBuilder.DeepCopy().
WithOwnerReferences([]metav1.OwnerReference{{Name: cp.GetName(), Kind: cp.GetKind(), APIVersion: cp.GetAPIVersion(), UID: cp.GetUID()}}).
Build(),
wantErr: false,
},

{
name: "Update a MachineHealthCheck with changes",
current: mhcBuilder.Build(),
current: mhcBuilder.DeepCopy().Build(),
// update the unhealthy conditions in the MachineHealthCheck
desired: mhcBuilder.WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
desired: mhcBuilder.DeepCopy().WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionUnknown,
Timeout: metav1.Duration{Duration: 1000 * time.Minute},
},
}).Build(),
want: mhcBuilder.WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
want: mhcBuilder.DeepCopy().WithUnhealthyConditions([]clusterv1.UnhealthyCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionUnknown,
Expand All @@ -2033,14 +2000,14 @@ func TestReconciler_reconcileMachineHealthCheck(t *testing.T) {
},
{
name: "Don't change a MachineHealthCheck with no difference between desired and current",
current: mhcBuilder.Build(),
current: mhcBuilder.DeepCopy().Build(),
// update the unhealthy conditions in the MachineHealthCheck
desired: mhcBuilder.Build(),
want: mhcBuilder.Build(),
desired: mhcBuilder.DeepCopy().Build(),
want: mhcBuilder.DeepCopy().Build(),
},
{
name: "Delete a MachineHealthCheck",
current: mhcBuilder.Build(),
current: mhcBuilder.DeepCopy().Build(),
// update the unhealthy conditions in the MachineHealthCheck
desired: nil,
want: nil,
Expand Down
6 changes: 6 additions & 0 deletions internal/test/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ func (m *MachineDeploymentClassBuilder) Build() *clusterv1.MachineDeploymentClas
}

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

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

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

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

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

// ControlPlaneBuilder holds the variables and objects needed to build a generic object for cluster.spec.controlPlaneRef.
// +kubebuilder:object:generate=false
type ControlPlaneBuilder struct {
namespace string
name string
Expand Down
1 change: 1 addition & 0 deletions internal/test/builder/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ limitations under the License.
*/

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

0 comments on commit 03d6487

Please sign in to comment.