github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/integration-cli/docker_api_swarm_config_test.go (about)

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