Skip to content

Commit

Permalink
Add conditions test
Browse files Browse the repository at this point in the history
  • Loading branch information
willie-yao committed Sep 13, 2023
1 parent a95dcd0 commit a3cbd60
Show file tree
Hide file tree
Showing 6 changed files with 387 additions and 36 deletions.
59 changes: 42 additions & 17 deletions internal/controllers/topology/cluster/cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,32 +763,32 @@ func setupTestEnvForIntegrationTests(ns *corev1.Namespace) (func() error, error)
Build()
bootstrapTemplate := builder.TestBootstrapTemplate(ns.Name, "bootstraptemplate").Build()

// 2) ClusterClass definitions including definitions of MachineDeploymentClasses used inside the ClusterClass.
machineDeploymentClass1 := builder.MachineDeploymentClass(workerClassName1).
// 2) ClusterClass definitions including definitions of MachineDeploymentClasses and MachinePoolClasses used inside the ClusterClass.
machineDeploymentClass1 := builder.MachineDeploymentClass(workerClassName1 + "-md").
WithInfrastructureTemplate(infrastructureMachineTemplate1).
WithBootstrapTemplate(bootstrapTemplate).
WithLabels(map[string]string{"foo": "bar"}).
WithAnnotations(map[string]string{"foo": "bar"}).
Build()
machineDeploymentClass2 := builder.MachineDeploymentClass(workerClassName2).
machineDeploymentClass2 := builder.MachineDeploymentClass(workerClassName2 + "-md").
WithInfrastructureTemplate(infrastructureMachineTemplate1).
WithBootstrapTemplate(bootstrapTemplate).
Build()
machineDeploymentClass3 := builder.MachineDeploymentClass(workerClassName3).
machineDeploymentClass3 := builder.MachineDeploymentClass(workerClassName3 + "-md").
WithInfrastructureTemplate(infrastructureMachineTemplate2).
WithBootstrapTemplate(bootstrapTemplate).
Build()
machinePoolClass1 := builder.MachinePoolClass(workerClassName1).
machinePoolClass1 := builder.MachinePoolClass(workerClassName1 + "-mp").
WithInfrastructureTemplate(infrastructureMachinePoolTemplate1).
WithBootstrapTemplate(bootstrapTemplate).
WithLabels(map[string]string{"foo": "bar"}).
WithAnnotations(map[string]string{"foo": "bar"}).
Build()
machinePoolClass2 := builder.MachinePoolClass(workerClassName2).
machinePoolClass2 := builder.MachinePoolClass(workerClassName2 + "-mp").
WithInfrastructureTemplate(infrastructureMachinePoolTemplate1).
WithBootstrapTemplate(bootstrapTemplate).
Build()
machinePoolClass3 := builder.MachinePoolClass(workerClassName3).
machinePoolClass3 := builder.MachinePoolClass(workerClassName3 + "-mp").
WithInfrastructureTemplate(infrastructureMachinePoolTemplate2).
WithBootstrapTemplate(bootstrapTemplate).
Build()
Expand All @@ -802,7 +802,7 @@ func setupTestEnvForIntegrationTests(ns *corev1.Namespace) (func() error, error)

// This ClusterClass changes a number of things in a ClusterClass in a way that is compatible for a ClusterClass rebase operation.
// 1) It changes the controlPlaneMachineInfrastructureTemplate to a new template.
// 2) It adds a new machineDeploymentClass
// 2) It adds a new machineDeploymentClass and machinePoolClass
// 3) It changes the infrastructureClusterTemplate.
clusterClassForRebase := builder.ClusterClass(ns.Name, clusterClassName2).
WithInfrastructureClusterTemplate(infrastructureClusterTemplate2).
Expand All @@ -815,19 +815,19 @@ func setupTestEnvForIntegrationTests(ns *corev1.Namespace) (func() error, error)
// 3) Two Clusters including a Cluster Topology objects and the MachineDeploymentTopology objects used in the
// Cluster Topology. The second cluster differs from the first both in version and in its MachineDeployment definition.
machineDeploymentTopology1 := builder.MachineDeploymentTopology("mdm1").
WithClass(workerClassName1).
WithClass(workerClassName1 + "-md").
WithReplicas(3).
Build()
machineDeploymentTopology2 := builder.MachineDeploymentTopology("mdm2").
WithClass(workerClassName2).
WithClass(workerClassName2 + "-md").
WithReplicas(1).
Build()
machinePoolTopology1 := builder.MachinePoolTopology("mp1").
WithClass(workerClassName1).
WithClass(workerClassName1 + "-mp").
WithReplicas(3).
Build()
machinePoolTopology2 := builder.MachinePoolTopology("mp2").
WithClass(workerClassName2).
WithClass(workerClassName2 + "-mp").
WithReplicas(1).
Build()

Expand Down Expand Up @@ -1086,14 +1086,14 @@ func assertMachinePoolsReconcile(cluster *clusterv1.Cluster) error {
// ClusterNameLabel to the items for further testing.
for _, m := range machinePools.Items {
// If the machinePool doesn't have the ClusterTopologyOwnedLabel and the ClusterNameLabel ignore.
md := m
if err := assertClusterTopologyOwnedLabel(&md); err != nil {
mp := m
if err := assertClusterTopologyOwnedLabel(&mp); err != nil {
continue
}
if err := assertClusterNameLabel(&md, cluster.Name); err != nil {
if err := assertClusterNameLabel(&mp, cluster.Name); err != nil {
continue
}
clusterMPs = append(clusterMPs, md)
clusterMPs = append(clusterMPs, mp)
}

// If the total number of machine pools is not as expected return false.
Expand Down Expand Up @@ -1242,6 +1242,11 @@ func TestReconciler_DefaultCluster(t *testing.T) {
mdTopologyBase := builder.MachineDeploymentTopology("md1").
WithClass("worker1").
WithReplicas(3)
mpClass1 := builder.MachinePoolClass("worker1").
Build()
mpTopologyBase := builder.MachinePoolTopology("mp1").
WithClass("worker1").
WithReplicas(3)
clusterBuilder := builder.Cluster(metav1.NamespaceDefault, clusterName1).
WithTopology(topologyBase.DeepCopy().Build())

Expand Down Expand Up @@ -1310,6 +1315,7 @@ func TestReconciler_DefaultCluster(t *testing.T) {
name: "Default nested values of Cluster variables with values from ClusterClass",
clusterClass: classBuilder.DeepCopy().
WithWorkerMachineDeploymentClasses(*mdClass1).
WithWorkerMachinePoolClasses(*mpClass1).
WithStatusVariables([]clusterv1.ClusterClassStatusVariable{
{
Name: "location",
Expand Down Expand Up @@ -1360,6 +1366,11 @@ func TestReconciler_DefaultCluster(t *testing.T) {
Name: "httpProxy",
Value: apiextensionsv1.JSON{Raw: []byte(`{"enabled":true}`)},
}).Build()).
WithMachinePool(mpTopologyBase.DeepCopy().
WithVariables(clusterv1.ClusterVariable{
Name: "httpProxy",
Value: apiextensionsv1.JSON{Raw: []byte(`{"enabled":true}`)},
}).Build()).
Build()).
Build(),
wantCluster: clusterBuilder.DeepCopy().WithTopology(
Expand All @@ -1377,6 +1388,16 @@ func TestReconciler_DefaultCluster(t *testing.T) {
},
}).
Build()).
WithMachinePool(
mpTopologyBase.DeepCopy().WithVariables(
clusterv1.ClusterVariable{
Name: "httpProxy",
Value: apiextensionsv1.JSON{
// url has been added by defaulting.
Raw: []byte(`{"enabled":true,"url":"http://localhost:3128"}`),
},
}).
Build()).
Build()).
Build(),
},
Expand Down Expand Up @@ -1404,6 +1425,9 @@ func TestReconciler_ValidateCluster(t *testing.T) {
mdTopologyBase := builder.MachineDeploymentTopology("md1").
WithClass("worker1").
WithReplicas(3)
mpTopologyBase := builder.MachinePoolTopology("mp1").
WithClass("worker1").
WithReplicas(3)
classBuilder := builder.ClusterClass(metav1.NamespaceDefault, clusterClassName1)
topologyBase := builder.ClusterTopology().
WithClass(clusterClassName1).
Expand Down Expand Up @@ -1464,7 +1488,8 @@ func TestReconciler_ValidateCluster(t *testing.T) {
WithClass(clusterClassName1).
WithVersion("1.22.2").
WithControlPlaneReplicas(3).
WithMachineDeployment(mdTopologyBase.Build()).Build(),
WithMachineDeployment(mdTopologyBase.Build()).
WithMachinePool(mpTopologyBase.Build()).Build(),
).
Build(),
wantValidationErr: true,
Expand Down
Loading

0 comments on commit a3cbd60

Please sign in to comment.