github.com/containers/podman/v4@v4.9.4/test/e2e/container_create_volume_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	. "github.com/containers/podman/v4/test/utils"
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    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())
    18  
    19  	// Create a data volume container image but no CMD binary in it
    20  	containerFile := fmt.Sprintf(`FROM scratch
    21  CMD doesnotexist.sh
    22  ADD %s %s/
    23  VOLUME %s/`, data, dest, dest)
    24  	pTest.BuildImage(containerFile, image, "false")
    25  }
    26  
    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())
    32  
    33  	// Set custom containers.conf file
    34  	os.Setenv("CONTAINERS_CONF", configPath)
    35  	if IsRemote() {
    36  		pTest.RestartRemoteService()
    37  	}
    38  }
    39  
    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())
    44  
    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))
    49  
    50  	mntName, mntSource := inspect[0].Mounts[0].Name, inspect[0].Mounts[0].Source
    51  
    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))
    57  
    58  	// Check the mount source directory
    59  	files, err := os.ReadDir(mntSource)
    60  	Expect(err).ToNot(HaveOccurred())
    61  
    62  	if data == "" {
    63  		Expect(files).To(BeEmpty())
    64  	} else {
    65  		Expect(files).To(HaveLen(1))
    66  		Expect(files[0].Name()).To(Equal(data))
    67  	}
    68  }
    69  
    70  var _ = Describe("Podman create data volume", func() {
    71  
    72  	It("podman create with volume data copy turned off", func() {
    73  		imgName, volData, volDest := "dataimg", "dummy", "/test"
    74  
    75  		buildDataVolumeImage(podmanTest, imgName, volData, volDest)
    76  
    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, "")
    80  	})
    81  
    82  	It("podman create with volume data copy turned on", func() {
    83  		imgName, volData, volDest := "dataimg", "dummy", "/test"
    84  
    85  		buildDataVolumeImage(podmanTest, imgName, volData, volDest)
    86  
    87  		// Create a container with the custom containers.conf and
    88  		// check that the volume is copied from the image.
    89  		createContainersConfFile(podmanTest)
    90  
    91  		checkDataVolumeContainer(podmanTest, imgName, "ctr-copy", volDest, volData)
    92  	})
    93  
    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)
    98  
    99  		session := podmanTest.Podman([]string{"run", "--rm", ALPINE, "echo"})
   100  		session.WaitWithDefaultTimeout()
   101  		Expect(session).Should(ExitCleanly())
   102  	})
   103  })