Skip to content

Commit a606483

Browse files
committed
add e2e tests to verify ValidatingAdmissionPolicy
1 parent 3d0f9cd commit a606483

5 files changed

Lines changed: 78 additions & 6 deletions

e2e/machine_migration_helpers.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,45 @@ func createMAPIMachineWithAuthority(ctx context.Context, cl client.Client, machi
128128
return newMachine
129129
}
130130

131+
// createSameNameMachineBlockedByVAPAuthMapi attempts to create a MAPI Machine with specified authoritativeAPI and expects the creation to fail.
132+
func createSameNameMachineBlockedByVAPAuthMapi(ctx context.Context, cl client.Client, machineName string, authority mapiv1beta1.MachineAuthority, expectedErrorMessage string) {
133+
Expect(machineName).NotTo(BeEmpty(), "Machine name cannot be empty")
134+
workerLabelSelector := metav1.LabelSelector{
135+
MatchLabels: map[string]string{
136+
"machine.openshift.io/cluster-api-machine-role": "worker",
137+
},
138+
}
139+
machineList, err := mapiframework.GetMachines(ctx, cl, &workerLabelSelector)
140+
141+
Expect(err).NotTo(HaveOccurred(), "Should have successfully listed MAPI machines")
142+
Expect(machineList).NotTo(BeEmpty(), "Should have found MAPI machines in the openshift-machine-api namespace to use as a reference for creating a new one")
143+
144+
referenceMachine := machineList[0]
145+
By(fmt.Sprintf("Using MAPI machine %s as a reference", referenceMachine.Name))
146+
147+
newMachine := &mapiv1beta1.Machine{
148+
TypeMeta: metav1.TypeMeta{
149+
Kind: "Machine",
150+
APIVersion: mapiv1beta1.GroupVersion.String(),
151+
},
152+
ObjectMeta: metav1.ObjectMeta{
153+
Name: machineName,
154+
Namespace: referenceMachine.Namespace,
155+
},
156+
Spec: *referenceMachine.Spec.DeepCopy(),
157+
}
158+
159+
newMachine.Spec.ProviderID = nil
160+
newMachine.ObjectMeta.Labels = nil
161+
newMachine.Status = mapiv1beta1.MachineStatus{}
162+
newMachine.Spec.AuthoritativeAPI = authority
163+
164+
By(fmt.Sprintf("Attempting to create a MAPI machine (expecting failure) with AuthoritativeAPI: %s in namespace: %s", authority, newMachine.Namespace))
165+
err = cl.Create(ctx, newMachine)
166+
Expect(err).To(HaveOccurred(), "MAPI Machine %s creation should fail", machineName)
167+
Expect(err.Error()).To(ContainSubstring(expectedErrorMessage), "Error message should contain expected text: %s", expectedErrorMessage)
168+
}
169+
131170
// verifyMachineRunning verifies that a machine reaches Running state using the machine object directly.
132171
func verifyMachineRunning(cl client.Client, machine client.Object) {
133172
Expect(machine).NotTo(BeNil(), "Machine parameter cannot be nil")

e2e/machine_migration_mapi_authoritative_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ var _ = Describe("[sig-cluster-lifecycle][OCPFeatureGate:MachineAPIMigration] Ma
2929
var newCapiMachine *clusterv1.Machine
3030
var newMapiMachine *mapiv1beta1.Machine
3131

32+
Context("With spec.authoritativeAPI: MachineAPI and existing CAPI Machine with that name", func() {
33+
BeforeAll(func() {
34+
newCapiMachine = createCAPIMachine(ctx, cl, mapiMachineAuthMAPIName)
35+
36+
DeferCleanup(func() {
37+
By("Cleaning up machine resources")
38+
cleanupMachineResources(
39+
ctx,
40+
cl,
41+
[]*clusterv1.Machine{newCapiMachine},
42+
[]*mapiv1beta1.Machine{},
43+
)
44+
})
45+
})
46+
47+
It("should reject creation of MAPI Machine with same name as existing CAPI Machine", func() {
48+
createSameNameMachineBlockedByVAPAuthMapi(ctx, cl, mapiMachineAuthMAPIName, mapiv1beta1.MachineAuthorityMachineAPI, "a Cluster API Machine with the same name already exists")
49+
})
50+
})
51+
3252
Context("With spec.authoritativeAPI: MachineAPI and no existing CAPI Machine with that name", func() {
3353
BeforeAll(func() {
3454
newMapiMachine = createMAPIMachineWithAuthority(ctx, cl, mapiMachineAuthMAPIName, mapiv1beta1.MachineAuthorityMachineAPI)

e2e/machineset_migration_capi_authoritative_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ var _ = Describe("[sig-cluster-lifecycle][OCPFeatureGate:MachineAPIMigration] Ma
5858
verifyMachineSetPausedCondition(mapiMachineSet, mapiv1beta1.MachineAuthorityClusterAPI)
5959
})
6060

61-
// bug https://issues.redhat.com/browse/OCPBUGS-55337
62-
PIt("should verify that the non-authoritative MAPI MachineSet providerSpec has been updated to reflect the authoritative CAPI MachineSet mirror values", func() {
61+
It("should verify that the non-authoritative MAPI MachineSet providerSpec has been updated to reflect the authoritative CAPI MachineSet mirror values", func() {
6362
verifyMAPIMachineSetProviderSpec(mapiMachineSet, HaveField("InstanceType", Equal(instanceType)))
6463
})
6564
})

e2e/machineset_migration_helpers.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ func createMAPIMachineSetWithAuthoritativeAPI(ctx context.Context, cl client.Cli
8282
return mapiMachineSet
8383
}
8484

85+
// createSameNameMachineSetBlockedByVAPAuthMapi attempts to create a MAPI MachineSet with specified authoritativeAPI and expects the creation to fail.
86+
func createSameNameMachineSetBlockedByVAPAuthMapi(ctx context.Context, cl client.Client, replicas int, machineSetName string, machineSetAuthority mapiv1beta1.MachineAuthority, machineAuthority mapiv1beta1.MachineAuthority, expectedErrorMessage string) {
87+
By(fmt.Sprintf("Attempting to create MAPI MachineSet (expecting failure) with spec.authoritativeAPI: %s, spec.template.spec.authoritativeAPI: %s, replicas=%d", machineSetAuthority, machineAuthority, replicas))
88+
machineSetParams := mapiframework.BuildMachineSetParams(ctx, cl, replicas)
89+
machineSetParams.Name = machineSetName
90+
machineSetParams.Labels[mapiframework.MachineSetKey] = machineSetName
91+
machineSetParams.MachinesetAuthoritativeAPI = machineSetAuthority
92+
machineSetParams.MachineAuthoritativeAPI = machineAuthority
93+
// Remove taints as CAPI MachineSets don't support them yet. This is a known limitation tracked in https://issues.redhat.com/browse/OCPCLOUD-2861
94+
machineSetParams.Taints = []corev1.Taint{}
95+
96+
_, err := mapiframework.CreateMachineSet(cl, machineSetParams)
97+
Expect(err).To(HaveOccurred(), "MAPI MachineSet %s creation should fail", machineSetName)
98+
Expect(err.Error()).To(ContainSubstring(expectedErrorMessage), "Error message should contain expected text: %s", expectedErrorMessage)
99+
}
100+
85101
// switchMachineSetAuthoritativeAPI updates the authoritativeAPI fields of a MAPI MachineSet and its template.
86102
func switchMachineSetAuthoritativeAPI(mapiMachineSet *mapiv1beta1.MachineSet, machineSetAuthority mapiv1beta1.MachineAuthority, machineAuthority mapiv1beta1.MachineAuthority) {
87103
By(fmt.Sprintf("Switching MachineSet %s AuthoritativeAPI to spec.authoritativeAPI: %s, spec.template.spec.authoritativeAPI: %s", mapiMachineSet.Name, machineSetAuthority, machineAuthority))

e2e/machineset_migration_mapi_authoritative_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@ var _ = Describe("[sig-cluster-lifecycle][OCPFeatureGate:MachineAPIMigration] Ma
5555
})
5656
})
5757

58-
// https://issues.redhat.com/browse/OCPCLOUD-3188
59-
PIt("should reject creation of MAPI MachineSet with same name as existing CAPI MachineSet", func() {
60-
By("Creating a same name MAPI MachineSet")
61-
createMAPIMachineSetWithAuthoritativeAPI(ctx, cl, 0, existingCAPIMSAuthorityMAPIName, mapiv1beta1.MachineAuthorityMachineAPI, mapiv1beta1.MachineAuthorityMachineAPI)
58+
It("should reject creation of MAPI MachineSet with same name as existing CAPI MachineSet", func() {
59+
createSameNameMachineSetBlockedByVAPAuthMapi(ctx, cl, 0, existingCAPIMSAuthorityMAPIName, mapiv1beta1.MachineAuthorityMachineAPI, mapiv1beta1.MachineAuthorityMachineAPI, "a Cluster API MachineSet with the same name already exists")
6260
})
6361
})
6462

0 commit comments

Comments
 (0)