github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/volume/service/service_test.go (about) 1 package service 2 3 import ( 4 "context" 5 "io/ioutil" 6 "os" 7 "testing" 8 9 "github.com/demonoid81/moby/api/types/filters" 10 "github.com/demonoid81/moby/errdefs" 11 "github.com/demonoid81/moby/volume" 12 volumedrivers "github.com/demonoid81/moby/volume/drivers" 13 "github.com/demonoid81/moby/volume/service/opts" 14 "github.com/demonoid81/moby/volume/testutils" 15 "gotest.tools/v3/assert" 16 is "gotest.tools/v3/assert/cmp" 17 ) 18 19 func TestServiceCreate(t *testing.T) { 20 t.Parallel() 21 22 ds := volumedrivers.NewStore(nil) 23 assert.Assert(t, ds.Register(testutils.NewFakeDriver("d1"), "d1")) 24 assert.Assert(t, ds.Register(testutils.NewFakeDriver("d2"), "d2")) 25 26 ctx := context.Background() 27 service, cleanup := newTestService(t, ds) 28 defer cleanup() 29 30 _, err := service.Create(ctx, "v1", "notexist") 31 assert.Assert(t, errdefs.IsNotFound(err), err) 32 33 v, err := service.Create(ctx, "v1", "d1") 34 assert.NilError(t, err) 35 36 vCopy, err := service.Create(ctx, "v1", "d1") 37 assert.NilError(t, err) 38 assert.Assert(t, is.DeepEqual(v, vCopy)) 39 40 _, err = service.Create(ctx, "v1", "d2") 41 assert.Check(t, IsNameConflict(err), err) 42 assert.Check(t, errdefs.IsConflict(err), err) 43 44 assert.Assert(t, service.Remove(ctx, "v1")) 45 _, err = service.Create(ctx, "v1", "d2") 46 assert.NilError(t, err) 47 _, err = service.Create(ctx, "v1", "d2") 48 assert.NilError(t, err) 49 50 } 51 52 func TestServiceList(t *testing.T) { 53 t.Parallel() 54 55 ds := volumedrivers.NewStore(nil) 56 assert.Assert(t, ds.Register(testutils.NewFakeDriver("d1"), "d1")) 57 assert.Assert(t, ds.Register(testutils.NewFakeDriver("d2"), "d2")) 58 59 service, cleanup := newTestService(t, ds) 60 defer cleanup() 61 62 ctx := context.Background() 63 64 _, err := service.Create(ctx, "v1", "d1") 65 assert.NilError(t, err) 66 _, err = service.Create(ctx, "v2", "d1") 67 assert.NilError(t, err) 68 _, err = service.Create(ctx, "v3", "d2") 69 assert.NilError(t, err) 70 71 ls, _, err := service.List(ctx, filters.NewArgs(filters.Arg("driver", "d1"))) 72 assert.NilError(t, err) 73 assert.Check(t, is.Len(ls, 2)) 74 75 ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("driver", "d2"))) 76 assert.NilError(t, err) 77 assert.Check(t, is.Len(ls, 1)) 78 79 ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("driver", "notexist"))) 80 assert.NilError(t, err) 81 assert.Check(t, is.Len(ls, 0)) 82 83 ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("dangling", "true"))) 84 assert.NilError(t, err) 85 assert.Check(t, is.Len(ls, 3)) 86 ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("dangling", "false"))) 87 assert.NilError(t, err) 88 assert.Check(t, is.Len(ls, 0)) 89 90 _, err = service.Get(ctx, "v1", opts.WithGetReference("foo")) 91 assert.NilError(t, err) 92 ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("dangling", "true"))) 93 assert.NilError(t, err) 94 assert.Check(t, is.Len(ls, 2)) 95 ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("dangling", "false"))) 96 assert.NilError(t, err) 97 assert.Check(t, is.Len(ls, 1)) 98 99 ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("dangling", "false"), filters.Arg("driver", "d2"))) 100 assert.NilError(t, err) 101 assert.Check(t, is.Len(ls, 0)) 102 ls, _, err = service.List(ctx, filters.NewArgs(filters.Arg("dangling", "true"), filters.Arg("driver", "d2"))) 103 assert.NilError(t, err) 104 assert.Check(t, is.Len(ls, 1)) 105 } 106 107 func TestServiceRemove(t *testing.T) { 108 t.Parallel() 109 110 ds := volumedrivers.NewStore(nil) 111 assert.Assert(t, ds.Register(testutils.NewFakeDriver("d1"), "d1")) 112 113 service, cleanup := newTestService(t, ds) 114 defer cleanup() 115 ctx := context.Background() 116 117 _, err := service.Create(ctx, "test", "d1") 118 assert.NilError(t, err) 119 120 assert.Assert(t, service.Remove(ctx, "test")) 121 assert.Assert(t, service.Remove(ctx, "test", opts.WithPurgeOnError(true))) 122 } 123 124 func TestServiceGet(t *testing.T) { 125 t.Parallel() 126 127 ds := volumedrivers.NewStore(nil) 128 assert.Assert(t, ds.Register(testutils.NewFakeDriver("d1"), "d1")) 129 130 service, cleanup := newTestService(t, ds) 131 defer cleanup() 132 ctx := context.Background() 133 134 v, err := service.Get(ctx, "notexist") 135 assert.Assert(t, IsNotExist(err)) 136 assert.Check(t, v == nil) 137 138 created, err := service.Create(ctx, "test", "d1") 139 assert.NilError(t, err) 140 assert.Assert(t, created != nil) 141 142 v, err = service.Get(ctx, "test") 143 assert.NilError(t, err) 144 assert.Assert(t, is.DeepEqual(created, v)) 145 146 v, err = service.Get(ctx, "test", opts.WithGetResolveStatus) 147 assert.NilError(t, err) 148 assert.Assert(t, is.Len(v.Status, 1), v.Status) 149 150 _, err = service.Get(ctx, "test", opts.WithGetDriver("notarealdriver")) 151 assert.Assert(t, errdefs.IsConflict(err), err) 152 v, err = service.Get(ctx, "test", opts.WithGetDriver("d1")) 153 assert.Assert(t, err == nil) 154 assert.Assert(t, is.DeepEqual(created, v)) 155 156 assert.Assert(t, ds.Register(testutils.NewFakeDriver("d2"), "d2")) 157 _, err = service.Get(ctx, "test", opts.WithGetDriver("d2")) 158 assert.Assert(t, errdefs.IsConflict(err), err) 159 } 160 161 func TestServicePrune(t *testing.T) { 162 t.Parallel() 163 164 ds := volumedrivers.NewStore(nil) 165 assert.Assert(t, ds.Register(testutils.NewFakeDriver(volume.DefaultDriverName), volume.DefaultDriverName)) 166 assert.Assert(t, ds.Register(testutils.NewFakeDriver("other"), "other")) 167 168 service, cleanup := newTestService(t, ds) 169 defer cleanup() 170 ctx := context.Background() 171 172 _, err := service.Create(ctx, "test", volume.DefaultDriverName) 173 assert.NilError(t, err) 174 _, err = service.Create(ctx, "test2", "other") 175 assert.NilError(t, err) 176 177 pr, err := service.Prune(ctx, filters.NewArgs(filters.Arg("label", "banana"))) 178 assert.NilError(t, err) 179 assert.Assert(t, is.Len(pr.VolumesDeleted, 0)) 180 181 pr, err = service.Prune(ctx, filters.NewArgs()) 182 assert.NilError(t, err) 183 assert.Assert(t, is.Len(pr.VolumesDeleted, 1)) 184 assert.Assert(t, is.Equal(pr.VolumesDeleted[0], "test")) 185 186 _, err = service.Get(ctx, "test") 187 assert.Assert(t, IsNotExist(err), err) 188 189 v, err := service.Get(ctx, "test2") 190 assert.NilError(t, err) 191 assert.Assert(t, is.Equal(v.Driver, "other")) 192 193 _, err = service.Create(ctx, "test", volume.DefaultDriverName) 194 assert.NilError(t, err) 195 196 pr, err = service.Prune(ctx, filters.NewArgs(filters.Arg("label!", "banana"))) 197 assert.NilError(t, err) 198 assert.Assert(t, is.Len(pr.VolumesDeleted, 1)) 199 assert.Assert(t, is.Equal(pr.VolumesDeleted[0], "test")) 200 v, err = service.Get(ctx, "test2") 201 assert.NilError(t, err) 202 assert.Assert(t, is.Equal(v.Driver, "other")) 203 204 _, err = service.Create(ctx, "test", volume.DefaultDriverName, opts.WithCreateLabels(map[string]string{"banana": ""})) 205 assert.NilError(t, err) 206 pr, err = service.Prune(ctx, filters.NewArgs(filters.Arg("label!", "banana"))) 207 assert.NilError(t, err) 208 assert.Assert(t, is.Len(pr.VolumesDeleted, 0)) 209 210 _, err = service.Create(ctx, "test3", volume.DefaultDriverName, opts.WithCreateLabels(map[string]string{"banana": "split"})) 211 assert.NilError(t, err) 212 pr, err = service.Prune(ctx, filters.NewArgs(filters.Arg("label!", "banana=split"))) 213 assert.NilError(t, err) 214 assert.Assert(t, is.Len(pr.VolumesDeleted, 1)) 215 assert.Assert(t, is.Equal(pr.VolumesDeleted[0], "test")) 216 217 pr, err = service.Prune(ctx, filters.NewArgs(filters.Arg("label", "banana=split"))) 218 assert.NilError(t, err) 219 assert.Assert(t, is.Len(pr.VolumesDeleted, 1)) 220 assert.Assert(t, is.Equal(pr.VolumesDeleted[0], "test3")) 221 222 v, err = service.Create(ctx, "test", volume.DefaultDriverName, opts.WithCreateReference(t.Name())) 223 assert.NilError(t, err) 224 225 pr, err = service.Prune(ctx, filters.NewArgs()) 226 assert.NilError(t, err) 227 assert.Assert(t, is.Len(pr.VolumesDeleted, 0)) 228 assert.Assert(t, service.Release(ctx, v.Name, t.Name())) 229 230 pr, err = service.Prune(ctx, filters.NewArgs()) 231 assert.NilError(t, err) 232 assert.Assert(t, is.Len(pr.VolumesDeleted, 1)) 233 assert.Assert(t, is.Equal(pr.VolumesDeleted[0], "test")) 234 } 235 236 func newTestService(t *testing.T, ds *volumedrivers.Store) (*VolumesService, func()) { 237 t.Helper() 238 239 dir, err := ioutil.TempDir("", t.Name()) 240 assert.NilError(t, err) 241 242 store, err := NewStore(dir, ds) 243 assert.NilError(t, err) 244 s := &VolumesService{vs: store, eventLogger: dummyEventLogger{}} 245 return s, func() { 246 assert.Check(t, s.Shutdown()) 247 assert.Check(t, os.RemoveAll(dir)) 248 } 249 } 250 251 type dummyEventLogger struct{} 252 253 func (dummyEventLogger) LogVolumeEvent(_, _ string, _ map[string]string) {}