check_test.go 6.1 KB

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