github.com/rish1988/moby@v25.0.2+incompatible/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(ctx context.Context, 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(ctx, 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(ctx context.Context, t testing.TB, f ...ServiceConstructor) string {
    37  	t.Helper()
    38  	return d.createServiceWithOptions(ctx, t, types.ServiceCreateOptions{}, f...)
    39  }
    40  
    41  // GetService returns the swarm service corresponding to the specified id
    42  func (d *Daemon) GetService(ctx context.Context, 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(ctx, 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(ctx context.Context, 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  		filters.Arg("desired-state", "running"),
    60  		filters.Arg("service", service),
    61  	)
    62  	for _, filter := range additionalFilters {
    63  		filterArgs.Add(filter.Key, filter.Value)
    64  	}
    65  
    66  	options := types.TaskListOptions{
    67  		Filters: filterArgs,
    68  	}
    69  
    70  	tasks, err := cli.TaskList(ctx, options)
    71  	assert.NilError(t, err)
    72  	return tasks
    73  }
    74  
    75  // UpdateService updates a swarm service with the specified service constructor
    76  func (d *Daemon) UpdateService(ctx context.Context, t testing.TB, service *swarm.Service, f ...ServiceConstructor) {
    77  	t.Helper()
    78  	cli := d.NewClientT(t)
    79  	defer cli.Close()
    80  
    81  	for _, fn := range f {
    82  		fn(service)
    83  	}
    84  
    85  	_, err := cli.ServiceUpdate(ctx, service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
    86  	assert.NilError(t, err)
    87  }
    88  
    89  // RemoveService removes the specified service
    90  func (d *Daemon) RemoveService(ctx context.Context, t testing.TB, id string) {
    91  	t.Helper()
    92  	cli := d.NewClientT(t)
    93  	defer cli.Close()
    94  
    95  	err := cli.ServiceRemove(ctx, id)
    96  	assert.NilError(t, err)
    97  }
    98  
    99  // ListServices returns the list of the current swarm services
   100  func (d *Daemon) ListServices(ctx context.Context, t testing.TB) []swarm.Service {
   101  	t.Helper()
   102  	cli := d.NewClientT(t)
   103  	defer cli.Close()
   104  
   105  	services, err := cli.ServiceList(ctx, types.ServiceListOptions{})
   106  	assert.NilError(t, err)
   107  	return services
   108  }
   109  
   110  // GetTask returns the swarm task identified by the specified id
   111  func (d *Daemon) GetTask(ctx context.Context, t testing.TB, id string) swarm.Task {
   112  	t.Helper()
   113  	cli := d.NewClientT(t)
   114  	defer cli.Close()
   115  
   116  	task, _, err := cli.TaskInspectWithRaw(ctx, id)
   117  	assert.NilError(t, err)
   118  	return task
   119  }