check_test.go 5.1 KB

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