docker_utils_test.go 13 KB

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