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