2018-04-11 06:10:17 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-23 08:23:01 -04:00
|
|
|
"testing"
|
2018-04-11 06:10:17 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2018-04-11 06:10:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServiceConstructor defines a swarm service constructor function
|
|
|
|
type ServiceConstructor func(*swarm.Service)
|
|
|
|
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) createServiceWithOptions(t testing.TB, opts types.ServiceCreateOptions, f ...ServiceConstructor) string {
|
|
|
|
t.Helper()
|
2018-04-11 06:10:17 -04:00
|
|
|
var service swarm.Service
|
|
|
|
for _, fn := range f {
|
|
|
|
fn(&service)
|
|
|
|
}
|
|
|
|
|
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
res, err := cli.ServiceCreate(ctx, service.Spec, opts)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
return res.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateService creates a swarm service given the specified service constructor
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) CreateService(t testing.TB, f ...ServiceConstructor) string {
|
|
|
|
t.Helper()
|
2018-04-13 11:02:56 -04:00
|
|
|
return d.createServiceWithOptions(t, types.ServiceCreateOptions{}, f...)
|
2018-04-11 06:10:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetService returns the swarm service corresponding to the specified id
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) GetService(t testing.TB, id string) *swarm.Service {
|
|
|
|
t.Helper()
|
2018-04-11 06:10:17 -04:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
service, _, err := cli.ServiceInspectWithRaw(context.Background(), id, types.ServiceInspectOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
return &service
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetServiceTasks returns the swarm tasks for the specified service
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) GetServiceTasks(t testing.TB, service string, additionalFilters ...filters.KeyValuePair) []swarm.Task {
|
|
|
|
t.Helper()
|
2018-04-11 06:10:17 -04:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("desired-state", "running")
|
|
|
|
filterArgs.Add("service", service)
|
2018-12-27 12:04:10 -05:00
|
|
|
for _, filter := range additionalFilters {
|
|
|
|
filterArgs.Add(filter.Key, filter.Value)
|
|
|
|
}
|
2018-04-11 06:10:17 -04:00
|
|
|
|
|
|
|
options := types.TaskListOptions{
|
|
|
|
Filters: filterArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks, err := cli.TaskList(context.Background(), options)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
return tasks
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateService updates a swarm service with the specified service constructor
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) UpdateService(t testing.TB, service *swarm.Service, f ...ServiceConstructor) {
|
|
|
|
t.Helper()
|
2018-04-11 06:10:17 -04:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
for _, fn := range f {
|
|
|
|
fn(service)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := cli.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveService removes the specified service
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) RemoveService(t testing.TB, id string) {
|
|
|
|
t.Helper()
|
2018-04-11 06:10:17 -04:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
err := cli.ServiceRemove(context.Background(), id)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListServices returns the list of the current swarm services
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) ListServices(t testing.TB) []swarm.Service {
|
|
|
|
t.Helper()
|
2018-04-11 06:10:17 -04:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
services, err := cli.ServiceList(context.Background(), types.ServiceListOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTask returns the swarm task identified by the specified id
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) GetTask(t testing.TB, id string) swarm.Task {
|
|
|
|
t.Helper()
|
2018-04-11 06:10:17 -04:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
task, _, err := cli.TaskInspectWithRaw(context.Background(), id)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
return task
|
|
|
|
}
|