check_test.go 7.1 KB

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