github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/integration-cli/check_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "sync" 8 "syscall" 9 "testing" 10 11 "github.com/docker/docker/cliconfig" 12 "github.com/docker/docker/pkg/integration/checker" 13 "github.com/docker/docker/pkg/reexec" 14 "github.com/docker/engine-api/types/swarm" 15 "github.com/go-check/check" 16 ) 17 18 func Test(t *testing.T) { 19 reexec.Init() // This is required for external graphdriver tests 20 21 if !isLocalDaemon { 22 fmt.Println("INFO: Testing against a remote daemon") 23 } else { 24 fmt.Println("INFO: Testing against a local daemon") 25 } 26 27 check.TestingT(t) 28 } 29 30 func init() { 31 check.Suite(&DockerSuite{}) 32 } 33 34 type DockerSuite struct { 35 } 36 37 func (s *DockerSuite) TearDownTest(c *check.C) { 38 unpauseAllContainers() 39 deleteAllContainers() 40 deleteAllImages() 41 deleteAllVolumes() 42 deleteAllNetworks() 43 } 44 45 func init() { 46 check.Suite(&DockerRegistrySuite{ 47 ds: &DockerSuite{}, 48 }) 49 } 50 51 type DockerRegistrySuite struct { 52 ds *DockerSuite 53 reg *testRegistryV2 54 d *Daemon 55 } 56 57 func (s *DockerRegistrySuite) SetUpTest(c *check.C) { 58 testRequires(c, DaemonIsLinux, RegistryHosting) 59 s.reg = setupRegistry(c, false, "", "") 60 s.d = NewDaemon(c) 61 } 62 63 func (s *DockerRegistrySuite) TearDownTest(c *check.C) { 64 if s.reg != nil { 65 s.reg.Close() 66 } 67 if s.d != nil { 68 s.d.Stop() 69 } 70 s.ds.TearDownTest(c) 71 } 72 73 func init() { 74 check.Suite(&DockerSchema1RegistrySuite{ 75 ds: &DockerSuite{}, 76 }) 77 } 78 79 type DockerSchema1RegistrySuite struct { 80 ds *DockerSuite 81 reg *testRegistryV2 82 d *Daemon 83 } 84 85 func (s *DockerSchema1RegistrySuite) SetUpTest(c *check.C) { 86 testRequires(c, DaemonIsLinux, RegistryHosting, NotArm64) 87 s.reg = setupRegistry(c, true, "", "") 88 s.d = NewDaemon(c) 89 } 90 91 func (s *DockerSchema1RegistrySuite) TearDownTest(c *check.C) { 92 if s.reg != nil { 93 s.reg.Close() 94 } 95 if s.d != nil { 96 s.d.Stop() 97 } 98 s.ds.TearDownTest(c) 99 } 100 101 func init() { 102 check.Suite(&DockerRegistryAuthHtpasswdSuite{ 103 ds: &DockerSuite{}, 104 }) 105 } 106 107 type DockerRegistryAuthHtpasswdSuite struct { 108 ds *DockerSuite 109 reg *testRegistryV2 110 d *Daemon 111 } 112 113 func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *check.C) { 114 testRequires(c, DaemonIsLinux, RegistryHosting) 115 s.reg = setupRegistry(c, false, "htpasswd", "") 116 s.d = NewDaemon(c) 117 } 118 119 func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *check.C) { 120 if s.reg != nil { 121 out, err := s.d.Cmd("logout", privateRegistryURL) 122 c.Assert(err, check.IsNil, check.Commentf(out)) 123 s.reg.Close() 124 } 125 if s.d != nil { 126 s.d.Stop() 127 } 128 s.ds.TearDownTest(c) 129 } 130 131 func init() { 132 check.Suite(&DockerRegistryAuthTokenSuite{ 133 ds: &DockerSuite{}, 134 }) 135 } 136 137 type DockerRegistryAuthTokenSuite struct { 138 ds *DockerSuite 139 reg *testRegistryV2 140 d *Daemon 141 } 142 143 func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *check.C) { 144 testRequires(c, DaemonIsLinux, RegistryHosting) 145 s.d = NewDaemon(c) 146 } 147 148 func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *check.C) { 149 if s.reg != nil { 150 out, err := s.d.Cmd("logout", privateRegistryURL) 151 c.Assert(err, check.IsNil, check.Commentf(out)) 152 s.reg.Close() 153 } 154 if s.d != nil { 155 s.d.Stop() 156 } 157 s.ds.TearDownTest(c) 158 } 159 160 func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *check.C, tokenURL string) { 161 if s == nil { 162 c.Fatal("registry suite isn't initialized") 163 } 164 s.reg = setupRegistry(c, false, "token", tokenURL) 165 } 166 167 func init() { 168 check.Suite(&DockerDaemonSuite{ 169 ds: &DockerSuite{}, 170 }) 171 } 172 173 type DockerDaemonSuite struct { 174 ds *DockerSuite 175 d *Daemon 176 } 177 178 func (s *DockerDaemonSuite) SetUpTest(c *check.C) { 179 testRequires(c, DaemonIsLinux) 180 s.d = NewDaemon(c) 181 } 182 183 func (s *DockerDaemonSuite) TearDownTest(c *check.C) { 184 testRequires(c, DaemonIsLinux) 185 if s.d != nil { 186 s.d.Stop() 187 } 188 s.ds.TearDownTest(c) 189 } 190 191 func (s *DockerDaemonSuite) TearDownSuite(c *check.C) { 192 err := filepath.Walk(daemonSockRoot, func(path string, fi os.FileInfo, err error) error { 193 if err != nil { 194 return err 195 } 196 if fi.Mode() == os.ModeSocket { 197 syscall.Unlink(path) 198 } 199 return nil 200 }) 201 c.Assert(err, checker.IsNil, check.Commentf("error while cleaning up daemon sockets")) 202 err = os.RemoveAll(daemonSockRoot) 203 c.Assert(err, checker.IsNil, check.Commentf("could not cleanup daemon socket root")) 204 } 205 206 const defaultSwarmPort = 2477 207 208 func init() { 209 check.Suite(&DockerSwarmSuite{ 210 ds: &DockerSuite{}, 211 }) 212 } 213 214 type DockerSwarmSuite struct { 215 ds *DockerSuite 216 daemons []*SwarmDaemon 217 daemonsLock sync.Mutex // protect access to daemons 218 portIndex int 219 } 220 221 func (s *DockerSwarmSuite) SetUpTest(c *check.C) { 222 testRequires(c, DaemonIsLinux) 223 } 224 225 func (s *DockerSwarmSuite) AddDaemon(c *check.C, joinSwarm, manager bool) *SwarmDaemon { 226 d := &SwarmDaemon{ 227 Daemon: NewDaemon(c), 228 port: defaultSwarmPort + s.portIndex, 229 } 230 d.listenAddr = fmt.Sprintf("0.0.0.0:%d", d.port) 231 err := d.StartWithBusybox("--iptables=false", "--swarm-default-advertise-addr=lo") // avoid networking conflicts 232 c.Assert(err, check.IsNil) 233 234 if joinSwarm == true { 235 if len(s.daemons) > 0 { 236 tokens := s.daemons[0].joinTokens(c) 237 token := tokens.Worker 238 if manager { 239 token = tokens.Manager 240 } 241 c.Assert(d.Join(swarm.JoinRequest{ 242 RemoteAddrs: []string{s.daemons[0].listenAddr}, 243 JoinToken: token, 244 }), check.IsNil) 245 } else { 246 c.Assert(d.Init(swarm.InitRequest{}), check.IsNil) 247 } 248 } 249 250 s.portIndex++ 251 s.daemonsLock.Lock() 252 s.daemons = append(s.daemons, d) 253 s.daemonsLock.Unlock() 254 255 return d 256 } 257 258 func (s *DockerSwarmSuite) TearDownTest(c *check.C) { 259 testRequires(c, DaemonIsLinux) 260 s.daemonsLock.Lock() 261 for _, d := range s.daemons { 262 d.Stop() 263 } 264 s.daemons = nil 265 s.daemonsLock.Unlock() 266 267 s.portIndex = 0 268 s.ds.TearDownTest(c) 269 } 270 271 func init() { 272 check.Suite(&DockerTrustSuite{ 273 ds: &DockerSuite{}, 274 }) 275 } 276 277 type DockerTrustSuite struct { 278 ds *DockerSuite 279 reg *testRegistryV2 280 not *testNotary 281 } 282 283 func (s *DockerTrustSuite) SetUpTest(c *check.C) { 284 testRequires(c, RegistryHosting, NotaryServerHosting) 285 s.reg = setupRegistry(c, false, "", "") 286 s.not = setupNotary(c) 287 } 288 289 func (s *DockerTrustSuite) TearDownTest(c *check.C) { 290 if s.reg != nil { 291 s.reg.Close() 292 } 293 if s.not != nil { 294 s.not.Close() 295 } 296 297 // Remove trusted keys and metadata after test 298 os.RemoveAll(filepath.Join(cliconfig.ConfigDir(), "trust")) 299 s.ds.TearDownTest(c) 300 }