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
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
// ConfigConstructor defines a swarm config constructor
|
|
|
|
type ConfigConstructor func(*swarm.Config)
|
|
|
|
|
|
|
|
// CreateConfig creates a config given the specified spec
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) CreateConfig(t testing.TB, configSpec swarm.ConfigSpec) string {
|
|
|
|
t.Helper()
|
2018-04-11 06:10:17 -04:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
scr, err := cli.ConfigCreate(context.Background(), configSpec)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
return scr.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListConfigs returns the list of the current swarm configs
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) ListConfigs(t testing.TB) []swarm.Config {
|
|
|
|
t.Helper()
|
2018-04-11 06:10:17 -04:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
configs, err := cli.ConfigList(context.Background(), types.ConfigListOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
return configs
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfig returns a swarm config identified by the specified id
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) GetConfig(t testing.TB, id string) *swarm.Config {
|
|
|
|
t.Helper()
|
2018-04-11 06:10:17 -04:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
config, _, err := cli.ConfigInspectWithRaw(context.Background(), id)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
return &config
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteConfig removes the swarm config identified by the specified id
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) DeleteConfig(t testing.TB, id string) {
|
|
|
|
t.Helper()
|
2018-04-11 06:10:17 -04:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
err := cli.ConfigRemove(context.Background(), id)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateConfig updates the swarm config identified by the specified id
|
|
|
|
// Currently, only label update is supported.
|
2019-09-23 08:23:01 -04:00
|
|
|
func (d *Daemon) UpdateConfig(t testing.TB, id string, f ...ConfigConstructor) {
|
|
|
|
t.Helper()
|
2018-04-11 06:10:17 -04:00
|
|
|
cli := d.NewClientT(t)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
config := d.GetConfig(t, id)
|
|
|
|
for _, fn := range f {
|
|
|
|
fn(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := cli.ConfigUpdate(context.Background(), config.ID, config.Version, config.Spec)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|