github.com/skippbox/kompose-origin@v0.0.0-20160524133224-16a9dca7bac2/integration/up_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"strings"
     7  
     8  	"golang.org/x/net/context"
     9  
    10  	"github.com/docker/engine-api/types"
    11  	"github.com/docker/go-connections/nat"
    12  	"github.com/docker/libcompose/utils"
    13  
    14  	. "gopkg.in/check.v1"
    15  )
    16  
    17  func (s *CliSuite) TestUp(c *C) {
    18  	p := s.ProjectFromText(c, "up", SimpleTemplate)
    19  
    20  	name := fmt.Sprintf("%s_%s_1", p, "hello")
    21  	cn := s.GetContainerByName(c, name)
    22  	c.Assert(cn, NotNil)
    23  
    24  	c.Assert(cn.State.Running, Equals, true)
    25  }
    26  
    27  func (s *CliSuite) TestUpNotExistService(c *C) {
    28  	p := s.ProjectFromText(c, "up", SimpleTemplate)
    29  
    30  	name := fmt.Sprintf("%s_%s_1", p, "not_exist")
    31  	cn := s.GetContainerByName(c, name)
    32  	c.Assert(cn, IsNil)
    33  }
    34  
    35  func (s *CliSuite) TestRecreateForceRecreate(c *C) {
    36  	p := s.ProjectFromText(c, "up", SimpleTemplate)
    37  
    38  	name := fmt.Sprintf("%s_%s_1", p, "hello")
    39  	cn := s.GetContainerByName(c, name)
    40  	c.Assert(cn, NotNil)
    41  
    42  	p = s.FromText(c, p, "up", "--force-recreate", SimpleTemplate)
    43  	cn2 := s.GetContainerByName(c, name)
    44  	c.Assert(cn.ID, Not(Equals), cn2.ID)
    45  }
    46  
    47  func mountSet(slice []types.MountPoint) map[string]bool {
    48  	result := map[string]bool{}
    49  	for _, v := range slice {
    50  		result[fmt.Sprint(v.Source, ":", v.Destination)] = true
    51  	}
    52  	return result
    53  }
    54  
    55  func (s *CliSuite) TestRecreateVols(c *C) {
    56  	p := s.ProjectFromText(c, "up", SimpleTemplateWithVols)
    57  
    58  	name := fmt.Sprintf("%s_%s_1", p, "hello")
    59  	cn := s.GetContainerByName(c, name)
    60  	c.Assert(cn, NotNil)
    61  
    62  	p = s.FromText(c, p, "up", "--force-recreate", SimpleTemplateWithVols2)
    63  	cn2 := s.GetContainerByName(c, name)
    64  	c.Assert(cn.ID, Not(Equals), cn2.ID)
    65  
    66  	notHomeRootOrVol2 := func(mount string) bool {
    67  		switch strings.SplitN(mount, ":", 2)[1] {
    68  		case "/home", "/root", "/var/lib/vol2":
    69  			return false
    70  		}
    71  		return true
    72  	}
    73  
    74  	shouldMigrate := utils.FilterStringSet(mountSet(cn.Mounts), notHomeRootOrVol2)
    75  	cn2Mounts := mountSet(cn2.Mounts)
    76  	for k := range shouldMigrate {
    77  		c.Assert(cn2Mounts[k], Equals, true)
    78  	}
    79  
    80  	almostTheSameButRoot := utils.FilterStringSet(cn2Mounts, notHomeRootOrVol2)
    81  	c.Assert(len(almostTheSameButRoot), Equals, len(cn2Mounts)-1)
    82  	c.Assert(cn2Mounts["/tmp/tmp-root:/root"], Equals, true)
    83  	c.Assert(cn2Mounts["/root:/root"], Equals, false)
    84  }
    85  
    86  func (s *CliSuite) TestRecreateNoRecreate(c *C) {
    87  	p := s.ProjectFromText(c, "up", SimpleTemplate)
    88  
    89  	name := fmt.Sprintf("%s_%s_1", p, "hello")
    90  	cn := s.GetContainerByName(c, name)
    91  	c.Assert(cn, NotNil)
    92  
    93  	p = s.FromText(c, p, "up", "--no-recreate", `
    94  	hello:
    95  	  labels:
    96  	    key: val
    97  	  image: busybox
    98  	  stdin_open: true
    99  	  tty: true
   100  	`)
   101  	cn2 := s.GetContainerByName(c, name)
   102  	c.Assert(cn.ID, Equals, cn2.ID)
   103  	_, ok := cn2.Config.Labels["key"]
   104  	c.Assert(ok, Equals, false)
   105  }
   106  
   107  func (s *CliSuite) TestRecreate(c *C) {
   108  	p := s.ProjectFromText(c, "up", SimpleTemplate)
   109  
   110  	name := fmt.Sprintf("%s_%s_1", p, "hello")
   111  	cn := s.GetContainerByName(c, name)
   112  	c.Assert(cn, NotNil)
   113  
   114  	p = s.FromText(c, p, "up", SimpleTemplate)
   115  	cn2 := s.GetContainerByName(c, name)
   116  	c.Assert(cn.ID, Equals, cn2.ID)
   117  
   118  	p = s.FromText(c, p, "up", `
   119  	hello:
   120  	  labels:
   121  	    key: val
   122  	  image: busybox
   123  	  stdin_open: true
   124  	  tty: true
   125  	`)
   126  	cn3 := s.GetContainerByName(c, name)
   127  	c.Assert(cn2.ID, Not(Equals), cn3.ID)
   128  	key3 := cn3.Config.Labels["key"]
   129  	c.Assert(key3, Equals, "val")
   130  
   131  	// Should still recreate because old has a different label
   132  	p = s.FromText(c, p, "up", `
   133  	hello:
   134  	  image: busybox
   135  	  stdin_open: true
   136  	  tty: true
   137  	`)
   138  	cn4 := s.GetContainerByName(c, name)
   139  	c.Assert(cn3.ID, Not(Equals), cn4.ID)
   140  	_, ok4 := cn4.Config.Labels["key"]
   141  	c.Assert(ok4, Equals, false)
   142  
   143  	p = s.FromText(c, p, "up", `
   144  	hello:
   145  	  image: busybox
   146  	  stdin_open: true
   147  	  tty: true
   148  	`)
   149  	cn5 := s.GetContainerByName(c, name)
   150  	c.Assert(cn4.ID, Equals, cn5.ID)
   151  	_, ok5 := cn5.Config.Labels["key"]
   152  	c.Assert(ok5, Equals, false)
   153  
   154  	p = s.FromText(c, p, "up", "--force-recreate", `
   155  	hello:
   156  	  image: busybox
   157  	  stdin_open: true
   158  	  tty: true
   159  	`)
   160  	cn6 := s.GetContainerByName(c, name)
   161  	c.Assert(cn5.ID, Not(Equals), cn6.ID)
   162  	_, ok6 := cn6.Config.Labels["key"]
   163  	c.Assert(ok6, Equals, false)
   164  
   165  	p = s.FromText(c, p, "up", "--force-recreate", `
   166  	hello:
   167  	  image: busybox
   168  	  stdin_open: true
   169  	  tty: true
   170  	`)
   171  	cn7 := s.GetContainerByName(c, name)
   172  	c.Assert(cn6.ID, Not(Equals), cn7.ID)
   173  	_, ok7 := cn7.Config.Labels["key"]
   174  	c.Assert(ok7, Equals, false)
   175  
   176  	c.Assert(cn.State.Running, Equals, true)
   177  }
   178  
   179  func (s *CliSuite) TestUpAfterImageTagDeleted(c *C) {
   180  	client := GetClient(c)
   181  	label := RandStr(7)
   182  	repo := "busybox"
   183  	image := fmt.Sprintf("%s:%s", repo, label)
   184  
   185  	template := fmt.Sprintf(`
   186  	hello:
   187  	  labels:
   188  	    key: val
   189  	  image: %s
   190  	  stdin_open: true
   191  	  tty: true
   192  	`, image)
   193  
   194  	err := client.ImageTag(context.Background(), "busybox:latest", repo+":"+label, types.ImageTagOptions{
   195  		Force: true,
   196  	})
   197  	c.Assert(err, IsNil)
   198  
   199  	p := s.ProjectFromText(c, "up", template)
   200  	name := fmt.Sprintf("%s_%s_1", p, "hello")
   201  	firstContainer := s.GetContainerByName(c, name)
   202  
   203  	_, err = client.ImageRemove(context.Background(), image, types.ImageRemoveOptions{})
   204  	c.Assert(err, IsNil)
   205  
   206  	p = s.FromText(c, p, "up", "--no-recreate", template)
   207  	latestContainer := s.GetContainerByName(c, name)
   208  	c.Assert(firstContainer.ID, Equals, latestContainer.ID)
   209  }
   210  
   211  func (s *CliSuite) TestRecreateImageChanging(c *C) {
   212  	client := GetClient(c)
   213  	label := "buildroot-2013.08.1"
   214  	repo := "busybox"
   215  	image := fmt.Sprintf("%s:%s", repo, label)
   216  
   217  	template := fmt.Sprintf(`
   218  	hello:
   219  	  labels:
   220  	    key: val
   221  	  image: %s
   222  	  stdin_open: true
   223  	  tty: true
   224  	`, image)
   225  
   226  	// Ignore error here
   227  	client.ImageRemove(context.Background(), image, types.ImageRemoveOptions{})
   228  
   229  	// Up, pull needed
   230  	p := s.ProjectFromText(c, "up", template)
   231  	name := fmt.Sprintf("%s_%s_1", p, "hello")
   232  	firstContainer := s.GetContainerByName(c, name)
   233  
   234  	// Up --no-recreate, no pull needed
   235  	p = s.FromText(c, p, "up", "--no-recreate", template)
   236  	latestContainer := s.GetContainerByName(c, name)
   237  	c.Assert(firstContainer.ID, Equals, latestContainer.ID)
   238  
   239  	// Up --no-recreate, no pull needed
   240  	p = s.FromText(c, p, "up", "--no-recreate", template)
   241  	latestContainer = s.GetContainerByName(c, name)
   242  	c.Assert(firstContainer.ID, Equals, latestContainer.ID)
   243  
   244  	// Change what tag points to
   245  	err := client.ImageTag(context.Background(), "busybox:latest", repo+":"+label, types.ImageTagOptions{
   246  		Force: true,
   247  	})
   248  	c.Assert(err, IsNil)
   249  
   250  	// Up (with recreate - the default), pull is needed and new container is created
   251  	p = s.FromText(c, p, "up", template)
   252  	latestContainer = s.GetContainerByName(c, name)
   253  	c.Assert(firstContainer.ID, Not(Equals), latestContainer.ID)
   254  
   255  	s.FromText(c, p, "rm", "-f", template)
   256  }
   257  
   258  func (s *CliSuite) TestLink(c *C) {
   259  	p := s.ProjectFromText(c, "up", `
   260          server:
   261            image: busybox
   262            command: cat
   263            stdin_open: true
   264            expose:
   265            - 80
   266          client:
   267            image: busybox
   268            links:
   269            - server:foo
   270            - server
   271          `)
   272  
   273  	serverName := fmt.Sprintf("%s_%s_1", p, "server")
   274  
   275  	cn := s.GetContainerByName(c, serverName)
   276  	c.Assert(cn, NotNil)
   277  	c.Assert(cn.Config.ExposedPorts, DeepEquals, map[nat.Port]struct{}{
   278  		"80/tcp": {},
   279  	})
   280  
   281  	clientName := fmt.Sprintf("%s_%s_1", p, "client")
   282  	cn = s.GetContainerByName(c, clientName)
   283  	c.Assert(cn, NotNil)
   284  	c.Assert(asMap(cn.HostConfig.Links), DeepEquals, asMap([]string{
   285  		fmt.Sprintf("/%s:/%s/%s", serverName, clientName, "foo"),
   286  		fmt.Sprintf("/%s:/%s/%s", serverName, clientName, "server"),
   287  		fmt.Sprintf("/%s:/%s/%s", serverName, clientName, serverName),
   288  	}))
   289  }
   290  
   291  func (s *CliSuite) TestUpNoBuildFailIfImageNotPresent(c *C) {
   292  	p := s.RandomProject()
   293  	cmd := exec.Command(s.command, "-f", "./assets/build/docker-compose.yml", "-p", p, "up", "--no-build")
   294  	err := cmd.Run()
   295  
   296  	c.Assert(err, NotNil)
   297  }
   298  
   299  func (s *CliSuite) TestUpNoBuildShouldWorkIfImageIsPresent(c *C) {
   300  	p := s.RandomProject()
   301  	cmd := exec.Command(s.command, "-f", "./assets/simple-build/docker-compose.yml", "-p", p, "build")
   302  	err := cmd.Run()
   303  
   304  	c.Assert(err, IsNil)
   305  
   306  	cmd = exec.Command(s.command, "-f", "./assets/simple-build/docker-compose.yml", "-p", p, "up", "-d", "--no-build")
   307  	err = cmd.Run()
   308  
   309  	c.Assert(err, IsNil)
   310  }