github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/test/e2e/volume_plugin_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	. "github.com/hanks177/podman/v4/test/utils"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gexec"
    12  )
    13  
    14  var _ = Describe("Podman volume plugins", func() {
    15  	var (
    16  		tempdir    string
    17  		err        error
    18  		podmanTest *PodmanTestIntegration
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		tempdir, err = CreateTempDirInTempDir()
    23  		if err != nil {
    24  			os.Exit(1)
    25  		}
    26  		podmanTest = PodmanTestCreate(tempdir)
    27  		podmanTest.Setup()
    28  		os.Setenv("CONTAINERS_CONF", "config/containers.conf")
    29  		SkipIfRemote("Volume plugins only supported as local")
    30  		SkipIfRootless("Root is required for volume plugin testing")
    31  		err = os.MkdirAll("/run/docker/plugins", 0755)
    32  		Expect(err).ToNot(HaveOccurred())
    33  	})
    34  
    35  	AfterEach(func() {
    36  		podmanTest.CleanupVolume()
    37  		f := CurrentGinkgoTestDescription()
    38  		processTestResult(f)
    39  		os.Unsetenv("CONTAINERS_CONF")
    40  	})
    41  
    42  	It("volume create with nonexistent plugin errors", func() {
    43  		session := podmanTest.Podman([]string{"volume", "create", "--driver", "notexist", "test_volume_name"})
    44  		session.WaitWithDefaultTimeout()
    45  		Expect(session).To(ExitWithError())
    46  	})
    47  
    48  	It("volume create with not-running plugin does not error", func() {
    49  		session := podmanTest.Podman([]string{"volume", "create", "--driver", "testvol0", "test_volume_name"})
    50  		session.WaitWithDefaultTimeout()
    51  		Expect(session).To(ExitWithError())
    52  	})
    53  
    54  	It("volume create and remove with running plugin succeeds", func() {
    55  		podmanTest.AddImageToRWStore(volumeTest)
    56  
    57  		pluginStatePath := filepath.Join(podmanTest.TempDir, "volumes")
    58  		err := os.Mkdir(pluginStatePath, 0755)
    59  		Expect(err).ToNot(HaveOccurred())
    60  
    61  		// Keep this distinct within tests to avoid multiple tests using the same plugin.
    62  		pluginName := "testvol1"
    63  		plugin := podmanTest.Podman([]string{"run", "--security-opt", "label=disable", "-v", "/run/docker/plugins:/run/docker/plugins", "-v", fmt.Sprintf("%v:%v", pluginStatePath, pluginStatePath), "-d", volumeTest, "--sock-name", pluginName, "--path", pluginStatePath})
    64  		plugin.WaitWithDefaultTimeout()
    65  		Expect(plugin).Should(Exit(0))
    66  
    67  		volName := "testVolume1"
    68  		create := podmanTest.Podman([]string{"volume", "create", "--driver", pluginName, volName})
    69  		create.WaitWithDefaultTimeout()
    70  		Expect(create).Should(Exit(0))
    71  
    72  		ls1 := podmanTest.Podman([]string{"volume", "ls", "-q"})
    73  		ls1.WaitWithDefaultTimeout()
    74  		Expect(ls1).Should(Exit(0))
    75  		arrOutput := ls1.OutputToStringArray()
    76  		Expect(arrOutput).To(HaveLen(1))
    77  		Expect(arrOutput[0]).To(ContainSubstring(volName))
    78  
    79  		remove := podmanTest.Podman([]string{"volume", "rm", volName})
    80  		remove.WaitWithDefaultTimeout()
    81  		Expect(remove).Should(Exit(0))
    82  
    83  		ls2 := podmanTest.Podman([]string{"volume", "ls", "-q"})
    84  		ls2.WaitWithDefaultTimeout()
    85  		Expect(ls2).Should(Exit(0))
    86  		Expect(ls2.OutputToStringArray()).To(BeEmpty())
    87  	})
    88  
    89  	It("volume inspect with running plugin succeeds", func() {
    90  		podmanTest.AddImageToRWStore(volumeTest)
    91  
    92  		pluginStatePath := filepath.Join(podmanTest.TempDir, "volumes")
    93  		err := os.Mkdir(pluginStatePath, 0755)
    94  		Expect(err).ToNot(HaveOccurred())
    95  
    96  		// Keep this distinct within tests to avoid multiple tests using the same plugin.
    97  		pluginName := "testvol2"
    98  		plugin := podmanTest.Podman([]string{"run", "--security-opt", "label=disable", "-v", "/run/docker/plugins:/run/docker/plugins", "-v", fmt.Sprintf("%v:%v", pluginStatePath, pluginStatePath), "-d", volumeTest, "--sock-name", pluginName, "--path", pluginStatePath})
    99  		plugin.WaitWithDefaultTimeout()
   100  		Expect(plugin).Should(Exit(0))
   101  
   102  		volName := "testVolume1"
   103  		create := podmanTest.Podman([]string{"volume", "create", "--driver", pluginName, volName})
   104  		create.WaitWithDefaultTimeout()
   105  		Expect(create).Should(Exit(0))
   106  
   107  		volInspect := podmanTest.Podman([]string{"volume", "inspect", "--format", "{{ .Driver }}", volName})
   108  		volInspect.WaitWithDefaultTimeout()
   109  		Expect(volInspect).Should(Exit(0))
   110  		Expect(volInspect.OutputToString()).To(ContainSubstring(pluginName))
   111  	})
   112  
   113  	It("remove plugin with stopped plugin succeeds", func() {
   114  		podmanTest.AddImageToRWStore(volumeTest)
   115  
   116  		pluginStatePath := filepath.Join(podmanTest.TempDir, "volumes")
   117  		err := os.Mkdir(pluginStatePath, 0755)
   118  		Expect(err).ToNot(HaveOccurred())
   119  
   120  		// Keep this distinct within tests to avoid multiple tests using the same plugin.
   121  		pluginName := "testvol3"
   122  		ctrName := "pluginCtr"
   123  		plugin := podmanTest.Podman([]string{"run", "--name", ctrName, "--security-opt", "label=disable", "-v", "/run/docker/plugins:/run/docker/plugins", "-v", fmt.Sprintf("%v:%v", pluginStatePath, pluginStatePath), "-d", volumeTest, "--sock-name", pluginName, "--path", pluginStatePath})
   124  		plugin.WaitWithDefaultTimeout()
   125  		Expect(plugin).Should(Exit(0))
   126  
   127  		volName := "testVolume1"
   128  		create := podmanTest.Podman([]string{"volume", "create", "--driver", pluginName, volName})
   129  		create.WaitWithDefaultTimeout()
   130  		Expect(create).Should(Exit(0))
   131  
   132  		ls1 := podmanTest.Podman([]string{"volume", "ls", "-q"})
   133  		ls1.WaitWithDefaultTimeout()
   134  		Expect(ls1).Should(Exit(0))
   135  		arrOutput := ls1.OutputToStringArray()
   136  		Expect(arrOutput).To(HaveLen(1))
   137  		Expect(arrOutput[0]).To(ContainSubstring(volName))
   138  
   139  		stop := podmanTest.Podman([]string{"stop", "--timeout", "0", ctrName})
   140  		stop.WaitWithDefaultTimeout()
   141  		Expect(stop).Should(Exit(0))
   142  
   143  		// Remove should exit non-zero because missing plugin
   144  		remove := podmanTest.Podman([]string{"volume", "rm", volName})
   145  		remove.WaitWithDefaultTimeout()
   146  		Expect(remove).To(ExitWithError())
   147  
   148  		// But the volume should still be gone
   149  		ls2 := podmanTest.Podman([]string{"volume", "ls", "-q"})
   150  		ls2.WaitWithDefaultTimeout()
   151  		Expect(ls2).Should(Exit(0))
   152  		Expect(ls2.OutputToStringArray()).To(BeEmpty())
   153  	})
   154  
   155  	It("use plugin in containers", func() {
   156  		podmanTest.AddImageToRWStore(volumeTest)
   157  
   158  		pluginStatePath := filepath.Join(podmanTest.TempDir, "volumes")
   159  		err := os.Mkdir(pluginStatePath, 0755)
   160  		Expect(err).ToNot(HaveOccurred())
   161  
   162  		// Keep this distinct within tests to avoid multiple tests using the same plugin.
   163  		pluginName := "testvol4"
   164  		plugin := podmanTest.Podman([]string{"run", "--security-opt", "label=disable", "-v", "/run/docker/plugins:/run/docker/plugins", "-v", fmt.Sprintf("%v:%v", pluginStatePath, pluginStatePath), "-d", volumeTest, "--sock-name", pluginName, "--path", pluginStatePath})
   165  		plugin.WaitWithDefaultTimeout()
   166  		Expect(plugin).Should(Exit(0))
   167  
   168  		volName := "testVolume1"
   169  		create := podmanTest.Podman([]string{"volume", "create", "--driver", pluginName, volName})
   170  		create.WaitWithDefaultTimeout()
   171  		Expect(create).Should(Exit(0))
   172  
   173  		ctr1Name := "ctr1"
   174  		ctr1 := podmanTest.Podman([]string{"run", "--security-opt", "label=disable", "--name", ctr1Name, "-v", fmt.Sprintf("%v:/test", volName), ALPINE, "sh", "-c", "touch /test/testfile && echo helloworld > /test/testfile"})
   175  		ctr1.WaitWithDefaultTimeout()
   176  		Expect(ctr1).Should(Exit(0))
   177  
   178  		ctr2Name := "ctr2"
   179  		ctr2 := podmanTest.Podman([]string{"run", "--security-opt", "label=disable", "--name", ctr2Name, "-v", fmt.Sprintf("%v:/test", volName), ALPINE, "cat", "/test/testfile"})
   180  		ctr2.WaitWithDefaultTimeout()
   181  		Expect(ctr2).Should(Exit(0))
   182  		Expect(ctr2.OutputToString()).To(ContainSubstring("helloworld"))
   183  
   184  		// HACK: `volume rm -f` is timing out trying to remove containers using the volume.
   185  		// Solution: remove them manually...
   186  		// TODO: fix this when I get back
   187  		rmAll := podmanTest.Podman([]string{"rm", "-f", ctr2Name, ctr1Name})
   188  		rmAll.WaitWithDefaultTimeout()
   189  		Expect(rmAll).Should(Exit(0))
   190  	})
   191  })