docker_api_swarm_config_test.go 3.5 KB

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