check_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http/httptest"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "strconv"
  10. "sync"
  11. "syscall"
  12. "testing"
  13. "time"
  14. "github.com/docker/docker/api/types/swarm"
  15. "github.com/docker/docker/cli/config"
  16. "github.com/docker/docker/integration-cli/checker"
  17. "github.com/docker/docker/integration-cli/cli"
  18. "github.com/docker/docker/integration-cli/cli/build/fakestorage"
  19. "github.com/docker/docker/integration-cli/daemon"
  20. "github.com/docker/docker/integration-cli/environment"
  21. "github.com/docker/docker/integration-cli/fixtures/plugin"
  22. "github.com/docker/docker/integration-cli/registry"
  23. ienv "github.com/docker/docker/internal/test/environment"
  24. "github.com/docker/docker/pkg/reexec"
  25. "github.com/go-check/check"
  26. "golang.org/x/net/context"
  27. )
  28. const (
  29. // the private registry to use for tests
  30. privateRegistryURL = "127.0.0.1:5000"
  31. // path to containerd's ctr binary
  32. ctrBinary = "docker-containerd-ctr"
  33. // the docker daemon binary to use
  34. dockerdBinary = "dockerd"
  35. )
  36. var (
  37. testEnv *environment.Execution
  38. // the docker client binary to use
  39. dockerBinary = ""
  40. )
  41. func init() {
  42. var err error
  43. reexec.Init() // This is required for external graphdriver tests
  44. testEnv, err = environment.New()
  45. if err != nil {
  46. fmt.Println(err)
  47. os.Exit(1)
  48. }
  49. }
  50. func TestMain(m *testing.M) {
  51. dockerBinary = testEnv.DockerBinary()
  52. testEnv.Print()
  53. os.Exit(m.Run())
  54. }
  55. func Test(t *testing.T) {
  56. cli.SetTestEnvironment(testEnv)
  57. fakestorage.SetTestEnvironment(&testEnv.Execution)
  58. ienv.ProtectAll(t, &testEnv.Execution)
  59. check.TestingT(t)
  60. }
  61. func init() {
  62. check.Suite(&DockerSuite{})
  63. }
  64. type DockerSuite struct {
  65. }
  66. func (s *DockerSuite) OnTimeout(c *check.C) {
  67. path := filepath.Join(os.Getenv("DEST"), "docker.pid")
  68. b, err := ioutil.ReadFile(path)
  69. if err != nil {
  70. c.Fatalf("Failed to get daemon PID from %s\n", path)
  71. }
  72. rawPid, err := strconv.ParseInt(string(b), 10, 32)
  73. if err != nil {
  74. c.Fatalf("Failed to parse pid from %s: %s\n", path, err)
  75. }
  76. daemonPid := int(rawPid)
  77. if daemonPid > 0 && testEnv.IsLocalDaemon() {
  78. daemon.SignalDaemonDump(daemonPid)
  79. }
  80. }
  81. func (s *DockerSuite) TearDownTest(c *check.C) {
  82. testEnv.Clean(c)
  83. }
  84. func init() {
  85. check.Suite(&DockerRegistrySuite{
  86. ds: &DockerSuite{},
  87. })
  88. }
  89. type DockerRegistrySuite struct {
  90. ds *DockerSuite
  91. reg *registry.V2
  92. d *daemon.Daemon
  93. }
  94. func (s *DockerRegistrySuite) OnTimeout(c *check.C) {
  95. s.d.DumpStackAndQuit()
  96. }
  97. func (s *DockerRegistrySuite) SetUpTest(c *check.C) {
  98. testRequires(c, DaemonIsLinux, registry.Hosting)
  99. s.reg = setupRegistry(c, false, "", "")
  100. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  101. Experimental: testEnv.ExperimentalDaemon(),
  102. })
  103. }
  104. func (s *DockerRegistrySuite) TearDownTest(c *check.C) {
  105. if s.reg != nil {
  106. s.reg.Close()
  107. }
  108. if s.d != nil {
  109. s.d.Stop(c)
  110. }
  111. s.ds.TearDownTest(c)
  112. }
  113. func init() {
  114. check.Suite(&DockerSchema1RegistrySuite{
  115. ds: &DockerSuite{},
  116. })
  117. }
  118. type DockerSchema1RegistrySuite struct {
  119. ds *DockerSuite
  120. reg *registry.V2
  121. d *daemon.Daemon
  122. }
  123. func (s *DockerSchema1RegistrySuite) OnTimeout(c *check.C) {
  124. s.d.DumpStackAndQuit()
  125. }
  126. func (s *DockerSchema1RegistrySuite) SetUpTest(c *check.C) {
  127. testRequires(c, DaemonIsLinux, registry.Hosting, NotArm64)
  128. s.reg = setupRegistry(c, true, "", "")
  129. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  130. Experimental: testEnv.ExperimentalDaemon(),
  131. })
  132. }
  133. func (s *DockerSchema1RegistrySuite) TearDownTest(c *check.C) {
  134. if s.reg != nil {
  135. s.reg.Close()
  136. }
  137. if s.d != nil {
  138. s.d.Stop(c)
  139. }
  140. s.ds.TearDownTest(c)
  141. }
  142. func init() {
  143. check.Suite(&DockerRegistryAuthHtpasswdSuite{
  144. ds: &DockerSuite{},
  145. })
  146. }
  147. type DockerRegistryAuthHtpasswdSuite struct {
  148. ds *DockerSuite
  149. reg *registry.V2
  150. d *daemon.Daemon
  151. }
  152. func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *check.C) {
  153. s.d.DumpStackAndQuit()
  154. }
  155. func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *check.C) {
  156. testRequires(c, DaemonIsLinux, registry.Hosting)
  157. s.reg = setupRegistry(c, false, "htpasswd", "")
  158. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  159. Experimental: testEnv.ExperimentalDaemon(),
  160. })
  161. }
  162. func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *check.C) {
  163. if s.reg != nil {
  164. out, err := s.d.Cmd("logout", privateRegistryURL)
  165. c.Assert(err, check.IsNil, check.Commentf(out))
  166. s.reg.Close()
  167. }
  168. if s.d != nil {
  169. s.d.Stop(c)
  170. }
  171. s.ds.TearDownTest(c)
  172. }
  173. func init() {
  174. check.Suite(&DockerRegistryAuthTokenSuite{
  175. ds: &DockerSuite{},
  176. })
  177. }
  178. type DockerRegistryAuthTokenSuite struct {
  179. ds *DockerSuite
  180. reg *registry.V2
  181. d *daemon.Daemon
  182. }
  183. func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *check.C) {
  184. s.d.DumpStackAndQuit()
  185. }
  186. func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *check.C) {
  187. testRequires(c, DaemonIsLinux, registry.Hosting)
  188. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  189. Experimental: testEnv.ExperimentalDaemon(),
  190. })
  191. }
  192. func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *check.C) {
  193. if s.reg != nil {
  194. out, err := s.d.Cmd("logout", privateRegistryURL)
  195. c.Assert(err, check.IsNil, check.Commentf(out))
  196. s.reg.Close()
  197. }
  198. if s.d != nil {
  199. s.d.Stop(c)
  200. }
  201. s.ds.TearDownTest(c)
  202. }
  203. func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *check.C, tokenURL string) {
  204. if s == nil {
  205. c.Fatal("registry suite isn't initialized")
  206. }
  207. s.reg = setupRegistry(c, false, "token", tokenURL)
  208. }
  209. func init() {
  210. check.Suite(&DockerDaemonSuite{
  211. ds: &DockerSuite{},
  212. })
  213. }
  214. type DockerDaemonSuite struct {
  215. ds *DockerSuite
  216. d *daemon.Daemon
  217. }
  218. func (s *DockerDaemonSuite) OnTimeout(c *check.C) {
  219. s.d.DumpStackAndQuit()
  220. }
  221. func (s *DockerDaemonSuite) SetUpTest(c *check.C) {
  222. testRequires(c, DaemonIsLinux, SameHostDaemon)
  223. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  224. Experimental: testEnv.ExperimentalDaemon(),
  225. })
  226. }
  227. func (s *DockerDaemonSuite) TearDownTest(c *check.C) {
  228. testRequires(c, DaemonIsLinux, SameHostDaemon)
  229. if s.d != nil {
  230. s.d.Stop(c)
  231. }
  232. s.ds.TearDownTest(c)
  233. }
  234. func (s *DockerDaemonSuite) TearDownSuite(c *check.C) {
  235. filepath.Walk(daemon.SockRoot, func(path string, fi os.FileInfo, err error) error {
  236. if err != nil {
  237. // ignore errors here
  238. // not cleaning up sockets is not really an error
  239. return nil
  240. }
  241. if fi.Mode() == os.ModeSocket {
  242. syscall.Unlink(path)
  243. }
  244. return nil
  245. })
  246. os.RemoveAll(daemon.SockRoot)
  247. }
  248. const defaultSwarmPort = 2477
  249. func init() {
  250. check.Suite(&DockerSwarmSuite{
  251. ds: &DockerSuite{},
  252. })
  253. }
  254. type DockerSwarmSuite struct {
  255. server *httptest.Server
  256. ds *DockerSuite
  257. daemons []*daemon.Swarm
  258. daemonsLock sync.Mutex // protect access to daemons
  259. portIndex int
  260. }
  261. func (s *DockerSwarmSuite) OnTimeout(c *check.C) {
  262. s.daemonsLock.Lock()
  263. defer s.daemonsLock.Unlock()
  264. for _, d := range s.daemons {
  265. d.DumpStackAndQuit()
  266. }
  267. }
  268. func (s *DockerSwarmSuite) SetUpTest(c *check.C) {
  269. testRequires(c, DaemonIsLinux)
  270. }
  271. func (s *DockerSwarmSuite) AddDaemon(c *check.C, joinSwarm, manager bool) *daemon.Swarm {
  272. d := &daemon.Swarm{
  273. Daemon: daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  274. Experimental: testEnv.ExperimentalDaemon(),
  275. }),
  276. Port: defaultSwarmPort + s.portIndex,
  277. }
  278. d.ListenAddr = fmt.Sprintf("0.0.0.0:%d", d.Port)
  279. args := []string{"--iptables=false", "--swarm-default-advertise-addr=lo"} // avoid networking conflicts
  280. d.StartWithBusybox(c, args...)
  281. if joinSwarm == true {
  282. if len(s.daemons) > 0 {
  283. tokens := s.daemons[0].JoinTokens(c)
  284. token := tokens.Worker
  285. if manager {
  286. token = tokens.Manager
  287. }
  288. c.Assert(d.Join(swarm.JoinRequest{
  289. RemoteAddrs: []string{s.daemons[0].ListenAddr},
  290. JoinToken: token,
  291. }), check.IsNil)
  292. } else {
  293. c.Assert(d.Init(swarm.InitRequest{}), check.IsNil)
  294. }
  295. }
  296. s.portIndex++
  297. s.daemonsLock.Lock()
  298. s.daemons = append(s.daemons, d)
  299. s.daemonsLock.Unlock()
  300. return d
  301. }
  302. func (s *DockerSwarmSuite) TearDownTest(c *check.C) {
  303. testRequires(c, DaemonIsLinux)
  304. s.daemonsLock.Lock()
  305. for _, d := range s.daemons {
  306. if d != nil {
  307. d.Stop(c)
  308. // FIXME(vdemeester) should be handled by SwarmDaemon ?
  309. // raft state file is quite big (64MB) so remove it after every test
  310. walDir := filepath.Join(d.Root, "swarm/raft/wal")
  311. if err := os.RemoveAll(walDir); err != nil {
  312. c.Logf("error removing %v: %v", walDir, err)
  313. }
  314. d.CleanupExecRoot(c)
  315. }
  316. }
  317. s.daemons = nil
  318. s.daemonsLock.Unlock()
  319. s.portIndex = 0
  320. s.ds.TearDownTest(c)
  321. }
  322. func init() {
  323. check.Suite(&DockerTrustSuite{
  324. ds: &DockerSuite{},
  325. })
  326. }
  327. type DockerTrustSuite struct {
  328. ds *DockerSuite
  329. reg *registry.V2
  330. not *testNotary
  331. }
  332. func (s *DockerTrustSuite) OnTimeout(c *check.C) {
  333. s.ds.OnTimeout(c)
  334. }
  335. func (s *DockerTrustSuite) SetUpTest(c *check.C) {
  336. testRequires(c, registry.Hosting, NotaryServerHosting)
  337. s.reg = setupRegistry(c, false, "", "")
  338. s.not = setupNotary(c)
  339. }
  340. func (s *DockerTrustSuite) TearDownTest(c *check.C) {
  341. if s.reg != nil {
  342. s.reg.Close()
  343. }
  344. if s.not != nil {
  345. s.not.Close()
  346. }
  347. // Remove trusted keys and metadata after test
  348. os.RemoveAll(filepath.Join(config.Dir(), "trust"))
  349. s.ds.TearDownTest(c)
  350. }
  351. func init() {
  352. ds := &DockerSuite{}
  353. check.Suite(&DockerTrustedSwarmSuite{
  354. trustSuite: DockerTrustSuite{
  355. ds: ds,
  356. },
  357. swarmSuite: DockerSwarmSuite{
  358. ds: ds,
  359. },
  360. })
  361. }
  362. type DockerTrustedSwarmSuite struct {
  363. swarmSuite DockerSwarmSuite
  364. trustSuite DockerTrustSuite
  365. reg *registry.V2
  366. not *testNotary
  367. }
  368. func (s *DockerTrustedSwarmSuite) SetUpTest(c *check.C) {
  369. s.swarmSuite.SetUpTest(c)
  370. s.trustSuite.SetUpTest(c)
  371. }
  372. func (s *DockerTrustedSwarmSuite) TearDownTest(c *check.C) {
  373. s.trustSuite.TearDownTest(c)
  374. s.swarmSuite.TearDownTest(c)
  375. }
  376. func (s *DockerTrustedSwarmSuite) OnTimeout(c *check.C) {
  377. s.swarmSuite.OnTimeout(c)
  378. }
  379. func init() {
  380. check.Suite(&DockerPluginSuite{
  381. ds: &DockerSuite{},
  382. })
  383. }
  384. type DockerPluginSuite struct {
  385. ds *DockerSuite
  386. registry *registry.V2
  387. }
  388. func (ps *DockerPluginSuite) registryHost() string {
  389. return privateRegistryURL
  390. }
  391. func (ps *DockerPluginSuite) getPluginRepo() string {
  392. return path.Join(ps.registryHost(), "plugin", "basic")
  393. }
  394. func (ps *DockerPluginSuite) getPluginRepoWithTag() string {
  395. return ps.getPluginRepo() + ":" + "latest"
  396. }
  397. func (ps *DockerPluginSuite) SetUpSuite(c *check.C) {
  398. testRequires(c, DaemonIsLinux)
  399. ps.registry = setupRegistry(c, false, "", "")
  400. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  401. defer cancel()
  402. err := plugin.CreateInRegistry(ctx, ps.getPluginRepo(), nil)
  403. c.Assert(err, checker.IsNil, check.Commentf("failed to create plugin"))
  404. }
  405. func (ps *DockerPluginSuite) TearDownSuite(c *check.C) {
  406. if ps.registry != nil {
  407. ps.registry.Close()
  408. }
  409. }
  410. func (ps *DockerPluginSuite) TearDownTest(c *check.C) {
  411. ps.ds.TearDownTest(c)
  412. }
  413. func (ps *DockerPluginSuite) OnTimeout(c *check.C) {
  414. ps.ds.OnTimeout(c)
  415. }