github.com/jfrazelle/docker@v1.1.2-0.20210712172922-bf78e25fe508/testutil/daemon/service.go (about) 1 package daemon 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/docker/docker/api/types" 9 "github.com/docker/docker/api/types/filters" 10 "github.com/docker/docker/api/types/swarm" 11 "gotest.tools/v3/assert" 12 ) 13 14 // ServiceConstructor defines a swarm service constructor function 15 type ServiceConstructor func(*swarm.Service) 16 17 func (d *Daemon) createServiceWithOptions(t testing.TB, opts types.ServiceCreateOptions, f ...ServiceConstructor) string { 18 t.Helper() 19 var service swarm.Service 20 for _, fn := range f { 21 fn(&service) 22 } 23 24 cli := d.NewClientT(t) 25 defer cli.Close() 26 27 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) 28 defer cancel() 29 30 res, err := cli.ServiceCreate(ctx, service.Spec, opts) 31 assert.NilError(t, err) 32 return res.ID 33 } 34 35 // CreateService creates a swarm service given the specified service constructor 36 func (d *Daemon) CreateService(t testing.TB, f ...ServiceConstructor) string { 37 t.Helper() 38 return d.createServiceWithOptions(t, types.ServiceCreateOptions{}, f...) 39 } 40 41 // GetService returns the swarm service corresponding to the specified id 42 func (d *Daemon) GetService(t testing.TB, id string) *swarm.Service { 43 t.Helper() 44 cli := d.NewClientT(t) 45 defer cli.Close() 46 47 service, _, err := cli.ServiceInspectWithRaw(context.Background(), id, types.ServiceInspectOptions{}) 48 assert.NilError(t, err) 49 return &service 50 } 51 52 // GetServiceTasks returns the swarm tasks for the specified service 53 func (d *Daemon) GetServiceTasks(t testing.TB, service string, additionalFilters ...filters.KeyValuePair) []swarm.Task { 54 t.Helper() 55 cli := d.NewClientT(t) 56 defer cli.Close() 57 58 filterArgs := filters.NewArgs() 59 filterArgs.Add("desired-state", "running") 60 filterArgs.Add("service", service) 61 for _, filter := range additionalFilters { 62 filterArgs.Add(filter.Key, filter.Value) 63 } 64 65 options := types.TaskListOptions{ 66 Filters: filterArgs, 67 } 68 69 tasks, err := cli.TaskList(context.Background(), options) 70 assert.NilError(t, err) 71 return tasks 72 } 73 74 // UpdateService updates a swarm service with the specified service constructor 75 func (d *Daemon) UpdateService(t testing.TB, service *swarm.Service, f ...ServiceConstructor) { 76 t.Helper() 77 cli := d.NewClientT(t) 78 defer cli.Close() 79 80 for _, fn := range f { 81 fn(service) 82 } 83 84 _, err := cli.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{}) 85 assert.NilError(t, err) 86 } 87 88 // RemoveService removes the specified service 89 func (d *Daemon) RemoveService(t testing.TB, id string) { 90 t.Helper() 91 cli := d.NewClientT(t) 92 defer cli.Close() 93 94 err := cli.ServiceRemove(context.Background(), id) 95 assert.NilError(t, err) 96 } 97 98 // ListServices returns the list of the current swarm services 99 func (d *Daemon) ListServices(t testing.TB) []swarm.Service { 100 t.Helper() 101 cli := d.NewClientT(t) 102 defer cli.Close() 103 104 services, err := cli.ServiceList(context.Background(), types.ServiceListOptions{}) 105 assert.NilError(t, err) 106 return services 107 } 108 109 // GetTask returns the swarm task identified by the specified id 110 func (d *Daemon) GetTask(t testing.TB, id string) swarm.Task { 111 t.Helper() 112 cli := d.NewClientT(t) 113 defer cli.Close() 114 115 task, _, err := cli.TaskInspectWithRaw(context.Background(), id) 116 assert.NilError(t, err) 117 return task 118 }