check_test.go 19 KB

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