aboutsummaryrefslogtreecommitdiff
path: root/pkg/bindings/test/volumes_test.go
blob: efc02a0d4db08f28dee5d262d942e6a906aaa664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package test_bindings

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/containers/libpod/v2/pkg/bindings"
	"github.com/containers/libpod/v2/pkg/bindings/containers"
	"github.com/containers/libpod/v2/pkg/bindings/volumes"
	"github.com/containers/libpod/v2/pkg/domain/entities"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
)

var _ = Describe("Podman volumes", func() {
	var (
		//tempdir    string
		//err        error
		//podmanTest *PodmanTestIntegration
		bt       *bindingTest
		s        *gexec.Session
		connText context.Context
		err      error
	)

	BeforeEach(func() {
		//tempdir, err = CreateTempDirInTempDir()
		//if err != nil {
		//	os.Exit(1)
		//}
		//podmanTest = PodmanTestCreate(tempdir)
		//podmanTest.Setup()
		//podmanTest.SeedImages()
		bt = newBindingTest()
		bt.RestoreImagesFromCache()
		s = bt.startAPIService()
		time.Sleep(1 * time.Second)
		connText, err = bindings.NewConnection(context.Background(), bt.sock)
		Expect(err).To(BeNil())
	})

	AfterEach(func() {
		//podmanTest.Cleanup()
		//f := CurrentGinkgoTestDescription()
		//processTestResult(f)
		s.Kill()
		bt.cleanup()
	})

	It("create volume", func() {
		// create a volume with blank config should work
		_, err := volumes.Create(connText, entities.VolumeCreateOptions{})
		Expect(err).To(BeNil())

		vcc := entities.VolumeCreateOptions{
			Name:    "foobar",
			Label:   nil,
			Options: nil,
		}
		vol, err := volumes.Create(connText, vcc)
		Expect(err).To(BeNil())
		Expect(vol.Name).To(Equal("foobar"))

		// create volume with same name should 500
		_, err = volumes.Create(connText, vcc)
		Expect(err).ToNot(BeNil())
		code, _ := bindings.CheckResponseCode(err)
		Expect(code).To(BeNumerically("==", http.StatusInternalServerError))
	})

	It("inspect volume", func() {
		vol, err := volumes.Create(connText, entities.VolumeCreateOptions{})
		Expect(err).To(BeNil())
		data, err := volumes.Inspect(connText, vol.Name)
		Expect(err).To(BeNil())
		Expect(data.Name).To(Equal(vol.Name))
	})

	It("remove volume", func() {
		// removing a bogus volume should result in 404
		err := volumes.Remove(connText, "foobar", nil)
		code, _ := bindings.CheckResponseCode(err)
		Expect(code).To(BeNumerically("==", http.StatusNotFound))

		// Removing an unused volume should work
		vol, err := volumes.Create(connText, entities.VolumeCreateOptions{})
		Expect(err).To(BeNil())
		err = volumes.Remove(connText, vol.Name, nil)
		Expect(err).To(BeNil())

		// Removing a volume that is being used without force should be 409
		vol, err = volumes.Create(connText, entities.VolumeCreateOptions{})
		Expect(err).To(BeNil())
		session := bt.runPodman([]string{"run", "-dt", "-v", fmt.Sprintf("%s:/foobar", vol.Name), "--name", "vtest", alpine.name, "top"})
		session.Wait(45)
		err = volumes.Remove(connText, vol.Name, nil)
		Expect(err).ToNot(BeNil())
		code, _ = bindings.CheckResponseCode(err)
		Expect(code).To(BeNumerically("==", http.StatusConflict))

		// Removing with a volume in use with force should work with a stopped container
		zero := uint(0)
		err = containers.Stop(connText, "vtest", &zero)
		Expect(err).To(BeNil())
		err = volumes.Remove(connText, vol.Name, bindings.PTrue)
		Expect(err).To(BeNil())
	})

	It("list volumes", func() {
		// no volumes should be ok
		vols, err := volumes.List(connText, nil)
		Expect(err).To(BeNil())
		Expect(len(vols)).To(BeZero())

		// create a bunch of named volumes and make verify with list
		volNames := []string{"homer", "bart", "lisa", "maggie", "marge"}
		for i := 0; i < 5; i++ {
			_, err = volumes.Create(connText, entities.VolumeCreateOptions{Name: volNames[i]})
			Expect(err).To(BeNil())
		}
		vols, err = volumes.List(connText, nil)
		Expect(err).To(BeNil())
		Expect(len(vols)).To(BeNumerically("==", 5))
		for _, v := range vols {
			Expect(StringInSlice(v.Name, volNames)).To(BeTrue())
		}

		// list with bad filter should be 500
		filters := make(map[string][]string)
		filters["foobar"] = []string{"1234"}
		_, err = volumes.List(connText, filters)
		Expect(err).ToNot(BeNil())
		code, _ := bindings.CheckResponseCode(err)
		Expect(code).To(BeNumerically("==", http.StatusInternalServerError))

		filters = make(map[string][]string)
		filters["name"] = []string{"homer"}
		vols, err = volumes.List(connText, filters)
		Expect(err).To(BeNil())
		Expect(len(vols)).To(BeNumerically("==", 1))
		Expect(vols[0].Name).To(Equal("homer"))
	})

	// TODO we need to add filtering to tests
	It("prune unused volume", func() {
		// Pruning when no volumes present should be ok
		_, err := volumes.Prune(connText)
		Expect(err).To(BeNil())

		// Removing an unused volume should work
		_, err = volumes.Create(connText, entities.VolumeCreateOptions{})
		Expect(err).To(BeNil())
		vols, err := volumes.Prune(connText)
		Expect(err).To(BeNil())
		Expect(len(vols)).To(BeNumerically("==", 1))

		_, err = volumes.Create(connText, entities.VolumeCreateOptions{Name: "homer"})
		Expect(err).To(BeNil())
		_, err = volumes.Create(connText, entities.VolumeCreateOptions{})
		Expect(err).To(BeNil())
		session := bt.runPodman([]string{"run", "-dt", "-v", fmt.Sprintf("%s:/homer", "homer"), "--name", "vtest", alpine.name, "top"})
		session.Wait(45)
		vols, err = volumes.Prune(connText)
		Expect(err).To(BeNil())
		Expect(len(vols)).To(BeNumerically("==", 1))
		_, err = volumes.Inspect(connText, "homer")
		Expect(err).To(BeNil())
	})

})