check_test.go 9.5 KB

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