check_test.go 6.0 KB

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