check_test.go 5.5 KB

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