github.com/containers/podman/v4@v4.9.4/pkg/machine/e2e/rm_test.go (about)

     1  package e2e_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/containers/podman/v4/pkg/machine"
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("podman machine rm", func() {
    14  	var (
    15  		mb      *machineTestBuilder
    16  		testDir string
    17  	)
    18  
    19  	BeforeEach(func() {
    20  		testDir, mb = setup()
    21  	})
    22  	AfterEach(func() {
    23  		teardown(originalHomeDir, testDir, mb)
    24  	})
    25  
    26  	It("bad init name", func() {
    27  		i := rmMachine{}
    28  		reallyLongName := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    29  		session, err := mb.setName(reallyLongName).setCmd(&i).run()
    30  		Expect(err).ToNot(HaveOccurred())
    31  		Expect(session).To(Exit(125))
    32  	})
    33  
    34  	It("Remove machine", func() {
    35  		name := randomString()
    36  		i := new(initMachine)
    37  		session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
    38  		Expect(err).ToNot(HaveOccurred())
    39  		Expect(session).To(Exit(0))
    40  		rm := rmMachine{}
    41  		removeSession, err := mb.setCmd(rm.withForce()).run()
    42  		Expect(err).ToNot(HaveOccurred())
    43  		Expect(removeSession).To(Exit(0))
    44  
    45  		// Inspecting a non-existent machine should fail
    46  		// which means it is gone
    47  		_, ec, err := mb.toQemuInspectInfo()
    48  		Expect(err).ToNot(HaveOccurred())
    49  		Expect(ec).To(Equal(125))
    50  
    51  		// Removing non-existent machine should fail
    52  		removeSession2, err := mb.setCmd(rm.withForce()).run()
    53  		Expect(err).ToNot(HaveOccurred())
    54  		Expect(removeSession2).To(Exit(125))
    55  		Expect(removeSession2.errorToString()).To(ContainSubstring(fmt.Sprintf("%s: VM does not exist", name)))
    56  	})
    57  
    58  	It("Remove running machine", func() {
    59  		name := randomString()
    60  		i := new(initMachine)
    61  		session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath).withNow()).run()
    62  		Expect(err).ToNot(HaveOccurred())
    63  		Expect(session).To(Exit(0))
    64  		rm := new(rmMachine)
    65  
    66  		// Removing a running machine should fail
    67  		stop, err := mb.setCmd(rm).run()
    68  		Expect(err).ToNot(HaveOccurred())
    69  		Expect(stop).To(Exit(125))
    70  		Expect(stop.errorToString()).To(ContainSubstring(fmt.Sprintf("vm \"%s\" cannot be destroyed", name)))
    71  
    72  		// Removing again with force
    73  		stopAgain, err := mb.setCmd(rm.withForce()).run()
    74  		Expect(err).ToNot(HaveOccurred())
    75  		Expect(stopAgain).To(Exit(0))
    76  
    77  		// Inspect to be dead sure
    78  		_, ec, err := mb.toQemuInspectInfo()
    79  		Expect(err).ToNot(HaveOccurred())
    80  		Expect(ec).To(Equal(125))
    81  	})
    82  
    83  	It("machine rm --save-keys, --save-ignition, --save-image", func() {
    84  		i := new(initMachine)
    85  		session, err := mb.setCmd(i.withImagePath(mb.imagePath)).run()
    86  		Expect(err).ToNot(HaveOccurred())
    87  		Expect(session).To(Exit(0))
    88  
    89  		inspect := new(inspectMachine)
    90  		inspect = inspect.withFormat("{{.SSHConfig.IdentityPath}}")
    91  		inspectSession, err := mb.setCmd(inspect).run()
    92  		Expect(err).ToNot(HaveOccurred())
    93  		key := inspectSession.outputToString()
    94  		pubkey := key + ".pub"
    95  
    96  		inspect = inspect.withFormat("{{.Image.IgnitionFile.Path}}")
    97  		inspectSession, err = mb.setCmd(inspect).run()
    98  		Expect(err).ToNot(HaveOccurred())
    99  		ign := inspectSession.outputToString()
   100  
   101  		inspect = inspect.withFormat("{{.Image.ImagePath.Path}}")
   102  		inspectSession, err = mb.setCmd(inspect).run()
   103  		Expect(err).ToNot(HaveOccurred())
   104  		img := inspectSession.outputToString()
   105  
   106  		rm := rmMachine{}
   107  		removeSession, err := mb.setCmd(rm.withForce().withSaveIgnition().withSaveImage().withSaveKeys()).run()
   108  		Expect(err).ToNot(HaveOccurred())
   109  		Expect(removeSession).To(Exit(0))
   110  
   111  		// Inspecting a non-existent machine should fail
   112  		// which means it is gone
   113  		_, ec, err := mb.toQemuInspectInfo()
   114  		Expect(err).ToNot(HaveOccurred())
   115  		Expect(ec).To(Equal(125))
   116  
   117  		_, err = os.Stat(key)
   118  		Expect(err).ToNot(HaveOccurred())
   119  		_, err = os.Stat(pubkey)
   120  		Expect(err).ToNot(HaveOccurred())
   121  
   122  		// WSL does not use ignition
   123  		if testProvider.VMType() != machine.WSLVirt {
   124  			_, err = os.Stat(ign)
   125  			Expect(err).ToNot(HaveOccurred())
   126  		}
   127  		_, err = os.Stat(img)
   128  		Expect(err).ToNot(HaveOccurred())
   129  	})
   130  })