github.com/bdwilliams/libcompose@v0.3.1-0.20160826154243-d81a9bdacff0/integration/create_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"strings"
     8  
     9  	. "gopkg.in/check.v1"
    10  	"path/filepath"
    11  )
    12  
    13  func (s *CliSuite) TestFields(c *C) {
    14  	p := s.CreateProjectFromText(c, `
    15          hello:
    16            image: tianon/true
    17            cpuset: 0,1
    18            mem_limit: 4194304
    19          `)
    20  
    21  	name := fmt.Sprintf("%s_%s_1", p, "hello")
    22  	cn := s.GetContainerByName(c, name)
    23  	c.Assert(cn, NotNil)
    24  
    25  	c.Assert(cn.Config.Image, Equals, "tianon/true")
    26  	c.Assert(cn.HostConfig.CpusetCpus, Equals, "0,1")
    27  	c.Assert(cn.HostConfig.Memory, Equals, int64(4194304))
    28  }
    29  
    30  func (s *CliSuite) TestEmptyEntrypoint(c *C) {
    31  	p := s.CreateProjectFromText(c, `
    32          nil-cmd:
    33            image: busybox
    34            entrypoint: []
    35          `)
    36  
    37  	name := fmt.Sprintf("%s_%s_1", p, "nil-cmd")
    38  	cn := s.GetContainerByName(c, name)
    39  	c.Assert(cn, NotNil)
    40  
    41  	c.Assert(cn.Config.Entrypoint, IsNil)
    42  }
    43  
    44  func (s *CliSuite) TestHelloWorld(c *C) {
    45  	p := s.CreateProjectFromText(c, `
    46          hello:
    47            image: tianon/true
    48          `)
    49  
    50  	name := fmt.Sprintf("%s_%s_1", p, "hello")
    51  	cn := s.GetContainerByName(c, name)
    52  	c.Assert(cn, NotNil)
    53  
    54  	c.Assert(cn.Name, Equals, "/"+name)
    55  }
    56  
    57  func (s *CliSuite) TestContainerName(c *C) {
    58  	containerName := "containerName"
    59  	template := fmt.Sprintf(`hello:
    60      image: busybox
    61      command: top
    62      container_name: %s`, containerName)
    63  	s.CreateProjectFromText(c, template)
    64  
    65  	cn := s.GetContainerByName(c, containerName)
    66  	c.Assert(cn, NotNil)
    67  
    68  	c.Assert(cn.Name, Equals, "/"+containerName)
    69  }
    70  
    71  func (s *CliSuite) TestContainerNameWithScale(c *C) {
    72  	containerName := "containerName"
    73  	template := fmt.Sprintf(`hello:
    74      image: busybox
    75      command: top
    76      container_name: %s`, containerName)
    77  	p := s.CreateProjectFromText(c, template)
    78  
    79  	s.FromText(c, p, "scale", "hello=2", template)
    80  	containers := s.GetContainersByProject(c, p)
    81  	c.Assert(len(containers), Equals, 1)
    82  
    83  }
    84  
    85  func (s *CliSuite) TestInterpolation(c *C) {
    86  	os.Setenv("IMAGE", "tianon/true")
    87  
    88  	p := s.CreateProjectFromText(c, `
    89          test:
    90            image: $IMAGE
    91          `)
    92  
    93  	name := fmt.Sprintf("%s_%s_1", p, "test")
    94  	testContainer := s.GetContainerByName(c, name)
    95  
    96  	p = s.CreateProjectFromText(c, `
    97          reference:
    98            image: tianon/true
    99          `)
   100  
   101  	name = fmt.Sprintf("%s_%s_1", p, "reference")
   102  	referenceContainer := s.GetContainerByName(c, name)
   103  
   104  	c.Assert(testContainer, NotNil)
   105  
   106  	c.Assert(referenceContainer.Image, Equals, testContainer.Image)
   107  
   108  	os.Unsetenv("IMAGE")
   109  }
   110  
   111  func (s *CliSuite) TestInterpolationWithExtends(c *C) {
   112  	os.Setenv("IMAGE", "tianon/true")
   113  	os.Setenv("TEST_PORT", "8000")
   114  
   115  	p := s.CreateProjectFromText(c, `
   116          test:
   117                  extends:
   118                          file: ./assets/interpolation/docker-compose.yml
   119                          service: base
   120                  ports:
   121                          - ${TEST_PORT}
   122          `)
   123  
   124  	name := fmt.Sprintf("%s_%s_1", p, "test")
   125  	testContainer := s.GetContainerByName(c, name)
   126  
   127  	p = s.CreateProjectFromText(c, `
   128  	reference:
   129  	  image: tianon/true
   130  		ports:
   131  		  - 8000
   132  	`)
   133  
   134  	name = fmt.Sprintf("%s_%s_1", p, "reference")
   135  	referenceContainer := s.GetContainerByName(c, name)
   136  
   137  	c.Assert(testContainer, NotNil)
   138  
   139  	c.Assert(referenceContainer.Image, Equals, testContainer.Image)
   140  
   141  	os.Unsetenv("TEST_PORT")
   142  	os.Unsetenv("IMAGE")
   143  }
   144  
   145  func (s *CliSuite) TestFieldTypeConversions(c *C) {
   146  	os.Setenv("LIMIT", "40000000")
   147  
   148  	p := s.CreateProjectFromText(c, `
   149          test:
   150            image: tianon/true
   151            mem_limit: $LIMIT
   152            memswap_limit: "40000000"
   153          `)
   154  
   155  	name := fmt.Sprintf("%s_%s_1", p, "test")
   156  	testContainer := s.GetContainerByName(c, name)
   157  
   158  	p = s.CreateProjectFromText(c, `
   159          reference:
   160            image: tianon/true
   161            mem_limit: 40000000
   162            memswap_limit: 40000000
   163          `)
   164  
   165  	name = fmt.Sprintf("%s_%s_1", p, "reference")
   166  	referenceContainer := s.GetContainerByName(c, name)
   167  
   168  	c.Assert(testContainer, NotNil)
   169  
   170  	c.Assert(referenceContainer.Image, Equals, testContainer.Image)
   171  
   172  	os.Unsetenv("LIMIT")
   173  }
   174  
   175  func (s *CliSuite) TestMultipleComposeFilesOneTwo(c *C) {
   176  	p := "multiple"
   177  	cmd := exec.Command(s.command, "-f", "./assets/multiple/one.yml", "-f", "./assets/multiple/two.yml", "create")
   178  	cmd.Stdout = os.Stdout
   179  	cmd.Stderr = os.Stderr
   180  	cmd.Stdin = os.Stdin
   181  	err := cmd.Run()
   182  
   183  	c.Assert(err, IsNil)
   184  
   185  	containerNames := []string{"multiple", "simple", "another", "yetanother"}
   186  
   187  	for _, containerName := range containerNames {
   188  		name := fmt.Sprintf("%s_%s_1", p, containerName)
   189  		container := s.GetContainerByName(c, name)
   190  
   191  		c.Assert(container, NotNil)
   192  	}
   193  
   194  	name := fmt.Sprintf("%s_%s_1", p, "multiple")
   195  	container := s.GetContainerByName(c, name)
   196  
   197  	c.Assert(container.Config.Image, Equals, "busybox")
   198  	c.Assert([]string(container.Config.Cmd), DeepEquals, []string{"echo", "two"})
   199  	c.Assert(contains(container.Config.Env, "KEY2=VAL2"), Equals, true)
   200  	c.Assert(contains(container.Config.Env, "KEY1=VAL1"), Equals, true)
   201  }
   202  
   203  func (s *CliSuite) TestMultipleComposeFilesTwoOne(c *C) {
   204  	p := "multiple"
   205  	cmd := exec.Command(s.command, "-f", "./assets/multiple/two.yml", "-f", "./assets/multiple/one.yml", "create")
   206  	cmd.Stdout = os.Stdout
   207  	cmd.Stderr = os.Stderr
   208  	cmd.Stdin = os.Stdin
   209  	err := cmd.Run()
   210  
   211  	c.Assert(err, IsNil)
   212  
   213  	containerNames := []string{"multiple", "simple", "another", "yetanother"}
   214  
   215  	for _, containerName := range containerNames {
   216  		name := fmt.Sprintf("%s_%s_1", p, containerName)
   217  		container := s.GetContainerByName(c, name)
   218  
   219  		c.Assert(container, NotNil)
   220  	}
   221  
   222  	name := fmt.Sprintf("%s_%s_1", p, "multiple")
   223  	container := s.GetContainerByName(c, name)
   224  
   225  	c.Assert(container.Config.Image, Equals, "tianon/true")
   226  	c.Assert([]string(container.Config.Cmd), DeepEquals, []string{"echo", "two"})
   227  	c.Assert(contains(container.Config.Env, "KEY2=VAL2"), Equals, true)
   228  	c.Assert(contains(container.Config.Env, "KEY1=VAL1"), Equals, true)
   229  }
   230  
   231  func contains(s []string, e string) bool {
   232  	for _, a := range s {
   233  		if a == e {
   234  			return true
   235  		}
   236  	}
   237  	return false
   238  }
   239  
   240  func (s *CliSuite) TestDefaultMultipleComposeFiles(c *C) {
   241  	p := s.RandomProject()
   242  	cmd := exec.Command(filepath.Join("../../", s.command), "-p", p, "create")
   243  	cmd.Stdout = os.Stdout
   244  	cmd.Stderr = os.Stderr
   245  	cmd.Stdin = os.Stdin
   246  	cmd.Dir = "./assets/multiple-composefiles-default/"
   247  	err := cmd.Run()
   248  
   249  	c.Assert(err, IsNil)
   250  
   251  	containerNames := []string{"simple", "another", "yetanother"}
   252  
   253  	for _, containerName := range containerNames {
   254  		name := fmt.Sprintf("%s_%s_1", p, containerName)
   255  		container := s.GetContainerByName(c, name)
   256  
   257  		c.Assert(container, NotNil)
   258  	}
   259  }
   260  
   261  func (s *CliSuite) TestValidation(c *C) {
   262  	template := `
   263    test:
   264      image: busybox
   265      ports: invalid_type
   266  	`
   267  	_, output := s.FromTextCaptureOutput(c, s.RandomProject(), "create", template)
   268  
   269  	c.Assert(strings.Contains(output, "Service 'test' configuration key 'ports' contains an invalid type, it should be an array."), Equals, true)
   270  
   271  	template = `
   272    test:
   273      image: busybox
   274      build: .
   275  	`
   276  	_, output = s.FromTextCaptureOutput(c, s.RandomProject(), "create", template)
   277  
   278  	c.Assert(strings.Contains(output, "Service 'test' has both an image and build path specified. A service can either be built to image or use an existing image, not both."), Equals, true)
   279  
   280  	template = `
   281    test:
   282      image: busybox
   283      ports: invalid_type
   284      links: invalid_type
   285      devices:
   286        - /dev/foo:/dev/foo
   287        - /dev/foo:/dev/foo
   288    `
   289  	_, output = s.FromTextCaptureOutput(c, s.RandomProject(), "create", template)
   290  
   291  	c.Assert(strings.Contains(output, "Service 'test' configuration key 'ports' contains an invalid type, it should be an array."), Equals, true)
   292  	c.Assert(strings.Contains(output, "Service 'test' configuration key 'links' contains an invalid type, it should be an array"), Equals, true)
   293  	c.Assert(strings.Contains(output, "Service 'test' configuration key 'devices' value [/dev/foo:/dev/foo /dev/foo:/dev/foo] has non-unique elements"), Equals, true)
   294  }
   295  
   296  func (s *CliSuite) TestValidationWithExtends(c *C) {
   297  	template := `
   298    base:
   299      image: busybox
   300      privilege: "something"
   301    test:
   302      extends:
   303        service: base
   304  	`
   305  
   306  	_, output := s.FromTextCaptureOutput(c, s.RandomProject(), "create", template)
   307  
   308  	c.Assert(strings.Contains(output, "Unsupported config option for base service: 'privilege' (did you mean 'privileged'?)"), Equals, true)
   309  
   310  	template = `
   311    base:
   312      image: busybox
   313    test:
   314      extends:
   315        service: base
   316      links: invalid_type
   317  	`
   318  
   319  	_, output = s.FromTextCaptureOutput(c, s.RandomProject(), "create", template)
   320  
   321  	c.Assert(strings.Contains(output, "Service 'test' configuration key 'links' contains an invalid type, it should be an array"), Equals, true)
   322  
   323  	template = `
   324    test:
   325      extends:
   326        file: ./assets/validation/valid/docker-compose.yml
   327        service: base
   328      devices:
   329        - /dev/foo:/dev/foo
   330        - /dev/foo:/dev/foo
   331  	`
   332  
   333  	_, output = s.FromTextCaptureOutput(c, s.RandomProject(), "create", template)
   334  
   335  	c.Assert(strings.Contains(output, "Service 'test' configuration key 'devices' value [/dev/foo:/dev/foo /dev/foo:/dev/foo] has non-unique elements"), Equals, true)
   336  
   337  	template = `
   338    test:
   339      extends:
   340        file: ./assets/validation/invalid/docker-compose.yml
   341        service: base
   342  	`
   343  
   344  	_, output = s.FromTextCaptureOutput(c, s.RandomProject(), "create", template)
   345  
   346  	c.Assert(strings.Contains(output, "Service 'base' configuration key 'ports' contains an invalid type, it should be an array."), Equals, true)
   347  }