github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/integration-cli/check_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http/httptest" 7 "os" 8 "path" 9 "path/filepath" 10 "strconv" 11 "sync" 12 "syscall" 13 "testing" 14 "time" 15 16 "github.com/docker/docker/integration-cli/checker" 17 "github.com/docker/docker/integration-cli/cli" 18 "github.com/docker/docker/integration-cli/daemon" 19 "github.com/docker/docker/integration-cli/environment" 20 testdaemon "github.com/docker/docker/internal/test/daemon" 21 ienv "github.com/docker/docker/internal/test/environment" 22 "github.com/docker/docker/internal/test/fakestorage" 23 "github.com/docker/docker/internal/test/fixtures/plugin" 24 "github.com/docker/docker/internal/test/registry" 25 "github.com/docker/docker/pkg/reexec" 26 "github.com/go-check/check" 27 "golang.org/x/net/context" 28 ) 29 30 const ( 31 // the private registry to use for tests 32 privateRegistryURL = registry.DefaultURL 33 34 // path to containerd's ctr binary 35 ctrBinary = "docker-containerd-ctr" 36 37 // the docker daemon binary to use 38 dockerdBinary = "dockerd" 39 ) 40 41 var ( 42 testEnv *environment.Execution 43 44 // the docker client binary to use 45 dockerBinary = "" 46 ) 47 48 func init() { 49 var err error 50 51 reexec.Init() // This is required for external graphdriver tests 52 53 testEnv, err = environment.New() 54 if err != nil { 55 fmt.Println(err) 56 os.Exit(1) 57 } 58 } 59 60 func TestMain(m *testing.M) { 61 dockerBinary = testEnv.DockerBinary() 62 err := ienv.EnsureFrozenImagesLinux(&testEnv.Execution) 63 if err != nil { 64 fmt.Println(err) 65 os.Exit(1) 66 } 67 68 testEnv.Print() 69 os.Exit(m.Run()) 70 } 71 72 func Test(t *testing.T) { 73 cli.SetTestEnvironment(testEnv) 74 fakestorage.SetTestEnvironment(&testEnv.Execution) 75 ienv.ProtectAll(t, &testEnv.Execution) 76 check.TestingT(t) 77 } 78 79 func init() { 80 check.Suite(&DockerSuite{}) 81 } 82 83 type DockerSuite struct { 84 } 85 86 func (s *DockerSuite) OnTimeout(c *check.C) { 87 if testEnv.IsRemoteDaemon() { 88 return 89 } 90 path := filepath.Join(os.Getenv("DEST"), "docker.pid") 91 b, err := ioutil.ReadFile(path) 92 if err != nil { 93 c.Fatalf("Failed to get daemon PID from %s\n", path) 94 } 95 96 rawPid, err := strconv.ParseInt(string(b), 10, 32) 97 if err != nil { 98 c.Fatalf("Failed to parse pid from %s: %s\n", path, err) 99 } 100 101 daemonPid := int(rawPid) 102 if daemonPid > 0 { 103 testdaemon.SignalDaemonDump(daemonPid) 104 } 105 } 106 107 func (s *DockerSuite) TearDownTest(c *check.C) { 108 testEnv.Clean(c) 109 } 110 111 func init() { 112 check.Suite(&DockerRegistrySuite{ 113 ds: &DockerSuite{}, 114 }) 115 } 116 117 type DockerRegistrySuite struct { 118 ds *DockerSuite 119 reg *registry.V2 120 d *daemon.Daemon 121 } 122 123 func (s *DockerRegistrySuite) OnTimeout(c *check.C) { 124 s.d.DumpStackAndQuit() 125 } 126 127 func (s *DockerRegistrySuite) SetUpTest(c *check.C) { 128 testRequires(c, DaemonIsLinux, RegistryHosting, SameHostDaemon) 129 s.reg = registry.NewV2(c) 130 s.reg.WaitReady(c) 131 s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution)) 132 } 133 134 func (s *DockerRegistrySuite) TearDownTest(c *check.C) { 135 if s.reg != nil { 136 s.reg.Close() 137 } 138 if s.d != nil { 139 s.d.Stop(c) 140 } 141 s.ds.TearDownTest(c) 142 } 143 144 func init() { 145 check.Suite(&DockerSchema1RegistrySuite{ 146 ds: &DockerSuite{}, 147 }) 148 } 149 150 type DockerSchema1RegistrySuite struct { 151 ds *DockerSuite 152 reg *registry.V2 153 d *daemon.Daemon 154 } 155 156 func (s *DockerSchema1RegistrySuite) OnTimeout(c *check.C) { 157 s.d.DumpStackAndQuit() 158 } 159 160 func (s *DockerSchema1RegistrySuite) SetUpTest(c *check.C) { 161 testRequires(c, DaemonIsLinux, RegistryHosting, NotArm64, SameHostDaemon) 162 s.reg = registry.NewV2(c, registry.Schema1) 163 s.reg.WaitReady(c) 164 s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution)) 165 } 166 167 func (s *DockerSchema1RegistrySuite) TearDownTest(c *check.C) { 168 if s.reg != nil { 169 s.reg.Close() 170 } 171 if s.d != nil { 172 s.d.Stop(c) 173 } 174 s.ds.TearDownTest(c) 175 } 176 177 func init() { 178 check.Suite(&DockerRegistryAuthHtpasswdSuite{ 179 ds: &DockerSuite{}, 180 }) 181 } 182 183 type DockerRegistryAuthHtpasswdSuite struct { 184 ds *DockerSuite 185 reg *registry.V2 186 d *daemon.Daemon 187 } 188 189 func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *check.C) { 190 s.d.DumpStackAndQuit() 191 } 192 193 func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *check.C) { 194 testRequires(c, DaemonIsLinux, RegistryHosting, SameHostDaemon) 195 s.reg = registry.NewV2(c, registry.Htpasswd) 196 s.reg.WaitReady(c) 197 s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution)) 198 } 199 200 func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *check.C) { 201 if s.reg != nil { 202 out, err := s.d.Cmd("logout", privateRegistryURL) 203 c.Assert(err, check.IsNil, check.Commentf(out)) 204 s.reg.Close() 205 } 206 if s.d != nil { 207 s.d.Stop(c) 208 } 209 s.ds.TearDownTest(c) 210 } 211 212 func init() { 213 check.Suite(&DockerRegistryAuthTokenSuite{ 214 ds: &DockerSuite{}, 215 }) 216 } 217 218 type DockerRegistryAuthTokenSuite struct { 219 ds *DockerSuite 220 reg *registry.V2 221 d *daemon.Daemon 222 } 223 224 func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *check.C) { 225 s.d.DumpStackAndQuit() 226 } 227 228 func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *check.C) { 229 testRequires(c, DaemonIsLinux, RegistryHosting, SameHostDaemon) 230 s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution)) 231 } 232 233 func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *check.C) { 234 if s.reg != nil { 235 out, err := s.d.Cmd("logout", privateRegistryURL) 236 c.Assert(err, check.IsNil, check.Commentf(out)) 237 s.reg.Close() 238 } 239 if s.d != nil { 240 s.d.Stop(c) 241 } 242 s.ds.TearDownTest(c) 243 } 244 245 func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *check.C, tokenURL string) { 246 if s == nil { 247 c.Fatal("registry suite isn't initialized") 248 } 249 s.reg = registry.NewV2(c, registry.Token(tokenURL)) 250 s.reg.WaitReady(c) 251 } 252 253 func init() { 254 check.Suite(&DockerDaemonSuite{ 255 ds: &DockerSuite{}, 256 }) 257 } 258 259 type DockerDaemonSuite struct { 260 ds *DockerSuite 261 d *daemon.Daemon 262 } 263 264 func (s *DockerDaemonSuite) OnTimeout(c *check.C) { 265 s.d.DumpStackAndQuit() 266 } 267 268 func (s *DockerDaemonSuite) SetUpTest(c *check.C) { 269 testRequires(c, DaemonIsLinux, SameHostDaemon) 270 s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution)) 271 } 272 273 func (s *DockerDaemonSuite) TearDownTest(c *check.C) { 274 testRequires(c, DaemonIsLinux, SameHostDaemon) 275 if s.d != nil { 276 s.d.Stop(c) 277 } 278 s.ds.TearDownTest(c) 279 } 280 281 func (s *DockerDaemonSuite) TearDownSuite(c *check.C) { 282 filepath.Walk(testdaemon.SockRoot, func(path string, fi os.FileInfo, err error) error { 283 if err != nil { 284 // ignore errors here 285 // not cleaning up sockets is not really an error 286 return nil 287 } 288 if fi.Mode() == os.ModeSocket { 289 syscall.Unlink(path) 290 } 291 return nil 292 }) 293 os.RemoveAll(testdaemon.SockRoot) 294 } 295 296 const defaultSwarmPort = 2477 297 298 func init() { 299 check.Suite(&DockerSwarmSuite{ 300 ds: &DockerSuite{}, 301 }) 302 } 303 304 type DockerSwarmSuite struct { 305 server *httptest.Server 306 ds *DockerSuite 307 daemons []*daemon.Daemon 308 daemonsLock sync.Mutex // protect access to daemons 309 portIndex int 310 } 311 312 func (s *DockerSwarmSuite) OnTimeout(c *check.C) { 313 s.daemonsLock.Lock() 314 defer s.daemonsLock.Unlock() 315 for _, d := range s.daemons { 316 d.DumpStackAndQuit() 317 } 318 } 319 320 func (s *DockerSwarmSuite) SetUpTest(c *check.C) { 321 testRequires(c, DaemonIsLinux, SameHostDaemon) 322 } 323 324 func (s *DockerSwarmSuite) AddDaemon(c *check.C, joinSwarm, manager bool) *daemon.Daemon { 325 d := daemon.New(c, dockerBinary, dockerdBinary, 326 testdaemon.WithEnvironment(testEnv.Execution), 327 testdaemon.WithSwarmPort(defaultSwarmPort+s.portIndex), 328 ) 329 if joinSwarm { 330 if len(s.daemons) > 0 { 331 d.StartAndSwarmJoin(c, s.daemons[0].Daemon, manager) 332 } else { 333 d.StartAndSwarmInit(c) 334 } 335 } else { 336 d.StartWithBusybox(c, "--iptables=false", "--swarm-default-advertise-addr=lo") 337 } 338 339 s.portIndex++ 340 s.daemonsLock.Lock() 341 s.daemons = append(s.daemons, d) 342 s.daemonsLock.Unlock() 343 344 return d 345 } 346 347 func (s *DockerSwarmSuite) TearDownTest(c *check.C) { 348 testRequires(c, DaemonIsLinux) 349 s.daemonsLock.Lock() 350 for _, d := range s.daemons { 351 if d != nil { 352 d.Stop(c) 353 // FIXME(vdemeester) should be handled by SwarmDaemon ? 354 // raft state file is quite big (64MB) so remove it after every test 355 walDir := filepath.Join(d.Root, "swarm/raft/wal") 356 if err := os.RemoveAll(walDir); err != nil { 357 c.Logf("error removing %v: %v", walDir, err) 358 } 359 360 d.CleanupExecRoot(c) 361 } 362 } 363 s.daemons = nil 364 s.daemonsLock.Unlock() 365 366 s.portIndex = 0 367 s.ds.TearDownTest(c) 368 } 369 370 func init() { 371 check.Suite(&DockerPluginSuite{ 372 ds: &DockerSuite{}, 373 }) 374 } 375 376 type DockerPluginSuite struct { 377 ds *DockerSuite 378 registry *registry.V2 379 } 380 381 func (ps *DockerPluginSuite) registryHost() string { 382 return privateRegistryURL 383 } 384 385 func (ps *DockerPluginSuite) getPluginRepo() string { 386 return path.Join(ps.registryHost(), "plugin", "basic") 387 } 388 func (ps *DockerPluginSuite) getPluginRepoWithTag() string { 389 return ps.getPluginRepo() + ":" + "latest" 390 } 391 392 func (ps *DockerPluginSuite) SetUpSuite(c *check.C) { 393 testRequires(c, DaemonIsLinux, RegistryHosting) 394 ps.registry = registry.NewV2(c) 395 ps.registry.WaitReady(c) 396 397 ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) 398 defer cancel() 399 400 err := plugin.CreateInRegistry(ctx, ps.getPluginRepo(), nil) 401 c.Assert(err, checker.IsNil, check.Commentf("failed to create plugin")) 402 } 403 404 func (ps *DockerPluginSuite) TearDownSuite(c *check.C) { 405 if ps.registry != nil { 406 ps.registry.Close() 407 } 408 } 409 410 func (ps *DockerPluginSuite) TearDownTest(c *check.C) { 411 ps.ds.TearDownTest(c) 412 } 413 414 func (ps *DockerPluginSuite) OnTimeout(c *check.C) { 415 ps.ds.OnTimeout(c) 416 }