docker_utils_test.go 13 KB

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