github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/integration-cli/check_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "net/http/httptest" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "strings" 10 "sync" 11 "syscall" 12 "testing" 13 14 "github.com/docker/docker/api/types/swarm" 15 "github.com/docker/docker/cli/config" 16 "github.com/docker/docker/integration-cli/cli" 17 "github.com/docker/docker/integration-cli/cli/build/fakestorage" 18 "github.com/docker/docker/integration-cli/daemon" 19 "github.com/docker/docker/integration-cli/environment" 20 "github.com/docker/docker/integration-cli/registry" 21 "github.com/docker/docker/pkg/reexec" 22 "github.com/go-check/check" 23 ) 24 25 const ( 26 // the private registry to use for tests 27 privateRegistryURL = "127.0.0.1:5000" 28 29 // path to containerd's ctr binary 30 ctrBinary = "docker-containerd-ctr" 31 32 // the docker daemon binary to use 33 dockerdBinary = "dockerd" 34 ) 35 36 var ( 37 testEnv *environment.Execution 38 39 // the docker client binary to use 40 dockerBinary = "" 41 ) 42 43 func init() { 44 var err error 45 46 reexec.Init() // This is required for external graphdriver tests 47 48 testEnv, err = environment.New() 49 if err != nil { 50 fmt.Println(err) 51 os.Exit(1) 52 } 53 } 54 55 func TestMain(m *testing.M) { 56 dockerBinary = testEnv.DockerBinary() 57 58 if testEnv.LocalDaemon() { 59 fmt.Println("INFO: Testing against a local daemon") 60 } else { 61 fmt.Println("INFO: Testing against a remote daemon") 62 } 63 exitCode := m.Run() 64 os.Exit(exitCode) 65 } 66 67 func Test(t *testing.T) { 68 cli.EnsureTestEnvIsLoaded(t) 69 fakestorage.EnsureTestEnvIsLoaded(t) 70 cmd := exec.Command(dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}") 71 cmd.Env = appendBaseEnv(true) 72 out, err := cmd.CombinedOutput() 73 if err != nil { 74 panic(fmt.Errorf("err=%v\nout=%s\n", err, out)) 75 } 76 images := strings.Split(strings.TrimSpace(string(out)), "\n") 77 testEnv.ProtectImage(t, images...) 78 if testEnv.DaemonPlatform() == "linux" { 79 ensureFrozenImagesLinux(t) 80 } 81 check.TestingT(t) 82 } 83 84 func init() { 85 check.Suite(&DockerSuite{}) 86 } 87 88 type DockerSuite struct { 89 } 90 91 func (s *DockerSuite) OnTimeout(c *check.C) { 92 if testEnv.DaemonPID() > 0 && testEnv.LocalDaemon() { 93 daemon.SignalDaemonDump(testEnv.DaemonPID()) 94 } 95 } 96 97 func (s *DockerSuite) TearDownTest(c *check.C) { 98 testEnv.Clean(c, dockerBinary) 99 } 100 101 func init() { 102 check.Suite(&DockerRegistrySuite{ 103 ds: &DockerSuite{}, 104 }) 105 } 106 107 type DockerRegistrySuite struct { 108 ds *DockerSuite 109 reg *registry.V2 110 d *daemon.Daemon 111 } 112 113 func (s *DockerRegistrySuite) OnTimeout(c *check.C) { 114 s.d.DumpStackAndQuit() 115 } 116 117 func (s *DockerRegistrySuite) SetUpTest(c *check.C) { 118 testRequires(c, DaemonIsLinux, registry.Hosting) 119 s.reg = setupRegistry(c, false, "", "") 120 s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{ 121 Experimental: testEnv.ExperimentalDaemon(), 122 }) 123 } 124 125 func (s *DockerRegistrySuite) TearDownTest(c *check.C) { 126 if s.reg != nil { 127 s.reg.Close() 128 } 129 if s.d != nil { 130 s.d.Stop(c) 131 } 132 s.ds.TearDownTest(c) 133 } 134 135 func init() { 136 check.Suite(&DockerSchema1RegistrySuite{ 137 ds: &DockerSuite{}, 138 }) 139 } 140 141 type DockerSchema1RegistrySuite struct { 142 ds *DockerSuite 143 reg *registry.V2 144 d *daemon.Daemon 145 } 146 147 func (s *DockerSchema1RegistrySuite) OnTimeout(c *check.C) { 148 s.d.DumpStackAndQuit() 149 } 150 151 func (s *DockerSchema1RegistrySuite) SetUpTest(c *check.C) { 152 testRequires(c, DaemonIsLinux, registry.Hosting, NotArm64) 153 s.reg = setupRegistry(c, true, "", "") 154 s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{ 155 Experimental: testEnv.ExperimentalDaemon(), 156 }) 157 } 158 159 func (s *DockerSchema1RegistrySuite) TearDownTest(c *check.C) { 160 if s.reg != nil { 161 s.reg.Close() 162 } 163 if s.d != nil { 164 s.d.Stop(c) 165 } 166 s.ds.TearDownTest(c) 167 } 168 169 func init() { 170 check.Suite(&DockerRegistryAuthHtpasswdSuite{ 171 ds: &DockerSuite{}, 172 }) 173 } 174 175 type DockerRegistryAuthHtpasswdSuite struct { 176 ds *DockerSuite 177 reg *registry.V2 178 d *daemon.Daemon 179 } 180 181 func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *check.C) { 182 s.d.DumpStackAndQuit() 183 } 184 185 func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *check.C) { 186 testRequires(c, DaemonIsLinux, registry.Hosting) 187 s.reg = setupRegistry(c, false, "htpasswd", "") 188 s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{ 189 Experimental: testEnv.ExperimentalDaemon(), 190 }) 191 } 192 193 func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *check.C) { 194 if s.reg != nil { 195 out, err := s.d.Cmd("logout", privateRegistryURL) 196 c.Assert(err, check.IsNil, check.Commentf(out)) 197 s.reg.Close() 198 } 199 if s.d != nil { 200 s.d.Stop(c) 201 } 202 s.ds.TearDownTest(c) 203 } 204 205 func init() { 206 check.Suite(&DockerRegistryAuthTokenSuite{ 207 ds: &DockerSuite{}, 208 }) 209 } 210 211 type DockerRegistryAuthTokenSuite struct { 212 ds *DockerSuite 213 reg *registry.V2 214 d *daemon.Daemon 215 } 216 217 func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *check.C) { 218 s.d.DumpStackAndQuit() 219 } 220 221 func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *check.C) { 222 testRequires(c, DaemonIsLinux, registry.Hosting) 223 s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{ 224 Experimental: testEnv.ExperimentalDaemon(), 225 }) 226 } 227 228 func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *check.C) { 229 if s.reg != nil { 230 out, err := s.d.Cmd("logout", privateRegistryURL) 231 c.Assert(err, check.IsNil, check.Commentf(out)) 232 s.reg.Close() 233 } 234 if s.d != nil { 235 s.d.Stop(c) 236 } 237 s.ds.TearDownTest(c) 238 } 239 240 func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *check.C, tokenURL string) { 241 if s == nil { 242 c.Fatal("registry suite isn't initialized") 243 } 244 s.reg = setupRegistry(c, false, "token", tokenURL) 245 } 246 247 func init() { 248 check.Suite(&DockerDaemonSuite{ 249 ds: &DockerSuite{}, 250 }) 251 } 252 253 type DockerDaemonSuite struct { 254 ds *DockerSuite 255 d *daemon.Daemon 256 } 257 258 func (s *DockerDaemonSuite) OnTimeout(c *check.C) { 259 s.d.DumpStackAndQuit() 260 } 261 262 func (s *DockerDaemonSuite) SetUpTest(c *check.C) { 263 testRequires(c, DaemonIsLinux, SameHostDaemon) 264 s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{ 265 Experimental: testEnv.ExperimentalDaemon(), 266 }) 267 } 268 269 func (s *DockerDaemonSuite) TearDownTest(c *check.C) { 270 testRequires(c, DaemonIsLinux, SameHostDaemon) 271 if s.d != nil { 272 s.d.Stop(c) 273 } 274 s.ds.TearDownTest(c) 275 } 276 277 func (s *DockerDaemonSuite) TearDownSuite(c *check.C) { 278 filepath.Walk(daemon.SockRoot, func(path string, fi os.FileInfo, err error) error { 279 if err != nil { 280 // ignore errors here 281 // not cleaning up sockets is not really an error 282 return nil 283 } 284 if fi.Mode() == os.ModeSocket { 285 syscall.Unlink(path) 286 } 287 return nil 288 }) 289 os.RemoveAll(daemon.SockRoot) 290 } 291 292 const defaultSwarmPort = 2477 293 294 func init() { 295 check.Suite(&DockerSwarmSuite{ 296 ds: &DockerSuite{}, 297 }) 298 } 299 300 type DockerSwarmSuite struct { 301 server *httptest.Server 302 ds *DockerSuite 303 daemons []*daemon.Swarm 304 daemonsLock sync.Mutex // protect access to daemons 305 portIndex int 306 } 307 308 func (s *DockerSwarmSuite) OnTimeout(c *check.C) { 309 s.daemonsLock.Lock() 310 defer s.daemonsLock.Unlock() 311 for _, d := range s.daemons { 312 d.DumpStackAndQuit() 313 } 314 } 315 316 func (s *DockerSwarmSuite) SetUpTest(c *check.C) { 317 testRequires(c, DaemonIsLinux) 318 } 319 320 func (s *DockerSwarmSuite) AddDaemon(c *check.C, joinSwarm, manager bool) *daemon.Swarm { 321 d := &daemon.Swarm{ 322 Daemon: daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{ 323 Experimental: testEnv.ExperimentalDaemon(), 324 }), 325 Port: defaultSwarmPort + s.portIndex, 326 } 327 d.ListenAddr = fmt.Sprintf("0.0.0.0:%d", d.Port) 328 args := []string{"--iptables=false", "--swarm-default-advertise-addr=lo"} // avoid networking conflicts 329 d.StartWithBusybox(c, args...) 330 331 if joinSwarm == true { 332 if len(s.daemons) > 0 { 333 tokens := s.daemons[0].JoinTokens(c) 334 token := tokens.Worker 335 if manager { 336 token = tokens.Manager 337 } 338 c.Assert(d.Join(swarm.JoinRequest{ 339 RemoteAddrs: []string{s.daemons[0].ListenAddr}, 340 JoinToken: token, 341 }), check.IsNil) 342 } else { 343 c.Assert(d.Init(swarm.InitRequest{}), check.IsNil) 344 } 345 } 346 347 s.portIndex++ 348 s.daemonsLock.Lock() 349 s.daemons = append(s.daemons, d) 350 s.daemonsLock.Unlock() 351 352 return d 353 } 354 355 func (s *DockerSwarmSuite) TearDownTest(c *check.C) { 356 testRequires(c, DaemonIsLinux) 357 s.daemonsLock.Lock() 358 for _, d := range s.daemons { 359 if d != nil { 360 d.Stop(c) 361 // FIXME(vdemeester) should be handled by SwarmDaemon ? 362 // raft state file is quite big (64MB) so remove it after every test 363 walDir := filepath.Join(d.Root, "swarm/raft/wal") 364 if err := os.RemoveAll(walDir); err != nil { 365 c.Logf("error removing %v: %v", walDir, err) 366 } 367 368 d.CleanupExecRoot(c) 369 } 370 } 371 s.daemons = nil 372 s.daemonsLock.Unlock() 373 374 s.portIndex = 0 375 s.ds.TearDownTest(c) 376 } 377 378 func init() { 379 check.Suite(&DockerTrustSuite{ 380 ds: &DockerSuite{}, 381 }) 382 } 383 384 type DockerTrustSuite struct { 385 ds *DockerSuite 386 reg *registry.V2 387 not *testNotary 388 } 389 390 func (s *DockerTrustSuite) OnTimeout(c *check.C) { 391 s.ds.OnTimeout(c) 392 } 393 394 func (s *DockerTrustSuite) SetUpTest(c *check.C) { 395 testRequires(c, registry.Hosting, NotaryServerHosting) 396 s.reg = setupRegistry(c, false, "", "") 397 s.not = setupNotary(c) 398 } 399 400 func (s *DockerTrustSuite) TearDownTest(c *check.C) { 401 if s.reg != nil { 402 s.reg.Close() 403 } 404 if s.not != nil { 405 s.not.Close() 406 } 407 408 // Remove trusted keys and metadata after test 409 os.RemoveAll(filepath.Join(config.Dir(), "trust")) 410 s.ds.TearDownTest(c) 411 } 412 413 func init() { 414 ds := &DockerSuite{} 415 check.Suite(&DockerTrustedSwarmSuite{ 416 trustSuite: DockerTrustSuite{ 417 ds: ds, 418 }, 419 swarmSuite: DockerSwarmSuite{ 420 ds: ds, 421 }, 422 }) 423 } 424 425 type DockerTrustedSwarmSuite struct { 426 swarmSuite DockerSwarmSuite 427 trustSuite DockerTrustSuite 428 reg *registry.V2 429 not *testNotary 430 } 431 432 func (s *DockerTrustedSwarmSuite) SetUpTest(c *check.C) { 433 s.swarmSuite.SetUpTest(c) 434 s.trustSuite.SetUpTest(c) 435 } 436 437 func (s *DockerTrustedSwarmSuite) TearDownTest(c *check.C) { 438 s.trustSuite.TearDownTest(c) 439 s.swarmSuite.TearDownTest(c) 440 } 441 442 func (s *DockerTrustedSwarmSuite) OnTimeout(c *check.C) { 443 s.swarmSuite.OnTimeout(c) 444 }