check_test.go 5.2 KB

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