github.com/stuarqi/moby@v1.13.1/integration-cli/docker_cli_service_create_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/api/types/mount"
    12  	"github.com/docker/docker/api/types/swarm"
    13  	"github.com/docker/docker/pkg/integration/checker"
    14  	"github.com/go-check/check"
    15  )
    16  
    17  func (s *DockerSwarmSuite) TestServiceCreateMountVolume(c *check.C) {
    18  	d := s.AddDaemon(c, true, true)
    19  	out, err := d.Cmd("service", "create", "--mount", "type=volume,source=foo,target=/foo,volume-nocopy", "busybox", "top")
    20  	c.Assert(err, checker.IsNil, check.Commentf(out))
    21  	id := strings.TrimSpace(out)
    22  
    23  	var tasks []swarm.Task
    24  	waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
    25  		tasks = d.getServiceTasks(c, id)
    26  		return len(tasks) > 0, nil
    27  	}, checker.Equals, true)
    28  
    29  	task := tasks[0]
    30  	waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
    31  		if task.NodeID == "" || task.Status.ContainerStatus.ContainerID == "" {
    32  			task = d.getTask(c, task.ID)
    33  		}
    34  		return task.NodeID != "" && task.Status.ContainerStatus.ContainerID != "", nil
    35  	}, checker.Equals, true)
    36  
    37  	// check container mount config
    38  	out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .HostConfig.Mounts}}", task.Status.ContainerStatus.ContainerID)
    39  	c.Assert(err, checker.IsNil, check.Commentf(out))
    40  
    41  	var mountConfig []mount.Mount
    42  	c.Assert(json.Unmarshal([]byte(out), &mountConfig), checker.IsNil)
    43  	c.Assert(mountConfig, checker.HasLen, 1)
    44  
    45  	c.Assert(mountConfig[0].Source, checker.Equals, "foo")
    46  	c.Assert(mountConfig[0].Target, checker.Equals, "/foo")
    47  	c.Assert(mountConfig[0].Type, checker.Equals, mount.TypeVolume)
    48  	c.Assert(mountConfig[0].VolumeOptions, checker.NotNil)
    49  	c.Assert(mountConfig[0].VolumeOptions.NoCopy, checker.True)
    50  
    51  	// check container mounts actual
    52  	out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .Mounts}}", task.Status.ContainerStatus.ContainerID)
    53  	c.Assert(err, checker.IsNil, check.Commentf(out))
    54  
    55  	var mounts []types.MountPoint
    56  	c.Assert(json.Unmarshal([]byte(out), &mounts), checker.IsNil)
    57  	c.Assert(mounts, checker.HasLen, 1)
    58  
    59  	c.Assert(mounts[0].Type, checker.Equals, mount.TypeVolume)
    60  	c.Assert(mounts[0].Name, checker.Equals, "foo")
    61  	c.Assert(mounts[0].Destination, checker.Equals, "/foo")
    62  	c.Assert(mounts[0].RW, checker.Equals, true)
    63  }
    64  
    65  func (s *DockerSwarmSuite) TestServiceCreateWithSecretSimple(c *check.C) {
    66  	d := s.AddDaemon(c, true, true)
    67  
    68  	serviceName := "test-service-secret"
    69  	testName := "test_secret"
    70  	id := d.createSecret(c, swarm.SecretSpec{
    71  		swarm.Annotations{
    72  			Name: testName,
    73  		},
    74  		[]byte("TESTINGDATA"),
    75  	})
    76  	c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("secrets: %s", id))
    77  
    78  	out, err := d.Cmd("service", "create", "--name", serviceName, "--secret", testName, "busybox", "top")
    79  	c.Assert(err, checker.IsNil, check.Commentf(out))
    80  
    81  	out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName)
    82  	c.Assert(err, checker.IsNil)
    83  
    84  	var refs []swarm.SecretReference
    85  	c.Assert(json.Unmarshal([]byte(out), &refs), checker.IsNil)
    86  	c.Assert(refs, checker.HasLen, 1)
    87  
    88  	c.Assert(refs[0].SecretName, checker.Equals, testName)
    89  	c.Assert(refs[0].File, checker.Not(checker.IsNil))
    90  	c.Assert(refs[0].File.Name, checker.Equals, testName)
    91  	c.Assert(refs[0].File.UID, checker.Equals, "0")
    92  	c.Assert(refs[0].File.GID, checker.Equals, "0")
    93  }
    94  
    95  func (s *DockerSwarmSuite) TestServiceCreateWithSecretSourceTarget(c *check.C) {
    96  	d := s.AddDaemon(c, true, true)
    97  
    98  	serviceName := "test-service-secret"
    99  	testName := "test_secret"
   100  	id := d.createSecret(c, swarm.SecretSpec{
   101  		swarm.Annotations{
   102  			Name: testName,
   103  		},
   104  		[]byte("TESTINGDATA"),
   105  	})
   106  	c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("secrets: %s", id))
   107  	testTarget := "testing"
   108  
   109  	out, err := d.Cmd("service", "create", "--name", serviceName, "--secret", fmt.Sprintf("source=%s,target=%s", testName, testTarget), "busybox", "top")
   110  	c.Assert(err, checker.IsNil, check.Commentf(out))
   111  
   112  	out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName)
   113  	c.Assert(err, checker.IsNil)
   114  
   115  	var refs []swarm.SecretReference
   116  	c.Assert(json.Unmarshal([]byte(out), &refs), checker.IsNil)
   117  	c.Assert(refs, checker.HasLen, 1)
   118  
   119  	c.Assert(refs[0].SecretName, checker.Equals, testName)
   120  	c.Assert(refs[0].File, checker.Not(checker.IsNil))
   121  	c.Assert(refs[0].File.Name, checker.Equals, testTarget)
   122  }
   123  
   124  func (s *DockerSwarmSuite) TestServiceCreateMountTmpfs(c *check.C) {
   125  	d := s.AddDaemon(c, true, true)
   126  	out, err := d.Cmd("service", "create", "--mount", "type=tmpfs,target=/foo,tmpfs-size=1MB", "busybox", "sh", "-c", "mount | grep foo; tail -f /dev/null")
   127  	c.Assert(err, checker.IsNil, check.Commentf(out))
   128  	id := strings.TrimSpace(out)
   129  
   130  	var tasks []swarm.Task
   131  	waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
   132  		tasks = d.getServiceTasks(c, id)
   133  		return len(tasks) > 0, nil
   134  	}, checker.Equals, true)
   135  
   136  	task := tasks[0]
   137  	waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
   138  		if task.NodeID == "" || task.Status.ContainerStatus.ContainerID == "" {
   139  			task = d.getTask(c, task.ID)
   140  		}
   141  		return task.NodeID != "" && task.Status.ContainerStatus.ContainerID != "", nil
   142  	}, checker.Equals, true)
   143  
   144  	// check container mount config
   145  	out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .HostConfig.Mounts}}", task.Status.ContainerStatus.ContainerID)
   146  	c.Assert(err, checker.IsNil, check.Commentf(out))
   147  
   148  	var mountConfig []mount.Mount
   149  	c.Assert(json.Unmarshal([]byte(out), &mountConfig), checker.IsNil)
   150  	c.Assert(mountConfig, checker.HasLen, 1)
   151  
   152  	c.Assert(mountConfig[0].Source, checker.Equals, "")
   153  	c.Assert(mountConfig[0].Target, checker.Equals, "/foo")
   154  	c.Assert(mountConfig[0].Type, checker.Equals, mount.TypeTmpfs)
   155  	c.Assert(mountConfig[0].TmpfsOptions, checker.NotNil)
   156  	c.Assert(mountConfig[0].TmpfsOptions.SizeBytes, checker.Equals, int64(1048576))
   157  
   158  	// check container mounts actual
   159  	out, err = s.nodeCmd(c, task.NodeID, "inspect", "--format", "{{json .Mounts}}", task.Status.ContainerStatus.ContainerID)
   160  	c.Assert(err, checker.IsNil, check.Commentf(out))
   161  
   162  	var mounts []types.MountPoint
   163  	c.Assert(json.Unmarshal([]byte(out), &mounts), checker.IsNil)
   164  	c.Assert(mounts, checker.HasLen, 1)
   165  
   166  	c.Assert(mounts[0].Type, checker.Equals, mount.TypeTmpfs)
   167  	c.Assert(mounts[0].Name, checker.Equals, "")
   168  	c.Assert(mounts[0].Destination, checker.Equals, "/foo")
   169  	c.Assert(mounts[0].RW, checker.Equals, true)
   170  
   171  	out, err = s.nodeCmd(c, task.NodeID, "logs", task.Status.ContainerStatus.ContainerID)
   172  	c.Assert(err, checker.IsNil, check.Commentf(out))
   173  	c.Assert(strings.TrimSpace(out), checker.HasPrefix, "tmpfs on /foo type tmpfs")
   174  	c.Assert(strings.TrimSpace(out), checker.Contains, "size=1024k")
   175  }