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"
20
var _ = Describe("Podman volumes", func() {
24
connText context.Context
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())
42
It("create volume", func() {
44
_, err := volumes.Create(connText, entities.VolumeCreateOptions{}, nil)
45
Expect(err).ToNot(HaveOccurred())
47
vcc := entities.VolumeCreateOptions{
52
vol, err := volumes.Create(connText, vcc, nil)
53
Expect(err).ToNot(HaveOccurred())
54
Expect(vol.Name).To(Equal("foobar"))
57
_, err = volumes.Create(connText, vcc, nil)
58
Expect(err).To(HaveOccurred())
59
code, _ := bindings.CheckResponseCode(err)
60
Expect(code).To(BeNumerically("==", http.StatusInternalServerError))
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))
71
It("remove volume", func() {
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))
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())
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"})
89
Expect(session.ExitCode()).To(BeZero())
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))
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())
105
It("list volumes", func() {
107
vols, err := volumes.List(connText, nil)
108
Expect(err).ToNot(HaveOccurred())
109
Expect(vols).To(BeEmpty())
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())
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())
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))
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"))
142
It("prune unused volume", func() {
144
_, err := volumes.Prune(connText, nil)
145
Expect(err).ToNot(HaveOccurred())
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))
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"})
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())
167
filters := make(map[string][]string)
168
filters["label"] = []string{"label1=idontmatch"}
169
_, err = volumes.Create(connText, entities.VolumeCreateOptions{Label: map[string]string{
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{
180
Expect(err).ToNot(HaveOccurred())
181
_, err = volumes.Create(connText, entities.VolumeCreateOptions{Label: map[string]string{
184
Expect(err).ToNot(HaveOccurred())
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))
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))