github.com/cookieai-jar/moby@v17.12.1-ce-rc2+incompatible/integration-cli/docker_api_swarm_config_test.go (about)

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