github.com/jfrazelle/docker@v1.1.2-0.20210712172922-bf78e25fe508/testutil/daemon/config.go (about)

     1  package daemon
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/api/types"
     8  	"github.com/docker/docker/api/types/swarm"
     9  	"gotest.tools/v3/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 testing.TB, configSpec swarm.ConfigSpec) string {
    17  	t.Helper()
    18  	cli := d.NewClientT(t)
    19  	defer cli.Close()
    20  
    21  	scr, err := cli.ConfigCreate(context.Background(), configSpec)
    22  	assert.NilError(t, err)
    23  	return scr.ID
    24  }
    25  
    26  // ListConfigs returns the list of the current swarm configs
    27  func (d *Daemon) ListConfigs(t testing.TB) []swarm.Config {
    28  	t.Helper()
    29  	cli := d.NewClientT(t)
    30  	defer cli.Close()
    31  
    32  	configs, err := cli.ConfigList(context.Background(), types.ConfigListOptions{})
    33  	assert.NilError(t, err)
    34  	return configs
    35  }
    36  
    37  // GetConfig returns a swarm config identified by the specified id
    38  func (d *Daemon) GetConfig(t testing.TB, id string) *swarm.Config {
    39  	t.Helper()
    40  	cli := d.NewClientT(t)
    41  	defer cli.Close()
    42  
    43  	config, _, err := cli.ConfigInspectWithRaw(context.Background(), id)
    44  	assert.NilError(t, err)
    45  	return &config
    46  }
    47  
    48  // DeleteConfig removes the swarm config identified by the specified id
    49  func (d *Daemon) DeleteConfig(t testing.TB, id string) {
    50  	t.Helper()
    51  	cli := d.NewClientT(t)
    52  	defer cli.Close()
    53  
    54  	err := cli.ConfigRemove(context.Background(), id)
    55  	assert.NilError(t, err)
    56  }
    57  
    58  // UpdateConfig updates the swarm config identified by the specified id
    59  // Currently, only label update is supported.
    60  func (d *Daemon) UpdateConfig(t testing.TB, id string, f ...ConfigConstructor) {
    61  	t.Helper()
    62  	cli := d.NewClientT(t)
    63  	defer cli.Close()
    64  
    65  	config := d.GetConfig(t, id)
    66  	for _, fn := range f {
    67  		fn(config)
    68  	}
    69  
    70  	err := cli.ConfigUpdate(context.Background(), config.ID, config.Version, config.Spec)
    71  	assert.NilError(t, err)
    72  }