github.com/portworx/docker@v1.12.1/integration-cli/docker_cli_service_update_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"encoding/json"
     7  
     8  	"github.com/docker/docker/pkg/integration/checker"
     9  	"github.com/docker/engine-api/types/swarm"
    10  	"github.com/go-check/check"
    11  )
    12  
    13  func (s *DockerSwarmSuite) TestServiceUpdatePort(c *check.C) {
    14  	d := s.AddDaemon(c, true, true)
    15  
    16  	serviceName := "TestServiceUpdatePort"
    17  	serviceArgs := append([]string{"create", "--name", serviceName, "-p", "8080:8081", defaultSleepImage}, defaultSleepCommand...)
    18  
    19  	// Create a service with a port mapping of 8080:8081.
    20  	out, err := d.Cmd("service", serviceArgs...)
    21  	c.Assert(err, checker.IsNil)
    22  	waitAndAssert(c, defaultReconciliationTimeout, d.checkActiveContainerCount, checker.Equals, 1)
    23  
    24  	// Update the service: changed the port mapping from 8080:8081 to 8082:8083.
    25  	_, err = d.Cmd("service", "update", "--publish-add", "8082:8083", "--publish-rm", "8081", serviceName)
    26  	c.Assert(err, checker.IsNil)
    27  
    28  	// Inspect the service and verify port mapping
    29  	expected := []swarm.PortConfig{
    30  		{
    31  			Protocol:      "tcp",
    32  			PublishedPort: 8082,
    33  			TargetPort:    8083,
    34  		},
    35  	}
    36  
    37  	out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.EndpointSpec.Ports }}", serviceName)
    38  	c.Assert(err, checker.IsNil)
    39  
    40  	var portConfig []swarm.PortConfig
    41  	if err := json.Unmarshal([]byte(out), &portConfig); err != nil {
    42  		c.Fatalf("invalid JSON in inspect result: %v (%s)", err, out)
    43  	}
    44  	c.Assert(portConfig, checker.DeepEquals, expected)
    45  }
    46  
    47  func (s *DockerSwarmSuite) TestServiceUpdateLabel(c *check.C) {
    48  	d := s.AddDaemon(c, true, true)
    49  	out, err := d.Cmd("service", "create", "--name=test", "busybox", "top")
    50  	c.Assert(err, checker.IsNil, check.Commentf(out))
    51  	service := d.getService(c, "test")
    52  	c.Assert(service.Spec.Labels, checker.HasLen, 0)
    53  
    54  	// add label to empty set
    55  	out, err = d.Cmd("service", "update", "test", "--label-add", "foo=bar")
    56  	c.Assert(err, checker.IsNil, check.Commentf(out))
    57  	service = d.getService(c, "test")
    58  	c.Assert(service.Spec.Labels, checker.HasLen, 1)
    59  	c.Assert(service.Spec.Labels["foo"], checker.Equals, "bar")
    60  
    61  	// add label to non-empty set
    62  	out, err = d.Cmd("service", "update", "test", "--label-add", "foo2=bar")
    63  	c.Assert(err, checker.IsNil, check.Commentf(out))
    64  	service = d.getService(c, "test")
    65  	c.Assert(service.Spec.Labels, checker.HasLen, 2)
    66  	c.Assert(service.Spec.Labels["foo2"], checker.Equals, "bar")
    67  
    68  	out, err = d.Cmd("service", "update", "test", "--label-rm", "foo2")
    69  	c.Assert(err, checker.IsNil, check.Commentf(out))
    70  	service = d.getService(c, "test")
    71  	c.Assert(service.Spec.Labels, checker.HasLen, 1)
    72  	c.Assert(service.Spec.Labels["foo2"], checker.Equals, "")
    73  
    74  	out, err = d.Cmd("service", "update", "test", "--label-rm", "foo")
    75  	c.Assert(err, checker.IsNil, check.Commentf(out))
    76  	service = d.getService(c, "test")
    77  	c.Assert(service.Spec.Labels, checker.HasLen, 0)
    78  	c.Assert(service.Spec.Labels["foo"], checker.Equals, "")
    79  
    80  	// now make sure we can add again
    81  	out, err = d.Cmd("service", "update", "test", "--label-add", "foo=bar")
    82  	c.Assert(err, checker.IsNil, check.Commentf(out))
    83  	service = d.getService(c, "test")
    84  	c.Assert(service.Spec.Labels, checker.HasLen, 1)
    85  	c.Assert(service.Spec.Labels["foo"], checker.Equals, "bar")
    86  }