Skip to content

Commit b7d313f

Browse files
committed
Enhance machine clean-up
1 parent 9c1afa7 commit b7d313f

13 files changed

+47
-165
lines changed

internal/controllers/controllers_suite_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,22 @@ func updateMachine(machine *api.Machine) (*api.Machine, error) {
268268

269269
func cleanupMachine(machineID string) func(SpecContext) {
270270
return func(ctx SpecContext) {
271-
By(fmt.Sprintf("cleaning up machine ID=%s", machineID))
272-
err := deleteMachine(machineID)
273-
Expect(err).To(SatisfyAny(
274-
BeNil(),
275-
MatchError(ContainSubstring("NotFound")),
276-
))
271+
By(fmt.Sprintf("Cleaning up machine ID=%s", machineID))
272+
Eventually(func(g Gomega) error {
273+
err := deleteMachine(machineID)
274+
GinkgoWriter.Printf("Deleting machine ID=%s: err=%v\n", machineID, err)
275+
if success, _ := MatchError(ContainSubstring("no such file or directory")).Match(err); success {
276+
return nil
277+
}
278+
return err
279+
}).Should(Succeed())
280+
277281
Eventually(func(g Gomega) bool {
278-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(machineID))
282+
_, err := libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(machineID))
279283
if err != nil {
280284
GinkgoWriter.Printf("Checking if domain still exists for machine ID=%s: err=%v\n", machineID, err)
281285
}
282286
return libvirt.IsNotFound(err)
283-
}).Should(BeTrue())
287+
}).WithPolling(time.Second).Should(BeTrue())
284288
}
285289
}

internal/server/integration/event_list_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,7 @@ var _ = Describe("ListEvents", func() {
3636
Expect(err).NotTo(HaveOccurred())
3737
Expect(createResp).NotTo(BeNil())
3838

39-
DeferCleanup(func(ctx SpecContext) {
40-
Eventually(func(g Gomega) bool {
41-
_, err := machineClient.DeleteMachine(ctx, &iri.DeleteMachineRequest{MachineId: createResp.Machine.Metadata.Id})
42-
g.Expect(err).To(SatisfyAny(
43-
BeNil(),
44-
MatchError(ContainSubstring("NotFound")),
45-
))
46-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(createResp.Machine.Metadata.Id))
47-
return libvirt.IsNotFound(err)
48-
}).Should(BeTrue())
49-
})
39+
DeferCleanup(cleanupMachine(createResp.Machine.Metadata.Id))
5040

5141
By("ensuring domain and domain XML is created for machine")
5242
var domain libvirt.Domain

internal/server/integration/exec_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,7 @@ var _ = Describe("Exec", func() {
3737
Expect(err).NotTo(HaveOccurred())
3838
Expect(createResp).NotTo(BeNil())
3939

40-
DeferCleanup(func(ctx SpecContext) {
41-
Eventually(func(g Gomega) bool {
42-
_, err := machineClient.DeleteMachine(ctx, &iri.DeleteMachineRequest{MachineId: createResp.Machine.Metadata.Id})
43-
g.Expect(err).To(SatisfyAny(
44-
BeNil(),
45-
MatchError(ContainSubstring("NotFound")),
46-
))
47-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(createResp.Machine.Metadata.Id))
48-
return libvirt.IsNotFound(err)
49-
}).Should(BeTrue())
50-
})
40+
DeferCleanup(cleanupMachine(createResp.Machine.Metadata.Id))
5141

5242
By("ensuring domain and domain XML is created for machine")
5343
var domain libvirt.Domain

internal/server/integration/machine_annotations_update_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,7 @@ var _ = Describe("UpdateMachineAnnotations", func() {
3535
Expect(err).NotTo(HaveOccurred())
3636
Expect(createResp).NotTo(BeNil())
3737

38-
DeferCleanup(func(ctx SpecContext) {
39-
Eventually(func(g Gomega) bool {
40-
_, err := machineClient.DeleteMachine(ctx, &iri.DeleteMachineRequest{MachineId: createResp.Machine.Metadata.Id})
41-
g.Expect(err).To(SatisfyAny(
42-
BeNil(),
43-
MatchError(ContainSubstring("NotFound")),
44-
))
45-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(createResp.Machine.Metadata.Id))
46-
return libvirt.IsNotFound(err)
47-
}).Should(BeTrue())
48-
})
38+
DeferCleanup(cleanupMachine(createResp.Machine.Metadata.Id))
4939

5040
By("ensuring domain and domain XML is created for machine")
5141
var domain libvirt.Domain

internal/server/integration/machine_create_test.go

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ var _ = Describe("CreateMachine", func() {
3333
Expect(err).NotTo(HaveOccurred())
3434
Expect(createResp).NotTo(BeNil())
3535

36+
DeferCleanup(cleanupMachine(createResp.Machine.Metadata.Id))
37+
3638
By("ensuring the correct creation response")
3739
Expect(createResp).Should(SatisfyAll(
3840
HaveField("Machine.Metadata.Id", Not(BeEmpty())),
@@ -48,18 +50,6 @@ var _ = Describe("CreateMachine", func() {
4850
HaveField("Machine.Status.NetworkInterfaces", BeNil()),
4951
))
5052

51-
DeferCleanup(func(ctx SpecContext) {
52-
Eventually(func(g Gomega) bool {
53-
_, err := machineClient.DeleteMachine(ctx, &iri.DeleteMachineRequest{MachineId: createResp.Machine.Metadata.Id})
54-
g.Expect(err).To(SatisfyAny(
55-
BeNil(),
56-
MatchError(ContainSubstring("NotFound")),
57-
))
58-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(createResp.Machine.Metadata.Id))
59-
return libvirt.IsNotFound(err)
60-
}).Should(BeTrue())
61-
})
62-
6353
By("ensuring domain and domain XML is created for machine")
6454
var domain libvirt.Domain
6555
Eventually(func() error {
@@ -152,17 +142,7 @@ var _ = Describe("CreateMachine", func() {
152142
HaveField("Machine.Status.NetworkInterfaces", BeNil()),
153143
))
154144

155-
DeferCleanup(func(ctx SpecContext) {
156-
Eventually(func(g Gomega) bool {
157-
_, err := machineClient.DeleteMachine(ctx, &iri.DeleteMachineRequest{MachineId: createResp.Machine.Metadata.Id})
158-
g.Expect(err).To(SatisfyAny(
159-
BeNil(),
160-
MatchError(ContainSubstring("NotFound")),
161-
))
162-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(createResp.Machine.Metadata.Id))
163-
return libvirt.IsNotFound(err)
164-
}).Should(BeTrue())
165-
})
145+
DeferCleanup(cleanupMachine(createResp.Machine.Metadata.Id))
166146

167147
By("ensuring domain and domain XML is created for machine")
168148
var domain libvirt.Domain
@@ -251,6 +231,8 @@ var _ = Describe("CreateMachine", func() {
251231
Expect(err).NotTo(HaveOccurred())
252232
Expect(createResp).NotTo(BeNil())
253233

234+
DeferCleanup(cleanupMachine(createResp.Machine.Metadata.Id))
235+
254236
By("ensuring the correct creation response")
255237
Expect(createResp).Should(SatisfyAll(
256238
HaveField("Machine.Metadata.Id", Not(BeEmpty())),
@@ -285,18 +267,6 @@ var _ = Describe("CreateMachine", func() {
285267
HaveField("Machine.Status.NetworkInterfaces", BeNil()),
286268
))
287269

288-
DeferCleanup(func(ctx SpecContext) {
289-
Eventually(func(g Gomega) bool {
290-
_, err := machineClient.DeleteMachine(ctx, &iri.DeleteMachineRequest{MachineId: createResp.Machine.Metadata.Id})
291-
g.Expect(err).To(SatisfyAny(
292-
BeNil(),
293-
MatchError(ContainSubstring("NotFound")),
294-
))
295-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(createResp.Machine.Metadata.Id))
296-
return libvirt.IsNotFound(err)
297-
}).Should(BeTrue())
298-
})
299-
300270
By("ensuring domain and domain XML is created for machine")
301271
var domain libvirt.Domain
302272
Eventually(func() error {
@@ -390,6 +360,8 @@ var _ = Describe("CreateMachine", func() {
390360
Expect(err).NotTo(HaveOccurred())
391361
Expect(createResp).NotTo(BeNil())
392362

363+
DeferCleanup(cleanupMachine(createResp.Machine.Metadata.Id))
364+
393365
By("ensuring the correct creation response")
394366
Expect(createResp).Should(SatisfyAll(
395367
HaveField("Machine.Metadata.Id", Not(BeEmpty())),
@@ -430,18 +402,6 @@ var _ = Describe("CreateMachine", func() {
430402
HaveField("Machine.Status.NetworkInterfaces", BeNil()),
431403
))
432404

433-
DeferCleanup(func(ctx SpecContext) {
434-
Eventually(func(g Gomega) bool {
435-
_, err := machineClient.DeleteMachine(ctx, &iri.DeleteMachineRequest{MachineId: createResp.Machine.Metadata.Id})
436-
g.Expect(err).To(SatisfyAny(
437-
BeNil(),
438-
MatchError(ContainSubstring("NotFound")),
439-
))
440-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(createResp.Machine.Metadata.Id))
441-
return libvirt.IsNotFound(err)
442-
}).Should(BeTrue())
443-
})
444-
445405
By("ensuring domain and domain XML is created for machine")
446406
var domain libvirt.Domain
447407
Eventually(func() error {

internal/server/integration/machine_list_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,7 @@ var _ = Describe("ListMachine", func() {
4141
Expect(err).NotTo(HaveOccurred())
4242
Expect(createResp).NotTo(BeNil())
4343

44-
DeferCleanup(func(ctx SpecContext) {
45-
Eventually(func(g Gomega) bool {
46-
_, err := machineClient.DeleteMachine(ctx, &iri.DeleteMachineRequest{MachineId: createResp.Machine.Metadata.Id})
47-
g.Expect(err).To(SatisfyAny(
48-
BeNil(),
49-
MatchError(ContainSubstring("NotFound")),
50-
))
51-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(createResp.Machine.Metadata.Id))
52-
return libvirt.IsNotFound(err)
53-
}).Should(BeTrue())
54-
})
44+
DeferCleanup(cleanupMachine(createResp.Machine.Metadata.Id))
5545

5646
By("ensuring domain and domain XML is created for machine")
5747
var domain libvirt.Domain

internal/server/integration/machine_networkinterface_attach_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,7 @@ var _ = Describe("AttachNetworkInterface", func() {
4545
Expect(err).NotTo(HaveOccurred())
4646
Expect(createResp).NotTo(BeNil())
4747

48-
DeferCleanup(func(ctx SpecContext) {
49-
Eventually(func(g Gomega) bool {
50-
_, err := machineClient.DeleteMachine(ctx, &iri.DeleteMachineRequest{MachineId: createResp.Machine.Metadata.Id})
51-
g.Expect(err).To(SatisfyAny(
52-
BeNil(),
53-
MatchError(ContainSubstring("NotFound")),
54-
))
55-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(createResp.Machine.Metadata.Id))
56-
return libvirt.IsNotFound(err)
57-
}).Should(BeTrue())
58-
})
48+
DeferCleanup(cleanupMachine(createResp.Machine.Metadata.Id))
5949

6050
By("ensuring domain and domain XML is created for machine")
6151
var domain libvirt.Domain

internal/server/integration/machine_networkinterface_detach_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,7 @@ var _ = Describe("DetachNetworkInterface", func() {
6363
Expect(err).NotTo(HaveOccurred())
6464
Expect(createResp).NotTo(BeNil())
6565

66-
DeferCleanup(func(ctx SpecContext) {
67-
Eventually(func(g Gomega) bool {
68-
_, err := machineClient.DeleteMachine(ctx, &iri.DeleteMachineRequest{MachineId: createResp.Machine.Metadata.Id})
69-
g.Expect(err).To(SatisfyAny(
70-
BeNil(),
71-
MatchError(ContainSubstring("NotFound")),
72-
))
73-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(createResp.Machine.Metadata.Id))
74-
return libvirt.IsNotFound(err)
75-
}).Should(BeTrue())
76-
})
66+
DeferCleanup(cleanupMachine(createResp.Machine.Metadata.Id))
7767

7868
By("ensuring domain and domain XML is created for machine")
7969
var domain libvirt.Domain

internal/server/integration/machine_powerstate_update_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,7 @@ var _ = Describe("UpdateMachinePower", func() {
3636
Expect(err).NotTo(HaveOccurred())
3737
Expect(createResp).NotTo(BeNil())
3838

39-
DeferCleanup(func(ctx SpecContext) {
40-
Eventually(func(g Gomega) bool {
41-
_, err := machineClient.DeleteMachine(ctx, &iri.DeleteMachineRequest{MachineId: createResp.Machine.Metadata.Id})
42-
g.Expect(err).To(SatisfyAny(
43-
BeNil(),
44-
MatchError(ContainSubstring("NotFound")),
45-
))
46-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(createResp.Machine.Metadata.Id))
47-
return libvirt.IsNotFound(err)
48-
}).Should(BeTrue())
49-
})
39+
DeferCleanup(cleanupMachine(createResp.Machine.Metadata.Id))
5040

5141
By("ensuring domain and domain XML is created for machine")
5242
var domain libvirt.Domain

internal/server/integration/machine_volume_attach_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,7 @@ var _ = Describe("AttachVolume", func() {
4646
Expect(err).NotTo(HaveOccurred())
4747
Expect(createResp).NotTo(BeNil())
4848

49-
DeferCleanup(func(ctx SpecContext) {
50-
Eventually(func(g Gomega) bool {
51-
_, err := machineClient.DeleteMachine(ctx, &iri.DeleteMachineRequest{MachineId: createResp.Machine.Metadata.Id})
52-
g.Expect(err).To(SatisfyAny(
53-
BeNil(),
54-
MatchError(ContainSubstring("NotFound")),
55-
))
56-
_, err = libvirtConn.DomainLookupByUUID(libvirtutils.UUIDStringToBytes(createResp.Machine.Metadata.Id))
57-
return libvirt.IsNotFound(err)
58-
}).Should(BeTrue())
59-
})
49+
DeferCleanup(cleanupMachine(createResp.Machine.Metadata.Id))
6050

6151
By("ensuring domain and domain XML is created for machine")
6252
var domain libvirt.Domain

0 commit comments

Comments
 (0)