check_test.go 6.7 KB

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