docker_utils.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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/http/httputil"
  13. "net/url"
  14. "os"
  15. "os/exec"
  16. "path"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. "testing"
  21. "time"
  22. )
  23. // Daemon represents a Docker daemon for the testing framework.
  24. type Daemon struct {
  25. t *testing.T
  26. logFile *os.File
  27. folder string
  28. stdin io.WriteCloser
  29. stdout, stderr io.ReadCloser
  30. cmd *exec.Cmd
  31. storageDriver string
  32. execDriver string
  33. wait chan error
  34. }
  35. // NewDaemon returns a Daemon instance to be used for testing.
  36. // This will create a directory such as daemon123456789 in the folder specified by $DEST.
  37. // The daemon will not automatically start.
  38. func NewDaemon(t *testing.T) *Daemon {
  39. dest := os.Getenv("DEST")
  40. if dest == "" {
  41. t.Fatal("Please set the DEST environment variable")
  42. }
  43. dir := filepath.Join(dest, fmt.Sprintf("daemon%d", time.Now().UnixNano()%100000000))
  44. daemonFolder, err := filepath.Abs(dir)
  45. if err != nil {
  46. t.Fatalf("Could not make %q an absolute path: %v", dir, err)
  47. }
  48. if err := os.MkdirAll(filepath.Join(daemonFolder, "graph"), 0600); err != nil {
  49. t.Fatalf("Could not create %s/graph directory", daemonFolder)
  50. }
  51. return &Daemon{
  52. t: t,
  53. folder: daemonFolder,
  54. storageDriver: os.Getenv("DOCKER_GRAPHDRIVER"),
  55. execDriver: os.Getenv("DOCKER_EXECDRIVER"),
  56. }
  57. }
  58. // Start will start the daemon and return once it is ready to receive requests.
  59. // You can specify additional daemon flags.
  60. func (d *Daemon) Start(arg ...string) error {
  61. dockerBinary, err := exec.LookPath(dockerBinary)
  62. if err != nil {
  63. d.t.Fatalf("could not find docker binary in $PATH: %v", err)
  64. }
  65. args := []string{
  66. "--host", d.sock(),
  67. "--daemon",
  68. "--graph", fmt.Sprintf("%s/graph", d.folder),
  69. "--pidfile", fmt.Sprintf("%s/docker.pid", d.folder),
  70. }
  71. // If we don't explicitly set the log-level or debug flag(-D) then
  72. // turn on debug mode
  73. foundIt := false
  74. for _, a := range arg {
  75. if strings.Contains(a, "--log-level") || strings.Contains(a, "-D") {
  76. foundIt = true
  77. }
  78. }
  79. if !foundIt {
  80. args = append(args, "--debug")
  81. }
  82. if d.storageDriver != "" {
  83. args = append(args, "--storage-driver", d.storageDriver)
  84. }
  85. if d.execDriver != "" {
  86. args = append(args, "--exec-driver", d.execDriver)
  87. }
  88. args = append(args, arg...)
  89. d.cmd = exec.Command(dockerBinary, args...)
  90. d.logFile, err = os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
  91. if err != nil {
  92. d.t.Fatalf("Could not create %s/docker.log: %v", d.folder, err)
  93. }
  94. d.cmd.Stdout = d.logFile
  95. d.cmd.Stderr = d.logFile
  96. if err := d.cmd.Start(); err != nil {
  97. return fmt.Errorf("could not start daemon container: %v", err)
  98. }
  99. wait := make(chan error)
  100. go func() {
  101. wait <- d.cmd.Wait()
  102. d.t.Log("exiting daemon")
  103. close(wait)
  104. }()
  105. d.wait = wait
  106. tick := time.Tick(500 * time.Millisecond)
  107. // make sure daemon is ready to receive requests
  108. startTime := time.Now().Unix()
  109. for {
  110. d.t.Log("waiting for daemon to start")
  111. if time.Now().Unix()-startTime > 5 {
  112. // After 5 seconds, give up
  113. return errors.New("Daemon exited and never started")
  114. }
  115. select {
  116. case <-time.After(2 * time.Second):
  117. return errors.New("timeout: daemon does not respond")
  118. case <-tick:
  119. c, err := net.Dial("unix", filepath.Join(d.folder, "docker.sock"))
  120. if err != nil {
  121. continue
  122. }
  123. client := httputil.NewClientConn(c, nil)
  124. defer client.Close()
  125. req, err := http.NewRequest("GET", "/_ping", nil)
  126. if err != nil {
  127. d.t.Fatalf("could not create new request: %v", err)
  128. }
  129. resp, err := client.Do(req)
  130. if err != nil {
  131. continue
  132. }
  133. if resp.StatusCode != http.StatusOK {
  134. d.t.Logf("received status != 200 OK: %s", resp.Status)
  135. }
  136. d.t.Log("daemon started")
  137. return nil
  138. }
  139. }
  140. }
  141. // StartWithBusybox will first start the daemon with Daemon.Start()
  142. // then save the busybox image from the main daemon and load it into this Daemon instance.
  143. func (d *Daemon) StartWithBusybox(arg ...string) error {
  144. if err := d.Start(arg...); err != nil {
  145. return err
  146. }
  147. bb := filepath.Join(d.folder, "busybox.tar")
  148. if _, err := os.Stat(bb); err != nil {
  149. if !os.IsNotExist(err) {
  150. return fmt.Errorf("unexpected error on busybox.tar stat: %v", err)
  151. }
  152. // saving busybox image from main daemon
  153. if err := exec.Command(dockerBinary, "save", "--output", bb, "busybox:latest").Run(); err != nil {
  154. return fmt.Errorf("could not save busybox image: %v", err)
  155. }
  156. }
  157. // loading busybox image to this daemon
  158. if _, err := d.Cmd("load", "--input", bb); err != nil {
  159. return fmt.Errorf("could not load busybox image: %v", err)
  160. }
  161. if err := os.Remove(bb); err != nil {
  162. d.t.Logf("Could not remove %s: %v", bb, err)
  163. }
  164. return nil
  165. }
  166. // Stop will send a SIGINT every second and wait for the daemon to stop.
  167. // If it timeouts, a SIGKILL is sent.
  168. // Stop will not delete the daemon directory. If a purged daemon is needed,
  169. // instantiate a new one with NewDaemon.
  170. func (d *Daemon) Stop() error {
  171. if d.cmd == nil || d.wait == nil {
  172. return errors.New("daemon not started")
  173. }
  174. defer func() {
  175. d.logFile.Close()
  176. d.cmd = nil
  177. }()
  178. i := 1
  179. tick := time.Tick(time.Second)
  180. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  181. return fmt.Errorf("could not send signal: %v", err)
  182. }
  183. out1:
  184. for {
  185. select {
  186. case err := <-d.wait:
  187. return err
  188. case <-time.After(15 * time.Second):
  189. // time for stopping jobs and run onShutdown hooks
  190. d.t.Log("timeout")
  191. break out1
  192. }
  193. }
  194. out2:
  195. for {
  196. select {
  197. case err := <-d.wait:
  198. return err
  199. case <-tick:
  200. i++
  201. if i > 4 {
  202. d.t.Logf("tried to interrupt daemon for %d times, now try to kill it", i)
  203. break out2
  204. }
  205. d.t.Logf("Attempt #%d: daemon is still running with pid %d", i, d.cmd.Process.Pid)
  206. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  207. return fmt.Errorf("could not send signal: %v", err)
  208. }
  209. }
  210. }
  211. if err := d.cmd.Process.Kill(); err != nil {
  212. d.t.Logf("Could not kill daemon: %v", err)
  213. return err
  214. }
  215. return nil
  216. }
  217. // Restart will restart the daemon by first stopping it and then starting it.
  218. func (d *Daemon) Restart(arg ...string) error {
  219. d.Stop()
  220. return d.Start(arg...)
  221. }
  222. func (d *Daemon) sock() string {
  223. return fmt.Sprintf("unix://%s/docker.sock", d.folder)
  224. }
  225. // Cmd will execute a docker CLI command against this Daemon.
  226. // Example: d.Cmd("version") will run docker -H unix://path/to/unix.sock version
  227. func (d *Daemon) Cmd(name string, arg ...string) (string, error) {
  228. args := []string{"--host", d.sock(), name}
  229. args = append(args, arg...)
  230. c := exec.Command(dockerBinary, args...)
  231. b, err := c.CombinedOutput()
  232. return string(b), err
  233. }
  234. func daemonHost() string {
  235. daemonUrlStr := "unix:///var/run/docker.sock"
  236. if daemonHostVar := os.Getenv("DOCKER_TEST_HOST"); daemonHostVar != "" {
  237. daemonUrlStr = daemonHostVar
  238. }
  239. return daemonUrlStr
  240. }
  241. func sockRequest(method, endpoint string, data interface{}) ([]byte, error) {
  242. jsonData := bytes.NewBuffer(nil)
  243. if err := json.NewEncoder(jsonData).Encode(data); err != nil {
  244. return nil, err
  245. }
  246. return sockRequestRaw(method, endpoint, jsonData, "application/json")
  247. }
  248. func sockRequestRaw(method, endpoint string, data io.Reader, ct string) ([]byte, error) {
  249. daemon := daemonHost()
  250. daemonUrl, err := url.Parse(daemon)
  251. if err != nil {
  252. return nil, fmt.Errorf("could not parse url %q: %v", daemon, err)
  253. }
  254. var c net.Conn
  255. switch daemonUrl.Scheme {
  256. case "unix":
  257. c, err = net.DialTimeout(daemonUrl.Scheme, daemonUrl.Path, time.Duration(10*time.Second))
  258. case "tcp":
  259. c, err = net.DialTimeout(daemonUrl.Scheme, daemonUrl.Host, time.Duration(10*time.Second))
  260. default:
  261. err = fmt.Errorf("unknown scheme %v", daemonUrl.Scheme)
  262. }
  263. if err != nil {
  264. return nil, fmt.Errorf("could not dial docker daemon at %s: %v", daemon, err)
  265. }
  266. client := httputil.NewClientConn(c, nil)
  267. defer client.Close()
  268. req, err := http.NewRequest(method, endpoint, data)
  269. if err != nil {
  270. return nil, fmt.Errorf("could not create new request: %v", err)
  271. }
  272. if ct == "" {
  273. ct = "application/json"
  274. }
  275. req.Header.Set("Content-Type", ct)
  276. resp, err := client.Do(req)
  277. if err != nil {
  278. return nil, fmt.Errorf("could not perform request: %v", err)
  279. }
  280. defer resp.Body.Close()
  281. if resp.StatusCode != http.StatusOK {
  282. body, _ := ioutil.ReadAll(resp.Body)
  283. return body, fmt.Errorf("received status != 200 OK: %s", resp.Status)
  284. }
  285. return ioutil.ReadAll(resp.Body)
  286. }
  287. func deleteContainer(container string) error {
  288. container = strings.Replace(container, "\n", " ", -1)
  289. container = strings.Trim(container, " ")
  290. killArgs := fmt.Sprintf("kill %v", container)
  291. killSplitArgs := strings.Split(killArgs, " ")
  292. killCmd := exec.Command(dockerBinary, killSplitArgs...)
  293. runCommand(killCmd)
  294. rmArgs := fmt.Sprintf("rm -v %v", container)
  295. rmSplitArgs := strings.Split(rmArgs, " ")
  296. rmCmd := exec.Command(dockerBinary, rmSplitArgs...)
  297. exitCode, err := runCommand(rmCmd)
  298. // set error manually if not set
  299. if exitCode != 0 && err == nil {
  300. err = fmt.Errorf("failed to remove container: `docker rm` exit is non-zero")
  301. }
  302. return err
  303. }
  304. func getAllContainers() (string, error) {
  305. getContainersCmd := exec.Command(dockerBinary, "ps", "-q", "-a")
  306. out, exitCode, err := runCommandWithOutput(getContainersCmd)
  307. if exitCode != 0 && err == nil {
  308. err = fmt.Errorf("failed to get a list of containers: %v\n", out)
  309. }
  310. return out, err
  311. }
  312. func deleteAllContainers() error {
  313. containers, err := getAllContainers()
  314. if err != nil {
  315. fmt.Println(containers)
  316. return err
  317. }
  318. if err = deleteContainer(containers); err != nil {
  319. return err
  320. }
  321. return nil
  322. }
  323. func getPausedContainers() (string, error) {
  324. getPausedContainersCmd := exec.Command(dockerBinary, "ps", "-f", "status=paused", "-q", "-a")
  325. out, exitCode, err := runCommandWithOutput(getPausedContainersCmd)
  326. if exitCode != 0 && err == nil {
  327. err = fmt.Errorf("failed to get a list of paused containers: %v\n", out)
  328. }
  329. return out, err
  330. }
  331. func unpauseContainer(container string) error {
  332. unpauseCmd := exec.Command(dockerBinary, "unpause", container)
  333. exitCode, err := runCommand(unpauseCmd)
  334. if exitCode != 0 && err == nil {
  335. err = fmt.Errorf("failed to unpause container")
  336. }
  337. return nil
  338. }
  339. func unpauseAllContainers() error {
  340. containers, err := getPausedContainers()
  341. if err != nil {
  342. fmt.Println(containers)
  343. return err
  344. }
  345. containers = strings.Replace(containers, "\n", " ", -1)
  346. containers = strings.Trim(containers, " ")
  347. containerList := strings.Split(containers, " ")
  348. for _, value := range containerList {
  349. if err = unpauseContainer(value); err != nil {
  350. return err
  351. }
  352. }
  353. return nil
  354. }
  355. func deleteImages(images ...string) error {
  356. args := make([]string, 1, 2)
  357. args[0] = "rmi"
  358. args = append(args, images...)
  359. rmiCmd := exec.Command(dockerBinary, args...)
  360. exitCode, err := runCommand(rmiCmd)
  361. // set error manually if not set
  362. if exitCode != 0 && err == nil {
  363. err = fmt.Errorf("failed to remove image: `docker rmi` exit is non-zero")
  364. }
  365. return err
  366. }
  367. func imageExists(image string) error {
  368. inspectCmd := exec.Command(dockerBinary, "inspect", image)
  369. exitCode, err := runCommand(inspectCmd)
  370. if exitCode != 0 && err == nil {
  371. err = fmt.Errorf("couldn't find image %q", image)
  372. }
  373. return err
  374. }
  375. func pullImageIfNotExist(image string) (err error) {
  376. if err := imageExists(image); err != nil {
  377. pullCmd := exec.Command(dockerBinary, "pull", image)
  378. _, exitCode, err := runCommandWithOutput(pullCmd)
  379. if err != nil || exitCode != 0 {
  380. err = fmt.Errorf("image %q wasn't found locally and it couldn't be pulled: %s", image, err)
  381. }
  382. }
  383. return
  384. }
  385. func dockerCmd(t *testing.T, args ...string) (string, int, error) {
  386. out, status, err := runCommandWithOutput(exec.Command(dockerBinary, args...))
  387. if err != nil {
  388. t.Fatalf("%q failed with errors: %s, %v", strings.Join(args, " "), out, err)
  389. }
  390. return out, status, err
  391. }
  392. // execute a docker ocmmand with a timeout
  393. func dockerCmdWithTimeout(timeout time.Duration, args ...string) (string, int, error) {
  394. out, status, err := runCommandWithOutputAndTimeout(exec.Command(dockerBinary, args...), timeout)
  395. if err != nil {
  396. return out, status, fmt.Errorf("%q failed with errors: %v : %q)", strings.Join(args, " "), err, out)
  397. }
  398. return out, status, err
  399. }
  400. // execute a docker command in a directory
  401. func dockerCmdInDir(t *testing.T, path string, args ...string) (string, int, error) {
  402. dockerCommand := exec.Command(dockerBinary, args...)
  403. dockerCommand.Dir = path
  404. out, status, err := runCommandWithOutput(dockerCommand)
  405. if err != nil {
  406. return out, status, fmt.Errorf("%q failed with errors: %v : %q)", strings.Join(args, " "), err, out)
  407. }
  408. return out, status, err
  409. }
  410. // execute a docker command in a directory with a timeout
  411. func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) (string, int, error) {
  412. dockerCommand := exec.Command(dockerBinary, args...)
  413. dockerCommand.Dir = path
  414. out, status, err := runCommandWithOutputAndTimeout(dockerCommand, timeout)
  415. if err != nil {
  416. return out, status, fmt.Errorf("%q failed with errors: %v : %q)", strings.Join(args, " "), err, out)
  417. }
  418. return out, status, err
  419. }
  420. func findContainerIP(t *testing.T, id string) string {
  421. cmd := exec.Command(dockerBinary, "inspect", "--format='{{ .NetworkSettings.IPAddress }}'", id)
  422. out, _, err := runCommandWithOutput(cmd)
  423. if err != nil {
  424. t.Fatal(err, out)
  425. }
  426. return strings.Trim(out, " \r\n'")
  427. }
  428. func getContainerCount() (int, error) {
  429. const containers = "Containers:"
  430. cmd := exec.Command(dockerBinary, "info")
  431. out, _, err := runCommandWithOutput(cmd)
  432. if err != nil {
  433. return 0, err
  434. }
  435. lines := strings.Split(out, "\n")
  436. for _, line := range lines {
  437. if strings.Contains(line, containers) {
  438. output := stripTrailingCharacters(line)
  439. output = strings.TrimLeft(output, containers)
  440. output = strings.Trim(output, " ")
  441. containerCount, err := strconv.Atoi(output)
  442. if err != nil {
  443. return 0, err
  444. }
  445. return containerCount, nil
  446. }
  447. }
  448. return 0, fmt.Errorf("couldn't find the Container count in the output")
  449. }
  450. type FakeContext struct {
  451. Dir string
  452. }
  453. func (f *FakeContext) Add(file, content string) error {
  454. filepath := path.Join(f.Dir, file)
  455. dirpath := path.Dir(filepath)
  456. if dirpath != "." {
  457. if err := os.MkdirAll(dirpath, 0755); err != nil {
  458. return err
  459. }
  460. }
  461. return ioutil.WriteFile(filepath, []byte(content), 0644)
  462. }
  463. func (f *FakeContext) Delete(file string) error {
  464. filepath := path.Join(f.Dir, file)
  465. return os.RemoveAll(filepath)
  466. }
  467. func (f *FakeContext) Close() error {
  468. return os.RemoveAll(f.Dir)
  469. }
  470. func fakeContext(dockerfile string, files map[string]string) (*FakeContext, error) {
  471. tmp, err := ioutil.TempDir("", "fake-context")
  472. if err != nil {
  473. return nil, err
  474. }
  475. if err := os.Chmod(tmp, 0755); err != nil {
  476. return nil, err
  477. }
  478. ctx := &FakeContext{tmp}
  479. for file, content := range files {
  480. if err := ctx.Add(file, content); err != nil {
  481. ctx.Close()
  482. return nil, err
  483. }
  484. }
  485. if err := ctx.Add("Dockerfile", dockerfile); err != nil {
  486. ctx.Close()
  487. return nil, err
  488. }
  489. return ctx, nil
  490. }
  491. type FakeStorage struct {
  492. *FakeContext
  493. *httptest.Server
  494. }
  495. func (f *FakeStorage) Close() error {
  496. f.Server.Close()
  497. return f.FakeContext.Close()
  498. }
  499. func fakeStorage(files map[string]string) (*FakeStorage, error) {
  500. tmp, err := ioutil.TempDir("", "fake-storage")
  501. if err != nil {
  502. return nil, err
  503. }
  504. ctx := &FakeContext{tmp}
  505. for file, content := range files {
  506. if err := ctx.Add(file, content); err != nil {
  507. ctx.Close()
  508. return nil, err
  509. }
  510. }
  511. handler := http.FileServer(http.Dir(ctx.Dir))
  512. server := httptest.NewServer(handler)
  513. return &FakeStorage{
  514. FakeContext: ctx,
  515. Server: server,
  516. }, nil
  517. }
  518. func inspectField(name, field string) (string, error) {
  519. format := fmt.Sprintf("{{.%s}}", field)
  520. inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
  521. out, exitCode, err := runCommandWithOutput(inspectCmd)
  522. if err != nil || exitCode != 0 {
  523. return "", fmt.Errorf("failed to inspect %s: %s", name, out)
  524. }
  525. return strings.TrimSpace(out), nil
  526. }
  527. func inspectFieldJSON(name, field string) (string, error) {
  528. format := fmt.Sprintf("{{json .%s}}", field)
  529. inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
  530. out, exitCode, err := runCommandWithOutput(inspectCmd)
  531. if err != nil || exitCode != 0 {
  532. return "", fmt.Errorf("failed to inspect %s: %s", name, out)
  533. }
  534. return strings.TrimSpace(out), nil
  535. }
  536. func inspectFieldMap(name, path, field string) (string, error) {
  537. format := fmt.Sprintf("{{index .%s %q}}", path, field)
  538. inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
  539. out, exitCode, err := runCommandWithOutput(inspectCmd)
  540. if err != nil || exitCode != 0 {
  541. return "", fmt.Errorf("failed to inspect %s: %s", name, out)
  542. }
  543. return strings.TrimSpace(out), nil
  544. }
  545. func getIDByName(name string) (string, error) {
  546. return inspectField(name, "Id")
  547. }
  548. // getContainerState returns the exit code of the container
  549. // and true if it's running
  550. // the exit code should be ignored if it's running
  551. func getContainerState(t *testing.T, id string) (int, bool, error) {
  552. var (
  553. exitStatus int
  554. running bool
  555. )
  556. out, exitCode, err := dockerCmd(t, "inspect", "--format={{.State.Running}} {{.State.ExitCode}}", id)
  557. if err != nil || exitCode != 0 {
  558. return 0, false, fmt.Errorf("%q doesn't exist: %s", id, err)
  559. }
  560. out = strings.Trim(out, "\n")
  561. splitOutput := strings.Split(out, " ")
  562. if len(splitOutput) != 2 {
  563. return 0, false, fmt.Errorf("failed to get container state: output is broken")
  564. }
  565. if splitOutput[0] == "true" {
  566. running = true
  567. }
  568. if n, err := strconv.Atoi(splitOutput[1]); err == nil {
  569. exitStatus = n
  570. } else {
  571. return 0, false, fmt.Errorf("failed to get container state: couldn't parse integer")
  572. }
  573. return exitStatus, running, nil
  574. }
  575. func buildImageWithOut(name, dockerfile string, useCache bool) (string, string, error) {
  576. args := []string{"build", "-t", name}
  577. if !useCache {
  578. args = append(args, "--no-cache")
  579. }
  580. args = append(args, "-")
  581. buildCmd := exec.Command(dockerBinary, args...)
  582. buildCmd.Stdin = strings.NewReader(dockerfile)
  583. out, exitCode, err := runCommandWithOutput(buildCmd)
  584. if err != nil || exitCode != 0 {
  585. return "", out, fmt.Errorf("failed to build the image: %s", out)
  586. }
  587. id, err := getIDByName(name)
  588. if err != nil {
  589. return "", out, err
  590. }
  591. return id, out, nil
  592. }
  593. func buildImageWithStdoutStderr(name, dockerfile string, useCache bool) (string, string, string, error) {
  594. args := []string{"build", "-t", name}
  595. if !useCache {
  596. args = append(args, "--no-cache")
  597. }
  598. args = append(args, "-")
  599. buildCmd := exec.Command(dockerBinary, args...)
  600. buildCmd.Stdin = strings.NewReader(dockerfile)
  601. stdout, stderr, exitCode, err := runCommandWithStdoutStderr(buildCmd)
  602. if err != nil || exitCode != 0 {
  603. return "", stdout, stderr, fmt.Errorf("failed to build the image: %s", stdout)
  604. }
  605. id, err := getIDByName(name)
  606. if err != nil {
  607. return "", stdout, stderr, err
  608. }
  609. return id, stdout, stderr, nil
  610. }
  611. func buildImage(name, dockerfile string, useCache bool) (string, error) {
  612. id, _, err := buildImageWithOut(name, dockerfile, useCache)
  613. return id, err
  614. }
  615. func buildImageFromContext(name string, ctx *FakeContext, useCache bool) (string, error) {
  616. args := []string{"build", "-t", name}
  617. if !useCache {
  618. args = append(args, "--no-cache")
  619. }
  620. args = append(args, ".")
  621. buildCmd := exec.Command(dockerBinary, args...)
  622. buildCmd.Dir = ctx.Dir
  623. out, exitCode, err := runCommandWithOutput(buildCmd)
  624. if err != nil || exitCode != 0 {
  625. return "", fmt.Errorf("failed to build the image: %s", out)
  626. }
  627. return getIDByName(name)
  628. }
  629. func buildImageFromPath(name, path string, useCache bool) (string, error) {
  630. args := []string{"build", "-t", name}
  631. if !useCache {
  632. args = append(args, "--no-cache")
  633. }
  634. args = append(args, path)
  635. buildCmd := exec.Command(dockerBinary, args...)
  636. out, exitCode, err := runCommandWithOutput(buildCmd)
  637. if err != nil || exitCode != 0 {
  638. return "", fmt.Errorf("failed to build the image: %s", out)
  639. }
  640. return getIDByName(name)
  641. }
  642. type FakeGIT struct {
  643. *httptest.Server
  644. Root string
  645. RepoURL string
  646. }
  647. func (g *FakeGIT) Close() {
  648. g.Server.Close()
  649. os.RemoveAll(g.Root)
  650. }
  651. func fakeGIT(name string, files map[string]string) (*FakeGIT, error) {
  652. tmp, err := ioutil.TempDir("", "fake-git-repo")
  653. if err != nil {
  654. return nil, err
  655. }
  656. ctx := &FakeContext{tmp}
  657. for file, content := range files {
  658. if err := ctx.Add(file, content); err != nil {
  659. ctx.Close()
  660. return nil, err
  661. }
  662. }
  663. defer ctx.Close()
  664. curdir, err := os.Getwd()
  665. if err != nil {
  666. return nil, err
  667. }
  668. defer os.Chdir(curdir)
  669. if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil {
  670. return nil, fmt.Errorf("error trying to init repo: %s (%s)", err, output)
  671. }
  672. err = os.Chdir(ctx.Dir)
  673. if err != nil {
  674. return nil, err
  675. }
  676. if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil {
  677. return nil, fmt.Errorf("error trying to add files to repo: %s (%s)", err, output)
  678. }
  679. if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil {
  680. return nil, fmt.Errorf("error trying to commit to repo: %s (%s)", err, output)
  681. }
  682. root, err := ioutil.TempDir("", "docker-test-git-repo")
  683. if err != nil {
  684. return nil, err
  685. }
  686. repoPath := filepath.Join(root, name+".git")
  687. if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil {
  688. os.RemoveAll(root)
  689. return nil, fmt.Errorf("error trying to clone --bare: %s (%s)", err, output)
  690. }
  691. err = os.Chdir(repoPath)
  692. if err != nil {
  693. os.RemoveAll(root)
  694. return nil, err
  695. }
  696. if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil {
  697. os.RemoveAll(root)
  698. return nil, fmt.Errorf("error trying to git update-server-info: %s (%s)", err, output)
  699. }
  700. err = os.Chdir(curdir)
  701. if err != nil {
  702. os.RemoveAll(root)
  703. return nil, err
  704. }
  705. handler := http.FileServer(http.Dir(root))
  706. server := httptest.NewServer(handler)
  707. return &FakeGIT{
  708. Server: server,
  709. Root: root,
  710. RepoURL: fmt.Sprintf("%s/%s.git", server.URL, name),
  711. }, nil
  712. }
  713. // Write `content` to the file at path `dst`, creating it if necessary,
  714. // as well as any missing directories.
  715. // The file is truncated if it already exists.
  716. // Call t.Fatal() at the first error.
  717. func writeFile(dst, content string, t *testing.T) {
  718. // Create subdirectories if necessary
  719. if err := os.MkdirAll(path.Dir(dst), 0700); err != nil && !os.IsExist(err) {
  720. t.Fatal(err)
  721. }
  722. f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
  723. if err != nil {
  724. t.Fatal(err)
  725. }
  726. // Write content (truncate if it exists)
  727. if _, err := io.Copy(f, strings.NewReader(content)); err != nil {
  728. t.Fatal(err)
  729. }
  730. }
  731. // Return the contents of file at path `src`.
  732. // Call t.Fatal() at the first error (including if the file doesn't exist)
  733. func readFile(src string, t *testing.T) (content string) {
  734. f, err := os.Open(src)
  735. if err != nil {
  736. t.Fatal(err)
  737. }
  738. data, err := ioutil.ReadAll(f)
  739. if err != nil {
  740. t.Fatal(err)
  741. }
  742. return string(data)
  743. }
  744. func containerStorageFile(containerId, basename string) string {
  745. return filepath.Join("/var/lib/docker/containers", containerId, basename)
  746. }
  747. // docker commands that use this function must be run with the '-d' switch.
  748. func runCommandAndReadContainerFile(filename string, cmd *exec.Cmd) ([]byte, error) {
  749. out, _, err := runCommandWithOutput(cmd)
  750. if err != nil {
  751. return nil, fmt.Errorf("%v: %q", err, out)
  752. }
  753. time.Sleep(1 * time.Second)
  754. contID := strings.TrimSpace(out)
  755. return readContainerFile(contID, filename)
  756. }
  757. func readContainerFile(containerId, filename string) ([]byte, error) {
  758. f, err := os.Open(containerStorageFile(containerId, filename))
  759. if err != nil {
  760. return nil, err
  761. }
  762. defer f.Close()
  763. content, err := ioutil.ReadAll(f)
  764. if err != nil {
  765. return nil, err
  766. }
  767. return content, nil
  768. }
  769. func setupRegistry(t *testing.T) func() {
  770. reg, err := newTestRegistryV2(t)
  771. if err != nil {
  772. t.Fatal(err)
  773. }
  774. // Wait for registry to be ready to serve requests.
  775. for i := 0; i != 5; i++ {
  776. if err = reg.Ping(); err == nil {
  777. break
  778. }
  779. time.Sleep(100 * time.Millisecond)
  780. }
  781. if err != nil {
  782. t.Fatal("Timeout waiting for test registry to become available")
  783. }
  784. return func() { reg.Close() }
  785. }
  786. // appendDockerHostEnv adds given env slice DOCKER_HOST value if set in the
  787. // environment. Useful when environment is cleared but we want to preserve DOCKER_HOST
  788. // to execute tests against a remote daemon.
  789. func appendDockerHostEnv(env []string) []string {
  790. if dockerHost := os.Getenv("DOCKER_HOST"); dockerHost != "" {
  791. env = append(env, fmt.Sprintf("DOCKER_HOST=%s", dockerHost))
  792. }
  793. return env
  794. }