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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
package service
import (
"context"
"io/ioutil"
"os"
"testing"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/volume"
volumedrivers "github.com/docker/docker/volume/drivers"
"github.com/docker/docker/volume/service/opts"
"github.com/docker/docker/volume/testutils"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestServiceCreate(t *testing.T) {
t.Parallel()
ds := volumedrivers.NewStore(nil)
assert.Assert(t, ds.Register(testutils.NewFakeDriver("d1"), "d1"))
assert.Assert(t, ds.Register(testutils.NewFakeDriver("d2"), "d2"))
ctx := context.Background()
service, cleanup := newTestService(t, ds)
defer cleanup()
_, err := service.Create(ctx, "v1", "notexist")
assert.Assert(t, errdefs.IsNotFound(err), err)
v, err := service.Create(ctx, "v1", "d1")
assert.NilError(t, err)
vCopy, err := service.Create(ctx, "v1", "d1")
assert.NilError(t, err)
assert.Assert(t, is.DeepEqual(v, vCopy))
_, err = service.Create(ctx, "v1", "d2")
assert.Check(t, IsNameConflict(err), err)
assert.Check(t, errdefs.IsConflict(err), err)
assert.Assert(t, service.Remove(ctx, "v1"))
_, err = service.Create(ctx, "v1", "d2")
assert.NilError(t, err)
_, err = service.Create(ctx, "v1", "d2")
assert.NilError(t, err)
}
func TestServiceList(t *testing.T) {
t.Parallel()
ds := volumedrivers.NewStore(nil)
assert.Assert(t, ds.Register(testutils.NewFakeDriver("d1"), "d1"))
assert.Assert(t, ds.Register(testutils.NewFakeDriver("d2"), "d2"))
service, cleanup := newTestService(t, ds)
defer cleanup()
ctx := context.Background()
_, err := service.Create(ctx, "v1", "d1")
assert.NilError(t, err)
_, err = service.Create(ctx, "v2", "d1")
assert.NilError(t, err)
_, err = service.Create(ctx, "v3", "d2")
assert.NilError(t, err)
ls, _, err := service.List(ctx, filters.NewArgs(filters.Arg("driver", "d1")))
assert.NilError(t, err)
assert.Check(t, is.Len(ls, 2))
ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("driver", "d2")))
assert.NilError(t, err)
assert.Check(t, is.Len(ls, 1))
ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("driver", "notexist")))
assert.NilError(t, err)
assert.Check(t, is.Len(ls, 0))
ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("dangling", "true")))
assert.NilError(t, err)
assert.Check(t, is.Len(ls, 3))
ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("dangling", "false")))
assert.NilError(t, err)
assert.Check(t, is.Len(ls, 0))
_, err = service.Get(ctx, "v1", opts.WithGetReference("foo"))
assert.NilError(t, err)
ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("dangling", "true")))
assert.NilError(t, err)
assert.Check(t, is.Len(ls, 2))
ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("dangling", "false")))
assert.NilError(t, err)
assert.Check(t, is.Len(ls, 1))
ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("dangling", "false"), filters.Arg("driver", "d2")))
assert.NilError(t, err)
assert.Check(t, is.Len(ls, 0))
ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("dangling", "true"), filters.Arg("driver", "d2")))
assert.NilError(t, err)
assert.Check(t, is.Len(ls, 1))
}
func TestServiceRemove(t *testing.T) {
t.Parallel()
ds := volumedrivers.NewStore(nil)
assert.Assert(t, ds.Register(testutils.NewFakeDriver("d1"), "d1"))
service, cleanup := newTestService(t, ds)
defer cleanup()
ctx := context.Background()
_, err := service.Create(ctx, "test", "d1")
assert.NilError(t, err)
assert.Assert(t, service.Remove(ctx, "test"))
assert.Assert(t, service.Remove(ctx, "test", opts.WithPurgeOnError(true)))
}
func TestServiceGet(t *testing.T) {
t.Parallel()
ds := volumedrivers.NewStore(nil)
assert.Assert(t, ds.Register(testutils.NewFakeDriver("d1"), "d1"))
service, cleanup := newTestService(t, ds)
defer cleanup()
ctx := context.Background()
v, err := service.Get(ctx, "notexist")
assert.Assert(t, IsNotExist(err))
assert.Check(t, v == nil)
created, err := service.Create(ctx, "test", "d1")
assert.NilError(t, err)
assert.Assert(t, created != nil)
v, err = service.Get(ctx, "test")
assert.NilError(t, err)
assert.Assert(t, is.DeepEqual(created, v))
v, err = service.Get(ctx, "test", opts.WithGetResolveStatus)
assert.NilError(t, err)
assert.Assert(t, is.Len(v.Status, 1), v.Status)
_, err = service.Get(ctx, "test", opts.WithGetDriver("notarealdriver"))
assert.Assert(t, errdefs.IsConflict(err), err)
v, err = service.Get(ctx, "test", opts.WithGetDriver("d1"))
assert.Assert(t, err == nil)
assert.Assert(t, is.DeepEqual(created, v))
assert.Assert(t, ds.Register(testutils.NewFakeDriver("d2"), "d2"))
_, err = service.Get(ctx, "test", opts.WithGetDriver("d2"))
assert.Assert(t, errdefs.IsConflict(err), err)
}
func TestServicePrune(t *testing.T) {
t.Parallel()
ds := volumedrivers.NewStore(nil)
assert.Assert(t, ds.Register(testutils.NewFakeDriver(volume.DefaultDriverName), volume.DefaultDriverName))
assert.Assert(t, ds.Register(testutils.NewFakeDriver("other"), "other"))
service, cleanup := newTestService(t, ds)
defer cleanup()
ctx := context.Background()
_, err := service.Create(ctx, "test", volume.DefaultDriverName)
assert.NilError(t, err)
_, err = service.Create(ctx, "test2", "other")
assert.NilError(t, err)
pr, err := service.Prune(ctx, filters.NewArgs(filters.Arg("label", "banana")))
assert.NilError(t, err)
assert.Assert(t, is.Len(pr.VolumesDeleted, 0))
pr, err = service.Prune(ctx, filters.NewArgs())
assert.NilError(t, err)
assert.Assert(t, is.Len(pr.VolumesDeleted, 1))
assert.Assert(t, is.Equal(pr.VolumesDeleted[0], "test"))
_, err = service.Get(ctx, "test")
assert.Assert(t, IsNotExist(err), err)
v, err := service.Get(ctx, "test2")
assert.NilError(t, err)
assert.Assert(t, is.Equal(v.Driver, "other"))
_, err = service.Create(ctx, "test", volume.DefaultDriverName)
assert.NilError(t, err)
pr, err = service.Prune(ctx, filters.NewArgs(filters.Arg("label!", "banana")))
assert.NilError(t, err)
assert.Assert(t, is.Len(pr.VolumesDeleted, 1))
assert.Assert(t, is.Equal(pr.VolumesDeleted[0], "test"))
v, err = service.Get(ctx, "test2")
assert.NilError(t, err)
assert.Assert(t, is.Equal(v.Driver, "other"))
_, err = service.Create(ctx, "test", volume.DefaultDriverName, opts.WithCreateLabels(map[string]string{"banana": ""}))
assert.NilError(t, err)
pr, err = service.Prune(ctx, filters.NewArgs(filters.Arg("label!", "banana")))
assert.NilError(t, err)
assert.Assert(t, is.Len(pr.VolumesDeleted, 0))
_, err = service.Create(ctx, "test3", volume.DefaultDriverName, opts.WithCreateLabels(map[string]string{"banana": "split"}))
assert.NilError(t, err)
pr, err = service.Prune(ctx, filters.NewArgs(filters.Arg("label!", "banana=split")))
assert.NilError(t, err)
assert.Assert(t, is.Len(pr.VolumesDeleted, 1))
assert.Assert(t, is.Equal(pr.VolumesDeleted[0], "test"))
pr, err = service.Prune(ctx, filters.NewArgs(filters.Arg("label", "banana=split")))
assert.NilError(t, err)
assert.Assert(t, is.Len(pr.VolumesDeleted, 1))
assert.Assert(t, is.Equal(pr.VolumesDeleted[0], "test3"))
v, err = service.Create(ctx, "test", volume.DefaultDriverName, opts.WithCreateReference(t.Name()))
assert.NilError(t, err)
pr, err = service.Prune(ctx, filters.NewArgs())
assert.NilError(t, err)
assert.Assert(t, is.Len(pr.VolumesDeleted, 0))
assert.Assert(t, service.Release(ctx, v.Name, t.Name()))
pr, err = service.Prune(ctx, filters.NewArgs())
assert.NilError(t, err)
assert.Assert(t, is.Len(pr.VolumesDeleted, 1))
assert.Assert(t, is.Equal(pr.VolumesDeleted[0], "test"))
}
func newTestService(t *testing.T, ds *volumedrivers.Store) (*VolumesService, func()) {
t.Helper()
dir, err := ioutil.TempDir("", t.Name())
assert.NilError(t, err)
store, err := NewStore(dir, ds)
assert.NilError(t, err)
s := &VolumesService{vs: store, eventLogger: dummyEventLogger{}}
return s, func() {
assert.Check(t, s.Shutdown())
assert.Check(t, os.RemoveAll(dir))
}
}
type dummyEventLogger struct{}
func (dummyEventLogger) LogVolumeEvent(_, _ string, _ map[string]string) {}
|