check_test.go 11 KB

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