docker_api_swarm_config_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // +build !windows
  2. package main
  3. import (
  4. "github.com/docker/docker/api/types/swarm"
  5. "github.com/docker/docker/integration-cli/checker"
  6. "github.com/go-check/check"
  7. "golang.org/x/net/context"
  8. )
  9. func (s *DockerSwarmSuite) TestAPISwarmConfigsEmptyList(c *check.C) {
  10. d := s.AddDaemon(c, true, true)
  11. configs := d.ListConfigs(c)
  12. c.Assert(configs, checker.NotNil)
  13. c.Assert(len(configs), checker.Equals, 0, check.Commentf("configs: %#v", configs))
  14. }
  15. func (s *DockerSwarmSuite) TestAPISwarmConfigsCreate(c *check.C) {
  16. d := s.AddDaemon(c, true, true)
  17. testName := "test_config"
  18. id := d.CreateConfig(c, swarm.ConfigSpec{
  19. Annotations: swarm.Annotations{
  20. Name: testName,
  21. },
  22. Data: []byte("TESTINGDATA"),
  23. })
  24. c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("configs: %s", id))
  25. configs := d.ListConfigs(c)
  26. c.Assert(len(configs), checker.Equals, 1, check.Commentf("configs: %#v", configs))
  27. name := configs[0].Spec.Annotations.Name
  28. c.Assert(name, checker.Equals, testName, check.Commentf("configs: %s", name))
  29. }
  30. func (s *DockerSwarmSuite) TestAPISwarmConfigsDelete(c *check.C) {
  31. d := s.AddDaemon(c, true, true)
  32. testName := "test_config"
  33. id := d.CreateConfig(c, swarm.ConfigSpec{Annotations: swarm.Annotations{
  34. Name: testName,
  35. },
  36. Data: []byte("TESTINGDATA"),
  37. })
  38. c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("configs: %s", id))
  39. config := d.GetConfig(c, id)
  40. c.Assert(config.ID, checker.Equals, id, check.Commentf("config: %v", config))
  41. d.DeleteConfig(c, config.ID)
  42. cli, err := d.NewClient()
  43. c.Assert(err, checker.IsNil)
  44. defer cli.Close()
  45. _, _, err = cli.ConfigInspectWithRaw(context.Background(), id)
  46. c.Assert(err.Error(), checker.Contains, "No such config")
  47. }
  48. func (s *DockerSwarmSuite) TestAPISwarmConfigsUpdate(c *check.C) {
  49. d := s.AddDaemon(c, true, true)
  50. testName := "test_config"
  51. id := d.CreateConfig(c, swarm.ConfigSpec{
  52. Annotations: swarm.Annotations{
  53. Name: testName,
  54. Labels: map[string]string{
  55. "test": "test1",
  56. },
  57. },
  58. Data: []byte("TESTINGDATA"),
  59. })
  60. c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("configs: %s", id))
  61. config := d.GetConfig(c, id)
  62. c.Assert(config.ID, checker.Equals, id, check.Commentf("config: %v", config))
  63. // test UpdateConfig with full ID
  64. d.UpdateConfig(c, id, func(s *swarm.Config) {
  65. s.Spec.Labels = map[string]string{
  66. "test": "test1",
  67. }
  68. })
  69. config = d.GetConfig(c, id)
  70. c.Assert(config.Spec.Labels["test"], checker.Equals, "test1", check.Commentf("config: %v", config))
  71. // test UpdateConfig with full name
  72. d.UpdateConfig(c, config.Spec.Name, func(s *swarm.Config) {
  73. s.Spec.Labels = map[string]string{
  74. "test": "test2",
  75. }
  76. })
  77. config = d.GetConfig(c, id)
  78. c.Assert(config.Spec.Labels["test"], checker.Equals, "test2", check.Commentf("config: %v", config))
  79. // test UpdateConfig with prefix ID
  80. d.UpdateConfig(c, id[:1], func(s *swarm.Config) {
  81. s.Spec.Labels = map[string]string{
  82. "test": "test3",
  83. }
  84. })
  85. config = d.GetConfig(c, id)
  86. c.Assert(config.Spec.Labels["test"], checker.Equals, "test3", check.Commentf("config: %v", config))
  87. // test UpdateConfig in updating Data which is not supported in daemon
  88. // this test will produce an error in func UpdateConfig
  89. config = d.GetConfig(c, id)
  90. config.Spec.Data = []byte("TESTINGDATA2")
  91. cli, err := d.NewClient()
  92. c.Assert(err, checker.IsNil)
  93. defer cli.Close()
  94. expected := "only updates to Labels are allowed"
  95. err = cli.ConfigUpdate(context.Background(), config.ID, config.Version, config.Spec)
  96. c.Assert(err.Error(), checker.Contains, expected)
  97. }