github.com/containers/podman/v4@v4.9.4/test/e2e/volume_create_test.go (about) 1 package integration 2 3 import ( 4 "fmt" 5 "path/filepath" 6 7 . "github.com/containers/podman/v4/test/utils" 8 . "github.com/onsi/ginkgo/v2" 9 . "github.com/onsi/gomega" 10 . "github.com/onsi/gomega/gexec" 11 ) 12 13 var _ = Describe("Podman volume create", func() { 14 15 AfterEach(func() { 16 podmanTest.CleanupVolume() 17 }) 18 19 It("podman create volume", func() { 20 session := podmanTest.Podman([]string{"volume", "create"}) 21 session.WaitWithDefaultTimeout() 22 volName := session.OutputToString() 23 Expect(session).Should(ExitCleanly()) 24 25 check := podmanTest.Podman([]string{"volume", "ls", "-q"}) 26 check.WaitWithDefaultTimeout() 27 Expect(check.OutputToString()).To(ContainSubstring(volName)) 28 Expect(check.OutputToStringArray()).To(HaveLen(1)) 29 }) 30 31 It("podman create volume with name", func() { 32 session := podmanTest.Podman([]string{"volume", "create", "myvol"}) 33 session.WaitWithDefaultTimeout() 34 volName := session.OutputToString() 35 Expect(session).Should(ExitCleanly()) 36 37 check := podmanTest.Podman([]string{"volume", "ls", "-q"}) 38 check.WaitWithDefaultTimeout() 39 Expect(check.OutputToString()).To(ContainSubstring(volName)) 40 Expect(check.OutputToStringArray()).To(HaveLen(1)) 41 }) 42 43 It("podman create volume with existing name fails", func() { 44 session := podmanTest.Podman([]string{"volume", "create", "myvol"}) 45 session.WaitWithDefaultTimeout() 46 Expect(session).Should(ExitCleanly()) 47 48 session = podmanTest.Podman([]string{"volume", "create", "myvol"}) 49 session.WaitWithDefaultTimeout() 50 Expect(session).To(ExitWithError()) 51 }) 52 53 It("podman create volume --ignore", func() { 54 session := podmanTest.Podman([]string{"volume", "create", "myvol"}) 55 session.WaitWithDefaultTimeout() 56 volName := session.OutputToString() 57 Expect(session).Should(ExitCleanly()) 58 59 session = podmanTest.Podman([]string{"volume", "create", "--ignore", "myvol"}) 60 session.WaitWithDefaultTimeout() 61 Expect(session).Should(ExitCleanly()) 62 Expect(session.OutputToString()).To(Equal(volName)) 63 }) 64 65 It("podman create and export volume", func() { 66 if podmanTest.RemoteTest { 67 Skip("Volume export check does not work with a remote client") 68 } 69 70 volName := "my_vol_" + RandomString(10) 71 session := podmanTest.Podman([]string{"volume", "create", volName}) 72 session.WaitWithDefaultTimeout() 73 Expect(session).Should(ExitCleanly()) 74 Expect(session.OutputToString()).To(Equal(volName)) 75 76 helloString := "hello-" + RandomString(20) 77 session = podmanTest.Podman([]string{"run", "--volume", volName + ":/data", ALPINE, "sh", "-c", "echo " + helloString + " >> /data/test"}) 78 session.WaitWithDefaultTimeout() 79 Expect(session).Should(ExitCleanly()) 80 81 // export to tar file... 82 helloTar := filepath.Join(podmanTest.TempDir, "hello.tar") 83 check := podmanTest.Podman([]string{"volume", "export", "-o", helloTar, volName}) 84 check.WaitWithDefaultTimeout() 85 Expect(check).Should(ExitCleanly()) 86 87 // ...then confirm that tar file has our desired content. 88 // These flags emit filename to stderr (-v), contents to stdout 89 tar := SystemExec("tar", []string{"-x", "-v", "--to-stdout", "-f", helloTar}) 90 tar.WaitWithDefaultTimeout() 91 Expect(tar).To(Exit(0)) 92 Expect(tar.ErrorToString()).To(Equal("test")) 93 Expect(tar.OutputToString()).To(Equal(helloString)) 94 }) 95 96 It("podman create and import volume", func() { 97 if podmanTest.RemoteTest { 98 Skip("Volume export check does not work with a remote client") 99 } 100 101 volName := "my_vol_" + RandomString(10) 102 session := podmanTest.Podman([]string{"volume", "create", volName}) 103 session.WaitWithDefaultTimeout() 104 Expect(session).Should(ExitCleanly()) 105 Expect(session.OutputToString()).To(Equal(volName)) 106 107 session = podmanTest.Podman([]string{"run", "--volume", volName + ":/data", ALPINE, "sh", "-c", "echo hello >> /data/test"}) 108 session.WaitWithDefaultTimeout() 109 Expect(session).Should(ExitCleanly()) 110 111 helloTar := filepath.Join(podmanTest.TempDir, "hello.tar") 112 session = podmanTest.Podman([]string{"volume", "export", volName, "--output", helloTar}) 113 session.WaitWithDefaultTimeout() 114 Expect(session).Should(ExitCleanly()) 115 116 session = podmanTest.Podman([]string{"volume", "create", "my_vol2"}) 117 session.WaitWithDefaultTimeout() 118 Expect(session).Should(ExitCleanly()) 119 120 session = podmanTest.Podman([]string{"volume", "import", "my_vol2", helloTar}) 121 session.WaitWithDefaultTimeout() 122 Expect(session).Should(ExitCleanly()) 123 Expect(session.OutputToString()).To(Equal(""), "output of volume import") 124 125 session = podmanTest.Podman([]string{"run", "--volume", "my_vol2:/data", ALPINE, "cat", "/data/test"}) 126 session.WaitWithDefaultTimeout() 127 Expect(session.OutputToString()).To(ContainSubstring("hello")) 128 }) 129 130 It("podman import/export volume should fail", func() { 131 // try import on volume or source which does not exist 132 SkipIfRemote("Volume export check does not work with a remote client") 133 134 session := podmanTest.Podman([]string{"volume", "import", "notfound", "notfound.tar"}) 135 session.WaitWithDefaultTimeout() 136 Expect(session).To(ExitWithError()) 137 Expect(session.ErrorToString()).To(ContainSubstring("open notfound.tar: no such file or directory")) 138 139 session = podmanTest.Podman([]string{"volume", "import", "notfound", "-"}) 140 session.WaitWithDefaultTimeout() 141 Expect(session).To(ExitWithError()) 142 Expect(session.ErrorToString()).To(ContainSubstring("no such volume notfound")) 143 144 session = podmanTest.Podman([]string{"volume", "export", "notfound"}) 145 session.WaitWithDefaultTimeout() 146 Expect(session).To(ExitWithError()) 147 Expect(session.ErrorToString()).To(ContainSubstring("no such volume notfound")) 148 }) 149 150 It("podman create volume with bad volume option", func() { 151 session := podmanTest.Podman([]string{"volume", "create", "--opt", "badOpt=bad"}) 152 session.WaitWithDefaultTimeout() 153 Expect(session).To(ExitWithError()) 154 }) 155 156 It("podman create volume with o=uid,gid", func() { 157 volName := "testVol" 158 uid := "3000" 159 gid := "4000" 160 session := podmanTest.Podman([]string{"volume", "create", "--opt", fmt.Sprintf("o=uid=%s,gid=%s", uid, gid), volName}) 161 session.WaitWithDefaultTimeout() 162 Expect(session).Should(ExitCleanly()) 163 164 inspectUID := podmanTest.Podman([]string{"volume", "inspect", "--format", "{{ .UID }}", volName}) 165 inspectUID.WaitWithDefaultTimeout() 166 Expect(inspectUID).Should(ExitCleanly()) 167 Expect(inspectUID.OutputToString()).To(Equal(uid)) 168 169 inspectGID := podmanTest.Podman([]string{"volume", "inspect", "--format", "{{ .GID }}", volName}) 170 inspectGID.WaitWithDefaultTimeout() 171 Expect(inspectGID).Should(ExitCleanly()) 172 Expect(inspectGID.OutputToString()).To(Equal(gid)) 173 174 // options should contain `uid=3000,gid=4000:3000:4000` 175 optionFormat := `{{ .Options.o }}:{{ .Options.UID }}:{{ .Options.GID }}` 176 optionStrFormatExpect := fmt.Sprintf(`uid=%s,gid=%s:%s:%s`, uid, gid, uid, gid) 177 inspectOpts := podmanTest.Podman([]string{"volume", "inspect", "--format", optionFormat, volName}) 178 inspectOpts.WaitWithDefaultTimeout() 179 Expect(inspectOpts).Should(ExitCleanly()) 180 Expect(inspectOpts.OutputToString()).To(Equal(optionStrFormatExpect)) 181 }) 182 183 It("image-backed volume basic functionality", func() { 184 podmanTest.AddImageToRWStore(fedoraMinimal) 185 volName := "testvol" 186 volCreate := podmanTest.Podman([]string{"volume", "create", "--driver", "image", "--opt", fmt.Sprintf("image=%s", fedoraMinimal), volName}) 187 volCreate.WaitWithDefaultTimeout() 188 Expect(volCreate).Should(ExitCleanly()) 189 190 runCmd := podmanTest.Podman([]string{"run", "-v", fmt.Sprintf("%s:/test", volName), ALPINE, "cat", "/test/etc/redhat-release"}) 191 runCmd.WaitWithDefaultTimeout() 192 Expect(runCmd).Should(ExitCleanly()) 193 Expect(runCmd.OutputToString()).To(ContainSubstring("Fedora")) 194 195 rmCmd := podmanTest.Podman([]string{"rmi", "--force", fedoraMinimal}) 196 rmCmd.WaitWithDefaultTimeout() 197 Expect(rmCmd).Should(ExitCleanly()) 198 199 psCmd := podmanTest.Podman([]string{"ps", "-aq"}) 200 psCmd.WaitWithDefaultTimeout() 201 Expect(psCmd).Should(ExitCleanly()) 202 Expect(psCmd.OutputToString()).To(BeEmpty()) 203 204 volumesCmd := podmanTest.Podman([]string{"volume", "ls", "-q"}) 205 volumesCmd.WaitWithDefaultTimeout() 206 Expect(volumesCmd).Should(ExitCleanly()) 207 Expect(volumesCmd.OutputToString()).To(Not(ContainSubstring(volName))) 208 }) 209 210 It("image-backed volume force removal", func() { 211 podmanTest.AddImageToRWStore(fedoraMinimal) 212 volName := "testvol" 213 volCreate := podmanTest.Podman([]string{"volume", "create", "--driver", "image", "--opt", fmt.Sprintf("image=%s", fedoraMinimal), volName}) 214 volCreate.WaitWithDefaultTimeout() 215 Expect(volCreate).Should(ExitCleanly()) 216 217 runCmd := podmanTest.Podman([]string{"run", "-v", fmt.Sprintf("%s:/test", volName), ALPINE, "cat", "/test/etc/redhat-release"}) 218 runCmd.WaitWithDefaultTimeout() 219 Expect(runCmd).Should(ExitCleanly()) 220 Expect(runCmd.OutputToString()).To(ContainSubstring("Fedora")) 221 222 rmCmd := podmanTest.Podman([]string{"volume", "rm", "--force", volName}) 223 rmCmd.WaitWithDefaultTimeout() 224 Expect(rmCmd).Should(ExitCleanly()) 225 226 psCmd := podmanTest.Podman([]string{"ps", "-aq"}) 227 psCmd.WaitWithDefaultTimeout() 228 Expect(psCmd).Should(ExitCleanly()) 229 Expect(psCmd.OutputToString()).To(BeEmpty()) 230 231 volumesCmd := podmanTest.Podman([]string{"volume", "ls", "-q"}) 232 volumesCmd.WaitWithDefaultTimeout() 233 Expect(volumesCmd).Should(ExitCleanly()) 234 Expect(volumesCmd.OutputToString()).To(Not(ContainSubstring(volName))) 235 }) 236 })