github.com/containers/podman/v5@v5.1.0-rc1/test/e2e/volume_create_test.go (about) 1 package integration 2 3 import ( 4 "fmt" 5 "path/filepath" 6 7 . "github.com/containers/podman/v5/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(125, "volume with name myvol already exists: volume already exists")) 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(125, "open notfound.tar: no such file or directory")) 137 138 session = podmanTest.Podman([]string{"volume", "import", "notfound", "-"}) 139 session.WaitWithDefaultTimeout() 140 Expect(session).To(ExitWithError(125, "no such volume notfound")) 141 142 session = podmanTest.Podman([]string{"volume", "export", "notfound"}) 143 session.WaitWithDefaultTimeout() 144 Expect(session).To(ExitWithError(125, "no such volume notfound")) 145 }) 146 147 It("podman create volume with bad volume option", func() { 148 session := podmanTest.Podman([]string{"volume", "create", "--opt", "badOpt=bad"}) 149 session.WaitWithDefaultTimeout() 150 Expect(session).To(ExitWithError(125, "invalid mount option badOpt for driver 'local': invalid argument")) 151 }) 152 153 It("podman create volume with o=uid,gid", func() { 154 volName := "testVol" 155 uid := "3000" 156 gid := "4000" 157 session := podmanTest.Podman([]string{"volume", "create", "--opt", fmt.Sprintf("o=uid=%s,gid=%s", uid, gid), volName}) 158 session.WaitWithDefaultTimeout() 159 Expect(session).Should(ExitCleanly()) 160 161 inspectUID := podmanTest.Podman([]string{"volume", "inspect", "--format", "{{ .UID }}", volName}) 162 inspectUID.WaitWithDefaultTimeout() 163 Expect(inspectUID).Should(ExitCleanly()) 164 Expect(inspectUID.OutputToString()).To(Equal(uid)) 165 166 inspectGID := podmanTest.Podman([]string{"volume", "inspect", "--format", "{{ .GID }}", volName}) 167 inspectGID.WaitWithDefaultTimeout() 168 Expect(inspectGID).Should(ExitCleanly()) 169 Expect(inspectGID.OutputToString()).To(Equal(gid)) 170 171 // options should contain `uid=3000,gid=4000:3000:4000` 172 optionFormat := `{{ .Options.o }}:{{ .Options.UID }}:{{ .Options.GID }}` 173 optionStrFormatExpect := fmt.Sprintf(`uid=%s,gid=%s:%s:%s`, uid, gid, uid, gid) 174 inspectOpts := podmanTest.Podman([]string{"volume", "inspect", "--format", optionFormat, volName}) 175 inspectOpts.WaitWithDefaultTimeout() 176 Expect(inspectOpts).Should(ExitCleanly()) 177 Expect(inspectOpts.OutputToString()).To(Equal(optionStrFormatExpect)) 178 }) 179 180 It("image-backed volume basic functionality", func() { 181 podmanTest.AddImageToRWStore(fedoraMinimal) 182 volName := "testvol" 183 volCreate := podmanTest.Podman([]string{"volume", "create", "--driver", "image", "--opt", fmt.Sprintf("image=%s", fedoraMinimal), volName}) 184 volCreate.WaitWithDefaultTimeout() 185 Expect(volCreate).Should(ExitCleanly()) 186 187 runCmd := podmanTest.Podman([]string{"run", "-v", fmt.Sprintf("%s:/test", volName), ALPINE, "cat", "/test/etc/redhat-release"}) 188 runCmd.WaitWithDefaultTimeout() 189 Expect(runCmd).Should(ExitCleanly()) 190 Expect(runCmd.OutputToString()).To(ContainSubstring("Fedora")) 191 192 rmCmd := podmanTest.Podman([]string{"rmi", "--force", fedoraMinimal}) 193 rmCmd.WaitWithDefaultTimeout() 194 Expect(rmCmd).Should(ExitCleanly()) 195 196 psCmd := podmanTest.Podman([]string{"ps", "-aq"}) 197 psCmd.WaitWithDefaultTimeout() 198 Expect(psCmd).Should(ExitCleanly()) 199 Expect(psCmd.OutputToString()).To(BeEmpty()) 200 201 volumesCmd := podmanTest.Podman([]string{"volume", "ls", "-q"}) 202 volumesCmd.WaitWithDefaultTimeout() 203 Expect(volumesCmd).Should(ExitCleanly()) 204 Expect(volumesCmd.OutputToString()).To(Not(ContainSubstring(volName))) 205 }) 206 207 It("image-backed volume force removal", func() { 208 podmanTest.AddImageToRWStore(fedoraMinimal) 209 volName := "testvol" 210 volCreate := podmanTest.Podman([]string{"volume", "create", "--driver", "image", "--opt", fmt.Sprintf("image=%s", fedoraMinimal), volName}) 211 volCreate.WaitWithDefaultTimeout() 212 Expect(volCreate).Should(ExitCleanly()) 213 214 runCmd := podmanTest.Podman([]string{"run", "-v", fmt.Sprintf("%s:/test", volName), ALPINE, "cat", "/test/etc/redhat-release"}) 215 runCmd.WaitWithDefaultTimeout() 216 Expect(runCmd).Should(ExitCleanly()) 217 Expect(runCmd.OutputToString()).To(ContainSubstring("Fedora")) 218 219 rmCmd := podmanTest.Podman([]string{"volume", "rm", "--force", volName}) 220 rmCmd.WaitWithDefaultTimeout() 221 Expect(rmCmd).Should(ExitCleanly()) 222 223 psCmd := podmanTest.Podman([]string{"ps", "-aq"}) 224 psCmd.WaitWithDefaultTimeout() 225 Expect(psCmd).Should(ExitCleanly()) 226 Expect(psCmd.OutputToString()).To(BeEmpty()) 227 228 volumesCmd := podmanTest.Podman([]string{"volume", "ls", "-q"}) 229 volumesCmd.WaitWithDefaultTimeout() 230 Expect(volumesCmd).Should(ExitCleanly()) 231 Expect(volumesCmd.OutputToString()).To(Not(ContainSubstring(volName))) 232 }) 233 })