docker_utils_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "net"
  10. "net/http"
  11. "net/http/httptest"
  12. "net/url"
  13. "os"
  14. "os/exec"
  15. "path"
  16. "path/filepath"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/docker/docker/api/types"
  21. "github.com/docker/docker/integration-cli/checker"
  22. "github.com/docker/docker/integration-cli/cli"
  23. "github.com/docker/docker/integration-cli/cli/build"
  24. "github.com/docker/docker/integration-cli/daemon"
  25. "github.com/docker/docker/integration-cli/registry"
  26. "github.com/docker/docker/integration-cli/request"
  27. "github.com/docker/docker/pkg/stringutils"
  28. icmd "github.com/docker/docker/pkg/testutil/cmd"
  29. "github.com/go-check/check"
  30. )
  31. // Deprecated
  32. func daemonHost() string {
  33. return request.DaemonHost()
  34. }
  35. // FIXME(vdemeester) move this away are remove ignoreNoSuchContainer bool
  36. func deleteContainer(container ...string) error {
  37. return icmd.RunCommand(dockerBinary, append([]string{"rm", "-fv"}, container...)...).Compare(icmd.Success)
  38. }
  39. func getAllContainers(c *check.C) string {
  40. result := icmd.RunCommand(dockerBinary, "ps", "-q", "-a")
  41. result.Assert(c, icmd.Success)
  42. return result.Combined()
  43. }
  44. // Deprecated
  45. func deleteAllContainers(c *check.C) {
  46. containers := getAllContainers(c)
  47. if containers != "" {
  48. err := deleteContainer(strings.Split(strings.TrimSpace(containers), "\n")...)
  49. c.Assert(err, checker.IsNil)
  50. }
  51. }
  52. func getPausedContainers(c *check.C) []string {
  53. result := icmd.RunCommand(dockerBinary, "ps", "-f", "status=paused", "-q", "-a")
  54. result.Assert(c, icmd.Success)
  55. return strings.Fields(result.Combined())
  56. }
  57. func unpauseContainer(c *check.C, container string) {
  58. dockerCmd(c, "unpause", container)
  59. }
  60. // Deprecated
  61. func unpauseAllContainers(c *check.C) {
  62. containers := getPausedContainers(c)
  63. for _, value := range containers {
  64. unpauseContainer(c, value)
  65. }
  66. }
  67. func deleteImages(images ...string) error {
  68. args := []string{dockerBinary, "rmi", "-f"}
  69. return icmd.RunCmd(icmd.Cmd{Command: append(args, images...)}).Error
  70. }
  71. // Deprecated: use cli.Docker or cli.DockerCmd
  72. func dockerCmdWithError(args ...string) (string, int, error) {
  73. result := cli.Docker(cli.Args(args...))
  74. if result.Error != nil {
  75. return result.Combined(), result.ExitCode, result.Compare(icmd.Success)
  76. }
  77. return result.Combined(), result.ExitCode, result.Error
  78. }
  79. // Deprecated: use cli.Docker or cli.DockerCmd
  80. func dockerCmd(c *check.C, args ...string) (string, int) {
  81. result := cli.DockerCmd(c, args...)
  82. return result.Combined(), result.ExitCode
  83. }
  84. // Deprecated: use cli.Docker or cli.DockerCmd
  85. func dockerCmdWithResult(args ...string) *icmd.Result {
  86. return cli.Docker(cli.Args(args...))
  87. }
  88. func findContainerIP(c *check.C, id string, network string) string {
  89. out, _ := dockerCmd(c, "inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.%s.IPAddress }}'", network), id)
  90. return strings.Trim(out, " \r\n'")
  91. }
  92. func getContainerCount(c *check.C) int {
  93. const containers = "Containers:"
  94. result := icmd.RunCommand(dockerBinary, "info")
  95. result.Assert(c, icmd.Success)
  96. lines := strings.Split(result.Combined(), "\n")
  97. for _, line := range lines {
  98. if strings.Contains(line, containers) {
  99. output := strings.TrimSpace(line)
  100. output = strings.TrimLeft(output, containers)
  101. output = strings.Trim(output, " ")
  102. containerCount, err := strconv.Atoi(output)
  103. c.Assert(err, checker.IsNil)
  104. return containerCount
  105. }
  106. }
  107. return 0
  108. }
  109. // FakeContext creates directories that can be used as a build context
  110. type FakeContext struct {
  111. Dir string
  112. }
  113. // Add a file at a path, creating directories where necessary
  114. func (f *FakeContext) Add(file, content string) error {
  115. return f.addFile(file, []byte(content))
  116. }
  117. func (f *FakeContext) addFile(file string, content []byte) error {
  118. fp := filepath.Join(f.Dir, filepath.FromSlash(file))
  119. dirpath := filepath.Dir(fp)
  120. if dirpath != "." {
  121. if err := os.MkdirAll(dirpath, 0755); err != nil {
  122. return err
  123. }
  124. }
  125. return ioutil.WriteFile(fp, content, 0644)
  126. }
  127. // Delete a file at a path
  128. func (f *FakeContext) Delete(file string) error {
  129. fp := filepath.Join(f.Dir, filepath.FromSlash(file))
  130. return os.RemoveAll(fp)
  131. }
  132. // Close deletes the context
  133. func (f *FakeContext) Close() error {
  134. return os.RemoveAll(f.Dir)
  135. }
  136. func fakeContextFromNewTempDir(c *check.C) *FakeContext {
  137. tmp, err := ioutil.TempDir("", "fake-context")
  138. c.Assert(err, checker.IsNil)
  139. if err := os.Chmod(tmp, 0755); err != nil {
  140. c.Fatal(err)
  141. }
  142. return fakeContextFromDir(tmp)
  143. }
  144. func fakeContextFromDir(dir string) *FakeContext {
  145. return &FakeContext{dir}
  146. }
  147. func fakeContextWithFiles(c *check.C, files map[string]string) *FakeContext {
  148. ctx := fakeContextFromNewTempDir(c)
  149. for file, content := range files {
  150. if err := ctx.Add(file, content); err != nil {
  151. ctx.Close()
  152. c.Fatal(err)
  153. }
  154. }
  155. return ctx
  156. }
  157. func fakeContextAddDockerfile(c *check.C, ctx *FakeContext, dockerfile string) {
  158. if err := ctx.Add("Dockerfile", dockerfile); err != nil {
  159. ctx.Close()
  160. c.Fatal(err)
  161. }
  162. }
  163. func fakeContext(c *check.C, dockerfile string, files map[string]string) *FakeContext {
  164. ctx := fakeContextWithFiles(c, files)
  165. fakeContextAddDockerfile(c, ctx, dockerfile)
  166. return ctx
  167. }
  168. // FakeStorage is a static file server. It might be running locally or remotely
  169. // on test host.
  170. type FakeStorage interface {
  171. Close() error
  172. URL() string
  173. CtxDir() string
  174. }
  175. func fakeBinaryStorage(c *check.C, archives map[string]*bytes.Buffer) FakeStorage {
  176. ctx := fakeContextFromNewTempDir(c)
  177. for name, content := range archives {
  178. if err := ctx.addFile(name, content.Bytes()); err != nil {
  179. c.Fatal(err)
  180. }
  181. }
  182. return fakeStorageWithContext(c, ctx)
  183. }
  184. // fakeStorage returns either a local or remote (at daemon machine) file server
  185. func fakeStorage(c *check.C, files map[string]string) FakeStorage {
  186. ctx := fakeContextWithFiles(c, files)
  187. return fakeStorageWithContext(c, ctx)
  188. }
  189. // fakeStorageWithContext returns either a local or remote (at daemon machine) file server
  190. func fakeStorageWithContext(c *check.C, ctx *FakeContext) FakeStorage {
  191. if testEnv.LocalDaemon() {
  192. return newLocalFakeStorage(c, ctx)
  193. }
  194. return newRemoteFileServer(c, ctx)
  195. }
  196. // localFileStorage is a file storage on the running machine
  197. type localFileStorage struct {
  198. *FakeContext
  199. *httptest.Server
  200. }
  201. func (s *localFileStorage) URL() string {
  202. return s.Server.URL
  203. }
  204. func (s *localFileStorage) CtxDir() string {
  205. return s.FakeContext.Dir
  206. }
  207. func (s *localFileStorage) Close() error {
  208. defer s.Server.Close()
  209. return s.FakeContext.Close()
  210. }
  211. func newLocalFakeStorage(c *check.C, ctx *FakeContext) *localFileStorage {
  212. handler := http.FileServer(http.Dir(ctx.Dir))
  213. server := httptest.NewServer(handler)
  214. return &localFileStorage{
  215. FakeContext: ctx,
  216. Server: server,
  217. }
  218. }
  219. // remoteFileServer is a containerized static file server started on the remote
  220. // testing machine to be used in URL-accepting docker build functionality.
  221. type remoteFileServer struct {
  222. host string // hostname/port web server is listening to on docker host e.g. 0.0.0.0:43712
  223. container string
  224. image string
  225. ctx *FakeContext
  226. }
  227. func (f *remoteFileServer) URL() string {
  228. u := url.URL{
  229. Scheme: "http",
  230. Host: f.host}
  231. return u.String()
  232. }
  233. func (f *remoteFileServer) CtxDir() string {
  234. return f.ctx.Dir
  235. }
  236. func (f *remoteFileServer) Close() error {
  237. defer func() {
  238. if f.ctx != nil {
  239. f.ctx.Close()
  240. }
  241. if f.image != "" {
  242. deleteImages(f.image)
  243. }
  244. }()
  245. if f.container == "" {
  246. return nil
  247. }
  248. return deleteContainer(f.container)
  249. }
  250. func newRemoteFileServer(c *check.C, ctx *FakeContext) *remoteFileServer {
  251. var (
  252. image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
  253. container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
  254. )
  255. ensureHTTPServerImage(c)
  256. // Build the image
  257. fakeContextAddDockerfile(c, ctx, `FROM httpserver
  258. COPY . /static`)
  259. buildImageSuccessfully(c, image, build.WithoutCache, withExternalBuildContext(ctx))
  260. // Start the container
  261. dockerCmd(c, "run", "-d", "-P", "--name", container, image)
  262. // Find out the system assigned port
  263. out, _ := dockerCmd(c, "port", container, "80/tcp")
  264. fileserverHostPort := strings.Trim(out, "\n")
  265. _, port, err := net.SplitHostPort(fileserverHostPort)
  266. if err != nil {
  267. c.Fatalf("unable to parse file server host:port: %v", err)
  268. }
  269. dockerHostURL, err := url.Parse(daemonHost())
  270. if err != nil {
  271. c.Fatalf("unable to parse daemon host URL: %v", err)
  272. }
  273. host, _, err := net.SplitHostPort(dockerHostURL.Host)
  274. if err != nil {
  275. c.Fatalf("unable to parse docker daemon host:port: %v", err)
  276. }
  277. return &remoteFileServer{
  278. container: container,
  279. image: image,
  280. host: fmt.Sprintf("%s:%s", host, port),
  281. ctx: ctx}
  282. }
  283. func inspectFieldAndUnmarshall(c *check.C, name, field string, output interface{}) {
  284. str := inspectFieldJSON(c, name, field)
  285. err := json.Unmarshal([]byte(str), output)
  286. if c != nil {
  287. c.Assert(err, check.IsNil, check.Commentf("failed to unmarshal: %v", err))
  288. }
  289. }
  290. // Deprecated: use cli.Inspect
  291. func inspectFilter(name, filter string) (string, error) {
  292. format := fmt.Sprintf("{{%s}}", filter)
  293. result := icmd.RunCommand(dockerBinary, "inspect", "-f", format, name)
  294. if result.Error != nil || result.ExitCode != 0 {
  295. return "", fmt.Errorf("failed to inspect %s: %s", name, result.Combined())
  296. }
  297. return strings.TrimSpace(result.Combined()), nil
  298. }
  299. // Deprecated: use cli.Inspect
  300. func inspectFieldWithError(name, field string) (string, error) {
  301. return inspectFilter(name, fmt.Sprintf(".%s", field))
  302. }
  303. // Deprecated: use cli.Inspect
  304. func inspectField(c *check.C, name, field string) string {
  305. out, err := inspectFilter(name, fmt.Sprintf(".%s", field))
  306. if c != nil {
  307. c.Assert(err, check.IsNil)
  308. }
  309. return out
  310. }
  311. // Deprecated: use cli.Inspect
  312. func inspectFieldJSON(c *check.C, name, field string) string {
  313. out, err := inspectFilter(name, fmt.Sprintf("json .%s", field))
  314. if c != nil {
  315. c.Assert(err, check.IsNil)
  316. }
  317. return out
  318. }
  319. // Deprecated: use cli.Inspect
  320. func inspectFieldMap(c *check.C, name, path, field string) string {
  321. out, err := inspectFilter(name, fmt.Sprintf("index .%s %q", path, field))
  322. if c != nil {
  323. c.Assert(err, check.IsNil)
  324. }
  325. return out
  326. }
  327. // Deprecated: use cli.Inspect
  328. func inspectMountSourceField(name, destination string) (string, error) {
  329. m, err := inspectMountPoint(name, destination)
  330. if err != nil {
  331. return "", err
  332. }
  333. return m.Source, nil
  334. }
  335. // Deprecated: use cli.Inspect
  336. func inspectMountPoint(name, destination string) (types.MountPoint, error) {
  337. out, err := inspectFilter(name, "json .Mounts")
  338. if err != nil {
  339. return types.MountPoint{}, err
  340. }
  341. return inspectMountPointJSON(out, destination)
  342. }
  343. var errMountNotFound = errors.New("mount point not found")
  344. // Deprecated: use cli.Inspect
  345. func inspectMountPointJSON(j, destination string) (types.MountPoint, error) {
  346. var mp []types.MountPoint
  347. if err := json.Unmarshal([]byte(j), &mp); err != nil {
  348. return types.MountPoint{}, err
  349. }
  350. var m *types.MountPoint
  351. for _, c := range mp {
  352. if c.Destination == destination {
  353. m = &c
  354. break
  355. }
  356. }
  357. if m == nil {
  358. return types.MountPoint{}, errMountNotFound
  359. }
  360. return *m, nil
  361. }
  362. // Deprecated: use cli.Inspect
  363. func inspectImage(c *check.C, name, filter string) string {
  364. args := []string{"inspect", "--type", "image"}
  365. if filter != "" {
  366. format := fmt.Sprintf("{{%s}}", filter)
  367. args = append(args, "-f", format)
  368. }
  369. args = append(args, name)
  370. result := icmd.RunCommand(dockerBinary, args...)
  371. result.Assert(c, icmd.Success)
  372. return strings.TrimSpace(result.Combined())
  373. }
  374. func getIDByName(c *check.C, name string) string {
  375. id, err := inspectFieldWithError(name, "Id")
  376. c.Assert(err, checker.IsNil)
  377. return id
  378. }
  379. // Deprecated: use cli.Build
  380. func buildImageSuccessfully(c *check.C, name string, cmdOperators ...cli.CmdOperator) {
  381. buildImage(name, cmdOperators...).Assert(c, icmd.Success)
  382. }
  383. // Deprecated: use cli.Build
  384. func buildImage(name string, cmdOperators ...cli.CmdOperator) *icmd.Result {
  385. return cli.Docker(cli.Build(name), cmdOperators...)
  386. }
  387. func withExternalBuildContext(ctx *FakeContext) func(*icmd.Cmd) func() {
  388. return func(cmd *icmd.Cmd) func() {
  389. cmd.Dir = ctx.Dir
  390. cmd.Command = append(cmd.Command, ".")
  391. return nil
  392. }
  393. }
  394. func withBuildContext(c *check.C, contextOperators ...func(*FakeContext) error) func(*icmd.Cmd) func() {
  395. ctx := fakeContextFromNewTempDir(c)
  396. for _, op := range contextOperators {
  397. if err := op(ctx); err != nil {
  398. c.Fatal(err)
  399. }
  400. }
  401. return func(cmd *icmd.Cmd) func() {
  402. cmd.Dir = ctx.Dir
  403. cmd.Command = append(cmd.Command, ".")
  404. return closeBuildContext(c, ctx)
  405. }
  406. }
  407. func withFile(name, content string) func(*FakeContext) error {
  408. return func(ctx *FakeContext) error {
  409. return ctx.Add(name, content)
  410. }
  411. }
  412. func closeBuildContext(c *check.C, ctx *FakeContext) func() {
  413. return func() {
  414. if err := ctx.Close(); err != nil {
  415. c.Fatal(err)
  416. }
  417. }
  418. }
  419. func trustedBuild(cmd *icmd.Cmd) func() {
  420. trustedCmd(cmd)
  421. return nil
  422. }
  423. type gitServer interface {
  424. URL() string
  425. Close() error
  426. }
  427. type localGitServer struct {
  428. *httptest.Server
  429. }
  430. func (r *localGitServer) Close() error {
  431. r.Server.Close()
  432. return nil
  433. }
  434. func (r *localGitServer) URL() string {
  435. return r.Server.URL
  436. }
  437. type fakeGit struct {
  438. root string
  439. server gitServer
  440. RepoURL string
  441. }
  442. func (g *fakeGit) Close() {
  443. g.server.Close()
  444. os.RemoveAll(g.root)
  445. }
  446. func newFakeGit(c *check.C, name string, files map[string]string, enforceLocalServer bool) *fakeGit {
  447. ctx := fakeContextWithFiles(c, files)
  448. defer ctx.Close()
  449. curdir, err := os.Getwd()
  450. if err != nil {
  451. c.Fatal(err)
  452. }
  453. defer os.Chdir(curdir)
  454. if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil {
  455. c.Fatalf("error trying to init repo: %s (%s)", err, output)
  456. }
  457. err = os.Chdir(ctx.Dir)
  458. if err != nil {
  459. c.Fatal(err)
  460. }
  461. if output, err := exec.Command("git", "config", "user.name", "Fake User").CombinedOutput(); err != nil {
  462. c.Fatalf("error trying to set 'user.name': %s (%s)", err, output)
  463. }
  464. if output, err := exec.Command("git", "config", "user.email", "fake.user@example.com").CombinedOutput(); err != nil {
  465. c.Fatalf("error trying to set 'user.email': %s (%s)", err, output)
  466. }
  467. if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil {
  468. c.Fatalf("error trying to add files to repo: %s (%s)", err, output)
  469. }
  470. if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil {
  471. c.Fatalf("error trying to commit to repo: %s (%s)", err, output)
  472. }
  473. root, err := ioutil.TempDir("", "docker-test-git-repo")
  474. if err != nil {
  475. c.Fatal(err)
  476. }
  477. repoPath := filepath.Join(root, name+".git")
  478. if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil {
  479. os.RemoveAll(root)
  480. c.Fatalf("error trying to clone --bare: %s (%s)", err, output)
  481. }
  482. err = os.Chdir(repoPath)
  483. if err != nil {
  484. os.RemoveAll(root)
  485. c.Fatal(err)
  486. }
  487. if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil {
  488. os.RemoveAll(root)
  489. c.Fatalf("error trying to git update-server-info: %s (%s)", err, output)
  490. }
  491. err = os.Chdir(curdir)
  492. if err != nil {
  493. os.RemoveAll(root)
  494. c.Fatal(err)
  495. }
  496. var server gitServer
  497. if !enforceLocalServer {
  498. // use fakeStorage server, which might be local or remote (at test daemon)
  499. server = fakeStorageWithContext(c, fakeContextFromDir(root))
  500. } else {
  501. // always start a local http server on CLI test machine
  502. httpServer := httptest.NewServer(http.FileServer(http.Dir(root)))
  503. server = &localGitServer{httpServer}
  504. }
  505. return &fakeGit{
  506. root: root,
  507. server: server,
  508. RepoURL: fmt.Sprintf("%s/%s.git", server.URL(), name),
  509. }
  510. }
  511. // Write `content` to the file at path `dst`, creating it if necessary,
  512. // as well as any missing directories.
  513. // The file is truncated if it already exists.
  514. // Fail the test when error occurs.
  515. func writeFile(dst, content string, c *check.C) {
  516. // Create subdirectories if necessary
  517. c.Assert(os.MkdirAll(path.Dir(dst), 0700), check.IsNil)
  518. f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
  519. c.Assert(err, check.IsNil)
  520. defer f.Close()
  521. // Write content (truncate if it exists)
  522. _, err = io.Copy(f, strings.NewReader(content))
  523. c.Assert(err, check.IsNil)
  524. }
  525. // Return the contents of file at path `src`.
  526. // Fail the test when error occurs.
  527. func readFile(src string, c *check.C) (content string) {
  528. data, err := ioutil.ReadFile(src)
  529. c.Assert(err, check.IsNil)
  530. return string(data)
  531. }
  532. func containerStorageFile(containerID, basename string) string {
  533. return filepath.Join(testEnv.ContainerStoragePath(), containerID, basename)
  534. }
  535. // docker commands that use this function must be run with the '-d' switch.
  536. func runCommandAndReadContainerFile(c *check.C, filename string, command string, args ...string) []byte {
  537. result := icmd.RunCommand(command, args...)
  538. result.Assert(c, icmd.Success)
  539. contID := strings.TrimSpace(result.Combined())
  540. if err := waitRun(contID); err != nil {
  541. c.Fatalf("%v: %q", contID, err)
  542. }
  543. return readContainerFile(c, contID, filename)
  544. }
  545. func readContainerFile(c *check.C, containerID, filename string) []byte {
  546. f, err := os.Open(containerStorageFile(containerID, filename))
  547. c.Assert(err, checker.IsNil)
  548. defer f.Close()
  549. content, err := ioutil.ReadAll(f)
  550. c.Assert(err, checker.IsNil)
  551. return content
  552. }
  553. func readContainerFileWithExec(c *check.C, containerID, filename string) []byte {
  554. result := icmd.RunCommand(dockerBinary, "exec", containerID, "cat", filename)
  555. result.Assert(c, icmd.Success)
  556. return []byte(result.Combined())
  557. }
  558. // daemonTime provides the current time on the daemon host
  559. func daemonTime(c *check.C) time.Time {
  560. if testEnv.LocalDaemon() {
  561. return time.Now()
  562. }
  563. status, body, err := request.SockRequest("GET", "/info", nil, daemonHost())
  564. c.Assert(err, check.IsNil)
  565. c.Assert(status, check.Equals, http.StatusOK)
  566. type infoJSON struct {
  567. SystemTime string
  568. }
  569. var info infoJSON
  570. err = json.Unmarshal(body, &info)
  571. c.Assert(err, check.IsNil, check.Commentf("unable to unmarshal GET /info response"))
  572. dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
  573. c.Assert(err, check.IsNil, check.Commentf("invalid time format in GET /info response"))
  574. return dt
  575. }
  576. // daemonUnixTime returns the current time on the daemon host with nanoseconds precision.
  577. // It return the time formatted how the client sends timestamps to the server.
  578. func daemonUnixTime(c *check.C) string {
  579. return parseEventTime(daemonTime(c))
  580. }
  581. func parseEventTime(t time.Time) string {
  582. return fmt.Sprintf("%d.%09d", t.Unix(), int64(t.Nanosecond()))
  583. }
  584. func setupRegistry(c *check.C, schema1 bool, auth, tokenURL string) *registry.V2 {
  585. reg, err := registry.NewV2(schema1, auth, tokenURL, privateRegistryURL)
  586. c.Assert(err, check.IsNil)
  587. // Wait for registry to be ready to serve requests.
  588. for i := 0; i != 50; i++ {
  589. if err = reg.Ping(); err == nil {
  590. break
  591. }
  592. time.Sleep(100 * time.Millisecond)
  593. }
  594. c.Assert(err, check.IsNil, check.Commentf("Timeout waiting for test registry to become available: %v", err))
  595. return reg
  596. }
  597. func setupNotary(c *check.C) *testNotary {
  598. ts, err := newTestNotary(c)
  599. c.Assert(err, check.IsNil)
  600. return ts
  601. }
  602. // appendBaseEnv appends the minimum set of environment variables to exec the
  603. // docker cli binary for testing with correct configuration to the given env
  604. // list.
  605. func appendBaseEnv(isTLS bool, env ...string) []string {
  606. preserveList := []string{
  607. // preserve remote test host
  608. "DOCKER_HOST",
  609. // windows: requires preserving SystemRoot, otherwise dial tcp fails
  610. // with "GetAddrInfoW: A non-recoverable error occurred during a database lookup."
  611. "SystemRoot",
  612. // testing help text requires the $PATH to dockerd is set
  613. "PATH",
  614. }
  615. if isTLS {
  616. preserveList = append(preserveList, "DOCKER_TLS_VERIFY", "DOCKER_CERT_PATH")
  617. }
  618. for _, key := range preserveList {
  619. if val := os.Getenv(key); val != "" {
  620. env = append(env, fmt.Sprintf("%s=%s", key, val))
  621. }
  622. }
  623. return env
  624. }
  625. func createTmpFile(c *check.C, content string) string {
  626. f, err := ioutil.TempFile("", "testfile")
  627. c.Assert(err, check.IsNil)
  628. filename := f.Name()
  629. err = ioutil.WriteFile(filename, []byte(content), 0644)
  630. c.Assert(err, check.IsNil)
  631. return filename
  632. }
  633. func waitForContainer(contID string, args ...string) error {
  634. args = append([]string{dockerBinary, "run", "--name", contID}, args...)
  635. result := icmd.RunCmd(icmd.Cmd{Command: args})
  636. if result.Error != nil {
  637. return result.Error
  638. }
  639. return waitRun(contID)
  640. }
  641. // waitRestart will wait for the specified container to restart once
  642. func waitRestart(contID string, duration time.Duration) error {
  643. return waitInspect(contID, "{{.RestartCount}}", "1", duration)
  644. }
  645. // waitRun will wait for the specified container to be running, maximum 5 seconds.
  646. func waitRun(contID string) error {
  647. return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)
  648. }
  649. // waitExited will wait for the specified container to state exit, subject
  650. // to a maximum time limit in seconds supplied by the caller
  651. func waitExited(contID string, duration time.Duration) error {
  652. return waitInspect(contID, "{{.State.Status}}", "exited", duration)
  653. }
  654. // waitInspect will wait for the specified container to have the specified string
  655. // in the inspect output. It will wait until the specified timeout (in seconds)
  656. // is reached.
  657. func waitInspect(name, expr, expected string, timeout time.Duration) error {
  658. return waitInspectWithArgs(name, expr, expected, timeout)
  659. }
  660. func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg ...string) error {
  661. return daemon.WaitInspectWithArgs(dockerBinary, name, expr, expected, timeout, arg...)
  662. }
  663. func getInspectBody(c *check.C, version, id string) []byte {
  664. endpoint := fmt.Sprintf("/%s/containers/%s/json", version, id)
  665. status, body, err := request.SockRequest("GET", endpoint, nil, daemonHost())
  666. c.Assert(err, check.IsNil)
  667. c.Assert(status, check.Equals, http.StatusOK)
  668. return body
  669. }
  670. // Run a long running idle task in a background container using the
  671. // system-specific default image and command.
  672. func runSleepingContainer(c *check.C, extraArgs ...string) (string, int) {
  673. return runSleepingContainerInImage(c, defaultSleepImage, extraArgs...)
  674. }
  675. // Run a long running idle task in a background container using the specified
  676. // image and the system-specific command.
  677. func runSleepingContainerInImage(c *check.C, image string, extraArgs ...string) (string, int) {
  678. args := []string{"run", "-d"}
  679. args = append(args, extraArgs...)
  680. args = append(args, image)
  681. args = append(args, sleepCommandForDaemonPlatform()...)
  682. return dockerCmd(c, args...)
  683. }
  684. // minimalBaseImage returns the name of the minimal base image for the current
  685. // daemon platform.
  686. func minimalBaseImage() string {
  687. return testEnv.MinimalBaseImage()
  688. }
  689. func getGoroutineNumber() (int, error) {
  690. i := struct {
  691. NGoroutines int
  692. }{}
  693. status, b, err := request.SockRequest("GET", "/info", nil, daemonHost())
  694. if err != nil {
  695. return 0, err
  696. }
  697. if status != http.StatusOK {
  698. return 0, fmt.Errorf("http status code: %d", status)
  699. }
  700. if err := json.Unmarshal(b, &i); err != nil {
  701. return 0, err
  702. }
  703. return i.NGoroutines, nil
  704. }
  705. func waitForGoroutines(expected int) error {
  706. t := time.After(30 * time.Second)
  707. for {
  708. select {
  709. case <-t:
  710. n, err := getGoroutineNumber()
  711. if err != nil {
  712. return err
  713. }
  714. if n > expected {
  715. return fmt.Errorf("leaked goroutines: expected less than or equal to %d, got: %d", expected, n)
  716. }
  717. default:
  718. n, err := getGoroutineNumber()
  719. if err != nil {
  720. return err
  721. }
  722. if n <= expected {
  723. return nil
  724. }
  725. time.Sleep(200 * time.Millisecond)
  726. }
  727. }
  728. }
  729. // getErrorMessage returns the error message from an error API response
  730. func getErrorMessage(c *check.C, body []byte) string {
  731. var resp types.ErrorResponse
  732. c.Assert(json.Unmarshal(body, &resp), check.IsNil)
  733. return strings.TrimSpace(resp.Message)
  734. }
  735. func waitAndAssert(c *check.C, timeout time.Duration, f checkF, checker check.Checker, args ...interface{}) {
  736. after := time.After(timeout)
  737. for {
  738. v, comment := f(c)
  739. assert, _ := checker.Check(append([]interface{}{v}, args...), checker.Info().Params)
  740. select {
  741. case <-after:
  742. assert = true
  743. default:
  744. }
  745. if assert {
  746. if comment != nil {
  747. args = append(args, comment)
  748. }
  749. c.Assert(v, checker, args...)
  750. return
  751. }
  752. time.Sleep(100 * time.Millisecond)
  753. }
  754. }
  755. type checkF func(*check.C) (interface{}, check.CommentInterface)
  756. type reducer func(...interface{}) interface{}
  757. func reducedCheck(r reducer, funcs ...checkF) checkF {
  758. return func(c *check.C) (interface{}, check.CommentInterface) {
  759. var values []interface{}
  760. var comments []string
  761. for _, f := range funcs {
  762. v, comment := f(c)
  763. values = append(values, v)
  764. if comment != nil {
  765. comments = append(comments, comment.CheckCommentString())
  766. }
  767. }
  768. return r(values...), check.Commentf("%v", strings.Join(comments, ", "))
  769. }
  770. }
  771. func sumAsIntegers(vals ...interface{}) interface{} {
  772. var s int
  773. for _, v := range vals {
  774. s += v.(int)
  775. }
  776. return s
  777. }