github.com/dmaizel/tests@v0.0.0-20210728163746-cae6a2d9cee8/integration/docker/volume_test.go (about) 1 // Copyright (c) 2018 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package docker 6 7 import ( 8 "fmt" 9 "io/ioutil" 10 "os" 11 "path" 12 13 "github.com/kata-containers/tests" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("[Serial Test] docker volume", func() { 19 var ( 20 args []string 21 id string = randomDockerName() 22 id2 string = randomDockerName() 23 volumeName string = "cc3volume" 24 containerPath string = "/attached_vol/" 25 fileTest string = "hello" 26 exitCode int 27 stdout string 28 loopFile string 29 err error 30 diskFile string 31 ) 32 33 if os.Getuid() != 0 { 34 GinkgoT().Skip("only root user can create files under /dev") 35 return 36 } 37 38 Context("create volume", func() { 39 It("should display the volume's name", func() { 40 _, _, exitCode = dockerVolume("create", "--name", volumeName) 41 Expect(exitCode).To(Equal(0)) 42 _, _, exitCode = dockerVolume("inspect", volumeName) 43 Expect(exitCode).To(Equal(0)) 44 _, _, exitCode = dockerVolume("rm", volumeName) 45 Expect(exitCode).To(Equal(0)) 46 stdout, _, exitCode = dockerVolume("ls") 47 Expect(exitCode).To(Equal(0)) 48 Expect(stdout).NotTo(ContainSubstring(volumeName)) 49 }) 50 }) 51 52 Context("use volume in a container", func() { 53 It("should display the volume", func() { 54 args = []string{"--name", id, "-t", "-v", volumeName + ":" + containerPath, Image, "touch", containerPath + fileTest} 55 _, _, exitCode = dockerRun(args...) 56 Expect(exitCode).To(Equal(0)) 57 58 args = []string{"--name", id2, "-t", "-v", volumeName + ":" + containerPath, Image, "ls", containerPath} 59 stdout, _, exitCode = dockerRun(args...) 60 Expect(exitCode).To(Equal(0)) 61 Expect(stdout).To(ContainSubstring(fileTest)) 62 63 Expect(RemoveDockerContainer(id)).To(BeTrue()) 64 Expect(ExistDockerContainer(id)).NotTo(BeTrue()) 65 Expect(RemoveDockerContainer(id2)).To(BeTrue()) 66 Expect(ExistDockerContainer(id2)).NotTo(BeTrue()) 67 68 _, _, exitCode = dockerVolume("rm", volumeName) 69 Expect(exitCode).To(Equal(0)) 70 71 stdout, _, exitCode = dockerVolume("ls") 72 Expect(exitCode).To(Equal(0)) 73 Expect(stdout).NotTo(ContainSubstring(volumeName)) 74 }) 75 }) 76 77 Context("volume bind-mount a directory", func() { 78 It("should display directory's name", func() { 79 file, err := ioutil.TempFile(os.TempDir(), fileTest) 80 Expect(err).ToNot(HaveOccurred()) 81 err = file.Close() 82 Expect(err).ToNot(HaveOccurred()) 83 defer os.Remove(file.Name()) 84 Expect(file.Name()).To(BeAnExistingFile()) 85 86 testFile := path.Base(file.Name()) 87 args = []string{"--name", id, "-v", testFile + ":/root/" + fileTest, Image, "ls", "/root/"} 88 stdout, _, exitCode = dockerRun(args...) 89 Expect(exitCode).To(Equal(0)) 90 Expect(stdout).To(ContainSubstring(fileTest)) 91 92 Expect(RemoveDockerContainer(id)).To(BeTrue()) 93 Expect(ExistDockerContainer(id)).NotTo(BeTrue()) 94 95 _, _, exitCode = dockerVolume("rm", testFile) 96 Expect(exitCode).To(Equal(0)) 97 98 stdout, _, exitCode = dockerVolume("ls") 99 Expect(exitCode).To(Equal(0)) 100 Expect(stdout).NotTo(ContainSubstring(testFile)) 101 }) 102 }) 103 104 Context("creating a text file under /dev", func() { 105 It("should be passed the content to the container", func() { 106 fileName := "/dev/foo" 107 textContent := "hello" 108 err = ioutil.WriteFile(fileName, []byte(textContent), 0644) 109 Expect(err).ToNot(HaveOccurred()) 110 defer os.Remove(fileName) 111 112 args = []string{"--name", id, "-v", fileName + ":" + fileName, Image, "cat", fileName} 113 stdout, _, exitCode = dockerRun(args...) 114 Expect(exitCode).To(Equal(0)) 115 Expect(stdout).To(ContainSubstring(textContent)) 116 117 Expect(RemoveDockerContainer(id)).To(BeTrue()) 118 Expect(ExistDockerContainer(id)).NotTo(BeTrue()) 119 }) 120 }) 121 122 Context("passing a block device", func() { 123 It("should be mounted", func() { 124 diskFile, loopFile, err = createLoopDevice() 125 Expect(err).ToNot(HaveOccurred()) 126 127 loopFileP1 := fmt.Sprintf("%sp1", loopFile) 128 mkfsCmd := tests.NewCommand("mkfs.ext4", loopFileP1) 129 _, _, exitCode := mkfsCmd.Run() 130 Expect(exitCode).To(Equal(0)) 131 Expect(err).ToNot(HaveOccurred()) 132 133 args = []string{"--name", id, "--cap-add=SYS_ADMIN", "--device", loopFileP1, "-v", loopFileP1 + ":" + loopFileP1, DebianImage, "bash", "-c", fmt.Sprintf("sleep 15; mount %s /mnt", loopFileP1)} 134 _, _, exitCode = dockerRun(args...) 135 Expect(exitCode).To(Equal(0)) 136 137 err = deleteLoopDevice(loopFile) 138 Expect(err).ToNot(HaveOccurred()) 139 140 err = os.Remove(diskFile) 141 Expect(err).ToNot(HaveOccurred()) 142 143 Expect(RemoveDockerContainer(id)).To(BeTrue()) 144 Expect(ExistDockerContainer(id)).NotTo(BeTrue()) 145 }) 146 }) 147 148 Context("remove bind-mount source before container exits", func() { 149 It("should exit cleanly without leaking process", func() { 150 file, err := ioutil.TempFile(os.TempDir(), fileTest) 151 Expect(err).ToNot(HaveOccurred()) 152 err = file.Close() 153 Expect(err).ToNot(HaveOccurred()) 154 155 testFile := file.Name() 156 Expect(testFile).To(BeAnExistingFile()) 157 158 args = []string{"--name", id, "-d", "-v", testFile + ":/volume_file", Image, "top"} 159 stdout, _, exitCode = dockerRun(args...) 160 Expect(exitCode).To(Equal(0)) 161 162 // remove the test temp file before stop the container 163 os.Remove(testFile) 164 Expect(testFile).NotTo(BeAnExistingFile()) 165 166 // remove container 167 Expect(RemoveDockerContainer(id)).To(BeTrue()) 168 Expect(ExistDockerContainer(id)).NotTo(BeTrue()) 169 }) 170 }) 171 })