gopkg.in/docker/libcompose.v0@v0.4.0/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/docker/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) 195 c.Assert(err, IsNil) 196 197 p := s.ProjectFromText(c, "up", template) 198 name := fmt.Sprintf("%s_%s_1", p, "hello") 199 firstContainer := s.GetContainerByName(c, name) 200 201 _, err = client.ImageRemove(context.Background(), image, types.ImageRemoveOptions{}) 202 c.Assert(err, IsNil) 203 204 p = s.FromText(c, p, "up", "--no-recreate", template) 205 latestContainer := s.GetContainerByName(c, name) 206 c.Assert(firstContainer.ID, Equals, latestContainer.ID) 207 } 208 209 func (s *CliSuite) TestRecreateImageChanging(c *C) { 210 client := GetClient(c) 211 label := "buildroot-2013.08.1" 212 repo := "busybox" 213 image := fmt.Sprintf("%s:%s", repo, label) 214 215 template := fmt.Sprintf(` 216 hello: 217 labels: 218 key: val 219 image: %s 220 stdin_open: true 221 tty: true 222 `, image) 223 224 ctx := context.Background() 225 226 // Ignore error here 227 client.ImageRemove(ctx, 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 // Note: depending on the daemon version it can fail with --force (which is no more possible to pass using engine-api) 246 // thus, the next following lines are a hackā¦ 247 err := client.ImageTag(ctx, image, image+"backup") 248 c.Assert(err, IsNil) 249 _, err = client.ImageRemove(ctx, image, types.ImageRemoveOptions{}) 250 c.Assert(err, IsNil) 251 err = client.ImageTag(ctx, "busybox:latest", image) 252 c.Assert(err, IsNil) 253 254 // Up (with recreate - the default), pull is needed and new container is created 255 p = s.FromText(c, p, "up", template) 256 latestContainer = s.GetContainerByName(c, name) 257 c.Assert(firstContainer.ID, Not(Equals), latestContainer.ID) 258 259 s.FromText(c, p, "rm", "-f", template) 260 } 261 262 func (s *CliSuite) TestLink(c *C) { 263 p := s.ProjectFromText(c, "up", ` 264 server: 265 image: busybox 266 command: cat 267 stdin_open: true 268 expose: 269 - 80 270 client: 271 image: busybox 272 links: 273 - server:foo 274 - server 275 `) 276 277 serverName := fmt.Sprintf("%s_%s_1", p, "server") 278 279 cn := s.GetContainerByName(c, serverName) 280 c.Assert(cn, NotNil) 281 c.Assert(cn.Config.ExposedPorts, DeepEquals, map[nat.Port]struct{}{ 282 "80/tcp": {}, 283 }) 284 285 clientName := fmt.Sprintf("%s_%s_1", p, "client") 286 cn = s.GetContainerByName(c, clientName) 287 c.Assert(cn, NotNil) 288 c.Assert(asMap(cn.HostConfig.Links), DeepEquals, asMap([]string{ 289 fmt.Sprintf("/%s:/%s/%s", serverName, clientName, "foo"), 290 fmt.Sprintf("/%s:/%s/%s", serverName, clientName, "server"), 291 fmt.Sprintf("/%s:/%s/%s", serverName, clientName, serverName), 292 })) 293 } 294 295 func (s *CliSuite) TestUpNoBuildFailIfImageNotPresent(c *C) { 296 p := s.RandomProject() 297 cmd := exec.Command(s.command, "-f", "./assets/build/docker-compose.yml", "-p", p, "up", "--no-build") 298 err := cmd.Run() 299 300 c.Assert(err, NotNil) 301 } 302 303 func (s *CliSuite) TestUpNoBuildShouldWorkIfImageIsPresent(c *C) { 304 p := s.RandomProject() 305 cmd := exec.Command(s.command, "-f", "./assets/simple-build/docker-compose.yml", "-p", p, "build") 306 err := cmd.Run() 307 308 c.Assert(err, IsNil) 309 310 cmd = exec.Command(s.command, "-f", "./assets/simple-build/docker-compose.yml", "-p", p, "up", "-d", "--no-build") 311 err = cmd.Run() 312 313 c.Assert(err, IsNil) 314 }