docker_utils_test.go 27 KB

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