check_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. package main
  2. import (
  3. "fmt"
  4. "net/http/httptest"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "strings"
  9. "sync"
  10. "syscall"
  11. "testing"
  12. "github.com/docker/docker/api/types/container"
  13. "github.com/docker/docker/api/types/swarm"
  14. cliconfig "github.com/docker/docker/cli/config"
  15. "github.com/docker/docker/integration-cli/daemon"
  16. "github.com/docker/docker/integration-cli/environment"
  17. "github.com/docker/docker/pkg/reexec"
  18. "github.com/go-check/check"
  19. )
  20. const (
  21. // the private registry to use for tests
  22. privateRegistryURL = "127.0.0.1:5000"
  23. // path to containerd's ctr binary
  24. ctrBinary = "docker-containerd-ctr"
  25. // the docker daemon binary to use
  26. dockerdBinary = "dockerd"
  27. )
  28. var (
  29. testEnv *environment.Execution
  30. // FIXME(vdemeester) remove these and use environmentdaemonPid
  31. protectedImages = map[string]struct{}{}
  32. // the docker client binary to use
  33. dockerBinary = "docker"
  34. // isLocalDaemon is true if the daemon under test is on the same
  35. // host as the CLI.
  36. isLocalDaemon bool
  37. // daemonPlatform is held globally so that tests can make intelligent
  38. // decisions on how to configure themselves according to the platform
  39. // of the daemon. This is initialized in docker_utils by sending
  40. // a version call to the daemon and examining the response header.
  41. daemonPlatform string
  42. // WindowsBaseImage is the name of the base image for Windows testing
  43. // Environment variable WINDOWS_BASE_IMAGE can override this
  44. WindowsBaseImage string
  45. // For a local daemon on Linux, these values will be used for testing
  46. // user namespace support as the standard graph path(s) will be
  47. // appended with the root remapped uid.gid prefix
  48. dockerBasePath string
  49. volumesConfigPath string
  50. containerStoragePath string
  51. // daemonStorageDriver is held globally so that tests can know the storage
  52. // driver of the daemon. This is initialized in docker_utils by sending
  53. // a version call to the daemon and examining the response header.
  54. daemonStorageDriver string
  55. // isolation is the isolation mode of the daemon under test
  56. isolation container.Isolation
  57. // experimentalDaemon tell whether the main daemon has
  58. // experimental features enabled or not
  59. experimentalDaemon bool
  60. daemonKernelVersion string
  61. )
  62. func init() {
  63. var err error
  64. reexec.Init() // This is required for external graphdriver tests
  65. testEnv, err = environment.New()
  66. if err != nil {
  67. fmt.Println(err)
  68. os.Exit(1)
  69. }
  70. assignGlobalVariablesFromTestEnv(testEnv)
  71. }
  72. // FIXME(vdemeester) remove this and use environment
  73. func assignGlobalVariablesFromTestEnv(testEnv *environment.Execution) {
  74. isLocalDaemon = testEnv.LocalDaemon()
  75. daemonPlatform = testEnv.DaemonPlatform()
  76. dockerBasePath = testEnv.DockerBasePath()
  77. volumesConfigPath = testEnv.VolumesConfigPath()
  78. containerStoragePath = testEnv.ContainerStoragePath()
  79. daemonStorageDriver = testEnv.DaemonStorageDriver()
  80. isolation = testEnv.Isolation()
  81. experimentalDaemon = testEnv.ExperimentalDaemon()
  82. daemonKernelVersion = testEnv.DaemonKernelVersion()
  83. WindowsBaseImage = testEnv.MinimalBaseImage()
  84. }
  85. func TestMain(m *testing.M) {
  86. var err error
  87. if dockerBin := os.Getenv("DOCKER_BINARY"); dockerBin != "" {
  88. dockerBinary = dockerBin
  89. }
  90. dockerBinary, err = exec.LookPath(dockerBinary)
  91. if err != nil {
  92. fmt.Printf("ERROR: couldn't resolve full path to the Docker binary (%v)\n", err)
  93. os.Exit(1)
  94. }
  95. cmd := exec.Command(dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}")
  96. cmd.Env = appendBaseEnv(true)
  97. out, err := cmd.CombinedOutput()
  98. if err != nil {
  99. panic(fmt.Errorf("err=%v\nout=%s\n", err, out))
  100. }
  101. images := strings.Split(strings.TrimSpace(string(out)), "\n")
  102. for _, img := range images {
  103. protectedImages[img] = struct{}{}
  104. }
  105. if !isLocalDaemon {
  106. fmt.Println("INFO: Testing against a remote daemon")
  107. } else {
  108. fmt.Println("INFO: Testing against a local daemon")
  109. }
  110. exitCode := m.Run()
  111. os.Exit(exitCode)
  112. }
  113. func Test(t *testing.T) {
  114. if daemonPlatform == "linux" {
  115. ensureFrozenImagesLinux(t)
  116. }
  117. check.TestingT(t)
  118. }
  119. func init() {
  120. check.Suite(&DockerSuite{})
  121. }
  122. type DockerSuite struct {
  123. }
  124. func (s *DockerSuite) OnTimeout(c *check.C) {
  125. if testEnv.DaemonPID() > 0 && isLocalDaemon {
  126. daemon.SignalDaemonDump(testEnv.DaemonPID())
  127. }
  128. }
  129. func (s *DockerSuite) TearDownTest(c *check.C) {
  130. unpauseAllContainers(c)
  131. deleteAllContainers(c)
  132. deleteAllImages(c)
  133. deleteAllVolumes(c)
  134. deleteAllNetworks(c)
  135. if daemonPlatform == "linux" {
  136. deleteAllPlugins(c)
  137. }
  138. }
  139. func init() {
  140. check.Suite(&DockerRegistrySuite{
  141. ds: &DockerSuite{},
  142. })
  143. }
  144. type DockerRegistrySuite struct {
  145. ds *DockerSuite
  146. reg *testRegistryV2
  147. d *daemon.Daemon
  148. }
  149. func (s *DockerRegistrySuite) OnTimeout(c *check.C) {
  150. s.d.DumpStackAndQuit()
  151. }
  152. func (s *DockerRegistrySuite) SetUpTest(c *check.C) {
  153. testRequires(c, DaemonIsLinux, RegistryHosting)
  154. s.reg = setupRegistry(c, false, "", "")
  155. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  156. Experimental: experimentalDaemon,
  157. })
  158. }
  159. func (s *DockerRegistrySuite) TearDownTest(c *check.C) {
  160. if s.reg != nil {
  161. s.reg.Close()
  162. }
  163. if s.d != nil {
  164. s.d.Stop(c)
  165. }
  166. s.ds.TearDownTest(c)
  167. }
  168. func init() {
  169. check.Suite(&DockerSchema1RegistrySuite{
  170. ds: &DockerSuite{},
  171. })
  172. }
  173. type DockerSchema1RegistrySuite struct {
  174. ds *DockerSuite
  175. reg *testRegistryV2
  176. d *daemon.Daemon
  177. }
  178. func (s *DockerSchema1RegistrySuite) OnTimeout(c *check.C) {
  179. s.d.DumpStackAndQuit()
  180. }
  181. func (s *DockerSchema1RegistrySuite) SetUpTest(c *check.C) {
  182. testRequires(c, DaemonIsLinux, RegistryHosting, NotArm64)
  183. s.reg = setupRegistry(c, true, "", "")
  184. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  185. Experimental: experimentalDaemon,
  186. })
  187. }
  188. func (s *DockerSchema1RegistrySuite) TearDownTest(c *check.C) {
  189. if s.reg != nil {
  190. s.reg.Close()
  191. }
  192. if s.d != nil {
  193. s.d.Stop(c)
  194. }
  195. s.ds.TearDownTest(c)
  196. }
  197. func init() {
  198. check.Suite(&DockerRegistryAuthHtpasswdSuite{
  199. ds: &DockerSuite{},
  200. })
  201. }
  202. type DockerRegistryAuthHtpasswdSuite struct {
  203. ds *DockerSuite
  204. reg *testRegistryV2
  205. d *daemon.Daemon
  206. }
  207. func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *check.C) {
  208. s.d.DumpStackAndQuit()
  209. }
  210. func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *check.C) {
  211. testRequires(c, DaemonIsLinux, RegistryHosting)
  212. s.reg = setupRegistry(c, false, "htpasswd", "")
  213. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  214. Experimental: experimentalDaemon,
  215. })
  216. }
  217. func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *check.C) {
  218. if s.reg != nil {
  219. out, err := s.d.Cmd("logout", privateRegistryURL)
  220. c.Assert(err, check.IsNil, check.Commentf(out))
  221. s.reg.Close()
  222. }
  223. if s.d != nil {
  224. s.d.Stop(c)
  225. }
  226. s.ds.TearDownTest(c)
  227. }
  228. func init() {
  229. check.Suite(&DockerRegistryAuthTokenSuite{
  230. ds: &DockerSuite{},
  231. })
  232. }
  233. type DockerRegistryAuthTokenSuite struct {
  234. ds *DockerSuite
  235. reg *testRegistryV2
  236. d *daemon.Daemon
  237. }
  238. func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *check.C) {
  239. s.d.DumpStackAndQuit()
  240. }
  241. func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *check.C) {
  242. testRequires(c, DaemonIsLinux, RegistryHosting)
  243. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  244. Experimental: experimentalDaemon,
  245. })
  246. }
  247. func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *check.C) {
  248. if s.reg != nil {
  249. out, err := s.d.Cmd("logout", privateRegistryURL)
  250. c.Assert(err, check.IsNil, check.Commentf(out))
  251. s.reg.Close()
  252. }
  253. if s.d != nil {
  254. s.d.Stop(c)
  255. }
  256. s.ds.TearDownTest(c)
  257. }
  258. func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *check.C, tokenURL string) {
  259. if s == nil {
  260. c.Fatal("registry suite isn't initialized")
  261. }
  262. s.reg = setupRegistry(c, false, "token", tokenURL)
  263. }
  264. func init() {
  265. check.Suite(&DockerDaemonSuite{
  266. ds: &DockerSuite{},
  267. })
  268. }
  269. type DockerDaemonSuite struct {
  270. ds *DockerSuite
  271. d *daemon.Daemon
  272. }
  273. func (s *DockerDaemonSuite) OnTimeout(c *check.C) {
  274. s.d.DumpStackAndQuit()
  275. }
  276. func (s *DockerDaemonSuite) SetUpTest(c *check.C) {
  277. testRequires(c, DaemonIsLinux, SameHostDaemon)
  278. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  279. Experimental: experimentalDaemon,
  280. })
  281. }
  282. func (s *DockerDaemonSuite) TearDownTest(c *check.C) {
  283. testRequires(c, DaemonIsLinux, SameHostDaemon)
  284. if s.d != nil {
  285. s.d.Stop(c)
  286. }
  287. s.ds.TearDownTest(c)
  288. }
  289. func (s *DockerDaemonSuite) TearDownSuite(c *check.C) {
  290. filepath.Walk(daemon.SockRoot, func(path string, fi os.FileInfo, err error) error {
  291. if err != nil {
  292. // ignore errors here
  293. // not cleaning up sockets is not really an error
  294. return nil
  295. }
  296. if fi.Mode() == os.ModeSocket {
  297. syscall.Unlink(path)
  298. }
  299. return nil
  300. })
  301. os.RemoveAll(daemon.SockRoot)
  302. }
  303. const defaultSwarmPort = 2477
  304. func init() {
  305. check.Suite(&DockerSwarmSuite{
  306. ds: &DockerSuite{},
  307. })
  308. }
  309. type DockerSwarmSuite struct {
  310. server *httptest.Server
  311. ds *DockerSuite
  312. daemons []*daemon.Swarm
  313. daemonsLock sync.Mutex // protect access to daemons
  314. portIndex int
  315. }
  316. func (s *DockerSwarmSuite) OnTimeout(c *check.C) {
  317. s.daemonsLock.Lock()
  318. defer s.daemonsLock.Unlock()
  319. for _, d := range s.daemons {
  320. d.DumpStackAndQuit()
  321. }
  322. }
  323. func (s *DockerSwarmSuite) SetUpTest(c *check.C) {
  324. testRequires(c, DaemonIsLinux)
  325. }
  326. func (s *DockerSwarmSuite) AddDaemon(c *check.C, joinSwarm, manager bool) *daemon.Swarm {
  327. d := &daemon.Swarm{
  328. Daemon: daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  329. Experimental: experimentalDaemon,
  330. }),
  331. Port: defaultSwarmPort + s.portIndex,
  332. }
  333. d.ListenAddr = fmt.Sprintf("0.0.0.0:%d", d.Port)
  334. args := []string{"--iptables=false", "--swarm-default-advertise-addr=lo"} // avoid networking conflicts
  335. d.StartWithBusybox(c, args...)
  336. if joinSwarm == true {
  337. if len(s.daemons) > 0 {
  338. tokens := s.daemons[0].JoinTokens(c)
  339. token := tokens.Worker
  340. if manager {
  341. token = tokens.Manager
  342. }
  343. c.Assert(d.Join(swarm.JoinRequest{
  344. RemoteAddrs: []string{s.daemons[0].ListenAddr},
  345. JoinToken: token,
  346. }), check.IsNil)
  347. } else {
  348. c.Assert(d.Init(swarm.InitRequest{}), check.IsNil)
  349. }
  350. }
  351. s.portIndex++
  352. s.daemonsLock.Lock()
  353. s.daemons = append(s.daemons, d)
  354. s.daemonsLock.Unlock()
  355. return d
  356. }
  357. func (s *DockerSwarmSuite) TearDownTest(c *check.C) {
  358. testRequires(c, DaemonIsLinux)
  359. s.daemonsLock.Lock()
  360. for _, d := range s.daemons {
  361. if d != nil {
  362. d.Stop(c)
  363. // FIXME(vdemeester) should be handled by SwarmDaemon ?
  364. // raft state file is quite big (64MB) so remove it after every test
  365. walDir := filepath.Join(d.Root, "swarm/raft/wal")
  366. if err := os.RemoveAll(walDir); err != nil {
  367. c.Logf("error removing %v: %v", walDir, err)
  368. }
  369. d.CleanupExecRoot(c)
  370. }
  371. }
  372. s.daemons = nil
  373. s.daemonsLock.Unlock()
  374. s.portIndex = 0
  375. s.ds.TearDownTest(c)
  376. }
  377. func init() {
  378. check.Suite(&DockerTrustSuite{
  379. ds: &DockerSuite{},
  380. })
  381. }
  382. type DockerTrustSuite struct {
  383. ds *DockerSuite
  384. reg *testRegistryV2
  385. not *testNotary
  386. }
  387. func (s *DockerTrustSuite) SetUpTest(c *check.C) {
  388. testRequires(c, RegistryHosting, NotaryServerHosting)
  389. s.reg = setupRegistry(c, false, "", "")
  390. s.not = setupNotary(c)
  391. }
  392. func (s *DockerTrustSuite) TearDownTest(c *check.C) {
  393. if s.reg != nil {
  394. s.reg.Close()
  395. }
  396. if s.not != nil {
  397. s.not.Close()
  398. }
  399. // Remove trusted keys and metadata after test
  400. os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
  401. s.ds.TearDownTest(c)
  402. }
  403. func init() {
  404. ds := &DockerSuite{}
  405. check.Suite(&DockerTrustedSwarmSuite{
  406. trustSuite: DockerTrustSuite{
  407. ds: ds,
  408. },
  409. swarmSuite: DockerSwarmSuite{
  410. ds: ds,
  411. },
  412. })
  413. }
  414. type DockerTrustedSwarmSuite struct {
  415. swarmSuite DockerSwarmSuite
  416. trustSuite DockerTrustSuite
  417. reg *testRegistryV2
  418. not *testNotary
  419. }
  420. func (s *DockerTrustedSwarmSuite) SetUpTest(c *check.C) {
  421. s.swarmSuite.SetUpTest(c)
  422. s.trustSuite.SetUpTest(c)
  423. }
  424. func (s *DockerTrustedSwarmSuite) TearDownTest(c *check.C) {
  425. s.trustSuite.TearDownTest(c)
  426. s.swarmSuite.TearDownTest(c)
  427. }
  428. func (s *DockerTrustedSwarmSuite) OnTimeout(c *check.C) {
  429. s.swarmSuite.OnTimeout(c)
  430. }