check_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "net/http/httptest"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "strconv"
  11. "sync"
  12. "syscall"
  13. "testing"
  14. "time"
  15. "github.com/docker/docker/integration-cli/cli"
  16. "github.com/docker/docker/integration-cli/daemon"
  17. "github.com/docker/docker/integration-cli/environment"
  18. "github.com/docker/docker/internal/test/suite"
  19. "github.com/docker/docker/pkg/reexec"
  20. testdaemon "github.com/docker/docker/testutil/daemon"
  21. ienv "github.com/docker/docker/testutil/environment"
  22. "github.com/docker/docker/testutil/fakestorage"
  23. "github.com/docker/docker/testutil/fixtures/plugin"
  24. "github.com/docker/docker/testutil/registry"
  25. "gotest.tools/v3/assert"
  26. )
  27. const (
  28. // the private registry to use for tests
  29. privateRegistryURL = registry.DefaultURL
  30. // path to containerd's ctr binary
  31. ctrBinary = "ctr"
  32. // the docker daemon binary to use
  33. dockerdBinary = "dockerd"
  34. )
  35. var (
  36. testEnv *environment.Execution
  37. // the docker client binary to use
  38. dockerBinary = ""
  39. testEnvOnce sync.Once
  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. flag.Parse()
  52. // Global set up
  53. dockerBinary = testEnv.DockerBinary()
  54. err := ienv.EnsureFrozenImagesLinux(&testEnv.Execution)
  55. if err != nil {
  56. fmt.Println(err)
  57. os.Exit(1)
  58. }
  59. testEnv.Print()
  60. os.Exit(m.Run())
  61. }
  62. func ensureTestEnvSetup(t *testing.T) {
  63. testEnvOnce.Do(func() {
  64. cli.SetTestEnvironment(testEnv)
  65. fakestorage.SetTestEnvironment(&testEnv.Execution)
  66. ienv.ProtectAll(t, &testEnv.Execution)
  67. })
  68. }
  69. func TestDockerSuite(t *testing.T) {
  70. ensureTestEnvSetup(t)
  71. suite.Run(t, &DockerSuite{})
  72. }
  73. func TestDockerRegistrySuite(t *testing.T) {
  74. ensureTestEnvSetup(t)
  75. suite.Run(t, &DockerRegistrySuite{ds: &DockerSuite{}})
  76. }
  77. func TestDockerSchema1RegistrySuite(t *testing.T) {
  78. ensureTestEnvSetup(t)
  79. suite.Run(t, &DockerSchema1RegistrySuite{ds: &DockerSuite{}})
  80. }
  81. func TestDockerRegistryAuthHtpasswdSuite(t *testing.T) {
  82. ensureTestEnvSetup(t)
  83. suite.Run(t, &DockerRegistryAuthHtpasswdSuite{ds: &DockerSuite{}})
  84. }
  85. func TestDockerRegistryAuthTokenSuite(t *testing.T) {
  86. ensureTestEnvSetup(t)
  87. suite.Run(t, &DockerRegistryAuthTokenSuite{ds: &DockerSuite{}})
  88. }
  89. func TestDockerDaemonSuite(t *testing.T) {
  90. ensureTestEnvSetup(t)
  91. suite.Run(t, &DockerDaemonSuite{ds: &DockerSuite{}})
  92. }
  93. func TestDockerSwarmSuite(t *testing.T) {
  94. ensureTestEnvSetup(t)
  95. suite.Run(t, &DockerSwarmSuite{ds: &DockerSuite{}})
  96. }
  97. func TestDockerPluginSuite(t *testing.T) {
  98. ensureTestEnvSetup(t)
  99. suite.Run(t, &DockerPluginSuite{ds: &DockerSuite{}})
  100. }
  101. func TestDockerExternalVolumeSuite(t *testing.T) {
  102. ensureTestEnvSetup(t)
  103. testRequires(t, DaemonIsLinux)
  104. suite.Run(t, &DockerExternalVolumeSuite{ds: &DockerSuite{}})
  105. }
  106. func TestDockerNetworkSuite(t *testing.T) {
  107. ensureTestEnvSetup(t)
  108. testRequires(t, DaemonIsLinux)
  109. suite.Run(t, &DockerNetworkSuite{ds: &DockerSuite{}})
  110. }
  111. func TestDockerHubPullSuite(t *testing.T) {
  112. ensureTestEnvSetup(t)
  113. // FIXME. Temporarily turning this off for Windows as GH16039 was breaking
  114. // Windows to Linux CI @icecrime
  115. testRequires(t, DaemonIsLinux)
  116. suite.Run(t, newDockerHubPullSuite())
  117. }
  118. type DockerSuite struct {
  119. }
  120. func (s *DockerSuite) OnTimeout(c *testing.T) {
  121. if testEnv.IsRemoteDaemon() {
  122. return
  123. }
  124. path := filepath.Join(os.Getenv("DEST"), "docker.pid")
  125. b, err := os.ReadFile(path)
  126. if err != nil {
  127. c.Fatalf("Failed to get daemon PID from %s\n", path)
  128. }
  129. rawPid, err := strconv.ParseInt(string(b), 10, 32)
  130. if err != nil {
  131. c.Fatalf("Failed to parse pid from %s: %s\n", path, err)
  132. }
  133. daemonPid := int(rawPid)
  134. if daemonPid > 0 {
  135. testdaemon.SignalDaemonDump(daemonPid)
  136. }
  137. }
  138. func (s *DockerSuite) TearDownTest(c *testing.T) {
  139. testEnv.Clean(c)
  140. }
  141. type DockerRegistrySuite struct {
  142. ds *DockerSuite
  143. reg *registry.V2
  144. d *daemon.Daemon
  145. }
  146. func (s *DockerRegistrySuite) OnTimeout(c *testing.T) {
  147. s.d.DumpStackAndQuit()
  148. }
  149. func (s *DockerRegistrySuite) SetUpTest(c *testing.T) {
  150. testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
  151. s.reg = registry.NewV2(c)
  152. s.reg.WaitReady(c)
  153. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  154. }
  155. func (s *DockerRegistrySuite) TearDownTest(c *testing.T) {
  156. if s.reg != nil {
  157. s.reg.Close()
  158. }
  159. if s.d != nil {
  160. s.d.Stop(c)
  161. }
  162. s.ds.TearDownTest(c)
  163. }
  164. type DockerSchema1RegistrySuite struct {
  165. ds *DockerSuite
  166. reg *registry.V2
  167. d *daemon.Daemon
  168. }
  169. func (s *DockerSchema1RegistrySuite) OnTimeout(c *testing.T) {
  170. s.d.DumpStackAndQuit()
  171. }
  172. func (s *DockerSchema1RegistrySuite) SetUpTest(c *testing.T) {
  173. testRequires(c, DaemonIsLinux, RegistryHosting, NotArm64, testEnv.IsLocalDaemon)
  174. s.reg = registry.NewV2(c, registry.Schema1)
  175. s.reg.WaitReady(c)
  176. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  177. }
  178. func (s *DockerSchema1RegistrySuite) TearDownTest(c *testing.T) {
  179. if s.reg != nil {
  180. s.reg.Close()
  181. }
  182. if s.d != nil {
  183. s.d.Stop(c)
  184. }
  185. s.ds.TearDownTest(c)
  186. }
  187. type DockerRegistryAuthHtpasswdSuite struct {
  188. ds *DockerSuite
  189. reg *registry.V2
  190. d *daemon.Daemon
  191. }
  192. func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *testing.T) {
  193. s.d.DumpStackAndQuit()
  194. }
  195. func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *testing.T) {
  196. testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
  197. s.reg = registry.NewV2(c, registry.Htpasswd)
  198. s.reg.WaitReady(c)
  199. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  200. }
  201. func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *testing.T) {
  202. if s.reg != nil {
  203. out, err := s.d.Cmd("logout", privateRegistryURL)
  204. assert.NilError(c, err, out)
  205. s.reg.Close()
  206. }
  207. if s.d != nil {
  208. s.d.Stop(c)
  209. }
  210. s.ds.TearDownTest(c)
  211. }
  212. type DockerRegistryAuthTokenSuite struct {
  213. ds *DockerSuite
  214. reg *registry.V2
  215. d *daemon.Daemon
  216. }
  217. func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *testing.T) {
  218. s.d.DumpStackAndQuit()
  219. }
  220. func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *testing.T) {
  221. testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
  222. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  223. }
  224. func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *testing.T) {
  225. if s.reg != nil {
  226. out, err := s.d.Cmd("logout", privateRegistryURL)
  227. assert.NilError(c, err, out)
  228. s.reg.Close()
  229. }
  230. if s.d != nil {
  231. s.d.Stop(c)
  232. }
  233. s.ds.TearDownTest(c)
  234. }
  235. func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *testing.T, tokenURL string) {
  236. if s == nil {
  237. c.Fatal("registry suite isn't initialized")
  238. }
  239. s.reg = registry.NewV2(c, registry.Token(tokenURL))
  240. s.reg.WaitReady(c)
  241. }
  242. type DockerDaemonSuite struct {
  243. ds *DockerSuite
  244. d *daemon.Daemon
  245. }
  246. func (s *DockerDaemonSuite) OnTimeout(c *testing.T) {
  247. s.d.DumpStackAndQuit()
  248. }
  249. func (s *DockerDaemonSuite) SetUpTest(c *testing.T) {
  250. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  251. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  252. }
  253. func (s *DockerDaemonSuite) TearDownTest(c *testing.T) {
  254. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  255. if s.d != nil {
  256. s.d.Stop(c)
  257. }
  258. s.ds.TearDownTest(c)
  259. }
  260. func (s *DockerDaemonSuite) TearDownSuite(c *testing.T) {
  261. filepath.Walk(testdaemon.SockRoot, func(path string, fi os.FileInfo, err error) error {
  262. if err != nil {
  263. // ignore errors here
  264. // not cleaning up sockets is not really an error
  265. return nil
  266. }
  267. if fi.Mode() == os.ModeSocket {
  268. syscall.Unlink(path)
  269. }
  270. return nil
  271. })
  272. os.RemoveAll(testdaemon.SockRoot)
  273. }
  274. const defaultSwarmPort = 2477
  275. type DockerSwarmSuite struct {
  276. server *httptest.Server
  277. ds *DockerSuite
  278. daemonsLock sync.Mutex // protect access to daemons and portIndex
  279. daemons []*daemon.Daemon
  280. portIndex int
  281. }
  282. func (s *DockerSwarmSuite) OnTimeout(c *testing.T) {
  283. s.daemonsLock.Lock()
  284. defer s.daemonsLock.Unlock()
  285. for _, d := range s.daemons {
  286. d.DumpStackAndQuit()
  287. }
  288. }
  289. func (s *DockerSwarmSuite) SetUpTest(c *testing.T) {
  290. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  291. }
  292. func (s *DockerSwarmSuite) AddDaemon(c *testing.T, joinSwarm, manager bool) *daemon.Daemon {
  293. c.Helper()
  294. d := daemon.New(c, dockerBinary, dockerdBinary,
  295. testdaemon.WithEnvironment(testEnv.Execution),
  296. testdaemon.WithSwarmPort(defaultSwarmPort+s.portIndex),
  297. )
  298. if joinSwarm {
  299. if len(s.daemons) > 0 {
  300. d.StartAndSwarmJoin(c, s.daemons[0].Daemon, manager)
  301. } else {
  302. d.StartAndSwarmInit(c)
  303. }
  304. } else {
  305. d.StartNodeWithBusybox(c)
  306. }
  307. s.daemonsLock.Lock()
  308. s.portIndex++
  309. s.daemons = append(s.daemons, d)
  310. s.daemonsLock.Unlock()
  311. return d
  312. }
  313. func (s *DockerSwarmSuite) TearDownTest(c *testing.T) {
  314. testRequires(c, DaemonIsLinux)
  315. s.daemonsLock.Lock()
  316. for _, d := range s.daemons {
  317. if d != nil {
  318. d.Stop(c)
  319. d.Cleanup(c)
  320. }
  321. }
  322. s.daemons = nil
  323. s.portIndex = 0
  324. s.daemonsLock.Unlock()
  325. s.ds.TearDownTest(c)
  326. }
  327. type DockerPluginSuite struct {
  328. ds *DockerSuite
  329. registry *registry.V2
  330. }
  331. func (ps *DockerPluginSuite) registryHost() string {
  332. return privateRegistryURL
  333. }
  334. func (ps *DockerPluginSuite) getPluginRepo() string {
  335. return path.Join(ps.registryHost(), "plugin", "basic")
  336. }
  337. func (ps *DockerPluginSuite) getPluginRepoWithTag() string {
  338. return ps.getPluginRepo() + ":" + "latest"
  339. }
  340. func (ps *DockerPluginSuite) SetUpSuite(c *testing.T) {
  341. testRequires(c, DaemonIsLinux, RegistryHosting)
  342. ps.registry = registry.NewV2(c)
  343. ps.registry.WaitReady(c)
  344. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  345. defer cancel()
  346. err := plugin.CreateInRegistry(ctx, ps.getPluginRepo(), nil)
  347. assert.NilError(c, err, "failed to create plugin")
  348. }
  349. func (ps *DockerPluginSuite) TearDownSuite(c *testing.T) {
  350. if ps.registry != nil {
  351. ps.registry.Close()
  352. }
  353. }
  354. func (ps *DockerPluginSuite) TearDownTest(c *testing.T) {
  355. ps.ds.TearDownTest(c)
  356. }
  357. func (ps *DockerPluginSuite) OnTimeout(c *testing.T) {
  358. ps.ds.OnTimeout(c)
  359. }