config.go 1.8 KB

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