check_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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/testutil"
  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. "go.opentelemetry.io/otel"
  26. "go.opentelemetry.io/otel/attribute"
  27. "go.opentelemetry.io/otel/codes"
  28. "gotest.tools/v3/assert"
  29. )
  30. const (
  31. // the private registry to use for tests
  32. privateRegistryURL = registry.DefaultURL
  33. // path to containerd's ctr binary
  34. ctrBinary = "ctr"
  35. // the docker daemon binary to use
  36. dockerdBinary = "dockerd"
  37. )
  38. var (
  39. testEnvOnce sync.Once
  40. testEnv *environment.Execution
  41. // the docker client binary to use
  42. dockerBinary = ""
  43. baseContext context.Context
  44. )
  45. func TestMain(m *testing.M) {
  46. flag.Parse()
  47. os.Exit(testRun(m))
  48. }
  49. func testRun(m *testing.M) (ret int) {
  50. // Global set up
  51. var err error
  52. shutdown := testutil.ConfigureTracing()
  53. ctx, span := otel.Tracer("").Start(context.Background(), "integration-cli/TestMain")
  54. defer func() {
  55. if err != nil {
  56. span.SetStatus(codes.Error, err.Error())
  57. ret = 255
  58. } else {
  59. if ret != 0 {
  60. span.SetAttributes(attribute.Int("exitCode", ret))
  61. span.SetStatus(codes.Error, "m.Run() exited with non-zero code")
  62. }
  63. }
  64. span.End()
  65. shutdown(ctx)
  66. }()
  67. baseContext = ctx
  68. testEnv, err = environment.New(ctx)
  69. if err != nil {
  70. return
  71. }
  72. if testEnv.IsLocalDaemon() {
  73. setupLocalInfo()
  74. }
  75. dockerBinary = testEnv.DockerBinary()
  76. err = ienv.EnsureFrozenImagesLinux(ctx, &testEnv.Execution)
  77. if err != nil {
  78. return
  79. }
  80. testEnv.Print()
  81. printCliVersion()
  82. return m.Run()
  83. }
  84. func printCliVersion() {
  85. // Print output of "docker version"
  86. cli.SetTestEnvironment(testEnv)
  87. cmd := cli.Docker(cli.Args("version"))
  88. if cmd.Error != nil {
  89. fmt.Printf("WARNING: Failed to run 'docker version': %+v\n", cmd.Error)
  90. return
  91. }
  92. fmt.Println("INFO: Testing with docker cli version:")
  93. fmt.Println(cmd.Stdout())
  94. }
  95. func ensureTestEnvSetup(ctx context.Context, t *testing.T) {
  96. testEnvOnce.Do(func() {
  97. cli.SetTestEnvironment(testEnv)
  98. fakestorage.SetTestEnvironment(&testEnv.Execution)
  99. ienv.ProtectAll(ctx, t, &testEnv.Execution)
  100. })
  101. }
  102. func TestDockerAPISuite(t *testing.T) {
  103. ctx := testutil.StartSpan(baseContext, t)
  104. ensureTestEnvSetup(ctx, t)
  105. suite.Run(ctx, t, &DockerAPISuite{ds: &DockerSuite{}})
  106. }
  107. func TestDockerBenchmarkSuite(t *testing.T) {
  108. ctx := testutil.StartSpan(baseContext, t)
  109. ensureTestEnvSetup(ctx, t)
  110. suite.Run(ctx, t, &DockerBenchmarkSuite{ds: &DockerSuite{}})
  111. }
  112. func TestDockerCLIAttachSuite(t *testing.T) {
  113. ctx := testutil.StartSpan(baseContext, t)
  114. ensureTestEnvSetup(ctx, t)
  115. suite.Run(ctx, t, &DockerCLIAttachSuite{ds: &DockerSuite{}})
  116. }
  117. func TestDockerCLIBuildSuite(t *testing.T) {
  118. ctx := testutil.StartSpan(baseContext, t)
  119. ensureTestEnvSetup(ctx, t)
  120. suite.Run(ctx, t, &DockerCLIBuildSuite{ds: &DockerSuite{}})
  121. }
  122. func TestDockerCLICommitSuite(t *testing.T) {
  123. ctx := testutil.StartSpan(baseContext, t)
  124. ensureTestEnvSetup(ctx, t)
  125. suite.Run(ctx, t, &DockerCLICommitSuite{ds: &DockerSuite{}})
  126. }
  127. func TestDockerCLICpSuite(t *testing.T) {
  128. ctx := testutil.StartSpan(baseContext, t)
  129. ensureTestEnvSetup(ctx, t)
  130. suite.Run(ctx, t, &DockerCLICpSuite{ds: &DockerSuite{}})
  131. }
  132. func TestDockerCLICreateSuite(t *testing.T) {
  133. ctx := testutil.StartSpan(baseContext, t)
  134. ensureTestEnvSetup(ctx, t)
  135. suite.Run(ctx, t, &DockerCLICreateSuite{ds: &DockerSuite{}})
  136. }
  137. func TestDockerCLIEventSuite(t *testing.T) {
  138. ctx := testutil.StartSpan(baseContext, t)
  139. ensureTestEnvSetup(ctx, t)
  140. suite.Run(ctx, t, &DockerCLIEventSuite{ds: &DockerSuite{}})
  141. }
  142. func TestDockerCLIExecSuite(t *testing.T) {
  143. ctx := testutil.StartSpan(baseContext, t)
  144. ensureTestEnvSetup(ctx, t)
  145. suite.Run(ctx, t, &DockerCLIExecSuite{ds: &DockerSuite{}})
  146. }
  147. func TestDockerCLIHealthSuite(t *testing.T) {
  148. ctx := testutil.StartSpan(baseContext, t)
  149. ensureTestEnvSetup(ctx, t)
  150. suite.Run(ctx, t, &DockerCLIHealthSuite{ds: &DockerSuite{}})
  151. }
  152. func TestDockerCLIHistorySuite(t *testing.T) {
  153. ctx := testutil.StartSpan(baseContext, t)
  154. ensureTestEnvSetup(ctx, t)
  155. suite.Run(ctx, t, &DockerCLIHistorySuite{ds: &DockerSuite{}})
  156. }
  157. func TestDockerCLIImagesSuite(t *testing.T) {
  158. ctx := testutil.StartSpan(baseContext, t)
  159. ensureTestEnvSetup(ctx, t)
  160. suite.Run(ctx, t, &DockerCLIImagesSuite{ds: &DockerSuite{}})
  161. }
  162. func TestDockerCLIImportSuite(t *testing.T) {
  163. ctx := testutil.StartSpan(baseContext, t)
  164. ensureTestEnvSetup(ctx, t)
  165. suite.Run(ctx, t, &DockerCLIImportSuite{ds: &DockerSuite{}})
  166. }
  167. func TestDockerCLIInfoSuite(t *testing.T) {
  168. ctx := testutil.StartSpan(baseContext, t)
  169. ensureTestEnvSetup(ctx, t)
  170. suite.Run(ctx, t, &DockerCLIInfoSuite{ds: &DockerSuite{}})
  171. }
  172. func TestDockerCLIInspectSuite(t *testing.T) {
  173. ctx := testutil.StartSpan(baseContext, t)
  174. ensureTestEnvSetup(ctx, t)
  175. suite.Run(ctx, t, &DockerCLIInspectSuite{ds: &DockerSuite{}})
  176. }
  177. func TestDockerCLILinksSuite(t *testing.T) {
  178. ctx := testutil.StartSpan(baseContext, t)
  179. ensureTestEnvSetup(ctx, t)
  180. suite.Run(ctx, t, &DockerCLILinksSuite{ds: &DockerSuite{}})
  181. }
  182. func TestDockerCLILoginSuite(t *testing.T) {
  183. ctx := testutil.StartSpan(baseContext, t)
  184. ensureTestEnvSetup(ctx, t)
  185. suite.Run(ctx, t, &DockerCLILoginSuite{ds: &DockerSuite{}})
  186. }
  187. func TestDockerCLILogsSuite(t *testing.T) {
  188. ctx := testutil.StartSpan(baseContext, t)
  189. ensureTestEnvSetup(ctx, t)
  190. suite.Run(ctx, t, &DockerCLILogsSuite{ds: &DockerSuite{}})
  191. }
  192. func TestDockerCLINetmodeSuite(t *testing.T) {
  193. ctx := testutil.StartSpan(baseContext, t)
  194. ensureTestEnvSetup(ctx, t)
  195. suite.Run(ctx, t, &DockerCLINetmodeSuite{ds: &DockerSuite{}})
  196. }
  197. func TestDockerCLINetworkSuite(t *testing.T) {
  198. ctx := testutil.StartSpan(baseContext, t)
  199. ensureTestEnvSetup(ctx, t)
  200. suite.Run(ctx, t, &DockerCLINetworkSuite{ds: &DockerSuite{}})
  201. }
  202. func TestDockerCLIPluginLogDriverSuite(t *testing.T) {
  203. ctx := testutil.StartSpan(baseContext, t)
  204. ensureTestEnvSetup(ctx, t)
  205. suite.Run(ctx, t, &DockerCLIPluginLogDriverSuite{ds: &DockerSuite{}})
  206. }
  207. func TestDockerCLIPluginsSuite(t *testing.T) {
  208. ctx := testutil.StartSpan(baseContext, t)
  209. ensureTestEnvSetup(ctx, t)
  210. suite.Run(ctx, t, &DockerCLIPluginsSuite{ds: &DockerSuite{}})
  211. }
  212. func TestDockerCLIPortSuite(t *testing.T) {
  213. ctx := testutil.StartSpan(baseContext, t)
  214. ensureTestEnvSetup(ctx, t)
  215. suite.Run(ctx, t, &DockerCLIPortSuite{ds: &DockerSuite{}})
  216. }
  217. func TestDockerCLIProxySuite(t *testing.T) {
  218. ctx := testutil.StartSpan(baseContext, t)
  219. ensureTestEnvSetup(ctx, t)
  220. suite.Run(ctx, t, &DockerCLIProxySuite{ds: &DockerSuite{}})
  221. }
  222. func TestDockerCLIPruneSuite(t *testing.T) {
  223. ctx := testutil.StartSpan(baseContext, t)
  224. ensureTestEnvSetup(ctx, t)
  225. suite.Run(ctx, t, &DockerCLIPruneSuite{ds: &DockerSuite{}})
  226. }
  227. func TestDockerCLIPsSuite(t *testing.T) {
  228. ctx := testutil.StartSpan(baseContext, t)
  229. ensureTestEnvSetup(ctx, t)
  230. suite.Run(ctx, t, &DockerCLIPsSuite{ds: &DockerSuite{}})
  231. }
  232. func TestDockerCLIPullSuite(t *testing.T) {
  233. ctx := testutil.StartSpan(baseContext, t)
  234. ensureTestEnvSetup(ctx, t)
  235. suite.Run(ctx, t, &DockerCLIPullSuite{ds: &DockerSuite{}})
  236. }
  237. func TestDockerCLIPushSuite(t *testing.T) {
  238. ctx := testutil.StartSpan(baseContext, t)
  239. ensureTestEnvSetup(ctx, t)
  240. suite.Run(ctx, t, &DockerCLIPushSuite{ds: &DockerSuite{}})
  241. }
  242. func TestDockerCLIRestartSuite(t *testing.T) {
  243. ctx := testutil.StartSpan(baseContext, t)
  244. ensureTestEnvSetup(ctx, t)
  245. suite.Run(ctx, t, &DockerCLIRestartSuite{ds: &DockerSuite{}})
  246. }
  247. func TestDockerCLIRmiSuite(t *testing.T) {
  248. ctx := testutil.StartSpan(baseContext, t)
  249. ensureTestEnvSetup(ctx, t)
  250. suite.Run(ctx, t, &DockerCLIRmiSuite{ds: &DockerSuite{}})
  251. }
  252. func TestDockerCLIRunSuite(t *testing.T) {
  253. ctx := testutil.StartSpan(baseContext, t)
  254. ensureTestEnvSetup(ctx, t)
  255. suite.Run(ctx, t, &DockerCLIRunSuite{ds: &DockerSuite{}})
  256. }
  257. func TestDockerCLISaveLoadSuite(t *testing.T) {
  258. ctx := testutil.StartSpan(baseContext, t)
  259. ensureTestEnvSetup(ctx, t)
  260. suite.Run(ctx, t, &DockerCLISaveLoadSuite{ds: &DockerSuite{}})
  261. }
  262. func TestDockerCLISearchSuite(t *testing.T) {
  263. ctx := testutil.StartSpan(baseContext, t)
  264. ensureTestEnvSetup(ctx, t)
  265. suite.Run(ctx, t, &DockerCLISearchSuite{ds: &DockerSuite{}})
  266. }
  267. func TestDockerCLISNISuite(t *testing.T) {
  268. ctx := testutil.StartSpan(baseContext, t)
  269. ensureTestEnvSetup(ctx, t)
  270. suite.Run(ctx, t, &DockerCLISNISuite{ds: &DockerSuite{}})
  271. }
  272. func TestDockerCLIStartSuite(t *testing.T) {
  273. ctx := testutil.StartSpan(baseContext, t)
  274. ensureTestEnvSetup(ctx, t)
  275. suite.Run(ctx, t, &DockerCLIStartSuite{ds: &DockerSuite{}})
  276. }
  277. func TestDockerCLIStatsSuite(t *testing.T) {
  278. ctx := testutil.StartSpan(baseContext, t)
  279. ensureTestEnvSetup(ctx, t)
  280. suite.Run(ctx, t, &DockerCLIStatsSuite{ds: &DockerSuite{}})
  281. }
  282. func TestDockerCLITopSuite(t *testing.T) {
  283. ctx := testutil.StartSpan(baseContext, t)
  284. ensureTestEnvSetup(ctx, t)
  285. suite.Run(ctx, t, &DockerCLITopSuite{ds: &DockerSuite{}})
  286. }
  287. func TestDockerCLIUpdateSuite(t *testing.T) {
  288. ctx := testutil.StartSpan(baseContext, t)
  289. ensureTestEnvSetup(ctx, t)
  290. suite.Run(ctx, t, &DockerCLIUpdateSuite{ds: &DockerSuite{}})
  291. }
  292. func TestDockerCLIVolumeSuite(t *testing.T) {
  293. ctx := testutil.StartSpan(baseContext, t)
  294. ensureTestEnvSetup(ctx, t)
  295. suite.Run(ctx, t, &DockerCLIVolumeSuite{ds: &DockerSuite{}})
  296. }
  297. func TestDockerRegistrySuite(t *testing.T) {
  298. ctx := testutil.StartSpan(baseContext, t)
  299. ensureTestEnvSetup(ctx, t)
  300. suite.Run(ctx, t, &DockerRegistrySuite{ds: &DockerSuite{}})
  301. }
  302. func TestDockerSchema1RegistrySuite(t *testing.T) {
  303. ctx := testutil.StartSpan(baseContext, t)
  304. ensureTestEnvSetup(ctx, t)
  305. suite.Run(ctx, t, &DockerSchema1RegistrySuite{ds: &DockerSuite{}})
  306. }
  307. func TestDockerRegistryAuthHtpasswdSuite(t *testing.T) {
  308. ctx := testutil.StartSpan(baseContext, t)
  309. ensureTestEnvSetup(ctx, t)
  310. suite.Run(ctx, t, &DockerRegistryAuthHtpasswdSuite{ds: &DockerSuite{}})
  311. }
  312. func TestDockerRegistryAuthTokenSuite(t *testing.T) {
  313. ctx := testutil.StartSpan(baseContext, t)
  314. ensureTestEnvSetup(ctx, t)
  315. suite.Run(ctx, t, &DockerRegistryAuthTokenSuite{ds: &DockerSuite{}})
  316. }
  317. func TestDockerDaemonSuite(t *testing.T) {
  318. ctx := testutil.StartSpan(baseContext, t)
  319. ensureTestEnvSetup(ctx, t)
  320. suite.Run(ctx, t, &DockerDaemonSuite{ds: &DockerSuite{}})
  321. }
  322. func TestDockerSwarmSuite(t *testing.T) {
  323. ctx := testutil.StartSpan(baseContext, t)
  324. ensureTestEnvSetup(ctx, t)
  325. suite.Run(ctx, t, &DockerSwarmSuite{ds: &DockerSuite{}})
  326. }
  327. func TestDockerPluginSuite(t *testing.T) {
  328. ctx := testutil.StartSpan(baseContext, t)
  329. ensureTestEnvSetup(ctx, t)
  330. suite.Run(ctx, t, &DockerPluginSuite{ds: &DockerSuite{}})
  331. }
  332. func TestDockerExternalVolumeSuite(t *testing.T) {
  333. testRequires(t, DaemonIsLinux)
  334. ctx := testutil.StartSpan(baseContext, t)
  335. ensureTestEnvSetup(ctx, t)
  336. suite.Run(ctx, t, &DockerExternalVolumeSuite{ds: &DockerSuite{}})
  337. }
  338. func TestDockerNetworkSuite(t *testing.T) {
  339. testRequires(t, DaemonIsLinux)
  340. ctx := testutil.StartSpan(baseContext, t)
  341. ensureTestEnvSetup(ctx, t)
  342. suite.Run(ctx, t, &DockerNetworkSuite{ds: &DockerSuite{}})
  343. }
  344. func TestDockerHubPullSuite(t *testing.T) {
  345. ctx := testutil.StartSpan(baseContext, t)
  346. ensureTestEnvSetup(ctx, t)
  347. // FIXME. Temporarily turning this off for Windows as GH16039 was breaking
  348. // Windows to Linux CI @icecrime
  349. testRequires(t, DaemonIsLinux)
  350. suite.Run(ctx, t, newDockerHubPullSuite())
  351. }
  352. type DockerSuite struct{}
  353. func (s *DockerSuite) OnTimeout(c *testing.T) {
  354. if testEnv.IsRemoteDaemon() {
  355. return
  356. }
  357. path := filepath.Join(os.Getenv("DEST"), "docker.pid")
  358. b, err := os.ReadFile(path)
  359. if err != nil {
  360. c.Fatalf("Failed to get daemon PID from %s\n", path)
  361. }
  362. rawPid, err := strconv.ParseInt(string(b), 10, 32)
  363. if err != nil {
  364. c.Fatalf("Failed to parse pid from %s: %s\n", path, err)
  365. }
  366. daemonPid := int(rawPid)
  367. if daemonPid > 0 {
  368. testdaemon.SignalDaemonDump(daemonPid)
  369. }
  370. }
  371. func (s *DockerSuite) TearDownTest(ctx context.Context, c *testing.T) {
  372. testEnv.Clean(ctx, c)
  373. }
  374. type DockerRegistrySuite struct {
  375. ds *DockerSuite
  376. reg *registry.V2
  377. d *daemon.Daemon
  378. }
  379. func (s *DockerRegistrySuite) OnTimeout(c *testing.T) {
  380. s.d.DumpStackAndQuit()
  381. }
  382. func (s *DockerRegistrySuite) SetUpTest(ctx context.Context, c *testing.T) {
  383. testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
  384. s.reg = registry.NewV2(c)
  385. s.reg.WaitReady(c)
  386. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  387. }
  388. func (s *DockerRegistrySuite) TearDownTest(ctx context.Context, c *testing.T) {
  389. if s.reg != nil {
  390. s.reg.Close()
  391. }
  392. if s.d != nil {
  393. s.d.Stop(c)
  394. }
  395. s.ds.TearDownTest(ctx, c)
  396. }
  397. type DockerSchema1RegistrySuite struct {
  398. ds *DockerSuite
  399. reg *registry.V2
  400. d *daemon.Daemon
  401. }
  402. func (s *DockerSchema1RegistrySuite) OnTimeout(c *testing.T) {
  403. s.d.DumpStackAndQuit()
  404. }
  405. func (s *DockerSchema1RegistrySuite) SetUpTest(ctx context.Context, c *testing.T) {
  406. testRequires(c, DaemonIsLinux, RegistryHosting, NotArm64, testEnv.IsLocalDaemon)
  407. s.reg = registry.NewV2(c, registry.Schema1)
  408. s.reg.WaitReady(c)
  409. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  410. }
  411. func (s *DockerSchema1RegistrySuite) TearDownTest(ctx context.Context, c *testing.T) {
  412. if s.reg != nil {
  413. s.reg.Close()
  414. }
  415. if s.d != nil {
  416. s.d.Stop(c)
  417. }
  418. s.ds.TearDownTest(ctx, c)
  419. }
  420. type DockerRegistryAuthHtpasswdSuite struct {
  421. ds *DockerSuite
  422. reg *registry.V2
  423. d *daemon.Daemon
  424. }
  425. func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *testing.T) {
  426. s.d.DumpStackAndQuit()
  427. }
  428. func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(ctx context.Context, c *testing.T) {
  429. testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
  430. s.reg = registry.NewV2(c, registry.Htpasswd)
  431. s.reg.WaitReady(c)
  432. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  433. }
  434. func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(ctx context.Context, c *testing.T) {
  435. if s.reg != nil {
  436. out, err := s.d.Cmd("logout", privateRegistryURL)
  437. assert.NilError(c, err, out)
  438. s.reg.Close()
  439. }
  440. if s.d != nil {
  441. s.d.Stop(c)
  442. }
  443. s.ds.TearDownTest(ctx, c)
  444. }
  445. type DockerRegistryAuthTokenSuite struct {
  446. ds *DockerSuite
  447. reg *registry.V2
  448. d *daemon.Daemon
  449. }
  450. func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *testing.T) {
  451. s.d.DumpStackAndQuit()
  452. }
  453. func (s *DockerRegistryAuthTokenSuite) SetUpTest(ctx context.Context, c *testing.T) {
  454. testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
  455. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  456. }
  457. func (s *DockerRegistryAuthTokenSuite) TearDownTest(ctx context.Context, c *testing.T) {
  458. if s.reg != nil {
  459. out, err := s.d.Cmd("logout", privateRegistryURL)
  460. assert.NilError(c, err, out)
  461. s.reg.Close()
  462. }
  463. if s.d != nil {
  464. s.d.Stop(c)
  465. }
  466. s.ds.TearDownTest(ctx, c)
  467. }
  468. func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *testing.T, tokenURL string) {
  469. if s == nil {
  470. c.Fatal("registry suite isn't initialized")
  471. }
  472. s.reg = registry.NewV2(c, registry.Token(tokenURL))
  473. s.reg.WaitReady(c)
  474. }
  475. type DockerDaemonSuite struct {
  476. ds *DockerSuite
  477. d *daemon.Daemon
  478. }
  479. func (s *DockerDaemonSuite) OnTimeout(c *testing.T) {
  480. s.d.DumpStackAndQuit()
  481. }
  482. func (s *DockerDaemonSuite) SetUpTest(ctx context.Context, c *testing.T) {
  483. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  484. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  485. }
  486. func (s *DockerDaemonSuite) TearDownTest(ctx context.Context, c *testing.T) {
  487. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  488. if s.d != nil {
  489. s.d.Stop(c)
  490. }
  491. s.ds.TearDownTest(ctx, c)
  492. }
  493. func (s *DockerDaemonSuite) TearDownSuite(ctx context.Context, c *testing.T) {
  494. filepath.Walk(testdaemon.SockRoot, func(path string, fi os.FileInfo, err error) error {
  495. if err != nil {
  496. // ignore errors here
  497. // not cleaning up sockets is not really an error
  498. return nil
  499. }
  500. if fi.Mode() == os.ModeSocket {
  501. syscall.Unlink(path)
  502. }
  503. return nil
  504. })
  505. os.RemoveAll(testdaemon.SockRoot)
  506. }
  507. const defaultSwarmPort = 2477
  508. type DockerSwarmSuite struct {
  509. server *httptest.Server
  510. ds *DockerSuite
  511. daemonsLock sync.Mutex // protect access to daemons and portIndex
  512. daemons []*daemon.Daemon
  513. portIndex int
  514. }
  515. func (s *DockerSwarmSuite) OnTimeout(c *testing.T) {
  516. s.daemonsLock.Lock()
  517. defer s.daemonsLock.Unlock()
  518. for _, d := range s.daemons {
  519. d.DumpStackAndQuit()
  520. }
  521. }
  522. func (s *DockerSwarmSuite) SetUpTest(ctx context.Context, c *testing.T) {
  523. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  524. }
  525. func (s *DockerSwarmSuite) AddDaemon(ctx context.Context, c *testing.T, joinSwarm, manager bool) *daemon.Daemon {
  526. c.Helper()
  527. d := daemon.New(c, dockerBinary, dockerdBinary,
  528. testdaemon.WithEnvironment(testEnv.Execution),
  529. testdaemon.WithSwarmPort(defaultSwarmPort+s.portIndex),
  530. )
  531. if joinSwarm {
  532. if len(s.daemons) > 0 {
  533. d.StartAndSwarmJoin(ctx, c, s.daemons[0].Daemon, manager)
  534. } else {
  535. d.StartAndSwarmInit(ctx, c)
  536. }
  537. } else {
  538. d.StartNodeWithBusybox(ctx, c)
  539. }
  540. s.daemonsLock.Lock()
  541. s.portIndex++
  542. s.daemons = append(s.daemons, d)
  543. s.daemonsLock.Unlock()
  544. return d
  545. }
  546. func (s *DockerSwarmSuite) TearDownTest(ctx context.Context, c *testing.T) {
  547. testRequires(c, DaemonIsLinux)
  548. s.daemonsLock.Lock()
  549. for _, d := range s.daemons {
  550. if d != nil {
  551. d.Stop(c)
  552. d.Cleanup(c)
  553. }
  554. }
  555. s.daemons = nil
  556. s.portIndex = 0
  557. s.daemonsLock.Unlock()
  558. s.ds.TearDownTest(ctx, c)
  559. }
  560. type DockerPluginSuite struct {
  561. ds *DockerSuite
  562. registry *registry.V2
  563. }
  564. func (ps *DockerPluginSuite) registryHost() string {
  565. return privateRegistryURL
  566. }
  567. func (ps *DockerPluginSuite) getPluginRepo() string {
  568. return path.Join(ps.registryHost(), "plugin", "basic")
  569. }
  570. func (ps *DockerPluginSuite) getPluginRepoWithTag() string {
  571. return ps.getPluginRepo() + ":" + "latest"
  572. }
  573. func (ps *DockerPluginSuite) SetUpSuite(ctx context.Context, c *testing.T) {
  574. testRequires(c, DaemonIsLinux, RegistryHosting)
  575. ps.registry = registry.NewV2(c)
  576. ps.registry.WaitReady(c)
  577. ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
  578. defer cancel()
  579. err := plugin.CreateInRegistry(ctx, ps.getPluginRepo(), nil)
  580. assert.NilError(c, err, "failed to create plugin")
  581. }
  582. func (ps *DockerPluginSuite) TearDownSuite(ctx context.Context, c *testing.T) {
  583. if ps.registry != nil {
  584. ps.registry.Close()
  585. }
  586. }
  587. func (ps *DockerPluginSuite) TearDownTest(ctx context.Context, c *testing.T) {
  588. ps.ds.TearDownTest(ctx, c)
  589. }
  590. func (ps *DockerPluginSuite) OnTimeout(c *testing.T) {
  591. ps.ds.OnTimeout(c)
  592. }