check_test.go 9.5 KB

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