docker_api_swarm_config_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. expected := "no such config"
  46. _, _, err = cli.ConfigInspectWithRaw(context.Background(), id)
  47. c.Assert(err.Error(), checker.Contains, expected)
  48. }
  49. func (s *DockerSwarmSuite) TestAPISwarmConfigsUpdate(c *check.C) {
  50. d := s.AddDaemon(c, true, true)
  51. testName := "test_config"
  52. id := d.CreateConfig(c, swarm.ConfigSpec{
  53. Annotations: swarm.Annotations{
  54. Name: testName,
  55. Labels: map[string]string{
  56. "test": "test1",
  57. },
  58. },
  59. Data: []byte("TESTINGDATA"),
  60. })
  61. c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("configs: %s", id))
  62. config := d.GetConfig(c, id)
  63. c.Assert(config.ID, checker.Equals, id, check.Commentf("config: %v", config))
  64. // test UpdateConfig with full ID
  65. d.UpdateConfig(c, id, func(s *swarm.Config) {
  66. s.Spec.Labels = map[string]string{
  67. "test": "test1",
  68. }
  69. })
  70. config = d.GetConfig(c, id)
  71. c.Assert(config.Spec.Labels["test"], checker.Equals, "test1", check.Commentf("config: %v", config))
  72. // test UpdateConfig with full name
  73. d.UpdateConfig(c, config.Spec.Name, func(s *swarm.Config) {
  74. s.Spec.Labels = map[string]string{
  75. "test": "test2",
  76. }
  77. })
  78. config = d.GetConfig(c, id)
  79. c.Assert(config.Spec.Labels["test"], checker.Equals, "test2", check.Commentf("config: %v", config))
  80. // test UpdateConfig with prefix ID
  81. d.UpdateConfig(c, id[:1], func(s *swarm.Config) {
  82. s.Spec.Labels = map[string]string{
  83. "test": "test3",
  84. }
  85. })
  86. config = d.GetConfig(c, id)
  87. c.Assert(config.Spec.Labels["test"], checker.Equals, "test3", check.Commentf("config: %v", config))
  88. // test UpdateConfig in updating Data which is not supported in daemon
  89. // this test will produce an error in func UpdateConfig
  90. config = d.GetConfig(c, id)
  91. config.Spec.Data = []byte("TESTINGDATA2")
  92. cli, err := d.NewClient()
  93. c.Assert(err, checker.IsNil)
  94. defer cli.Close()
  95. expected := "only updates to Labels are allowed"
  96. err = cli.ConfigUpdate(context.Background(), config.ID, config.Version, config.Spec)
  97. c.Assert(err.Error(), checker.Contains, expected)
  98. }