github.com/AbhinandanKurakure/podman/v3@v3.4.10/test/e2e/container_create_volume_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	. "github.com/containers/podman/v3/test/utils"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  func buildDataVolumeImage(pTest *PodmanTestIntegration, image, data, dest string) {
    16  	// Create a dummy file for data volume
    17  	dummyFile := filepath.Join(pTest.TempDir, data)
    18  	err := ioutil.WriteFile(dummyFile, []byte(data), 0644)
    19  	Expect(err).To(BeNil())
    20  
    21  	// Create a data volume container image but no CMD binary in it
    22  	containerFile := fmt.Sprintf(`FROM scratch
    23  CMD doesnotexist.sh
    24  ADD %s %s/
    25  VOLUME %s/`, data, dest, dest)
    26  	pTest.BuildImage(containerFile, image, "false")
    27  }
    28  
    29  func createContainersConfFile(pTest *PodmanTestIntegration) {
    30  	configPath := filepath.Join(pTest.TempDir, "containers.conf")
    31  	containersConf := []byte(fmt.Sprintf("[containers]\nprepare_volume_on_create = true\n"))
    32  	err := ioutil.WriteFile(configPath, containersConf, os.ModePerm)
    33  	Expect(err).To(BeNil())
    34  
    35  	// Set custom containers.conf file
    36  	os.Setenv("CONTAINERS_CONF", configPath)
    37  	if IsRemote() {
    38  		pTest.RestartRemoteService()
    39  	}
    40  }
    41  
    42  func checkDataVolumeContainer(pTest *PodmanTestIntegration, image, cont, dest, data string) {
    43  	create := pTest.Podman([]string{"create", "--name", cont, image})
    44  	create.WaitWithDefaultTimeout()
    45  	Expect(create).Should(Exit(0))
    46  
    47  	inspect := pTest.InspectContainer(cont)
    48  	Expect(len(inspect)).To(Equal(1))
    49  	Expect(len(inspect[0].Mounts)).To(Equal(1))
    50  	Expect(inspect[0].Mounts[0].Destination).To(Equal(dest))
    51  
    52  	mntName, mntSource := inspect[0].Mounts[0].Name, inspect[0].Mounts[0].Source
    53  
    54  	volList := pTest.Podman([]string{"volume", "list", "--quiet"})
    55  	volList.WaitWithDefaultTimeout()
    56  	Expect(volList).Should(Exit(0))
    57  	Expect(len(volList.OutputToStringArray())).To(Equal(1))
    58  	Expect(volList.OutputToStringArray()[0]).To(Equal(mntName))
    59  
    60  	// Check the mount source directory
    61  	files, err := ioutil.ReadDir(mntSource)
    62  	Expect(err).To(BeNil())
    63  
    64  	if data == "" {
    65  		Expect(len(files)).To(Equal(0))
    66  	} else {
    67  		Expect(len(files)).To(Equal(1))
    68  		Expect(files[0].Name()).To(Equal(data))
    69  	}
    70  }
    71  
    72  var _ = Describe("Podman create data volume", func() {
    73  	var (
    74  		tempdir    string
    75  		err        error
    76  		podmanTest *PodmanTestIntegration
    77  	)
    78  
    79  	BeforeEach(func() {
    80  		tempdir, err = CreateTempDirInTempDir()
    81  		if err != nil {
    82  			os.Exit(1)
    83  		}
    84  		podmanTest = PodmanTestCreate(tempdir)
    85  		podmanTest.Setup()
    86  		podmanTest.SeedImages()
    87  	})
    88  
    89  	AfterEach(func() {
    90  		podmanTest.Cleanup()
    91  		f := CurrentGinkgoTestDescription()
    92  		processTestResult(f)
    93  		os.Unsetenv("CONTAINERS_CONF")
    94  	})
    95  
    96  	It("podman create with volume data copy turned off", func() {
    97  		imgName, volData, volDest := "dataimg", "dummy", "/test"
    98  
    99  		buildDataVolumeImage(podmanTest, imgName, volData, volDest)
   100  
   101  		// Create a container with the default containers.conf and
   102  		// check that the volume is not copied from the image.
   103  		checkDataVolumeContainer(podmanTest, imgName, "ctr-nocopy", volDest, "")
   104  	})
   105  
   106  	It("podman create with volume data copy turned on", func() {
   107  		imgName, volData, volDest := "dataimg", "dummy", "/test"
   108  
   109  		buildDataVolumeImage(podmanTest, imgName, volData, volDest)
   110  
   111  		// Create a container with the custom containers.conf and
   112  		// check that the volume is copied from the image.
   113  		createContainersConfFile(podmanTest)
   114  
   115  		checkDataVolumeContainer(podmanTest, imgName, "ctr-copy", volDest, volData)
   116  	})
   117  
   118  	It("podman run with volume data copy turned on", func() {
   119  		// Create a container with the custom containers.conf and
   120  		// check that the container is run successfully
   121  		createContainersConfFile(podmanTest)
   122  
   123  		session := podmanTest.Podman([]string{"run", "--rm", ALPINE, "echo"})
   124  		session.WaitWithDefaultTimeout()
   125  		Expect(session).Should(Exit(0))
   126  	})
   127  })