Skip to content

Commit fda45dd

Browse files
author
Johannes Frey
committed
Add MachinePool test cases
1 parent 8516dae commit fda45dd

File tree

1 file changed

+368
-0
lines changed

1 file changed

+368
-0
lines changed

internal/controllers/topology/cluster/patches/variables/variables_test.go

Lines changed: 368 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"k8s.io/utils/pointer"
2929

3030
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
31+
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
3132
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
3233
"sigs.k8s.io/cluster-api/internal/test/builder"
3334
)
@@ -853,6 +854,373 @@ func TestMachineDeployment(t *testing.T) {
853854
}
854855
}
855856

857+
func TestMachinePool(t *testing.T) {
858+
tests := []struct {
859+
name string
860+
mpTopology *clusterv1.MachinePoolTopology
861+
forPatch string
862+
variableDefinitionsForPatch map[string]bool
863+
mp *expv1.MachinePool
864+
mpBootstrapConfig *unstructured.Unstructured
865+
mpInfrastructureMachinePool *unstructured.Unstructured
866+
want []runtimehooksv1.Variable
867+
}{
868+
{
869+
name: "Should calculate MachinePool variables",
870+
variableDefinitionsForPatch: map[string]bool{"location": true, "cpu": true},
871+
forPatch: "patch1",
872+
mpTopology: &clusterv1.MachinePoolTopology{
873+
Replicas: pointer.Int32(3),
874+
Name: "mp-topology",
875+
Class: "mp-class",
876+
Variables: &clusterv1.MachinePoolVariables{
877+
Overrides: []clusterv1.ClusterVariable{
878+
{
879+
Name: "location",
880+
Value: toJSON("\"us-central\""),
881+
},
882+
{
883+
Name: "cpu",
884+
Value: toJSON("8"),
885+
},
886+
},
887+
},
888+
},
889+
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
890+
WithReplicas(3).
891+
WithVersion("v1.21.1").
892+
Build(),
893+
want: []runtimehooksv1.Variable{
894+
{
895+
Name: "location",
896+
Value: toJSON("\"us-central\""),
897+
},
898+
{
899+
Name: "cpu",
900+
Value: toJSON("8"),
901+
},
902+
{
903+
Name: BuiltinsName,
904+
Value: toJSONCompact(`{
905+
"machinePool":{
906+
"version": "v1.21.1",
907+
"class": "mp-class",
908+
"name": "mp1",
909+
"topologyName": "mp-topology",
910+
"replicas":3
911+
}}`),
912+
},
913+
},
914+
},
915+
{
916+
name: "Should calculate MachinePool variables for a given patch name",
917+
forPatch: "patch1",
918+
variableDefinitionsForPatch: map[string]bool{
919+
"location": true,
920+
"cpu": true,
921+
},
922+
mpTopology: &clusterv1.MachinePoolTopology{
923+
Replicas: pointer.Int32(3),
924+
Name: "mp-topology",
925+
Class: "mp-class",
926+
Variables: &clusterv1.MachinePoolVariables{
927+
Overrides: []clusterv1.ClusterVariable{
928+
{
929+
Name: "location",
930+
Value: toJSON("\"us-central\""),
931+
DefinitionFrom: "patch1",
932+
},
933+
{
934+
Name: "location",
935+
Value: toJSON("\"us-east\""),
936+
// This variable should be excluded because it is defined for a different patch.
937+
DefinitionFrom: "anotherPatch",
938+
},
939+
940+
{
941+
Name: "http-proxy",
942+
Value: toJSON("\"internal.proxy.com\""),
943+
// This variable should be excluded because it is not in variableDefinitionsForPatch.
944+
DefinitionFrom: "",
945+
},
946+
{
947+
Name: "cpu",
948+
Value: toJSON("8"),
949+
// This variable should be included because it is defined for all patches.
950+
},
951+
},
952+
},
953+
},
954+
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
955+
WithReplicas(3).
956+
WithVersion("v1.21.1").
957+
Build(),
958+
want: []runtimehooksv1.Variable{
959+
{
960+
Name: "location",
961+
Value: toJSON("\"us-central\""),
962+
},
963+
{
964+
Name: "cpu",
965+
Value: toJSON("8"),
966+
},
967+
{
968+
Name: BuiltinsName,
969+
Value: toJSONCompact(`{
970+
"machinePool":{
971+
"version": "v1.21.1",
972+
"class": "mp-class",
973+
"name": "mp1",
974+
"topologyName": "mp-topology",
975+
"replicas":3
976+
}}`),
977+
},
978+
},
979+
},
980+
{
981+
name: "Should calculate MachinePool variables (without overrides)",
982+
forPatch: "patch1",
983+
variableDefinitionsForPatch: map[string]bool{"location": true, "cpu": true},
984+
mpTopology: &clusterv1.MachinePoolTopology{
985+
Replicas: pointer.Int32(3),
986+
Name: "mp-topology",
987+
Class: "mp-class",
988+
},
989+
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
990+
WithReplicas(3).
991+
WithVersion("v1.21.1").
992+
Build(),
993+
want: []runtimehooksv1.Variable{
994+
{
995+
Name: BuiltinsName,
996+
Value: toJSONCompact(`{
997+
"machinePool":{
998+
"version": "v1.21.1",
999+
"class": "mp-class",
1000+
"name": "mp1",
1001+
"topologyName": "mp-topology",
1002+
"replicas":3
1003+
}}`),
1004+
},
1005+
},
1006+
},
1007+
{
1008+
name: "Should calculate MachinePool variables, replicas not set",
1009+
forPatch: "patch1",
1010+
variableDefinitionsForPatch: map[string]bool{"location": true, "cpu": true},
1011+
mpTopology: &clusterv1.MachinePoolTopology{
1012+
Name: "mp-topology",
1013+
Class: "mp-class",
1014+
Variables: &clusterv1.MachinePoolVariables{
1015+
Overrides: []clusterv1.ClusterVariable{
1016+
{
1017+
Name: "location",
1018+
Value: toJSON("\"us-central\""),
1019+
},
1020+
{
1021+
Name: "cpu",
1022+
Value: toJSON("8"),
1023+
},
1024+
},
1025+
},
1026+
},
1027+
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
1028+
WithVersion("v1.21.1").
1029+
Build(),
1030+
want: []runtimehooksv1.Variable{
1031+
{
1032+
Name: "location",
1033+
Value: toJSON("\"us-central\""),
1034+
},
1035+
{
1036+
Name: "cpu",
1037+
Value: toJSON("8"),
1038+
},
1039+
{
1040+
Name: BuiltinsName,
1041+
Value: toJSONCompact(`{
1042+
"machinePool":{
1043+
"version": "v1.21.1",
1044+
"class": "mp-class",
1045+
"name": "mp1",
1046+
"topologyName": "mp-topology"
1047+
}}`),
1048+
},
1049+
},
1050+
},
1051+
{
1052+
name: "Should calculate MachinePool variables with BoostrapConfig",
1053+
variableDefinitionsForPatch: map[string]bool{"location": true, "cpu": true},
1054+
forPatch: "patch1",
1055+
mpTopology: &clusterv1.MachinePoolTopology{
1056+
Replicas: pointer.Int32(3),
1057+
Name: "mp-topology",
1058+
Class: "mp-class",
1059+
Variables: &clusterv1.MachinePoolVariables{
1060+
Overrides: []clusterv1.ClusterVariable{
1061+
{
1062+
Name: "location",
1063+
Value: toJSON("\"us-central\""),
1064+
},
1065+
{
1066+
Name: "cpu",
1067+
Value: toJSON("8"),
1068+
},
1069+
},
1070+
},
1071+
},
1072+
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
1073+
WithReplicas(3).
1074+
WithVersion("v1.21.1").
1075+
Build(),
1076+
mpBootstrapConfig: builder.BootstrapConfig(metav1.NamespaceDefault, "mpBC1").Build(),
1077+
want: []runtimehooksv1.Variable{
1078+
{
1079+
Name: "location",
1080+
Value: toJSON("\"us-central\""),
1081+
},
1082+
{
1083+
Name: "cpu",
1084+
Value: toJSON("8"),
1085+
},
1086+
{
1087+
Name: BuiltinsName,
1088+
Value: toJSONCompact(`{
1089+
"machinePool":{
1090+
"version": "v1.21.1",
1091+
"class": "mp-class",
1092+
"name": "mp1",
1093+
"topologyName": "mp-topology",
1094+
"replicas":3,
1095+
"bootstrap":{
1096+
"configRef":{
1097+
"name": "mpBC1"
1098+
}
1099+
}
1100+
}}`),
1101+
},
1102+
},
1103+
},
1104+
{
1105+
name: "Should calculate MachinePool variables with InfrastructureMachinePool",
1106+
variableDefinitionsForPatch: map[string]bool{"location": true, "cpu": true},
1107+
forPatch: "patch1",
1108+
mpTopology: &clusterv1.MachinePoolTopology{
1109+
Replicas: pointer.Int32(3),
1110+
Name: "mp-topology",
1111+
Class: "mp-class",
1112+
Variables: &clusterv1.MachinePoolVariables{
1113+
Overrides: []clusterv1.ClusterVariable{
1114+
{
1115+
Name: "location",
1116+
Value: toJSON("\"us-central\""),
1117+
},
1118+
{
1119+
Name: "cpu",
1120+
Value: toJSON("8"),
1121+
},
1122+
},
1123+
},
1124+
},
1125+
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
1126+
WithReplicas(3).
1127+
WithVersion("v1.21.1").
1128+
Build(),
1129+
mpInfrastructureMachinePool: builder.InfrastructureMachinePool(metav1.NamespaceDefault, "mpIMP1").Build(),
1130+
want: []runtimehooksv1.Variable{
1131+
{
1132+
Name: "location",
1133+
Value: toJSON("\"us-central\""),
1134+
},
1135+
{
1136+
Name: "cpu",
1137+
Value: toJSON("8"),
1138+
},
1139+
{
1140+
Name: BuiltinsName,
1141+
Value: toJSONCompact(`{
1142+
"machinePool":{
1143+
"version": "v1.21.1",
1144+
"class": "mp-class",
1145+
"name": "mp1",
1146+
"topologyName": "mp-topology",
1147+
"replicas":3,
1148+
"infrastructureRef":{
1149+
"name": "mpIMP1"
1150+
}
1151+
}}`),
1152+
},
1153+
},
1154+
},
1155+
{
1156+
name: "Should calculate MachinePool variables with BootstrapConfig and InfrastructureMachinePool",
1157+
variableDefinitionsForPatch: map[string]bool{"location": true, "cpu": true},
1158+
forPatch: "patch1",
1159+
mpTopology: &clusterv1.MachinePoolTopology{
1160+
Replicas: pointer.Int32(3),
1161+
Name: "mp-topology",
1162+
Class: "mp-class",
1163+
Variables: &clusterv1.MachinePoolVariables{
1164+
Overrides: []clusterv1.ClusterVariable{
1165+
{
1166+
Name: "location",
1167+
Value: toJSON("\"us-central\""),
1168+
},
1169+
{
1170+
Name: "cpu",
1171+
Value: toJSON("8"),
1172+
},
1173+
},
1174+
},
1175+
},
1176+
mp: builder.MachinePool(metav1.NamespaceDefault, "mp1").
1177+
WithReplicas(3).
1178+
WithVersion("v1.21.1").
1179+
Build(),
1180+
mpBootstrapConfig: builder.BootstrapConfig(metav1.NamespaceDefault, "mpBC1").Build(),
1181+
mpInfrastructureMachinePool: builder.InfrastructureMachinePool(metav1.NamespaceDefault, "mpIMP1").Build(),
1182+
want: []runtimehooksv1.Variable{
1183+
{
1184+
Name: "location",
1185+
Value: toJSON("\"us-central\""),
1186+
},
1187+
{
1188+
Name: "cpu",
1189+
Value: toJSON("8"),
1190+
},
1191+
{
1192+
Name: BuiltinsName,
1193+
Value: toJSONCompact(`{
1194+
"machinePool":{
1195+
"version": "v1.21.1",
1196+
"class": "mp-class",
1197+
"name": "mp1",
1198+
"topologyName": "mp-topology",
1199+
"replicas":3,
1200+
"bootstrap":{
1201+
"configRef":{
1202+
"name": "mpBC1"
1203+
}
1204+
},
1205+
"infrastructureRef":{
1206+
"name": "mpIMP1"
1207+
}
1208+
}}`),
1209+
},
1210+
},
1211+
},
1212+
}
1213+
for _, tt := range tests {
1214+
t.Run(tt.name, func(t *testing.T) {
1215+
g := NewWithT(t)
1216+
1217+
got, err := MachinePool(tt.mpTopology, tt.mp, tt.mpBootstrapConfig, tt.mpInfrastructureMachinePool, tt.forPatch, tt.variableDefinitionsForPatch)
1218+
g.Expect(err).ToNot(HaveOccurred())
1219+
g.Expect(got).To(BeComparableTo(tt.want))
1220+
})
1221+
}
1222+
}
1223+
8561224
func toJSON(value string) apiextensionsv1.JSON {
8571225
return apiextensionsv1.JSON{Raw: []byte(value)}
8581226
}

0 commit comments

Comments
 (0)