check_test.go 7.7 KB

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