github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/internal/test/daemon/service.go (about)

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