summaryrefslogtreecommitdiff
path: root/test/e2e/container_create_volume_test.go
blob: 3c54691aaf57aadc289c0b9f65ddfad5924f1b66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package integration

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	. "github.com/containers/podman/v4/test/utils"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gexec"
)

func buildDataVolumeImage(pTest *PodmanTestIntegration, image, data, dest string) {
	// Create a dummy file for data volume
	dummyFile := filepath.Join(pTest.TempDir, data)
	err := ioutil.WriteFile(dummyFile, []byte(data), 0644)
	Expect(err).To(BeNil())

	// Create a data volume container image but no CMD binary in it
	containerFile := fmt.Sprintf(`FROM scratch
CMD doesnotexist.sh
ADD %s %s/
VOLUME %s/`, data, dest, dest)
	pTest.BuildImage(containerFile, image, "false")
}

func createContainersConfFile(pTest *PodmanTestIntegration) {
	configPath := filepath.Join(pTest.TempDir, "containers.conf")
	containersConf := []byte("[containers]\nprepare_volume_on_create = true\n")
	err := ioutil.WriteFile(configPath, containersConf, os.ModePerm)
	Expect(err).To(BeNil())

	// Set custom containers.conf file
	os.Setenv("CONTAINERS_CONF", configPath)
	if IsRemote() {
		pTest.RestartRemoteService()
	}
}

func checkDataVolumeContainer(pTest *PodmanTestIntegration, image, cont, dest, data string) {
	create := pTest.Podman([]string{"create", "--name", cont, image})
	create.WaitWithDefaultTimeout()
	Expect(create).Should(Exit(0))

	inspect := pTest.InspectContainer(cont)
	Expect(inspect).To(HaveLen(1))
	Expect(inspect[0].Mounts).To(HaveLen(1))
	Expect(inspect[0].Mounts[0]).To(HaveField("Destination", dest))

	mntName, mntSource := inspect[0].Mounts[0].Name, inspect[0].Mounts[0].Source

	volList := pTest.Podman([]string{"volume", "list", "--quiet"})
	volList.WaitWithDefaultTimeout()
	Expect(volList).Should(Exit(0))
	Expect(volList.OutputToStringArray()).To(HaveLen(1))
	Expect(volList.OutputToStringArray()[0]).To(Equal(mntName))

	// Check the mount source directory
	files, err := os.ReadDir(mntSource)
	Expect(err).To(BeNil())

	if data == "" {
		Expect(files).To(BeEmpty())
	} else {
		Expect(files).To(HaveLen(1))
		Expect(files[0].Name()).To(Equal(data))
	}
}

var _ = Describe("Podman create data volume", func() {
	var (
		tempdir    string
		err        error
		podmanTest *PodmanTestIntegration
	)

	BeforeEach(func() {
		tempdir, err = CreateTempDirInTempDir()
		if err != nil {
			os.Exit(1)
		}
		podmanTest = PodmanTestCreate(tempdir)
		podmanTest.Setup()
	})

	AfterEach(func() {
		podmanTest.Cleanup()
		f := CurrentGinkgoTestDescription()
		processTestResult(f)
		os.Unsetenv("CONTAINERS_CONF")
	})

	It("podman create with volume data copy turned off", func() {
		imgName, volData, volDest := "dataimg", "dummy", "/test"

		buildDataVolumeImage(podmanTest, imgName, volData, volDest)

		// Create a container with the default containers.conf and
		// check that the volume is not copied from the image.
		checkDataVolumeContainer(podmanTest, imgName, "ctr-nocopy", volDest, "")
	})

	It("podman create with volume data copy turned on", func() {
		imgName, volData, volDest := "dataimg", "dummy", "/test"

		buildDataVolumeImage(podmanTest, imgName, volData, volDest)

		// Create a container with the custom containers.conf and
		// check that the volume is copied from the image.
		createContainersConfFile(podmanTest)

		checkDataVolumeContainer(podmanTest, imgName, "ctr-copy", volDest, volData)
	})

	It("podman run with volume data copy turned on", func() {
		// Create a container with the custom containers.conf and
		// check that the container is run successfully
		createContainersConfFile(podmanTest)

		session := podmanTest.Podman([]string{"run", "--rm", ALPINE, "echo"})
		session.WaitWithDefaultTimeout()
		Expect(session).Should(Exit(0))
	})
})