check_test.go 6.9 KB

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