docker_utils_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "strconv"
  12. "strings"
  13. "testing"
  14. "time"
  15. "github.com/docker/docker/api/types"
  16. "github.com/docker/docker/client"
  17. "github.com/docker/docker/integration-cli/cli"
  18. "github.com/docker/docker/integration-cli/daemon"
  19. "gotest.tools/v3/assert"
  20. "gotest.tools/v3/assert/cmp"
  21. "gotest.tools/v3/icmd"
  22. "gotest.tools/v3/poll"
  23. )
  24. func deleteImages(images ...string) error {
  25. args := []string{dockerBinary, "rmi", "-f"}
  26. return icmd.RunCmd(icmd.Cmd{Command: append(args, images...)}).Error
  27. }
  28. // Deprecated: use cli.Docker or cli.DockerCmd
  29. func dockerCmdWithError(args ...string) (string, int, error) {
  30. result := cli.Docker(cli.Args(args...))
  31. if result.Error != nil {
  32. return result.Combined(), result.ExitCode, result.Compare(icmd.Success)
  33. }
  34. return result.Combined(), result.ExitCode, result.Error
  35. }
  36. // Deprecated: use cli.Docker or cli.DockerCmd
  37. func dockerCmd(c testing.TB, args ...string) (string, int) {
  38. c.Helper()
  39. result := cli.DockerCmd(c, args...)
  40. return result.Combined(), result.ExitCode
  41. }
  42. // Deprecated: use cli.Docker or cli.DockerCmd
  43. func dockerCmdWithResult(args ...string) *icmd.Result {
  44. return cli.Docker(cli.Args(args...))
  45. }
  46. func findContainerIP(c *testing.T, id string, network string) string {
  47. c.Helper()
  48. out, _ := dockerCmd(c, "inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.%s.IPAddress }}'", network), id)
  49. return strings.Trim(out, " \r\n'")
  50. }
  51. func getContainerCount(c *testing.T) int {
  52. c.Helper()
  53. const containers = "Containers:"
  54. result := icmd.RunCommand(dockerBinary, "info")
  55. result.Assert(c, icmd.Success)
  56. lines := strings.Split(result.Combined(), "\n")
  57. for _, line := range lines {
  58. if strings.Contains(line, containers) {
  59. output := strings.TrimSpace(line)
  60. output = strings.TrimPrefix(output, containers)
  61. output = strings.Trim(output, " ")
  62. containerCount, err := strconv.Atoi(output)
  63. assert.NilError(c, err)
  64. return containerCount
  65. }
  66. }
  67. return 0
  68. }
  69. func inspectFieldAndUnmarshall(c *testing.T, name, field string, output interface{}) {
  70. c.Helper()
  71. str := inspectFieldJSON(c, name, field)
  72. err := json.Unmarshal([]byte(str), output)
  73. assert.Assert(c, err == nil, "failed to unmarshal: %v", err)
  74. }
  75. // Deprecated: use cli.Inspect
  76. func inspectFilter(name, filter string) (string, error) {
  77. format := fmt.Sprintf("{{%s}}", filter)
  78. result := icmd.RunCommand(dockerBinary, "inspect", "-f", format, name)
  79. if result.Error != nil || result.ExitCode != 0 {
  80. return "", fmt.Errorf("failed to inspect %s: %s", name, result.Combined())
  81. }
  82. return strings.TrimSpace(result.Combined()), nil
  83. }
  84. // Deprecated: use cli.Inspect
  85. func inspectFieldWithError(name, field string) (string, error) {
  86. return inspectFilter(name, "."+field)
  87. }
  88. // Deprecated: use cli.Inspect
  89. func inspectField(c *testing.T, name, field string) string {
  90. c.Helper()
  91. out, err := inspectFilter(name, "."+field)
  92. assert.NilError(c, err)
  93. return out
  94. }
  95. // Deprecated: use cli.Inspect
  96. func inspectFieldJSON(c *testing.T, name, field string) string {
  97. c.Helper()
  98. out, err := inspectFilter(name, "json ."+field)
  99. assert.NilError(c, err)
  100. return out
  101. }
  102. // Deprecated: use cli.Inspect
  103. func inspectFieldMap(c *testing.T, name, path, field string) string {
  104. c.Helper()
  105. out, err := inspectFilter(name, fmt.Sprintf("index .%s %q", path, field))
  106. assert.NilError(c, err)
  107. return out
  108. }
  109. // Deprecated: use cli.Inspect
  110. func inspectMountSourceField(name, destination string) (string, error) {
  111. m, err := inspectMountPoint(name, destination)
  112. if err != nil {
  113. return "", err
  114. }
  115. return m.Source, nil
  116. }
  117. // Deprecated: use cli.Inspect
  118. func inspectMountPoint(name, destination string) (types.MountPoint, error) {
  119. out, err := inspectFilter(name, "json .Mounts")
  120. if err != nil {
  121. return types.MountPoint{}, err
  122. }
  123. return inspectMountPointJSON(out, destination)
  124. }
  125. var errMountNotFound = errors.New("mount point not found")
  126. // Deprecated: use cli.Inspect
  127. func inspectMountPointJSON(j, destination string) (types.MountPoint, error) {
  128. var mp []types.MountPoint
  129. if err := json.Unmarshal([]byte(j), &mp); err != nil {
  130. return types.MountPoint{}, err
  131. }
  132. var m *types.MountPoint
  133. for _, c := range mp {
  134. if c.Destination == destination {
  135. m = &c
  136. break
  137. }
  138. }
  139. if m == nil {
  140. return types.MountPoint{}, errMountNotFound
  141. }
  142. return *m, nil
  143. }
  144. func getIDByName(c *testing.T, name string) string {
  145. c.Helper()
  146. id, err := inspectFieldWithError(name, "Id")
  147. assert.NilError(c, err)
  148. return id
  149. }
  150. // Deprecated: use cli.Build
  151. func buildImageSuccessfully(c *testing.T, name string, cmdOperators ...cli.CmdOperator) {
  152. c.Helper()
  153. buildImage(name, cmdOperators...).Assert(c, icmd.Success)
  154. }
  155. // Deprecated: use cli.Build
  156. func buildImage(name string, cmdOperators ...cli.CmdOperator) *icmd.Result {
  157. return cli.Docker(cli.Build(name), cmdOperators...)
  158. }
  159. // Write `content` to the file at path `dst`, creating it if necessary,
  160. // as well as any missing directories.
  161. // The file is truncated if it already exists.
  162. // Fail the test when error occurs.
  163. func writeFile(dst, content string, c *testing.T) {
  164. c.Helper()
  165. // Create subdirectories if necessary
  166. assert.Assert(c, os.MkdirAll(path.Dir(dst), 0700) == nil)
  167. f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
  168. assert.NilError(c, err)
  169. defer f.Close()
  170. // Write content (truncate if it exists)
  171. _, err = io.Copy(f, strings.NewReader(content))
  172. assert.NilError(c, err)
  173. }
  174. // Return the contents of file at path `src`.
  175. // Fail the test when error occurs.
  176. func readFile(src string, c *testing.T) (content string) {
  177. c.Helper()
  178. data, err := os.ReadFile(src)
  179. assert.NilError(c, err)
  180. return string(data)
  181. }
  182. func containerStorageFile(containerID, basename string) string {
  183. return filepath.Join(testEnv.PlatformDefaults.ContainerStoragePath, containerID, basename)
  184. }
  185. // docker commands that use this function must be run with the '-d' switch.
  186. func runCommandAndReadContainerFile(c *testing.T, filename string, command string, args ...string) []byte {
  187. c.Helper()
  188. result := icmd.RunCommand(command, args...)
  189. result.Assert(c, icmd.Success)
  190. contID := strings.TrimSpace(result.Combined())
  191. if err := waitRun(contID); err != nil {
  192. c.Fatalf("%v: %q", contID, err)
  193. }
  194. return readContainerFile(c, contID, filename)
  195. }
  196. func readContainerFile(c *testing.T, containerID, filename string) []byte {
  197. c.Helper()
  198. f, err := os.Open(containerStorageFile(containerID, filename))
  199. assert.NilError(c, err)
  200. defer f.Close()
  201. content, err := io.ReadAll(f)
  202. assert.NilError(c, err)
  203. return content
  204. }
  205. func readContainerFileWithExec(c *testing.T, containerID, filename string) []byte {
  206. c.Helper()
  207. result := icmd.RunCommand(dockerBinary, "exec", containerID, "cat", filename)
  208. result.Assert(c, icmd.Success)
  209. return []byte(result.Combined())
  210. }
  211. // daemonTime provides the current time on the daemon host
  212. func daemonTime(c *testing.T) time.Time {
  213. c.Helper()
  214. if testEnv.IsLocalDaemon() {
  215. return time.Now()
  216. }
  217. cli, err := client.NewClientWithOpts(client.FromEnv)
  218. assert.NilError(c, err)
  219. defer cli.Close()
  220. info, err := cli.Info(context.Background())
  221. assert.NilError(c, err)
  222. dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
  223. assert.Assert(c, err == nil, "invalid time format in GET /info response")
  224. return dt
  225. }
  226. // daemonUnixTime returns the current time on the daemon host with nanoseconds precision.
  227. // It return the time formatted how the client sends timestamps to the server.
  228. func daemonUnixTime(c *testing.T) string {
  229. c.Helper()
  230. return parseEventTime(daemonTime(c))
  231. }
  232. func parseEventTime(t time.Time) string {
  233. return fmt.Sprintf("%d.%09d", t.Unix(), int64(t.Nanosecond()))
  234. }
  235. // appendBaseEnv appends the minimum set of environment variables to exec the
  236. // docker cli binary for testing with correct configuration to the given env
  237. // list.
  238. func appendBaseEnv(isTLS bool, env ...string) []string {
  239. preserveList := []string{
  240. // preserve remote test host
  241. "DOCKER_HOST",
  242. // windows: requires preserving SystemRoot, otherwise dial tcp fails
  243. // with "GetAddrInfoW: A non-recoverable error occurred during a database lookup."
  244. "SystemRoot",
  245. // testing help text requires the $PATH to dockerd is set
  246. "PATH",
  247. }
  248. if isTLS {
  249. preserveList = append(preserveList, "DOCKER_TLS_VERIFY", "DOCKER_CERT_PATH")
  250. }
  251. for _, key := range preserveList {
  252. if val := os.Getenv(key); val != "" {
  253. env = append(env, fmt.Sprintf("%s=%s", key, val))
  254. }
  255. }
  256. return env
  257. }
  258. func createTmpFile(c *testing.T, content string) string {
  259. c.Helper()
  260. f, err := os.CreateTemp("", "testfile")
  261. assert.NilError(c, err)
  262. filename := f.Name()
  263. err = os.WriteFile(filename, []byte(content), 0644)
  264. assert.NilError(c, err)
  265. return filename
  266. }
  267. // waitRun will wait for the specified container to be running, maximum 5 seconds.
  268. // Deprecated: use cli.WaitFor
  269. func waitRun(contID string) error {
  270. return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)
  271. }
  272. // waitInspect will wait for the specified container to have the specified string
  273. // in the inspect output. It will wait until the specified timeout (in seconds)
  274. // is reached.
  275. // Deprecated: use cli.WaitFor
  276. func waitInspect(name, expr, expected string, timeout time.Duration) error {
  277. return waitInspectWithArgs(name, expr, expected, timeout)
  278. }
  279. // Deprecated: use cli.WaitFor
  280. func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg ...string) error {
  281. return daemon.WaitInspectWithArgs(dockerBinary, name, expr, expected, timeout, arg...)
  282. }
  283. func getInspectBody(c *testing.T, version, id string) []byte {
  284. c.Helper()
  285. cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(version))
  286. assert.NilError(c, err)
  287. defer cli.Close()
  288. _, body, err := cli.ContainerInspectWithRaw(context.Background(), id, false)
  289. assert.NilError(c, err)
  290. return body
  291. }
  292. // Run a long running idle task in a background container using the
  293. // system-specific default image and command.
  294. func runSleepingContainer(c *testing.T, extraArgs ...string) string {
  295. c.Helper()
  296. return runSleepingContainerInImage(c, "busybox", extraArgs...)
  297. }
  298. // Run a long running idle task in a background container using the specified
  299. // image and the system-specific command.
  300. func runSleepingContainerInImage(c *testing.T, image string, extraArgs ...string) string {
  301. c.Helper()
  302. args := []string{"run", "-d"}
  303. args = append(args, extraArgs...)
  304. args = append(args, image)
  305. args = append(args, sleepCommandForDaemonPlatform()...)
  306. return strings.TrimSpace(cli.DockerCmd(c, args...).Combined())
  307. }
  308. // minimalBaseImage returns the name of the minimal base image for the current
  309. // daemon platform.
  310. func minimalBaseImage() string {
  311. return testEnv.PlatformDefaults.BaseImage
  312. }
  313. func getGoroutineNumber() (int, error) {
  314. cli, err := client.NewClientWithOpts(client.FromEnv)
  315. if err != nil {
  316. return 0, err
  317. }
  318. defer cli.Close()
  319. info, err := cli.Info(context.Background())
  320. if err != nil {
  321. return 0, err
  322. }
  323. return info.NGoroutines, nil
  324. }
  325. func waitForGoroutines(expected int) error {
  326. t := time.After(30 * time.Second)
  327. for {
  328. select {
  329. case <-t:
  330. n, err := getGoroutineNumber()
  331. if err != nil {
  332. return err
  333. }
  334. if n > expected {
  335. return fmt.Errorf("leaked goroutines: expected less than or equal to %d, got: %d", expected, n)
  336. }
  337. default:
  338. n, err := getGoroutineNumber()
  339. if err != nil {
  340. return err
  341. }
  342. if n <= expected {
  343. return nil
  344. }
  345. time.Sleep(200 * time.Millisecond)
  346. }
  347. }
  348. }
  349. // getErrorMessage returns the error message from an error API response
  350. func getErrorMessage(c *testing.T, body []byte) string {
  351. c.Helper()
  352. var resp types.ErrorResponse
  353. assert.Assert(c, json.Unmarshal(body, &resp) == nil)
  354. return strings.TrimSpace(resp.Message)
  355. }
  356. type checkF func(*testing.T) (interface{}, string)
  357. type reducer func(...interface{}) interface{}
  358. func pollCheck(t *testing.T, f checkF, compare func(x interface{}) assert.BoolOrComparison) poll.Check {
  359. return func(poll.LogT) poll.Result {
  360. t.Helper()
  361. v, comment := f(t)
  362. r := compare(v)
  363. switch r := r.(type) {
  364. case bool:
  365. if r {
  366. return poll.Success()
  367. }
  368. case cmp.Comparison:
  369. if r().Success() {
  370. return poll.Success()
  371. }
  372. default:
  373. panic(fmt.Errorf("pollCheck: type %T not implemented", r))
  374. }
  375. return poll.Continue(comment)
  376. }
  377. }
  378. func reducedCheck(r reducer, funcs ...checkF) checkF {
  379. return func(c *testing.T) (interface{}, string) {
  380. c.Helper()
  381. var values []interface{}
  382. var comments []string
  383. for _, f := range funcs {
  384. v, comment := f(c)
  385. values = append(values, v)
  386. if len(comment) > 0 {
  387. comments = append(comments, comment)
  388. }
  389. }
  390. return r(values...), fmt.Sprintf("%v", strings.Join(comments, ", "))
  391. }
  392. }
  393. func sumAsIntegers(vals ...interface{}) interface{} {
  394. var s int
  395. for _, v := range vals {
  396. s += v.(int)
  397. }
  398. return s
  399. }