From c3dbfa9a1e15498ef00ae106e1b212040cdbc246 Mon Sep 17 00:00:00 2001 From: Ashley Cui Date: Thu, 17 Aug 2023 03:11:10 -0400 Subject: [PATCH] Update machine rm tests Remove with --save-ignition, --save-image, --save-keys Removing a running machine should result in error Signed-off-by: Ashley Cui --- pkg/machine/e2e/config_rm_test.go | 6 ++-- pkg/machine/e2e/rm_test.go | 56 +++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/pkg/machine/e2e/config_rm_test.go b/pkg/machine/e2e/config_rm_test.go index 1f9c9b4ecc..f0cf15a06f 100644 --- a/pkg/machine/e2e/config_rm_test.go +++ b/pkg/machine/e2e/config_rm_test.go @@ -40,17 +40,17 @@ func (i *rmMachine) withForce() *rmMachine { return i } -func (i *rmMachine) withSaveIgnition() *rmMachine { //nolint:unused +func (i *rmMachine) withSaveIgnition() *rmMachine { i.saveIgnition = true return i } -func (i *rmMachine) withSaveImage() *rmMachine { //nolint:unused +func (i *rmMachine) withSaveImage() *rmMachine { i.saveImage = true return i } -func (i *rmMachine) withSaveKeys() *rmMachine { //nolint:unused +func (i *rmMachine) withSaveKeys() *rmMachine { i.saveKeys = true return i } diff --git a/pkg/machine/e2e/rm_test.go b/pkg/machine/e2e/rm_test.go index 1b761d3700..c479292985 100644 --- a/pkg/machine/e2e/rm_test.go +++ b/pkg/machine/e2e/rm_test.go @@ -1,6 +1,9 @@ package e2e_test import ( + "fmt" + "os" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" . "github.com/onsi/gomega/gexec" @@ -33,8 +36,9 @@ var _ = Describe("podman machine rm", func() { Expect(err).ToNot(HaveOccurred()) Expect(session).To(Exit(0)) rm := rmMachine{} - _, err = mb.setCmd(rm.withForce()).run() + removeSession, err := mb.setCmd(rm.withForce()).run() Expect(err).ToNot(HaveOccurred()) + Expect(removeSession).To(Exit(0)) // Inspecting a non-existent machine should fail // which means it is gone @@ -44,8 +48,9 @@ var _ = Describe("podman machine rm", func() { }) It("Remove running machine", func() { + name := randomString() i := new(initMachine) - session, err := mb.setCmd(i.withImagePath(mb.imagePath).withNow()).run() + session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run() Expect(err).ToNot(HaveOccurred()) Expect(session).To(Exit(0)) rm := new(rmMachine) @@ -54,6 +59,7 @@ var _ = Describe("podman machine rm", func() { stop, err := mb.setCmd(rm).run() Expect(err).ToNot(HaveOccurred()) Expect(stop).To(Exit(125)) + Expect(stop.errorToString()).To(ContainSubstring(fmt.Sprintf("vm \"%s\" cannot be destroyed", name))) // Removing again with force stopAgain, err := mb.setCmd(rm.withForce()).run() @@ -65,4 +71,50 @@ var _ = Describe("podman machine rm", func() { Expect(err).ToNot(HaveOccurred()) Expect(ec).To(Equal(125)) }) + + It("machine rm --save-keys, --save-ignition, --save-image", func() { + + i := new(initMachine) + session, err := mb.setCmd(i.withImagePath(mb.imagePath)).run() + Expect(err).ToNot(HaveOccurred()) + Expect(session).To(Exit(0)) + + inspect := new(inspectMachine) + inspect = inspect.withFormat("{{.SSHConfig.IdentityPath}}") + inspectSession, err := mb.setCmd(inspect).run() + Expect(err).ToNot(HaveOccurred()) + key := inspectSession.outputToString() + pubkey := key + ".pub" + + inspect = inspect.withFormat("{{.Image.IgnitionFile.Path}}") + inspectSession, err = mb.setCmd(inspect).run() + Expect(err).ToNot(HaveOccurred()) + ign := inspectSession.outputToString() + + inspect = inspect.withFormat("{{.Image.ImagePath.Path}}") + inspectSession, err = mb.setCmd(inspect).run() + Expect(err).ToNot(HaveOccurred()) + img := inspectSession.outputToString() + + rm := rmMachine{} + removeSession, err := mb.setCmd(rm.withForce().withSaveIgnition().withSaveImage().withSaveKeys()).run() + Expect(err).ToNot(HaveOccurred()) + Expect(removeSession).To(Exit(0)) + + // Inspecting a non-existent machine should fail + // which means it is gone + _, ec, err := mb.toQemuInspectInfo() + Expect(err).ToNot(HaveOccurred()) + Expect(ec).To(Equal(125)) + + _, err = os.Stat(key) + Expect(err).ToNot(HaveOccurred()) + _, err = os.Stat(pubkey) + Expect(err).ToNot(HaveOccurred()) + _, err = os.Stat(ign) + Expect(err).ToNot(HaveOccurred()) + _, err = os.Stat(img) + Expect(err).ToNot(HaveOccurred()) + + }) })