Skip to content

Commit

Permalink
Merge pull request #9372 from mercedes-benz/cc-mp-variables-tests
Browse files Browse the repository at this point in the history
🌱 Add MachinePool test cases to variables tests
  • Loading branch information
k8s-ci-robot committed Sep 7, 2023
2 parents d169c40 + fda45dd commit 58557b7
Showing 1 changed file with 368 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/utils/pointer"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/internal/test/builder"
)
Expand Down Expand Up @@ -853,6 +854,373 @@ func TestMachineDeployment(t *testing.T) {
}
}

func TestMachinePool(t *testing.T) {
tests := []struct {
name string
mpTopology *clusterv1.MachinePoolTopology
forPatch string
variableDefinitionsForPatch map[string]bool
mp *expv1.MachinePool
mpBootstrapConfig *unstructured.Unstructured
mpInfrastructureMachinePool *unstructured.Unstructured
want []runtimehooksv1.Variable
}{
{
name: "Should calculate MachinePool variables",
variableDefinitionsForPatch: map[string]bool{"location": true, "cpu": true},
forPatch: "patch1",
mpTopology: &clusterv1.MachinePoolTopology{
Replicas: pointer.Int32(3),
Name: "mp-topology",
Class: "mp-class",
Variables: &clusterv1.MachinePoolVariables{
Overrides: []clusterv1.ClusterVariable{
{
Name: "location",
Value: toJSON("\"us-central\""),
},
{
Name: "cpu",
Value: toJSON("8"),
},
},
},
},
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
WithReplicas(3).
WithVersion("v1.21.1").
Build(),
want: []runtimehooksv1.Variable{
{
Name: "location",
Value: toJSON("\"us-central\""),
},
{
Name: "cpu",
Value: toJSON("8"),
},
{
Name: BuiltinsName,
Value: toJSONCompact(`{
"machinePool":{
"version": "v1.21.1",
"class": "mp-class",
"name": "mp1",
"topologyName": "mp-topology",
"replicas":3
}}`),
},
},
},
{
name: "Should calculate MachinePool variables for a given patch name",
forPatch: "patch1",
variableDefinitionsForPatch: map[string]bool{
"location": true,
"cpu": true,
},
mpTopology: &clusterv1.MachinePoolTopology{
Replicas: pointer.Int32(3),
Name: "mp-topology",
Class: "mp-class",
Variables: &clusterv1.MachinePoolVariables{
Overrides: []clusterv1.ClusterVariable{
{
Name: "location",
Value: toJSON("\"us-central\""),
DefinitionFrom: "patch1",
},
{
Name: "location",
Value: toJSON("\"us-east\""),
// This variable should be excluded because it is defined for a different patch.
DefinitionFrom: "anotherPatch",
},

{
Name: "http-proxy",
Value: toJSON("\"internal.proxy.com\""),
// This variable should be excluded because it is not in variableDefinitionsForPatch.
DefinitionFrom: "",
},
{
Name: "cpu",
Value: toJSON("8"),
// This variable should be included because it is defined for all patches.
},
},
},
},
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
WithReplicas(3).
WithVersion("v1.21.1").
Build(),
want: []runtimehooksv1.Variable{
{
Name: "location",
Value: toJSON("\"us-central\""),
},
{
Name: "cpu",
Value: toJSON("8"),
},
{
Name: BuiltinsName,
Value: toJSONCompact(`{
"machinePool":{
"version": "v1.21.1",
"class": "mp-class",
"name": "mp1",
"topologyName": "mp-topology",
"replicas":3
}}`),
},
},
},
{
name: "Should calculate MachinePool variables (without overrides)",
forPatch: "patch1",
variableDefinitionsForPatch: map[string]bool{"location": true, "cpu": true},
mpTopology: &clusterv1.MachinePoolTopology{
Replicas: pointer.Int32(3),
Name: "mp-topology",
Class: "mp-class",
},
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
WithReplicas(3).
WithVersion("v1.21.1").
Build(),
want: []runtimehooksv1.Variable{
{
Name: BuiltinsName,
Value: toJSONCompact(`{
"machinePool":{
"version": "v1.21.1",
"class": "mp-class",
"name": "mp1",
"topologyName": "mp-topology",
"replicas":3
}}`),
},
},
},
{
name: "Should calculate MachinePool variables, replicas not set",
forPatch: "patch1",
variableDefinitionsForPatch: map[string]bool{"location": true, "cpu": true},
mpTopology: &clusterv1.MachinePoolTopology{
Name: "mp-topology",
Class: "mp-class",
Variables: &clusterv1.MachinePoolVariables{
Overrides: []clusterv1.ClusterVariable{
{
Name: "location",
Value: toJSON("\"us-central\""),
},
{
Name: "cpu",
Value: toJSON("8"),
},
},
},
},
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
WithVersion("v1.21.1").
Build(),
want: []runtimehooksv1.Variable{
{
Name: "location",
Value: toJSON("\"us-central\""),
},
{
Name: "cpu",
Value: toJSON("8"),
},
{
Name: BuiltinsName,
Value: toJSONCompact(`{
"machinePool":{
"version": "v1.21.1",
"class": "mp-class",
"name": "mp1",
"topologyName": "mp-topology"
}}`),
},
},
},
{
name: "Should calculate MachinePool variables with BoostrapConfig",
variableDefinitionsForPatch: map[string]bool{"location": true, "cpu": true},
forPatch: "patch1",
mpTopology: &clusterv1.MachinePoolTopology{
Replicas: pointer.Int32(3),
Name: "mp-topology",
Class: "mp-class",
Variables: &clusterv1.MachinePoolVariables{
Overrides: []clusterv1.ClusterVariable{
{
Name: "location",
Value: toJSON("\"us-central\""),
},
{
Name: "cpu",
Value: toJSON("8"),
},
},
},
},
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
WithReplicas(3).
WithVersion("v1.21.1").
Build(),
mpBootstrapConfig: builder.BootstrapConfig(metav1.NamespaceDefault, "mpBC1").Build(),
want: []runtimehooksv1.Variable{
{
Name: "location",
Value: toJSON("\"us-central\""),
},
{
Name: "cpu",
Value: toJSON("8"),
},
{
Name: BuiltinsName,
Value: toJSONCompact(`{
"machinePool":{
"version": "v1.21.1",
"class": "mp-class",
"name": "mp1",
"topologyName": "mp-topology",
"replicas":3,
"bootstrap":{
"configRef":{
"name": "mpBC1"
}
}
}}`),
},
},
},
{
name: "Should calculate MachinePool variables with InfrastructureMachinePool",
variableDefinitionsForPatch: map[string]bool{"location": true, "cpu": true},
forPatch: "patch1",
mpTopology: &clusterv1.MachinePoolTopology{
Replicas: pointer.Int32(3),
Name: "mp-topology",
Class: "mp-class",
Variables: &clusterv1.MachinePoolVariables{
Overrides: []clusterv1.ClusterVariable{
{
Name: "location",
Value: toJSON("\"us-central\""),
},
{
Name: "cpu",
Value: toJSON("8"),
},
},
},
},
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
WithReplicas(3).
WithVersion("v1.21.1").
Build(),
mpInfrastructureMachinePool: builder.InfrastructureMachinePool(metav1.NamespaceDefault, "mpIMP1").Build(),
want: []runtimehooksv1.Variable{
{
Name: "location",
Value: toJSON("\"us-central\""),
},
{
Name: "cpu",
Value: toJSON("8"),
},
{
Name: BuiltinsName,
Value: toJSONCompact(`{
"machinePool":{
"version": "v1.21.1",
"class": "mp-class",
"name": "mp1",
"topologyName": "mp-topology",
"replicas":3,
"infrastructureRef":{
"name": "mpIMP1"
}
}}`),
},
},
},
{
name: "Should calculate MachinePool variables with BootstrapConfig and InfrastructureMachinePool",
variableDefinitionsForPatch: map[string]bool{"location": true, "cpu": true},
forPatch: "patch1",
mpTopology: &clusterv1.MachinePoolTopology{
Replicas: pointer.Int32(3),
Name: "mp-topology",
Class: "mp-class",
Variables: &clusterv1.MachinePoolVariables{
Overrides: []clusterv1.ClusterVariable{
{
Name: "location",
Value: toJSON("\"us-central\""),
},
{
Name: "cpu",
Value: toJSON("8"),
},
},
},
},
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
WithReplicas(3).
WithVersion("v1.21.1").
Build(),
mpBootstrapConfig: builder.BootstrapConfig(metav1.NamespaceDefault, "mpBC1").Build(),
mpInfrastructureMachinePool: builder.InfrastructureMachinePool(metav1.NamespaceDefault, "mpIMP1").Build(),
want: []runtimehooksv1.Variable{
{
Name: "location",
Value: toJSON("\"us-central\""),
},
{
Name: "cpu",
Value: toJSON("8"),
},
{
Name: BuiltinsName,
Value: toJSONCompact(`{
"machinePool":{
"version": "v1.21.1",
"class": "mp-class",
"name": "mp1",
"topologyName": "mp-topology",
"replicas":3,
"bootstrap":{
"configRef":{
"name": "mpBC1"
}
},
"infrastructureRef":{
"name": "mpIMP1"
}
}}`),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

got, err := MachinePool(tt.mpTopology, tt.mp, tt.mpBootstrapConfig, tt.mpInfrastructureMachinePool, tt.forPatch, tt.variableDefinitionsForPatch)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(got).To(BeComparableTo(tt.want))
})
}
}

func toJSON(value string) apiextensionsv1.JSON {
return apiextensionsv1.JSON{Raw: []byte(value)}
}
Expand Down

0 comments on commit 58557b7

Please sign in to comment.