check_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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/swarm"
  13. cliconfig "github.com/docker/docker/cli/config"
  14. "github.com/docker/docker/integration-cli/daemon"
  15. "github.com/docker/docker/integration-cli/environment"
  16. "github.com/docker/docker/integration-cli/registry"
  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. )
  35. func init() {
  36. var err error
  37. reexec.Init() // This is required for external graphdriver tests
  38. testEnv, err = environment.New()
  39. if err != nil {
  40. fmt.Println(err)
  41. os.Exit(1)
  42. }
  43. }
  44. func TestMain(m *testing.M) {
  45. var err error
  46. if dockerBin := os.Getenv("DOCKER_BINARY"); dockerBin != "" {
  47. dockerBinary = dockerBin
  48. }
  49. dockerBinary, err = exec.LookPath(dockerBinary)
  50. if err != nil {
  51. fmt.Printf("ERROR: couldn't resolve full path to the Docker binary (%v)\n", err)
  52. os.Exit(1)
  53. }
  54. cmd := exec.Command(dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}")
  55. cmd.Env = appendBaseEnv(true)
  56. out, err := cmd.CombinedOutput()
  57. if err != nil {
  58. panic(fmt.Errorf("err=%v\nout=%s\n", err, out))
  59. }
  60. images := strings.Split(strings.TrimSpace(string(out)), "\n")
  61. for _, img := range images {
  62. protectedImages[img] = struct{}{}
  63. }
  64. if testEnv.LocalDaemon() {
  65. fmt.Println("INFO: Testing against a local daemon")
  66. } else {
  67. fmt.Println("INFO: Testing against a remote daemon")
  68. }
  69. exitCode := m.Run()
  70. os.Exit(exitCode)
  71. }
  72. func Test(t *testing.T) {
  73. if testEnv.DaemonPlatform() == "linux" {
  74. ensureFrozenImagesLinux(t)
  75. }
  76. check.TestingT(t)
  77. }
  78. func init() {
  79. check.Suite(&DockerSuite{})
  80. }
  81. type DockerSuite struct {
  82. }
  83. func (s *DockerSuite) OnTimeout(c *check.C) {
  84. if testEnv.DaemonPID() > 0 && testEnv.LocalDaemon() {
  85. daemon.SignalDaemonDump(testEnv.DaemonPID())
  86. }
  87. }
  88. func (s *DockerSuite) TearDownTest(c *check.C) {
  89. unpauseAllContainers(c)
  90. deleteAllContainers(c)
  91. deleteAllImages(c)
  92. deleteAllVolumes(c)
  93. deleteAllNetworks(c)
  94. if testEnv.DaemonPlatform() == "linux" {
  95. deleteAllPlugins(c)
  96. }
  97. }
  98. func init() {
  99. check.Suite(&DockerRegistrySuite{
  100. ds: &DockerSuite{},
  101. })
  102. }
  103. type DockerRegistrySuite struct {
  104. ds *DockerSuite
  105. reg *registry.V2
  106. d *daemon.Daemon
  107. }
  108. func (s *DockerRegistrySuite) OnTimeout(c *check.C) {
  109. s.d.DumpStackAndQuit()
  110. }
  111. func (s *DockerRegistrySuite) SetUpTest(c *check.C) {
  112. testRequires(c, DaemonIsLinux, registry.Hosting)
  113. s.reg = setupRegistry(c, false, "", "")
  114. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  115. Experimental: testEnv.ExperimentalDaemon(),
  116. })
  117. }
  118. func (s *DockerRegistrySuite) TearDownTest(c *check.C) {
  119. if s.reg != nil {
  120. s.reg.Close()
  121. }
  122. if s.d != nil {
  123. s.d.Stop(c)
  124. }
  125. s.ds.TearDownTest(c)
  126. }
  127. func init() {
  128. check.Suite(&DockerSchema1RegistrySuite{
  129. ds: &DockerSuite{},
  130. })
  131. }
  132. type DockerSchema1RegistrySuite struct {
  133. ds *DockerSuite
  134. reg *registry.V2
  135. d *daemon.Daemon
  136. }
  137. func (s *DockerSchema1RegistrySuite) OnTimeout(c *check.C) {
  138. s.d.DumpStackAndQuit()
  139. }
  140. func (s *DockerSchema1RegistrySuite) SetUpTest(c *check.C) {
  141. testRequires(c, DaemonIsLinux, registry.Hosting, NotArm64)
  142. s.reg = setupRegistry(c, true, "", "")
  143. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  144. Experimental: testEnv.ExperimentalDaemon(),
  145. })
  146. }
  147. func (s *DockerSchema1RegistrySuite) TearDownTest(c *check.C) {
  148. if s.reg != nil {
  149. s.reg.Close()
  150. }
  151. if s.d != nil {
  152. s.d.Stop(c)
  153. }
  154. s.ds.TearDownTest(c)
  155. }
  156. func init() {
  157. check.Suite(&DockerRegistryAuthHtpasswdSuite{
  158. ds: &DockerSuite{},
  159. })
  160. }
  161. type DockerRegistryAuthHtpasswdSuite struct {
  162. ds *DockerSuite
  163. reg *registry.V2
  164. d *daemon.Daemon
  165. }
  166. func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *check.C) {
  167. s.d.DumpStackAndQuit()
  168. }
  169. func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *check.C) {
  170. testRequires(c, DaemonIsLinux, registry.Hosting)
  171. s.reg = setupRegistry(c, false, "htpasswd", "")
  172. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  173. Experimental: testEnv.ExperimentalDaemon(),
  174. })
  175. }
  176. func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *check.C) {
  177. if s.reg != nil {
  178. out, err := s.d.Cmd("logout", privateRegistryURL)
  179. c.Assert(err, check.IsNil, check.Commentf(out))
  180. s.reg.Close()
  181. }
  182. if s.d != nil {
  183. s.d.Stop(c)
  184. }
  185. s.ds.TearDownTest(c)
  186. }
  187. func init() {
  188. check.Suite(&DockerRegistryAuthTokenSuite{
  189. ds: &DockerSuite{},
  190. })
  191. }
  192. type DockerRegistryAuthTokenSuite struct {
  193. ds *DockerSuite
  194. reg *registry.V2
  195. d *daemon.Daemon
  196. }
  197. func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *check.C) {
  198. s.d.DumpStackAndQuit()
  199. }
  200. func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *check.C) {
  201. testRequires(c, DaemonIsLinux, registry.Hosting)
  202. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  203. Experimental: testEnv.ExperimentalDaemon(),
  204. })
  205. }
  206. func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *check.C) {
  207. if s.reg != nil {
  208. out, err := s.d.Cmd("logout", privateRegistryURL)
  209. c.Assert(err, check.IsNil, check.Commentf(out))
  210. s.reg.Close()
  211. }
  212. if s.d != nil {
  213. s.d.Stop(c)
  214. }
  215. s.ds.TearDownTest(c)
  216. }
  217. func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *check.C, tokenURL string) {
  218. if s == nil {
  219. c.Fatal("registry suite isn't initialized")
  220. }
  221. s.reg = setupRegistry(c, false, "token", tokenURL)
  222. }
  223. func init() {
  224. check.Suite(&DockerDaemonSuite{
  225. ds: &DockerSuite{},
  226. })
  227. }
  228. type DockerDaemonSuite struct {
  229. ds *DockerSuite
  230. d *daemon.Daemon
  231. }
  232. func (s *DockerDaemonSuite) OnTimeout(c *check.C) {
  233. s.d.DumpStackAndQuit()
  234. }
  235. func (s *DockerDaemonSuite) SetUpTest(c *check.C) {
  236. testRequires(c, DaemonIsLinux, SameHostDaemon)
  237. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  238. Experimental: testEnv.ExperimentalDaemon(),
  239. })
  240. }
  241. func (s *DockerDaemonSuite) TearDownTest(c *check.C) {
  242. testRequires(c, DaemonIsLinux, SameHostDaemon)
  243. if s.d != nil {
  244. s.d.Stop(c)
  245. }
  246. s.ds.TearDownTest(c)
  247. }
  248. func (s *DockerDaemonSuite) TearDownSuite(c *check.C) {
  249. filepath.Walk(daemon.SockRoot, func(path string, fi os.FileInfo, err error) error {
  250. if err != nil {
  251. // ignore errors here
  252. // not cleaning up sockets is not really an error
  253. return nil
  254. }
  255. if fi.Mode() == os.ModeSocket {
  256. syscall.Unlink(path)
  257. }
  258. return nil
  259. })
  260. os.RemoveAll(daemon.SockRoot)
  261. }
  262. const defaultSwarmPort = 2477
  263. func init() {
  264. check.Suite(&DockerSwarmSuite{
  265. ds: &DockerSuite{},
  266. })
  267. }
  268. type DockerSwarmSuite struct {
  269. server *httptest.Server
  270. ds *DockerSuite
  271. daemons []*daemon.Swarm
  272. daemonsLock sync.Mutex // protect access to daemons
  273. portIndex int
  274. }
  275. func (s *DockerSwarmSuite) OnTimeout(c *check.C) {
  276. s.daemonsLock.Lock()
  277. defer s.daemonsLock.Unlock()
  278. for _, d := range s.daemons {
  279. d.DumpStackAndQuit()
  280. }
  281. }
  282. func (s *DockerSwarmSuite) SetUpTest(c *check.C) {
  283. testRequires(c, DaemonIsLinux)
  284. }
  285. func (s *DockerSwarmSuite) AddDaemon(c *check.C, joinSwarm, manager bool) *daemon.Swarm {
  286. d := &daemon.Swarm{
  287. Daemon: daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  288. Experimental: testEnv.ExperimentalDaemon(),
  289. }),
  290. Port: defaultSwarmPort + s.portIndex,
  291. }
  292. d.ListenAddr = fmt.Sprintf("0.0.0.0:%d", d.Port)
  293. args := []string{"--iptables=false", "--swarm-default-advertise-addr=lo"} // avoid networking conflicts
  294. d.StartWithBusybox(c, args...)
  295. if joinSwarm == true {
  296. if len(s.daemons) > 0 {
  297. tokens := s.daemons[0].JoinTokens(c)
  298. token := tokens.Worker
  299. if manager {
  300. token = tokens.Manager
  301. }
  302. c.Assert(d.Join(swarm.JoinRequest{
  303. RemoteAddrs: []string{s.daemons[0].ListenAddr},
  304. JoinToken: token,
  305. }), check.IsNil)
  306. } else {
  307. c.Assert(d.Init(swarm.InitRequest{}), check.IsNil)
  308. }
  309. }
  310. s.portIndex++
  311. s.daemonsLock.Lock()
  312. s.daemons = append(s.daemons, d)
  313. s.daemonsLock.Unlock()
  314. return d
  315. }
  316. func (s *DockerSwarmSuite) TearDownTest(c *check.C) {
  317. testRequires(c, DaemonIsLinux)
  318. s.daemonsLock.Lock()
  319. for _, d := range s.daemons {
  320. if d != nil {
  321. d.Stop(c)
  322. // FIXME(vdemeester) should be handled by SwarmDaemon ?
  323. // raft state file is quite big (64MB) so remove it after every test
  324. walDir := filepath.Join(d.Root, "swarm/raft/wal")
  325. if err := os.RemoveAll(walDir); err != nil {
  326. c.Logf("error removing %v: %v", walDir, err)
  327. }
  328. d.CleanupExecRoot(c)
  329. }
  330. }
  331. s.daemons = nil
  332. s.daemonsLock.Unlock()
  333. s.portIndex = 0
  334. s.ds.TearDownTest(c)
  335. }
  336. func init() {
  337. check.Suite(&DockerTrustSuite{
  338. ds: &DockerSuite{},
  339. })
  340. }
  341. type DockerTrustSuite struct {
  342. ds *DockerSuite
  343. reg *registry.V2
  344. not *testNotary
  345. }
  346. func (s *DockerTrustSuite) OnTimeout(c *check.C) {
  347. s.ds.OnTimeout(c)
  348. }
  349. func (s *DockerTrustSuite) SetUpTest(c *check.C) {
  350. testRequires(c, registry.Hosting, NotaryServerHosting)
  351. s.reg = setupRegistry(c, false, "", "")
  352. s.not = setupNotary(c)
  353. }
  354. func (s *DockerTrustSuite) TearDownTest(c *check.C) {
  355. if s.reg != nil {
  356. s.reg.Close()
  357. }
  358. if s.not != nil {
  359. s.not.Close()
  360. }
  361. // Remove trusted keys and metadata after test
  362. os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
  363. s.ds.TearDownTest(c)
  364. }
  365. func init() {
  366. ds := &DockerSuite{}
  367. check.Suite(&DockerTrustedSwarmSuite{
  368. trustSuite: DockerTrustSuite{
  369. ds: ds,
  370. },
  371. swarmSuite: DockerSwarmSuite{
  372. ds: ds,
  373. },
  374. })
  375. }
  376. type DockerTrustedSwarmSuite struct {
  377. swarmSuite DockerSwarmSuite
  378. trustSuite DockerTrustSuite
  379. reg *registry.V2
  380. not *testNotary
  381. }
  382. func (s *DockerTrustedSwarmSuite) SetUpTest(c *check.C) {
  383. s.swarmSuite.SetUpTest(c)
  384. s.trustSuite.SetUpTest(c)
  385. }
  386. func (s *DockerTrustedSwarmSuite) TearDownTest(c *check.C) {
  387. s.trustSuite.TearDownTest(c)
  388. s.swarmSuite.TearDownTest(c)
  389. }
  390. func (s *DockerTrustedSwarmSuite) OnTimeout(c *check.C) {
  391. s.swarmSuite.OnTimeout(c)
  392. }