github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/internal/test/daemon/config.go (about)

     1  package daemon
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/docker/docker/api/types"
     7  	"github.com/docker/docker/api/types/swarm"
     8  	"github.com/docker/docker/internal/test"
     9  	"gotest.tools/assert"
    10  )
    11  
    12  // ConfigConstructor defines a swarm config constructor
    13  type ConfigConstructor func(*swarm.Config)
    14  
    15  // CreateConfig creates a config given the specified spec
    16  func (d *Daemon) CreateConfig(t assert.TestingT, configSpec swarm.ConfigSpec) string {
    17  	if ht, ok := t.(test.HelperT); ok {
    18  		ht.Helper()
    19  	}
    20  	cli := d.NewClientT(t)
    21  	defer cli.Close()
    22  
    23  	scr, err := cli.ConfigCreate(context.Background(), configSpec)
    24  	assert.NilError(t, err)
    25  	return scr.ID
    26  }
    27  
    28  // ListConfigs returns the list of the current swarm configs
    29  func (d *Daemon) ListConfigs(t assert.TestingT) []swarm.Config {
    30  	if ht, ok := t.(test.HelperT); ok {
    31  		ht.Helper()
    32  	}
    33  	cli := d.NewClientT(t)
    34  	defer cli.Close()
    35  
    36  	configs, err := cli.ConfigList(context.Background(), types.ConfigListOptions{})
    37  	assert.NilError(t, err)
    38  	return configs
    39  }
    40  
    41  // GetConfig returns a swarm config identified by the specified id
    42  func (d *Daemon) GetConfig(t assert.TestingT, id string) *swarm.Config {
    43  	if ht, ok := t.(test.HelperT); ok {
    44  		ht.Helper()
    45  	}
    46  	cli := d.NewClientT(t)
    47  	defer cli.Close()
    48  
    49  	config, _, err := cli.ConfigInspectWithRaw(context.Background(), id)
    50  	assert.NilError(t, err)
    51  	return &config
    52  }
    53  
    54  // DeleteConfig removes the swarm config identified by the specified id
    55  func (d *Daemon) DeleteConfig(t assert.TestingT, id string) {
    56  	if ht, ok := t.(test.HelperT); ok {
    57  		ht.Helper()
    58  	}
    59  	cli := d.NewClientT(t)
    60  	defer cli.Close()
    61  
    62  	err := cli.ConfigRemove(context.Background(), id)
    63  	assert.NilError(t, err)
    64  }
    65  
    66  // UpdateConfig updates the swarm config identified by the specified id
    67  // Currently, only label update is supported.
    68  func (d *Daemon) UpdateConfig(t assert.TestingT, id string, f ...ConfigConstructor) {
    69  	if ht, ok := t.(test.HelperT); ok {
    70  		ht.Helper()
    71  	}
    72  	cli := d.NewClientT(t)
    73  	defer cli.Close()
    74  
    75  	config := d.GetConfig(t, id)
    76  	for _, fn := range f {
    77  		fn(config)
    78  	}
    79  
    80  	err := cli.ConfigUpdate(context.Background(), config.ID, config.Version, config.Spec)
    81  	assert.NilError(t, err)
    82  }