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 getSliceOfPausedContainers() ([]string, error) {
  332. out, err := getPausedContainers()
  333. if err == nil {
  334. slice := strings.Split(strings.TrimSpace(out), "\n")
  335. return slice, err
  336. } else {
  337. return []string{out}, err
  338. }
  339. }
  340. func unpauseContainer(container string) error {
  341. unpauseCmd := exec.Command(dockerBinary, "unpause", container)
  342. exitCode, err := runCommand(unpauseCmd)
  343. if exitCode != 0 && err == nil {
  344. err = fmt.Errorf("failed to unpause container")
  345. }
  346. return nil
  347. }
  348. func unpauseAllContainers() error {
  349. containers, err := getPausedContainers()
  350. if err != nil {
  351. fmt.Println(containers)
  352. return err
  353. }
  354. containers = strings.Replace(containers, "\n", " ", -1)
  355. containers = strings.Trim(containers, " ")
  356. containerList := strings.Split(containers, " ")
  357. for _, value := range containerList {
  358. if err = unpauseContainer(value); err != nil {
  359. return err
  360. }
  361. }
  362. return nil
  363. }
  364. func deleteImages(images ...string) error {
  365. args := make([]string, 1, 2)
  366. args[0] = "rmi"
  367. args = append(args, images...)
  368. rmiCmd := exec.Command(dockerBinary, args...)
  369. exitCode, err := runCommand(rmiCmd)
  370. // set error manually if not set
  371. if exitCode != 0 && err == nil {
  372. err = fmt.Errorf("failed to remove image: `docker rmi` exit is non-zero")
  373. }
  374. return err
  375. }
  376. func imageExists(image string) error {
  377. inspectCmd := exec.Command(dockerBinary, "inspect", image)
  378. exitCode, err := runCommand(inspectCmd)
  379. if exitCode != 0 && err == nil {
  380. err = fmt.Errorf("couldn't find image %q", image)
  381. }
  382. return err
  383. }
  384. func pullImageIfNotExist(image string) (err error) {
  385. if err := imageExists(image); err != nil {
  386. pullCmd := exec.Command(dockerBinary, "pull", image)
  387. _, exitCode, err := runCommandWithOutput(pullCmd)
  388. if err != nil || exitCode != 0 {
  389. err = fmt.Errorf("image %q wasn't found locally and it couldn't be pulled: %s", image, err)
  390. }
  391. }
  392. return
  393. }
  394. func dockerCmd(t *testing.T, args ...string) (string, int, error) {
  395. out, status, err := runCommandWithOutput(exec.Command(dockerBinary, args...))
  396. if err != nil {
  397. t.Fatalf("%q failed with errors: %s, %v", strings.Join(args, " "), out, err)
  398. }
  399. return out, status, err
  400. }
  401. // execute a docker ocmmand with a timeout
  402. func dockerCmdWithTimeout(timeout time.Duration, args ...string) (string, int, error) {
  403. out, status, err := runCommandWithOutputAndTimeout(exec.Command(dockerBinary, args...), timeout)
  404. if err != nil {
  405. return out, status, fmt.Errorf("%q failed with errors: %v : %q)", strings.Join(args, " "), err, out)
  406. }
  407. return out, status, err
  408. }
  409. // execute a docker command in a directory
  410. func dockerCmdInDir(t *testing.T, path string, args ...string) (string, int, error) {
  411. dockerCommand := exec.Command(dockerBinary, args...)
  412. dockerCommand.Dir = path
  413. out, status, err := runCommandWithOutput(dockerCommand)
  414. if err != nil {
  415. return out, status, fmt.Errorf("%q failed with errors: %v : %q)", strings.Join(args, " "), err, out)
  416. }
  417. return out, status, err
  418. }
  419. // execute a docker command in a directory with a timeout
  420. func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) (string, int, error) {
  421. dockerCommand := exec.Command(dockerBinary, args...)
  422. dockerCommand.Dir = path
  423. out, status, err := runCommandWithOutputAndTimeout(dockerCommand, timeout)
  424. if err != nil {
  425. return out, status, fmt.Errorf("%q failed with errors: %v : %q)", strings.Join(args, " "), err, out)
  426. }
  427. return out, status, err
  428. }
  429. func findContainerIP(t *testing.T, id string) string {
  430. cmd := exec.Command(dockerBinary, "inspect", "--format='{{ .NetworkSettings.IPAddress }}'", id)
  431. out, _, err := runCommandWithOutput(cmd)
  432. if err != nil {
  433. t.Fatal(err, out)
  434. }
  435. return strings.Trim(out, " \r\n'")
  436. }
  437. func getContainerCount() (int, error) {
  438. const containers = "Containers:"
  439. cmd := exec.Command(dockerBinary, "info")
  440. out, _, err := runCommandWithOutput(cmd)
  441. if err != nil {
  442. return 0, err
  443. }
  444. lines := strings.Split(out, "\n")
  445. for _, line := range lines {
  446. if strings.Contains(line, containers) {
  447. output := stripTrailingCharacters(line)
  448. output = strings.TrimLeft(output, containers)
  449. output = strings.Trim(output, " ")
  450. containerCount, err := strconv.Atoi(output)
  451. if err != nil {
  452. return 0, err
  453. }
  454. return containerCount, nil
  455. }
  456. }
  457. return 0, fmt.Errorf("couldn't find the Container count in the output")
  458. }
  459. type FakeContext struct {
  460. Dir string
  461. }
  462. func (f *FakeContext) Add(file, content string) error {
  463. filepath := path.Join(f.Dir, file)
  464. dirpath := path.Dir(filepath)
  465. if dirpath != "." {
  466. if err := os.MkdirAll(dirpath, 0755); err != nil {
  467. return err
  468. }
  469. }
  470. return ioutil.WriteFile(filepath, []byte(content), 0644)
  471. }
  472. func (f *FakeContext) Delete(file string) error {
  473. filepath := path.Join(f.Dir, file)
  474. return os.RemoveAll(filepath)
  475. }
  476. func (f *FakeContext) Close() error {
  477. return os.RemoveAll(f.Dir)
  478. }
  479. func fakeContext(dockerfile string, files map[string]string) (*FakeContext, error) {
  480. tmp, err := ioutil.TempDir("", "fake-context")
  481. if err != nil {
  482. return nil, err
  483. }
  484. if err := os.Chmod(tmp, 0755); err != nil {
  485. return nil, err
  486. }
  487. ctx := &FakeContext{tmp}
  488. for file, content := range files {
  489. if err := ctx.Add(file, content); err != nil {
  490. ctx.Close()
  491. return nil, err
  492. }
  493. }
  494. if err := ctx.Add("Dockerfile", dockerfile); err != nil {
  495. ctx.Close()
  496. return nil, err
  497. }
  498. return ctx, nil
  499. }
  500. type FakeStorage struct {
  501. *FakeContext
  502. *httptest.Server
  503. }
  504. func (f *FakeStorage) Close() error {
  505. f.Server.Close()
  506. return f.FakeContext.Close()
  507. }
  508. func fakeStorage(files map[string]string) (*FakeStorage, error) {
  509. tmp, err := ioutil.TempDir("", "fake-storage")
  510. if err != nil {
  511. return nil, err
  512. }
  513. ctx := &FakeContext{tmp}
  514. for file, content := range files {
  515. if err := ctx.Add(file, content); err != nil {
  516. ctx.Close()
  517. return nil, err
  518. }
  519. }
  520. handler := http.FileServer(http.Dir(ctx.Dir))
  521. server := httptest.NewServer(handler)
  522. return &FakeStorage{
  523. FakeContext: ctx,
  524. Server: server,
  525. }, nil
  526. }
  527. func inspectField(name, field string) (string, error) {
  528. format := fmt.Sprintf("{{.%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 inspectFieldJSON(name, field string) (string, error) {
  537. format := fmt.Sprintf("{{json .%s}}", 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 inspectFieldMap(name, path, field string) (string, error) {
  546. format := fmt.Sprintf("{{index .%s %q}}", path, field)
  547. inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
  548. out, exitCode, err := runCommandWithOutput(inspectCmd)
  549. if err != nil || exitCode != 0 {
  550. return "", fmt.Errorf("failed to inspect %s: %s", name, out)
  551. }
  552. return strings.TrimSpace(out), nil
  553. }
  554. func getIDByName(name string) (string, error) {
  555. return inspectField(name, "Id")
  556. }
  557. // getContainerState returns the exit code of the container
  558. // and true if it's running
  559. // the exit code should be ignored if it's running
  560. func getContainerState(t *testing.T, id string) (int, bool, error) {
  561. var (
  562. exitStatus int
  563. running bool
  564. )
  565. out, exitCode, err := dockerCmd(t, "inspect", "--format={{.State.Running}} {{.State.ExitCode}}", id)
  566. if err != nil || exitCode != 0 {
  567. return 0, false, fmt.Errorf("%q doesn't exist: %s", id, err)
  568. }
  569. out = strings.Trim(out, "\n")
  570. splitOutput := strings.Split(out, " ")
  571. if len(splitOutput) != 2 {
  572. return 0, false, fmt.Errorf("failed to get container state: output is broken")
  573. }
  574. if splitOutput[0] == "true" {
  575. running = true
  576. }
  577. if n, err := strconv.Atoi(splitOutput[1]); err == nil {
  578. exitStatus = n
  579. } else {
  580. return 0, false, fmt.Errorf("failed to get container state: couldn't parse integer")
  581. }
  582. return exitStatus, running, nil
  583. }
  584. func buildImageWithOut(name, dockerfile string, useCache bool) (string, string, error) {
  585. args := []string{"build", "-t", name}
  586. if !useCache {
  587. args = append(args, "--no-cache")
  588. }
  589. args = append(args, "-")
  590. buildCmd := exec.Command(dockerBinary, args...)
  591. buildCmd.Stdin = strings.NewReader(dockerfile)
  592. out, exitCode, err := runCommandWithOutput(buildCmd)
  593. if err != nil || exitCode != 0 {
  594. return "", out, fmt.Errorf("failed to build the image: %s", out)
  595. }
  596. id, err := getIDByName(name)
  597. if err != nil {
  598. return "", out, err
  599. }
  600. return id, out, nil
  601. }
  602. func buildImageWithStdoutStderr(name, dockerfile string, useCache bool) (string, string, string, error) {
  603. args := []string{"build", "-t", name}
  604. if !useCache {
  605. args = append(args, "--no-cache")
  606. }
  607. args = append(args, "-")
  608. buildCmd := exec.Command(dockerBinary, args...)
  609. buildCmd.Stdin = strings.NewReader(dockerfile)
  610. stdout, stderr, exitCode, err := runCommandWithStdoutStderr(buildCmd)
  611. if err != nil || exitCode != 0 {
  612. return "", stdout, stderr, fmt.Errorf("failed to build the image: %s", stdout)
  613. }
  614. id, err := getIDByName(name)
  615. if err != nil {
  616. return "", stdout, stderr, err
  617. }
  618. return id, stdout, stderr, nil
  619. }
  620. func buildImage(name, dockerfile string, useCache bool) (string, error) {
  621. id, _, err := buildImageWithOut(name, dockerfile, useCache)
  622. return id, err
  623. }
  624. func buildImageFromContext(name string, ctx *FakeContext, useCache bool) (string, error) {
  625. args := []string{"build", "-t", name}
  626. if !useCache {
  627. args = append(args, "--no-cache")
  628. }
  629. args = append(args, ".")
  630. buildCmd := exec.Command(dockerBinary, args...)
  631. buildCmd.Dir = ctx.Dir
  632. out, exitCode, err := runCommandWithOutput(buildCmd)
  633. if err != nil || exitCode != 0 {
  634. return "", fmt.Errorf("failed to build the image: %s", out)
  635. }
  636. return getIDByName(name)
  637. }
  638. func buildImageFromPath(name, path string, useCache bool) (string, error) {
  639. args := []string{"build", "-t", name}
  640. if !useCache {
  641. args = append(args, "--no-cache")
  642. }
  643. args = append(args, path)
  644. buildCmd := exec.Command(dockerBinary, args...)
  645. out, exitCode, err := runCommandWithOutput(buildCmd)
  646. if err != nil || exitCode != 0 {
  647. return "", fmt.Errorf("failed to build the image: %s", out)
  648. }
  649. return getIDByName(name)
  650. }
  651. type FakeGIT struct {
  652. *httptest.Server
  653. Root string
  654. RepoURL string
  655. }
  656. func (g *FakeGIT) Close() {
  657. g.Server.Close()
  658. os.RemoveAll(g.Root)
  659. }
  660. func fakeGIT(name string, files map[string]string) (*FakeGIT, error) {
  661. tmp, err := ioutil.TempDir("", "fake-git-repo")
  662. if err != nil {
  663. return nil, err
  664. }
  665. ctx := &FakeContext{tmp}
  666. for file, content := range files {
  667. if err := ctx.Add(file, content); err != nil {
  668. ctx.Close()
  669. return nil, err
  670. }
  671. }
  672. defer ctx.Close()
  673. curdir, err := os.Getwd()
  674. if err != nil {
  675. return nil, err
  676. }
  677. defer os.Chdir(curdir)
  678. if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil {
  679. return nil, fmt.Errorf("error trying to init repo: %s (%s)", err, output)
  680. }
  681. err = os.Chdir(ctx.Dir)
  682. if err != nil {
  683. return nil, err
  684. }
  685. if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil {
  686. return nil, fmt.Errorf("error trying to add files to repo: %s (%s)", err, output)
  687. }
  688. if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil {
  689. return nil, fmt.Errorf("error trying to commit to repo: %s (%s)", err, output)
  690. }
  691. root, err := ioutil.TempDir("", "docker-test-git-repo")
  692. if err != nil {
  693. return nil, err
  694. }
  695. repoPath := filepath.Join(root, name+".git")
  696. if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil {
  697. os.RemoveAll(root)
  698. return nil, fmt.Errorf("error trying to clone --bare: %s (%s)", err, output)
  699. }
  700. err = os.Chdir(repoPath)
  701. if err != nil {
  702. os.RemoveAll(root)
  703. return nil, err
  704. }
  705. if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil {
  706. os.RemoveAll(root)
  707. return nil, fmt.Errorf("error trying to git update-server-info: %s (%s)", err, output)
  708. }
  709. err = os.Chdir(curdir)
  710. if err != nil {
  711. os.RemoveAll(root)
  712. return nil, err
  713. }
  714. handler := http.FileServer(http.Dir(root))
  715. server := httptest.NewServer(handler)
  716. return &FakeGIT{
  717. Server: server,
  718. Root: root,
  719. RepoURL: fmt.Sprintf("%s/%s.git", server.URL, name),
  720. }, nil
  721. }
  722. // Write `content` to the file at path `dst`, creating it if necessary,
  723. // as well as any missing directories.
  724. // The file is truncated if it already exists.
  725. // Call t.Fatal() at the first error.
  726. func writeFile(dst, content string, t *testing.T) {
  727. // Create subdirectories if necessary
  728. if err := os.MkdirAll(path.Dir(dst), 0700); err != nil && !os.IsExist(err) {
  729. t.Fatal(err)
  730. }
  731. f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
  732. if err != nil {
  733. t.Fatal(err)
  734. }
  735. // Write content (truncate if it exists)
  736. if _, err := io.Copy(f, strings.NewReader(content)); err != nil {
  737. t.Fatal(err)
  738. }
  739. }
  740. // Return the contents of file at path `src`.
  741. // Call t.Fatal() at the first error (including if the file doesn't exist)
  742. func readFile(src string, t *testing.T) (content string) {
  743. f, err := os.Open(src)
  744. if err != nil {
  745. t.Fatal(err)
  746. }
  747. data, err := ioutil.ReadAll(f)
  748. if err != nil {
  749. t.Fatal(err)
  750. }
  751. return string(data)
  752. }
  753. func containerStorageFile(containerId, basename string) string {
  754. return filepath.Join("/var/lib/docker/containers", containerId, basename)
  755. }
  756. // docker commands that use this function must be run with the '-d' switch.
  757. func runCommandAndReadContainerFile(filename string, cmd *exec.Cmd) ([]byte, error) {
  758. out, _, err := runCommandWithOutput(cmd)
  759. if err != nil {
  760. return nil, fmt.Errorf("%v: %q", err, out)
  761. }
  762. time.Sleep(1 * time.Second)
  763. contID := strings.TrimSpace(out)
  764. return readContainerFile(contID, filename)
  765. }
  766. func readContainerFile(containerId, filename string) ([]byte, error) {
  767. f, err := os.Open(containerStorageFile(containerId, filename))
  768. if err != nil {
  769. return nil, err
  770. }
  771. defer f.Close()
  772. content, err := ioutil.ReadAll(f)
  773. if err != nil {
  774. return nil, err
  775. }
  776. return content, nil
  777. }
  778. func setupRegistry(t *testing.T) func() {
  779. reg, err := newTestRegistryV2(t)
  780. if err != nil {
  781. t.Fatal(err)
  782. }
  783. // Wait for registry to be ready to serve requests.
  784. for i := 0; i != 5; i++ {
  785. if err = reg.Ping(); err == nil {
  786. break
  787. }
  788. time.Sleep(100 * time.Millisecond)
  789. }
  790. if err != nil {
  791. t.Fatal("Timeout waiting for test registry to become available")
  792. }
  793. return func() { reg.Close() }
  794. }