docker_utils_test.go 13 KB

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