check_test.go 7.0 KB

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