github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/bindings/test/volumes_test.go (about)

     1  package test_bindings
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"time"
     8  
     9  	"github.com/containers/podman/v2/pkg/bindings"
    10  	"github.com/containers/podman/v2/pkg/bindings/containers"
    11  	"github.com/containers/podman/v2/pkg/bindings/volumes"
    12  	"github.com/containers/podman/v2/pkg/domain/entities"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	"github.com/onsi/gomega/gexec"
    16  )
    17  
    18  var _ = Describe("Podman volumes", func() {
    19  	var (
    20  		//tempdir    string
    21  		//err        error
    22  		//podmanTest *PodmanTestIntegration
    23  		bt       *bindingTest
    24  		s        *gexec.Session
    25  		connText context.Context
    26  		err      error
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		//tempdir, err = CreateTempDirInTempDir()
    31  		//if err != nil {
    32  		//	os.Exit(1)
    33  		//}
    34  		//podmanTest = PodmanTestCreate(tempdir)
    35  		//podmanTest.Setup()
    36  		//podmanTest.SeedImages()
    37  		bt = newBindingTest()
    38  		bt.RestoreImagesFromCache()
    39  		s = bt.startAPIService()
    40  		time.Sleep(1 * time.Second)
    41  		connText, err = bindings.NewConnection(context.Background(), bt.sock)
    42  		Expect(err).To(BeNil())
    43  	})
    44  
    45  	AfterEach(func() {
    46  		//podmanTest.Cleanup()
    47  		//f := CurrentGinkgoTestDescription()
    48  		//processTestResult(f)
    49  		s.Kill()
    50  		bt.cleanup()
    51  	})
    52  
    53  	It("create volume", func() {
    54  		// create a volume with blank config should work
    55  		_, err := volumes.Create(connText, entities.VolumeCreateOptions{})
    56  		Expect(err).To(BeNil())
    57  
    58  		vcc := entities.VolumeCreateOptions{
    59  			Name:    "foobar",
    60  			Label:   nil,
    61  			Options: nil,
    62  		}
    63  		vol, err := volumes.Create(connText, vcc)
    64  		Expect(err).To(BeNil())
    65  		Expect(vol.Name).To(Equal("foobar"))
    66  
    67  		// create volume with same name should 500
    68  		_, err = volumes.Create(connText, vcc)
    69  		Expect(err).ToNot(BeNil())
    70  		code, _ := bindings.CheckResponseCode(err)
    71  		Expect(code).To(BeNumerically("==", http.StatusInternalServerError))
    72  	})
    73  
    74  	It("inspect volume", func() {
    75  		vol, err := volumes.Create(connText, entities.VolumeCreateOptions{})
    76  		Expect(err).To(BeNil())
    77  		data, err := volumes.Inspect(connText, vol.Name)
    78  		Expect(err).To(BeNil())
    79  		Expect(data.Name).To(Equal(vol.Name))
    80  	})
    81  
    82  	It("remove volume", func() {
    83  		// removing a bogus volume should result in 404
    84  		err := volumes.Remove(connText, "foobar", nil)
    85  		code, _ := bindings.CheckResponseCode(err)
    86  		Expect(code).To(BeNumerically("==", http.StatusNotFound))
    87  
    88  		// Removing an unused volume should work
    89  		vol, err := volumes.Create(connText, entities.VolumeCreateOptions{})
    90  		Expect(err).To(BeNil())
    91  		err = volumes.Remove(connText, vol.Name, nil)
    92  		Expect(err).To(BeNil())
    93  
    94  		// Removing a volume that is being used without force should be 409
    95  		vol, err = volumes.Create(connText, entities.VolumeCreateOptions{})
    96  		Expect(err).To(BeNil())
    97  		session := bt.runPodman([]string{"run", "-dt", "-v", fmt.Sprintf("%s:/foobar", vol.Name), "--name", "vtest", alpine.name, "top"})
    98  		session.Wait(45)
    99  		err = volumes.Remove(connText, vol.Name, nil)
   100  		Expect(err).ToNot(BeNil())
   101  		code, _ = bindings.CheckResponseCode(err)
   102  		Expect(code).To(BeNumerically("==", http.StatusConflict))
   103  
   104  		// Removing with a volume in use with force should work with a stopped container
   105  		zero := uint(0)
   106  		err = containers.Stop(connText, "vtest", &zero)
   107  		Expect(err).To(BeNil())
   108  		err = volumes.Remove(connText, vol.Name, bindings.PTrue)
   109  		Expect(err).To(BeNil())
   110  	})
   111  
   112  	It("list volumes", func() {
   113  		// no volumes should be ok
   114  		vols, err := volumes.List(connText, nil)
   115  		Expect(err).To(BeNil())
   116  		Expect(len(vols)).To(BeZero())
   117  
   118  		// create a bunch of named volumes and make verify with list
   119  		volNames := []string{"homer", "bart", "lisa", "maggie", "marge"}
   120  		for i := 0; i < 5; i++ {
   121  			_, err = volumes.Create(connText, entities.VolumeCreateOptions{Name: volNames[i]})
   122  			Expect(err).To(BeNil())
   123  		}
   124  		vols, err = volumes.List(connText, nil)
   125  		Expect(err).To(BeNil())
   126  		Expect(len(vols)).To(BeNumerically("==", 5))
   127  		for _, v := range vols {
   128  			Expect(StringInSlice(v.Name, volNames)).To(BeTrue())
   129  		}
   130  
   131  		// list with bad filter should be 500
   132  		filters := make(map[string][]string)
   133  		filters["foobar"] = []string{"1234"}
   134  		_, err = volumes.List(connText, filters)
   135  		Expect(err).ToNot(BeNil())
   136  		code, _ := bindings.CheckResponseCode(err)
   137  		Expect(code).To(BeNumerically("==", http.StatusInternalServerError))
   138  
   139  		filters = make(map[string][]string)
   140  		filters["name"] = []string{"homer"}
   141  		vols, err = volumes.List(connText, filters)
   142  		Expect(err).To(BeNil())
   143  		Expect(len(vols)).To(BeNumerically("==", 1))
   144  		Expect(vols[0].Name).To(Equal("homer"))
   145  	})
   146  
   147  	// TODO we need to add filtering to tests
   148  	It("prune unused volume", func() {
   149  		// Pruning when no volumes present should be ok
   150  		_, err := volumes.Prune(connText)
   151  		Expect(err).To(BeNil())
   152  
   153  		// Removing an unused volume should work
   154  		_, err = volumes.Create(connText, entities.VolumeCreateOptions{})
   155  		Expect(err).To(BeNil())
   156  		vols, err := volumes.Prune(connText)
   157  		Expect(err).To(BeNil())
   158  		Expect(len(vols)).To(BeNumerically("==", 1))
   159  
   160  		_, err = volumes.Create(connText, entities.VolumeCreateOptions{Name: "homer"})
   161  		Expect(err).To(BeNil())
   162  		_, err = volumes.Create(connText, entities.VolumeCreateOptions{})
   163  		Expect(err).To(BeNil())
   164  		session := bt.runPodman([]string{"run", "-dt", "-v", fmt.Sprintf("%s:/homer", "homer"), "--name", "vtest", alpine.name, "top"})
   165  		session.Wait(45)
   166  		vols, err = volumes.Prune(connText)
   167  		Expect(err).To(BeNil())
   168  		Expect(len(vols)).To(BeNumerically("==", 1))
   169  		_, err = volumes.Inspect(connText, "homer")
   170  		Expect(err).To(BeNil())
   171  	})
   172  
   173  })