check_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. cliconfig "github.com/docker/docker/cli/config"
  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(c)
  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(c)
  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(c)
  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(c)
  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, SameHostDaemon)
  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, SameHostDaemon)
  193. if s.d != nil {
  194. s.d.Stop(c)
  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. d.StartWithBusybox(c, args...)
  245. if joinSwarm == true {
  246. if len(s.daemons) > 0 {
  247. tokens := s.daemons[0].JoinTokens(c)
  248. token := tokens.Worker
  249. if manager {
  250. token = tokens.Manager
  251. }
  252. c.Assert(d.Join(swarm.JoinRequest{
  253. RemoteAddrs: []string{s.daemons[0].ListenAddr},
  254. JoinToken: token,
  255. }), check.IsNil)
  256. } else {
  257. c.Assert(d.Init(swarm.InitRequest{}), check.IsNil)
  258. }
  259. }
  260. s.portIndex++
  261. s.daemonsLock.Lock()
  262. s.daemons = append(s.daemons, d)
  263. s.daemonsLock.Unlock()
  264. return d
  265. }
  266. func (s *DockerSwarmSuite) TearDownTest(c *check.C) {
  267. testRequires(c, DaemonIsLinux)
  268. s.daemonsLock.Lock()
  269. for _, d := range s.daemons {
  270. if d != nil {
  271. d.Stop(c)
  272. // FIXME(vdemeester) should be handled by SwarmDaemon ?
  273. // raft state file is quite big (64MB) so remove it after every test
  274. walDir := filepath.Join(d.Root, "swarm/raft/wal")
  275. if err := os.RemoveAll(walDir); err != nil {
  276. c.Logf("error removing %v: %v", walDir, err)
  277. }
  278. d.CleanupExecRoot(c)
  279. }
  280. }
  281. s.daemons = nil
  282. s.daemonsLock.Unlock()
  283. s.portIndex = 0
  284. s.ds.TearDownTest(c)
  285. }
  286. func init() {
  287. check.Suite(&DockerTrustSuite{
  288. ds: &DockerSuite{},
  289. })
  290. }
  291. type DockerTrustSuite struct {
  292. ds *DockerSuite
  293. reg *testRegistryV2
  294. not *testNotary
  295. }
  296. func (s *DockerTrustSuite) SetUpTest(c *check.C) {
  297. testRequires(c, RegistryHosting, NotaryServerHosting)
  298. s.reg = setupRegistry(c, false, "", "")
  299. s.not = setupNotary(c)
  300. }
  301. func (s *DockerTrustSuite) TearDownTest(c *check.C) {
  302. if s.reg != nil {
  303. s.reg.Close()
  304. }
  305. if s.not != nil {
  306. s.not.Close()
  307. }
  308. // Remove trusted keys and metadata after test
  309. os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
  310. s.ds.TearDownTest(c)
  311. }
  312. func init() {
  313. ds := &DockerSuite{}
  314. check.Suite(&DockerTrustedSwarmSuite{
  315. trustSuite: DockerTrustSuite{
  316. ds: ds,
  317. },
  318. swarmSuite: DockerSwarmSuite{
  319. ds: ds,
  320. },
  321. })
  322. }
  323. type DockerTrustedSwarmSuite struct {
  324. swarmSuite DockerSwarmSuite
  325. trustSuite DockerTrustSuite
  326. reg *testRegistryV2
  327. not *testNotary
  328. }
  329. func (s *DockerTrustedSwarmSuite) SetUpTest(c *check.C) {
  330. s.swarmSuite.SetUpTest(c)
  331. s.trustSuite.SetUpTest(c)
  332. }
  333. func (s *DockerTrustedSwarmSuite) TearDownTest(c *check.C) {
  334. s.trustSuite.TearDownTest(c)
  335. s.swarmSuite.TearDownTest(c)
  336. }
  337. func (s *DockerTrustedSwarmSuite) OnTimeout(c *check.C) {
  338. s.swarmSuite.OnTimeout(c)
  339. }