check_test.go 11 KB

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