podman

Форк
0
/
volumes_test.go 
204 строки · 7.1 Кб
1
package bindings_test
2

3
import (
4
	"context"
5
	"fmt"
6
	"net/http"
7
	"time"
8

9
	"github.com/containers/podman/v5/pkg/bindings"
10
	"github.com/containers/podman/v5/pkg/bindings/containers"
11
	"github.com/containers/podman/v5/pkg/bindings/volumes"
12
	"github.com/containers/podman/v5/pkg/domain/entities"
13
	"github.com/containers/podman/v5/pkg/domain/entities/reports"
14
	. "github.com/onsi/ginkgo/v2"
15
	. "github.com/onsi/gomega"
16
	"github.com/onsi/gomega/gexec"
17
	"golang.org/x/exp/slices"
18
)
19

20
var _ = Describe("Podman volumes", func() {
21
	var (
22
		bt       *bindingTest
23
		s        *gexec.Session
24
		connText context.Context
25
		err      error
26
	)
27

28
	BeforeEach(func() {
29
		bt = newBindingTest()
30
		bt.RestoreImagesFromCache()
31
		s = bt.startAPIService()
32
		time.Sleep(1 * time.Second)
33
		connText, err = bindings.NewConnection(context.Background(), bt.sock)
34
		Expect(err).ToNot(HaveOccurred())
35
	})
36

37
	AfterEach(func() {
38
		s.Kill()
39
		bt.cleanup()
40
	})
41

42
	It("create volume", func() {
43
		// create a volume with blank config should work
44
		_, err := volumes.Create(connText, entities.VolumeCreateOptions{}, nil)
45
		Expect(err).ToNot(HaveOccurred())
46

47
		vcc := entities.VolumeCreateOptions{
48
			Name:    "foobar",
49
			Label:   nil,
50
			Options: nil,
51
		}
52
		vol, err := volumes.Create(connText, vcc, nil)
53
		Expect(err).ToNot(HaveOccurred())
54
		Expect(vol.Name).To(Equal("foobar"))
55

56
		// create volume with same name should 500
57
		_, err = volumes.Create(connText, vcc, nil)
58
		Expect(err).To(HaveOccurred())
59
		code, _ := bindings.CheckResponseCode(err)
60
		Expect(code).To(BeNumerically("==", http.StatusInternalServerError))
61
	})
62

63
	It("inspect volume", func() {
64
		vol, err := volumes.Create(connText, entities.VolumeCreateOptions{}, nil)
65
		Expect(err).ToNot(HaveOccurred())
66
		data, err := volumes.Inspect(connText, vol.Name, nil)
67
		Expect(err).ToNot(HaveOccurred())
68
		Expect(data.Name).To(Equal(vol.Name))
69
	})
70

71
	It("remove volume", func() {
72
		// removing a bogus volume should result in 404
73
		err := volumes.Remove(connText, "foobar", nil)
74
		code, err := bindings.CheckResponseCode(err)
75
		Expect(err).ToNot(HaveOccurred())
76
		Expect(code).To(BeNumerically("==", http.StatusNotFound))
77

78
		// Removing an unused volume should work
79
		vol, err := volumes.Create(connText, entities.VolumeCreateOptions{}, nil)
80
		Expect(err).ToNot(HaveOccurred())
81
		err = volumes.Remove(connText, vol.Name, nil)
82
		Expect(err).ToNot(HaveOccurred())
83

84
		// Removing a volume that is being used without force should be 409
85
		vol, err = volumes.Create(connText, entities.VolumeCreateOptions{}, nil)
86
		Expect(err).ToNot(HaveOccurred())
87
		session := bt.runPodman([]string{"run", "-dt", "-v", fmt.Sprintf("%s:/foobar", vol.Name), "--name", "vtest", alpine.name, "top"})
88
		session.Wait(45)
89
		Expect(session.ExitCode()).To(BeZero())
90

91
		err = volumes.Remove(connText, vol.Name, nil)
92
		Expect(err).To(HaveOccurred())
93
		code, err = bindings.CheckResponseCode(err)
94
		Expect(err).ToNot(HaveOccurred())
95
		Expect(code).To(BeNumerically("==", http.StatusConflict))
96

97
		// Removing with a volume in use with force should work with a stopped container
98
		err = containers.Stop(connText, "vtest", new(containers.StopOptions).WithTimeout(0))
99
		Expect(err).ToNot(HaveOccurred())
100
		options := new(volumes.RemoveOptions).WithForce(true)
101
		err = volumes.Remove(connText, vol.Name, options)
102
		Expect(err).ToNot(HaveOccurred())
103
	})
104

105
	It("list volumes", func() {
106
		// no volumes should be ok
107
		vols, err := volumes.List(connText, nil)
108
		Expect(err).ToNot(HaveOccurred())
109
		Expect(vols).To(BeEmpty())
110

111
		// create a bunch of named volumes and make verify with list
112
		volNames := []string{"homer", "bart", "lisa", "maggie", "marge"}
113
		for i := 0; i < 5; i++ {
114
			_, err = volumes.Create(connText, entities.VolumeCreateOptions{Name: volNames[i]}, nil)
115
			Expect(err).ToNot(HaveOccurred())
116
		}
117
		vols, err = volumes.List(connText, nil)
118
		Expect(err).ToNot(HaveOccurred())
119
		Expect(vols).To(HaveLen(5))
120
		for _, v := range vols {
121
			Expect(slices.Contains(volNames, v.Name)).To(BeTrue())
122
		}
123

124
		// list with bad filter should be 500
125
		filters := make(map[string][]string)
126
		filters["foobar"] = []string{"1234"}
127
		options := new(volumes.ListOptions).WithFilters(filters)
128
		_, err = volumes.List(connText, options)
129
		Expect(err).To(HaveOccurred())
130
		code, _ := bindings.CheckResponseCode(err)
131
		Expect(code).To(BeNumerically("==", http.StatusInternalServerError))
132

133
		filters = make(map[string][]string)
134
		filters["name"] = []string{"homer"}
135
		options = new(volumes.ListOptions).WithFilters(filters)
136
		vols, err = volumes.List(connText, options)
137
		Expect(err).ToNot(HaveOccurred())
138
		Expect(vols).To(HaveLen(1))
139
		Expect(vols[0].Name).To(Equal("homer"))
140
	})
141

142
	It("prune unused volume", func() {
143
		// Pruning when no volumes present should be ok
144
		_, err := volumes.Prune(connText, nil)
145
		Expect(err).ToNot(HaveOccurred())
146

147
		// Removing an unused volume should work
148
		_, err = volumes.Create(connText, entities.VolumeCreateOptions{}, nil)
149
		Expect(err).ToNot(HaveOccurred())
150
		vols, err := volumes.Prune(connText, nil)
151
		Expect(err).ToNot(HaveOccurred())
152
		Expect(vols).To(HaveLen(1))
153

154
		_, err = volumes.Create(connText, entities.VolumeCreateOptions{Name: "homer"}, nil)
155
		Expect(err).ToNot(HaveOccurred())
156
		_, err = volumes.Create(connText, entities.VolumeCreateOptions{}, nil)
157
		Expect(err).ToNot(HaveOccurred())
158
		session := bt.runPodman([]string{"run", "-dt", "-v", fmt.Sprintf("%s:/homer", "homer"), "--name", "vtest", alpine.name, "top"})
159
		session.Wait(45)
160
		vols, err = volumes.Prune(connText, nil)
161
		Expect(err).ToNot(HaveOccurred())
162
		Expect(reports.PruneReportsIds(vols)).To(HaveLen(1))
163
		_, err = volumes.Inspect(connText, "homer", nil)
164
		Expect(err).ToNot(HaveOccurred())
165

166
		// Removing volume with non matching filter shouldn't prune any volumes
167
		filters := make(map[string][]string)
168
		filters["label"] = []string{"label1=idontmatch"}
169
		_, err = volumes.Create(connText, entities.VolumeCreateOptions{Label: map[string]string{
170
			"label1": "value1",
171
		}}, nil)
172
		Expect(err).ToNot(HaveOccurred())
173
		options := new(volumes.PruneOptions).WithFilters(filters)
174
		vols, err = volumes.Prune(connText, options)
175
		Expect(err).ToNot(HaveOccurred())
176
		Expect(vols).To(BeEmpty())
177
		vol2, err := volumes.Create(connText, entities.VolumeCreateOptions{Label: map[string]string{
178
			"label1": "value2",
179
		}}, nil)
180
		Expect(err).ToNot(HaveOccurred())
181
		_, err = volumes.Create(connText, entities.VolumeCreateOptions{Label: map[string]string{
182
			"label1": "value3",
183
		}}, nil)
184
		Expect(err).ToNot(HaveOccurred())
185

186
		// Removing volume with matching filter label and value should remove specific entry
187
		filters = make(map[string][]string)
188
		filters["label"] = []string{"label1=value2"}
189
		options = new(volumes.PruneOptions).WithFilters(filters)
190
		vols, err = volumes.Prune(connText, options)
191
		Expect(err).ToNot(HaveOccurred())
192
		Expect(vols).To(HaveLen(1))
193
		Expect(vols[0].Id).To(Equal(vol2.Name))
194

195
		// Removing volumes with matching filter label should remove all matching volumes
196
		filters = make(map[string][]string)
197
		filters["label"] = []string{"label1"}
198
		options = new(volumes.PruneOptions).WithFilters(filters)
199
		vols, err = volumes.Prune(connText, options)
200
		Expect(err).ToNot(HaveOccurred())
201
		Expect(vols).To(HaveLen(2))
202
	})
203

204
})
205

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.