Files
podman/test/e2e/container_create_volume_test.go
Paul Holzinger ab29ff2f66 test/e2e: dedup Before/AfterEach nodes
There is no reason to define the same code every time in each file, just
use global nodes. This diff should speak for itself.

CleanupSecrets()/Volume() no longer call Cleanup() directly, as the
global AfterEach node will always call Cleanup() this is no longer
necessary. If one AfterEach() node fails it will still run the others.

Also always unset the CONTAINERS_CONF env vars. This prevents people
from forgetting to unset it. And fix the special CONTAINERS_CONF logic
in the system connection tests, we do not want to preserve
CONTAINERS_CONF anyway so just remove this logic.

Ginkgo orders the BeforeEach and AfterEach nodes. They will be executed
from the outer-most defined to inner-most. This means our global
BeforeEach is always first. Only then the inner one (in the Describe()
function in each file). For AfterEach it is inverted, from the inner to
the outer.
Also see https://onsi.github.io/ginkgo/#organizing-specs-with-container-nodes

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2023-05-15 16:56:18 +02:00

104 lines
3.2 KiB
Go

package integration
import (
"fmt"
"os"
"path/filepath"
. "github.com/onsi/ginkgo/v2"
. "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 := os.WriteFile(dummyFile, []byte(data), 0644)
Expect(err).ToNot(HaveOccurred())
// 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 := os.WriteFile(configPath, containersConf, os.ModePerm)
Expect(err).ToNot(HaveOccurred())
// 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).ToNot(HaveOccurred())
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() {
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))
})
})