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"
13
var _ = Describe("Podman volume create", func() {
16
podmanTest.CleanupVolume()
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())
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))
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())
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))
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())
48
session = podmanTest.Podman([]string{"volume", "create", "myvol"})
49
session.WaitWithDefaultTimeout()
50
Expect(session).To(ExitWithError())
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())
59
session = podmanTest.Podman([]string{"volume", "create", "--ignore", "myvol"})
60
session.WaitWithDefaultTimeout()
61
Expect(session).Should(ExitCleanly())
62
Expect(session.OutputToString()).To(Equal(volName))
65
It("podman create and export volume", func() {
66
if podmanTest.RemoteTest {
67
Skip("Volume export check does not work with a remote client")
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))
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())
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())
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))
96
It("podman create and import volume", func() {
97
if podmanTest.RemoteTest {
98
Skip("Volume export check does not work with a remote client")
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))
107
session = podmanTest.Podman([]string{"run", "--volume", volName + ":/data", ALPINE, "sh", "-c", "echo hello >> /data/test"})
108
session.WaitWithDefaultTimeout()
109
Expect(session).Should(ExitCleanly())
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())
116
session = podmanTest.Podman([]string{"volume", "create", "my_vol2"})
117
session.WaitWithDefaultTimeout()
118
Expect(session).Should(ExitCleanly())
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")
125
session = podmanTest.Podman([]string{"run", "--volume", "my_vol2:/data", ALPINE, "cat", "/data/test"})
126
session.WaitWithDefaultTimeout()
127
Expect(session.OutputToString()).To(ContainSubstring("hello"))
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")
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"))
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"))
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"))
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())
156
It("podman create volume with o=uid,gid", func() {
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())
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))
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))
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))
183
It("image-backed volume basic functionality", func() {
184
podmanTest.AddImageToRWStore(fedoraMinimal)
186
volCreate := podmanTest.Podman([]string{"volume", "create", "--driver", "image", "--opt", fmt.Sprintf("image=%s", fedoraMinimal), volName})
187
volCreate.WaitWithDefaultTimeout()
188
Expect(volCreate).Should(ExitCleanly())
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"))
195
rmCmd := podmanTest.Podman([]string{"rmi", "--force", fedoraMinimal})
196
rmCmd.WaitWithDefaultTimeout()
197
Expect(rmCmd).Should(ExitCleanly())
199
psCmd := podmanTest.Podman([]string{"ps", "-aq"})
200
psCmd.WaitWithDefaultTimeout()
201
Expect(psCmd).Should(ExitCleanly())
202
Expect(psCmd.OutputToString()).To(BeEmpty())
204
volumesCmd := podmanTest.Podman([]string{"volume", "ls", "-q"})
205
volumesCmd.WaitWithDefaultTimeout()
206
Expect(volumesCmd).Should(ExitCleanly())
207
Expect(volumesCmd.OutputToString()).To(Not(ContainSubstring(volName)))
210
It("image-backed volume force removal", func() {
211
podmanTest.AddImageToRWStore(fedoraMinimal)
213
volCreate := podmanTest.Podman([]string{"volume", "create", "--driver", "image", "--opt", fmt.Sprintf("image=%s", fedoraMinimal), volName})
214
volCreate.WaitWithDefaultTimeout()
215
Expect(volCreate).Should(ExitCleanly())
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"))
222
rmCmd := podmanTest.Podman([]string{"volume", "rm", "--force", volName})
223
rmCmd.WaitWithDefaultTimeout()
224
Expect(rmCmd).Should(ExitCleanly())
226
psCmd := podmanTest.Podman([]string{"ps", "-aq"})
227
psCmd.WaitWithDefaultTimeout()
228
Expect(psCmd).Should(ExitCleanly())
229
Expect(psCmd.OutputToString()).To(BeEmpty())
231
volumesCmd := podmanTest.Podman([]string{"volume", "ls", "-q"})
232
volumesCmd.WaitWithDefaultTimeout()
233
Expect(volumesCmd).Should(ExitCleanly())
234
Expect(volumesCmd.OutputToString()).To(Not(ContainSubstring(volName)))