github.com/zhgqiang/libcompose@v0.4.1-0.20210112080336-bff7ba3690e1/integration/up_test.go (about)

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