github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/machine/e2e/list_test.go (about) 1 package e2e 2 3 import ( 4 "strings" 5 6 "github.com/containers/common/pkg/util" 7 "github.com/hanks177/podman/v4/cmd/podman/machine" 8 jsoniter "github.com/json-iterator/go" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/gexec" 12 ) 13 14 var _ = Describe("podman machine list", func() { 15 var ( 16 mb *machineTestBuilder 17 testDir string 18 ) 19 20 BeforeEach(func() { 21 testDir, mb = setup() 22 }) 23 AfterEach(func() { 24 teardown(originalHomeDir, testDir, mb) 25 }) 26 27 It("list machine", func() { 28 list := new(listMachine) 29 firstList, err := mb.setCmd(list).run() 30 Expect(err).NotTo(HaveOccurred()) 31 Expect(firstList).Should(Exit(0)) 32 Expect(len(firstList.outputToStringSlice())).To(Equal(1)) // just the header 33 34 i := new(initMachine) 35 session, err := mb.setCmd(i.withImagePath(mb.imagePath)).run() 36 Expect(err).To(BeNil()) 37 Expect(session).To(Exit(0)) 38 39 secondList, err := mb.setCmd(list).run() 40 Expect(err).NotTo(HaveOccurred()) 41 Expect(secondList).To(Exit(0)) 42 Expect(len(secondList.outputToStringSlice())).To(Equal(2)) // one machine and the header 43 }) 44 45 It("list machines with quiet or noheading", func() { 46 // Random names for machines to test list 47 name1 := randomString(12) 48 name2 := randomString(12) 49 50 list := new(listMachine) 51 firstList, err := mb.setCmd(list.withQuiet()).run() 52 Expect(err).NotTo(HaveOccurred()) 53 Expect(firstList).Should(Exit(0)) 54 Expect(len(firstList.outputToStringSlice())).To(Equal(0)) // No header with quiet 55 56 noheaderSession, err := mb.setCmd(list.withNoHeading()).run() // noheader 57 Expect(err).NotTo(HaveOccurred()) 58 Expect(noheaderSession).Should(Exit(0)) 59 Expect(len(noheaderSession.outputToStringSlice())).To(Equal(0)) 60 61 i := new(initMachine) 62 session, err := mb.setName(name1).setCmd(i.withImagePath(mb.imagePath)).run() 63 Expect(err).To(BeNil()) 64 Expect(session).To(Exit(0)) 65 66 session2, err := mb.setName(name2).setCmd(i.withImagePath(mb.imagePath)).run() 67 Expect(err).To(BeNil()) 68 Expect(session2).To(Exit(0)) 69 70 secondList, err := mb.setCmd(list.withQuiet()).run() 71 Expect(err).NotTo(HaveOccurred()) 72 Expect(secondList).To(Exit(0)) 73 Expect(len(secondList.outputToStringSlice())).To(Equal(2)) // two machines, no header 74 75 listNames := secondList.outputToStringSlice() 76 stripAsterisk(listNames) 77 Expect(util.StringInSlice(name1, listNames)).To(BeTrue()) 78 Expect(util.StringInSlice(name2, listNames)).To(BeTrue()) 79 }) 80 81 It("list machine: check if running while starting", func() { 82 i := new(initMachine) 83 session, err := mb.setCmd(i.withImagePath(mb.imagePath)).run() 84 Expect(err).To(BeNil()) 85 Expect(session).To(Exit(0)) 86 s := new(startMachine) 87 startSession, err := mb.setCmd(s).runWithoutWait() 88 Expect(err).To(BeNil()) 89 l := new(listMachine) 90 for { // needs to be infinite because we need to check if running when inspect returns to avoid race conditions. 91 listSession, err := mb.setCmd(l).run() 92 Expect(listSession).To(Exit(0)) 93 Expect(err).To(BeNil()) 94 if startSession.ExitCode() == -1 { 95 Expect(listSession.outputToString()).NotTo(ContainSubstring("Currently running")) 96 } else { 97 break 98 } 99 } 100 Expect(startSession).To(Exit(0)) 101 listSession, err := mb.setCmd(l).run() 102 Expect(listSession).To(Exit(0)) 103 Expect(err).To(BeNil()) 104 Expect(listSession.outputToString()).To(ContainSubstring("Currently running")) 105 Expect(listSession.outputToString()).NotTo(ContainSubstring("Less than a second ago")) // check to make sure time created is accurate 106 }) 107 108 It("list with --format", func() { 109 // Random names for machines to test list 110 name1 := randomString(12) 111 112 i := new(initMachine) 113 session, err := mb.setName(name1).setCmd(i.withImagePath(mb.imagePath)).run() 114 Expect(err).To(BeNil()) 115 Expect(session).To(Exit(0)) 116 117 // go format 118 list := new(listMachine) 119 listSession, err := mb.setCmd(list.withFormat("{{.Name}}").withNoHeading()).run() 120 Expect(err).NotTo(HaveOccurred()) 121 Expect(listSession).To(Exit(0)) 122 Expect(len(listSession.outputToStringSlice())).To(Equal(1)) 123 124 listNames := listSession.outputToStringSlice() 125 stripAsterisk(listNames) 126 Expect(util.StringInSlice(name1, listNames)).To(BeTrue()) 127 128 // --format json 129 list2 := new(listMachine) 130 list2 = list2.withFormat("json") 131 listSession2, err := mb.setName("foo1").setCmd(list2).run() 132 Expect(err).To(BeNil()) 133 Expect(listSession2).To(Exit(0)) 134 135 var listResponse []*machine.ListReporter 136 err = jsoniter.Unmarshal(listSession.Bytes(), &listResponse) 137 Expect(err).To(BeNil()) 138 }) 139 }) 140 141 func stripAsterisk(sl []string) { 142 for idx, val := range sl { 143 sl[idx] = strings.TrimRight(val, "*") 144 } 145 }