github.com/docker/docker-ce@v17.12.1-ce-rc2+incompatible/components/engine/integration-cli/docker_cli_config_create_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/docker/docker/api/types/swarm"
    11  	"github.com/docker/docker/integration-cli/checker"
    12  	"github.com/go-check/check"
    13  )
    14  
    15  func (s *DockerSwarmSuite) TestConfigCreate(c *check.C) {
    16  	d := s.AddDaemon(c, true, true)
    17  
    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  
    27  	config := d.GetConfig(c, id)
    28  	c.Assert(config.Spec.Name, checker.Equals, testName)
    29  }
    30  
    31  func (s *DockerSwarmSuite) TestConfigCreateWithLabels(c *check.C) {
    32  	d := s.AddDaemon(c, true, true)
    33  
    34  	testName := "test_config"
    35  	id := d.CreateConfig(c, swarm.ConfigSpec{
    36  		Annotations: swarm.Annotations{
    37  			Name: testName,
    38  			Labels: map[string]string{
    39  				"key1": "value1",
    40  				"key2": "value2",
    41  			},
    42  		},
    43  		Data: []byte("TESTINGDATA"),
    44  	})
    45  	c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("configs: %s", id))
    46  
    47  	config := d.GetConfig(c, id)
    48  	c.Assert(config.Spec.Name, checker.Equals, testName)
    49  	c.Assert(len(config.Spec.Labels), checker.Equals, 2)
    50  	c.Assert(config.Spec.Labels["key1"], checker.Equals, "value1")
    51  	c.Assert(config.Spec.Labels["key2"], checker.Equals, "value2")
    52  }
    53  
    54  // Test case for 28884
    55  func (s *DockerSwarmSuite) TestConfigCreateResolve(c *check.C) {
    56  	d := s.AddDaemon(c, true, true)
    57  
    58  	name := "test_config"
    59  	id := d.CreateConfig(c, swarm.ConfigSpec{
    60  		Annotations: swarm.Annotations{
    61  			Name: name,
    62  		},
    63  		Data: []byte("foo"),
    64  	})
    65  	c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("configs: %s", id))
    66  
    67  	fake := d.CreateConfig(c, swarm.ConfigSpec{
    68  		Annotations: swarm.Annotations{
    69  			Name: id,
    70  		},
    71  		Data: []byte("fake foo"),
    72  	})
    73  	c.Assert(fake, checker.Not(checker.Equals), "", check.Commentf("configs: %s", fake))
    74  
    75  	out, err := d.Cmd("config", "ls")
    76  	c.Assert(err, checker.IsNil)
    77  	c.Assert(out, checker.Contains, name)
    78  	c.Assert(out, checker.Contains, fake)
    79  
    80  	out, err = d.Cmd("config", "rm", id)
    81  	c.Assert(out, checker.Contains, id)
    82  
    83  	// Fake one will remain
    84  	out, err = d.Cmd("config", "ls")
    85  	c.Assert(err, checker.IsNil)
    86  	c.Assert(out, checker.Not(checker.Contains), name)
    87  	c.Assert(out, checker.Contains, fake)
    88  
    89  	// Remove based on name prefix of the fake one
    90  	// (which is the same as the ID of foo one) should not work
    91  	// as search is only done based on:
    92  	// - Full ID
    93  	// - Full Name
    94  	// - Partial ID (prefix)
    95  	out, err = d.Cmd("config", "rm", id[:5])
    96  	c.Assert(out, checker.Not(checker.Contains), id)
    97  	out, err = d.Cmd("config", "ls")
    98  	c.Assert(err, checker.IsNil)
    99  	c.Assert(out, checker.Not(checker.Contains), name)
   100  	c.Assert(out, checker.Contains, fake)
   101  
   102  	// Remove based on ID prefix of the fake one should succeed
   103  	out, err = d.Cmd("config", "rm", fake[:5])
   104  	c.Assert(out, checker.Contains, fake[:5])
   105  	out, err = d.Cmd("config", "ls")
   106  	c.Assert(err, checker.IsNil)
   107  	c.Assert(out, checker.Not(checker.Contains), name)
   108  	c.Assert(out, checker.Not(checker.Contains), id)
   109  	c.Assert(out, checker.Not(checker.Contains), fake)
   110  }
   111  
   112  func (s *DockerSwarmSuite) TestConfigCreateWithFile(c *check.C) {
   113  	d := s.AddDaemon(c, true, true)
   114  
   115  	testFile, err := ioutil.TempFile("", "configCreateTest")
   116  	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
   117  	defer os.Remove(testFile.Name())
   118  
   119  	testData := "TESTINGDATA"
   120  	_, err = testFile.Write([]byte(testData))
   121  	c.Assert(err, checker.IsNil, check.Commentf("failed to write to temporary file"))
   122  
   123  	testName := "test_config"
   124  	out, err := d.Cmd("config", "create", testName, testFile.Name())
   125  	c.Assert(err, checker.IsNil)
   126  	c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "", check.Commentf(out))
   127  
   128  	id := strings.TrimSpace(out)
   129  	config := d.GetConfig(c, id)
   130  	c.Assert(config.Spec.Name, checker.Equals, testName)
   131  }