check_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. // the docker client binary to use
  31. dockerBinary = "docker"
  32. )
  33. func init() {
  34. var err error
  35. reexec.Init() // This is required for external graphdriver tests
  36. testEnv, err = environment.New()
  37. if err != nil {
  38. fmt.Println(err)
  39. os.Exit(1)
  40. }
  41. }
  42. func TestMain(m *testing.M) {
  43. var err error
  44. if dockerBin := os.Getenv("DOCKER_BINARY"); dockerBin != "" {
  45. dockerBinary = dockerBin
  46. }
  47. dockerBinary, err = exec.LookPath(dockerBinary)
  48. if err != nil {
  49. fmt.Printf("ERROR: couldn't resolve full path to the Docker binary (%v)\n", err)
  50. os.Exit(1)
  51. }
  52. if testEnv.LocalDaemon() {
  53. fmt.Println("INFO: Testing against a local daemon")
  54. } else {
  55. fmt.Println("INFO: Testing against a remote daemon")
  56. }
  57. exitCode := m.Run()
  58. os.Exit(exitCode)
  59. }
  60. func Test(t *testing.T) {
  61. cmd := exec.Command(dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}")
  62. cmd.Env = appendBaseEnv(true)
  63. out, err := cmd.CombinedOutput()
  64. if err != nil {
  65. panic(fmt.Errorf("err=%v\nout=%s\n", err, out))
  66. }
  67. images := strings.Split(strings.TrimSpace(string(out)), "\n")
  68. testEnv.ProtectImage(t, images...)
  69. if testEnv.DaemonPlatform() == "linux" {
  70. ensureFrozenImagesLinux(t)
  71. }
  72. check.TestingT(t)
  73. }
  74. func init() {
  75. check.Suite(&DockerSuite{})
  76. }
  77. type DockerSuite struct {
  78. }
  79. func (s *DockerSuite) OnTimeout(c *check.C) {
  80. if testEnv.DaemonPID() > 0 && testEnv.LocalDaemon() {
  81. daemon.SignalDaemonDump(testEnv.DaemonPID())
  82. }
  83. }
  84. func (s *DockerSuite) TearDownTest(c *check.C) {
  85. testEnv.Clean(c, dockerBinary)
  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)
  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)
  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)
  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)
  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)
  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(cliconfig.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. }