8
. "github.com/containers/podman/v5/test/utils"
9
. "github.com/onsi/ginkgo/v2"
10
. "github.com/onsi/gomega"
13
func buildDataVolumeImage(pTest *PodmanTestIntegration, image, data, dest string) {
14
// Create a dummy file for data volume
15
dummyFile := filepath.Join(pTest.TempDir, data)
16
err := os.WriteFile(dummyFile, []byte(data), 0644)
17
Expect(err).ToNot(HaveOccurred())
19
// Create a data volume container image but no CMD binary in it
20
containerFile := fmt.Sprintf(`FROM scratch
23
VOLUME %s/`, data, dest, dest)
24
pTest.BuildImage(containerFile, image, "false")
27
func createContainersConfFile(pTest *PodmanTestIntegration) {
28
configPath := filepath.Join(pTest.TempDir, "containers.conf")
29
containersConf := []byte("[containers]\nprepare_volume_on_create = true\n")
30
err := os.WriteFile(configPath, containersConf, os.ModePerm)
31
Expect(err).ToNot(HaveOccurred())
33
// Set custom containers.conf file
34
os.Setenv("CONTAINERS_CONF", configPath)
36
pTest.RestartRemoteService()
40
func checkDataVolumeContainer(pTest *PodmanTestIntegration, image, cont, dest, data string) {
41
create := pTest.Podman([]string{"create", "--name", cont, image})
42
create.WaitWithDefaultTimeout()
43
Expect(create).Should(ExitCleanly())
45
inspect := pTest.InspectContainer(cont)
46
Expect(inspect).To(HaveLen(1))
47
Expect(inspect[0].Mounts).To(HaveLen(1))
48
Expect(inspect[0].Mounts[0]).To(HaveField("Destination", dest))
50
mntName, mntSource := inspect[0].Mounts[0].Name, inspect[0].Mounts[0].Source
52
volList := pTest.Podman([]string{"volume", "list", "--quiet"})
53
volList.WaitWithDefaultTimeout()
54
Expect(volList).Should(ExitCleanly())
55
Expect(volList.OutputToStringArray()).To(HaveLen(1))
56
Expect(volList.OutputToStringArray()[0]).To(Equal(mntName))
58
// Check the mount source directory
59
files, err := os.ReadDir(mntSource)
60
Expect(err).ToNot(HaveOccurred())
63
Expect(files).To(BeEmpty())
65
Expect(files).To(HaveLen(1))
66
Expect(files[0].Name()).To(Equal(data))
70
var _ = Describe("Podman create data volume", func() {
72
It("podman create with volume data copy turned off", func() {
73
imgName, volData, volDest := "dataimg", "dummy", "/test"
75
buildDataVolumeImage(podmanTest, imgName, volData, volDest)
77
// Create a container with the default containers.conf and
78
// check that the volume is not copied from the image.
79
checkDataVolumeContainer(podmanTest, imgName, "ctr-nocopy", volDest, "")
82
It("podman create with volume data copy turned on", func() {
83
imgName, volData, volDest := "dataimg", "dummy", "/test"
85
buildDataVolumeImage(podmanTest, imgName, volData, volDest)
87
// Create a container with the custom containers.conf and
88
// check that the volume is copied from the image.
89
createContainersConfFile(podmanTest)
91
checkDataVolumeContainer(podmanTest, imgName, "ctr-copy", volDest, volData)
94
It("podman run with volume data copy turned on", func() {
95
// Create a container with the custom containers.conf and
96
// check that the container is run successfully
97
createContainersConfFile(podmanTest)
99
session := podmanTest.Podman([]string{"run", "--rm", ALPINE, "echo"})
100
session.WaitWithDefaultTimeout()
101
Expect(session).Should(ExitCleanly())