docker_utils.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. "time"
  21. "github.com/docker/docker/opts"
  22. "github.com/docker/docker/pkg/ioutils"
  23. "github.com/docker/docker/pkg/stringutils"
  24. "github.com/go-check/check"
  25. )
  26. // Daemon represents a Docker daemon for the testing framework.
  27. type Daemon struct {
  28. c *check.C
  29. logFile *os.File
  30. folder string
  31. stdin io.WriteCloser
  32. stdout, stderr io.ReadCloser
  33. cmd *exec.Cmd
  34. storageDriver string
  35. execDriver string
  36. wait chan error
  37. }
  38. // NewDaemon returns a Daemon instance to be used for testing.
  39. // This will create a directory such as daemon123456789 in the folder specified by $DEST.
  40. // The daemon will not automatically start.
  41. func NewDaemon(c *check.C) *Daemon {
  42. dest := os.Getenv("DEST")
  43. if dest == "" {
  44. c.Fatal("Please set the DEST environment variable")
  45. }
  46. dir := filepath.Join(dest, fmt.Sprintf("daemon%d", time.Now().UnixNano()%100000000))
  47. daemonFolder, err := filepath.Abs(dir)
  48. if err != nil {
  49. c.Fatalf("Could not make %q an absolute path: %v", dir, err)
  50. }
  51. if err := os.MkdirAll(filepath.Join(daemonFolder, "graph"), 0600); err != nil {
  52. c.Fatalf("Could not create %s/graph directory", daemonFolder)
  53. }
  54. return &Daemon{
  55. c: c,
  56. folder: daemonFolder,
  57. storageDriver: os.Getenv("DOCKER_GRAPHDRIVER"),
  58. execDriver: os.Getenv("DOCKER_EXECDRIVER"),
  59. }
  60. }
  61. // Start will start the daemon and return once it is ready to receive requests.
  62. // You can specify additional daemon flags.
  63. func (d *Daemon) Start(arg ...string) error {
  64. dockerBinary, err := exec.LookPath(dockerBinary)
  65. if err != nil {
  66. d.c.Fatalf("could not find docker binary in $PATH: %v", err)
  67. }
  68. args := []string{
  69. "--host", d.sock(),
  70. "--daemon",
  71. "--graph", fmt.Sprintf("%s/graph", d.folder),
  72. "--pidfile", fmt.Sprintf("%s/docker.pid", d.folder),
  73. }
  74. // If we don't explicitly set the log-level or debug flag(-D) then
  75. // turn on debug mode
  76. foundIt := false
  77. for _, a := range arg {
  78. if strings.Contains(a, "--log-level") || strings.Contains(a, "-D") {
  79. foundIt = true
  80. }
  81. }
  82. if !foundIt {
  83. args = append(args, "--debug")
  84. }
  85. if d.storageDriver != "" {
  86. args = append(args, "--storage-driver", d.storageDriver)
  87. }
  88. if d.execDriver != "" {
  89. args = append(args, "--exec-driver", d.execDriver)
  90. }
  91. args = append(args, arg...)
  92. d.cmd = exec.Command(dockerBinary, args...)
  93. d.logFile, err = os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
  94. if err != nil {
  95. d.c.Fatalf("Could not create %s/docker.log: %v", d.folder, err)
  96. }
  97. d.cmd.Stdout = d.logFile
  98. d.cmd.Stderr = d.logFile
  99. if err := d.cmd.Start(); err != nil {
  100. return fmt.Errorf("could not start daemon container: %v", err)
  101. }
  102. wait := make(chan error)
  103. go func() {
  104. wait <- d.cmd.Wait()
  105. d.c.Log("exiting daemon")
  106. close(wait)
  107. }()
  108. d.wait = wait
  109. tick := time.Tick(500 * time.Millisecond)
  110. // make sure daemon is ready to receive requests
  111. startTime := time.Now().Unix()
  112. for {
  113. d.c.Log("waiting for daemon to start")
  114. if time.Now().Unix()-startTime > 5 {
  115. // After 5 seconds, give up
  116. return errors.New("Daemon exited and never started")
  117. }
  118. select {
  119. case <-time.After(2 * time.Second):
  120. return errors.New("timeout: daemon does not respond")
  121. case <-tick:
  122. c, err := net.Dial("unix", filepath.Join(d.folder, "docker.sock"))
  123. if err != nil {
  124. continue
  125. }
  126. client := httputil.NewClientConn(c, nil)
  127. defer client.Close()
  128. req, err := http.NewRequest("GET", "/_ping", nil)
  129. if err != nil {
  130. d.c.Fatalf("could not create new request: %v", err)
  131. }
  132. resp, err := client.Do(req)
  133. if err != nil {
  134. continue
  135. }
  136. if resp.StatusCode != http.StatusOK {
  137. d.c.Logf("received status != 200 OK: %s", resp.Status)
  138. }
  139. d.c.Log("daemon started")
  140. return nil
  141. }
  142. }
  143. }
  144. // StartWithBusybox will first start the daemon with Daemon.Start()
  145. // then save the busybox image from the main daemon and load it into this Daemon instance.
  146. func (d *Daemon) StartWithBusybox(arg ...string) error {
  147. if err := d.Start(arg...); err != nil {
  148. return err
  149. }
  150. bb := filepath.Join(d.folder, "busybox.tar")
  151. if _, err := os.Stat(bb); err != nil {
  152. if !os.IsNotExist(err) {
  153. return fmt.Errorf("unexpected error on busybox.tar stat: %v", err)
  154. }
  155. // saving busybox image from main daemon
  156. if err := exec.Command(dockerBinary, "save", "--output", bb, "busybox:latest").Run(); err != nil {
  157. return fmt.Errorf("could not save busybox image: %v", err)
  158. }
  159. }
  160. // loading busybox image to this daemon
  161. if _, err := d.Cmd("load", "--input", bb); err != nil {
  162. return fmt.Errorf("could not load busybox image: %v", err)
  163. }
  164. if err := os.Remove(bb); err != nil {
  165. d.c.Logf("Could not remove %s: %v", bb, err)
  166. }
  167. return nil
  168. }
  169. // Stop will send a SIGINT every second and wait for the daemon to stop.
  170. // If it timeouts, a SIGKILL is sent.
  171. // Stop will not delete the daemon directory. If a purged daemon is needed,
  172. // instantiate a new one with NewDaemon.
  173. func (d *Daemon) Stop() error {
  174. if d.cmd == nil || d.wait == nil {
  175. return errors.New("daemon not started")
  176. }
  177. defer func() {
  178. d.logFile.Close()
  179. d.cmd = nil
  180. }()
  181. i := 1
  182. tick := time.Tick(time.Second)
  183. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  184. return fmt.Errorf("could not send signal: %v", err)
  185. }
  186. out1:
  187. for {
  188. select {
  189. case err := <-d.wait:
  190. return err
  191. case <-time.After(15 * time.Second):
  192. // time for stopping jobs and run onShutdown hooks
  193. d.c.Log("timeout")
  194. break out1
  195. }
  196. }
  197. out2:
  198. for {
  199. select {
  200. case err := <-d.wait:
  201. return err
  202. case <-tick:
  203. i++
  204. if i > 4 {
  205. d.c.Logf("tried to interrupt daemon for %d times, now try to kill it", i)
  206. break out2
  207. }
  208. d.c.Logf("Attempt #%d: daemon is still running with pid %d", i, d.cmd.Process.Pid)
  209. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  210. return fmt.Errorf("could not send signal: %v", err)
  211. }
  212. }
  213. }
  214. if err := d.cmd.Process.Kill(); err != nil {
  215. d.c.Logf("Could not kill daemon: %v", err)
  216. return err
  217. }
  218. return nil
  219. }
  220. // Restart will restart the daemon by first stopping it and then starting it.
  221. func (d *Daemon) Restart(arg ...string) error {
  222. d.Stop()
  223. return d.Start(arg...)
  224. }
  225. func (d *Daemon) sock() string {
  226. return fmt.Sprintf("unix://%s/docker.sock", d.folder)
  227. }
  228. // Cmd will execute a docker CLI command against this Daemon.
  229. // Example: d.Cmd("version") will run docker -H unix://path/to/unix.sock version
  230. func (d *Daemon) Cmd(name string, arg ...string) (string, error) {
  231. args := []string{"--host", d.sock(), name}
  232. args = append(args, arg...)
  233. c := exec.Command(dockerBinary, args...)
  234. b, err := c.CombinedOutput()
  235. return string(b), err
  236. }
  237. func (d *Daemon) CmdWithArgs(daemonArgs []string, name string, arg ...string) (string, error) {
  238. args := append(daemonArgs, name)
  239. args = append(args, arg...)
  240. c := exec.Command(dockerBinary, args...)
  241. b, err := c.CombinedOutput()
  242. return string(b), err
  243. }
  244. func (d *Daemon) LogfileName() string {
  245. return d.logFile.Name()
  246. }
  247. func daemonHost() string {
  248. daemonUrlStr := "unix://" + opts.DefaultUnixSocket
  249. if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" {
  250. daemonUrlStr = daemonHostVar
  251. }
  252. return daemonUrlStr
  253. }
  254. func sockConn(timeout time.Duration) (net.Conn, error) {
  255. daemon := daemonHost()
  256. daemonUrl, err := url.Parse(daemon)
  257. if err != nil {
  258. return nil, fmt.Errorf("could not parse url %q: %v", daemon, err)
  259. }
  260. var c net.Conn
  261. switch daemonUrl.Scheme {
  262. case "unix":
  263. return net.DialTimeout(daemonUrl.Scheme, daemonUrl.Path, timeout)
  264. case "tcp":
  265. return net.DialTimeout(daemonUrl.Scheme, daemonUrl.Host, timeout)
  266. default:
  267. return c, fmt.Errorf("unknown scheme %v (%s)", daemonUrl.Scheme, daemon)
  268. }
  269. }
  270. func sockRequest(method, endpoint string, data interface{}) (int, []byte, error) {
  271. jsonData := bytes.NewBuffer(nil)
  272. if err := json.NewEncoder(jsonData).Encode(data); err != nil {
  273. return -1, nil, err
  274. }
  275. res, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json")
  276. if err != nil {
  277. b, _ := ioutil.ReadAll(body)
  278. return -1, b, err
  279. }
  280. var b []byte
  281. b, err = readBody(body)
  282. return res.StatusCode, b, err
  283. }
  284. func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
  285. c, err := sockConn(time.Duration(10 * time.Second))
  286. if err != nil {
  287. return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err)
  288. }
  289. client := httputil.NewClientConn(c, nil)
  290. req, err := http.NewRequest(method, endpoint, data)
  291. if err != nil {
  292. client.Close()
  293. return nil, nil, fmt.Errorf("could not create new request: %v", err)
  294. }
  295. if ct != "" {
  296. req.Header.Set("Content-Type", ct)
  297. }
  298. resp, err := client.Do(req)
  299. if err != nil {
  300. client.Close()
  301. return nil, nil, fmt.Errorf("could not perform request: %v", err)
  302. }
  303. body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
  304. defer client.Close()
  305. return resp.Body.Close()
  306. })
  307. return resp, body, nil
  308. }
  309. func readBody(b io.ReadCloser) ([]byte, error) {
  310. defer b.Close()
  311. return ioutil.ReadAll(b)
  312. }
  313. func deleteContainer(container string) error {
  314. container = strings.TrimSpace(strings.Replace(container, "\n", " ", -1))
  315. rmArgs := strings.Split(fmt.Sprintf("rm -fv %v", container), " ")
  316. exitCode, err := runCommand(exec.Command(dockerBinary, rmArgs...))
  317. // set error manually if not set
  318. if exitCode != 0 && err == nil {
  319. err = fmt.Errorf("failed to remove container: `docker rm` exit is non-zero")
  320. }
  321. return err
  322. }
  323. func getAllContainers() (string, error) {
  324. getContainersCmd := exec.Command(dockerBinary, "ps", "-q", "-a")
  325. out, exitCode, err := runCommandWithOutput(getContainersCmd)
  326. if exitCode != 0 && err == nil {
  327. err = fmt.Errorf("failed to get a list of containers: %v\n", out)
  328. }
  329. return out, err
  330. }
  331. func deleteAllContainers() error {
  332. containers, err := getAllContainers()
  333. if err != nil {
  334. fmt.Println(containers)
  335. return err
  336. }
  337. if err = deleteContainer(containers); err != nil {
  338. return err
  339. }
  340. return nil
  341. }
  342. var protectedImages = map[string]struct{}{}
  343. func init() {
  344. out, err := exec.Command(dockerBinary, "images").CombinedOutput()
  345. if err != nil {
  346. panic(err)
  347. }
  348. lines := strings.Split(string(out), "\n")[1:]
  349. for _, l := range lines {
  350. if l == "" {
  351. continue
  352. }
  353. fields := strings.Fields(l)
  354. imgTag := fields[0] + ":" + fields[1]
  355. // just for case if we have dangling images in tested daemon
  356. if imgTag != "<none>:<none>" {
  357. protectedImages[imgTag] = struct{}{}
  358. }
  359. }
  360. }
  361. func deleteAllImages() error {
  362. out, err := exec.Command(dockerBinary, "images").CombinedOutput()
  363. if err != nil {
  364. return err
  365. }
  366. lines := strings.Split(string(out), "\n")[1:]
  367. var imgs []string
  368. for _, l := range lines {
  369. if l == "" {
  370. continue
  371. }
  372. fields := strings.Fields(l)
  373. imgTag := fields[0] + ":" + fields[1]
  374. if _, ok := protectedImages[imgTag]; !ok {
  375. if fields[0] == "<none>" {
  376. imgs = append(imgs, fields[2])
  377. continue
  378. }
  379. imgs = append(imgs, imgTag)
  380. }
  381. }
  382. if len(imgs) == 0 {
  383. return nil
  384. }
  385. args := append([]string{"rmi", "-f"}, imgs...)
  386. if err := exec.Command(dockerBinary, args...).Run(); err != nil {
  387. return err
  388. }
  389. return nil
  390. }
  391. func getPausedContainers() (string, error) {
  392. getPausedContainersCmd := exec.Command(dockerBinary, "ps", "-f", "status=paused", "-q", "-a")
  393. out, exitCode, err := runCommandWithOutput(getPausedContainersCmd)
  394. if exitCode != 0 && err == nil {
  395. err = fmt.Errorf("failed to get a list of paused containers: %v\n", out)
  396. }
  397. return out, err
  398. }
  399. func getSliceOfPausedContainers() ([]string, error) {
  400. out, err := getPausedContainers()
  401. if err == nil {
  402. if len(out) == 0 {
  403. return nil, err
  404. }
  405. slice := strings.Split(strings.TrimSpace(out), "\n")
  406. return slice, err
  407. }
  408. return []string{out}, err
  409. }
  410. func unpauseContainer(container string) error {
  411. unpauseCmd := exec.Command(dockerBinary, "unpause", container)
  412. exitCode, err := runCommand(unpauseCmd)
  413. if exitCode != 0 && err == nil {
  414. err = fmt.Errorf("failed to unpause container")
  415. }
  416. return nil
  417. }
  418. func unpauseAllContainers() error {
  419. containers, err := getPausedContainers()
  420. if err != nil {
  421. fmt.Println(containers)
  422. return err
  423. }
  424. containers = strings.Replace(containers, "\n", " ", -1)
  425. containers = strings.Trim(containers, " ")
  426. containerList := strings.Split(containers, " ")
  427. for _, value := range containerList {
  428. if err = unpauseContainer(value); err != nil {
  429. return err
  430. }
  431. }
  432. return nil
  433. }
  434. func deleteImages(images ...string) error {
  435. args := []string{"rmi", "-f"}
  436. args = append(args, images...)
  437. rmiCmd := exec.Command(dockerBinary, args...)
  438. exitCode, err := runCommand(rmiCmd)
  439. // set error manually if not set
  440. if exitCode != 0 && err == nil {
  441. err = fmt.Errorf("failed to remove image: `docker rmi` exit is non-zero")
  442. }
  443. return err
  444. }
  445. func imageExists(image string) error {
  446. inspectCmd := exec.Command(dockerBinary, "inspect", image)
  447. exitCode, err := runCommand(inspectCmd)
  448. if exitCode != 0 && err == nil {
  449. err = fmt.Errorf("couldn't find image %q", image)
  450. }
  451. return err
  452. }
  453. func pullImageIfNotExist(image string) (err error) {
  454. if err := imageExists(image); err != nil {
  455. pullCmd := exec.Command(dockerBinary, "pull", image)
  456. _, exitCode, err := runCommandWithOutput(pullCmd)
  457. if err != nil || exitCode != 0 {
  458. err = fmt.Errorf("image %q wasn't found locally and it couldn't be pulled: %s", image, err)
  459. }
  460. }
  461. return
  462. }
  463. func dockerCmd(c *check.C, args ...string) (string, int) {
  464. out, status, err := runCommandWithOutput(exec.Command(dockerBinary, args...))
  465. if err != nil {
  466. c.Fatalf("%q failed with errors: %s, %v", strings.Join(args, " "), out, err)
  467. }
  468. return out, status
  469. }
  470. // execute a docker command with a timeout
  471. func dockerCmdWithTimeout(timeout time.Duration, args ...string) (string, int, error) {
  472. out, status, err := runCommandWithOutputAndTimeout(exec.Command(dockerBinary, args...), timeout)
  473. if err != nil {
  474. return out, status, fmt.Errorf("%q failed with errors: %v : %q)", strings.Join(args, " "), err, out)
  475. }
  476. return out, status, err
  477. }
  478. // execute a docker command in a directory
  479. func dockerCmdInDir(c *check.C, path string, args ...string) (string, int, error) {
  480. dockerCommand := exec.Command(dockerBinary, args...)
  481. dockerCommand.Dir = path
  482. out, status, err := runCommandWithOutput(dockerCommand)
  483. if err != nil {
  484. return out, status, fmt.Errorf("%q failed with errors: %v : %q)", strings.Join(args, " "), err, out)
  485. }
  486. return out, status, err
  487. }
  488. // execute a docker command in a directory with a timeout
  489. func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) (string, int, error) {
  490. dockerCommand := exec.Command(dockerBinary, args...)
  491. dockerCommand.Dir = path
  492. out, status, err := runCommandWithOutputAndTimeout(dockerCommand, timeout)
  493. if err != nil {
  494. return out, status, fmt.Errorf("%q failed with errors: %v : %q)", strings.Join(args, " "), err, out)
  495. }
  496. return out, status, err
  497. }
  498. func findContainerIP(c *check.C, id string, vargs ...string) string {
  499. args := append(vargs, "inspect", "--format='{{ .NetworkSettings.IPAddress }}'", id)
  500. cmd := exec.Command(dockerBinary, args...)
  501. out, _, err := runCommandWithOutput(cmd)
  502. if err != nil {
  503. c.Fatal(err, out)
  504. }
  505. return strings.Trim(out, " \r\n'")
  506. }
  507. func (d *Daemon) findContainerIP(c *check.C, id string) string {
  508. return findContainerIP(c, id, "--host", d.sock())
  509. }
  510. func getContainerCount() (int, error) {
  511. const containers = "Containers:"
  512. cmd := exec.Command(dockerBinary, "info")
  513. out, _, err := runCommandWithOutput(cmd)
  514. if err != nil {
  515. return 0, err
  516. }
  517. lines := strings.Split(out, "\n")
  518. for _, line := range lines {
  519. if strings.Contains(line, containers) {
  520. output := strings.TrimSpace(line)
  521. output = strings.TrimLeft(output, containers)
  522. output = strings.Trim(output, " ")
  523. containerCount, err := strconv.Atoi(output)
  524. if err != nil {
  525. return 0, err
  526. }
  527. return containerCount, nil
  528. }
  529. }
  530. return 0, fmt.Errorf("couldn't find the Container count in the output")
  531. }
  532. type FakeContext struct {
  533. Dir string
  534. }
  535. func (f *FakeContext) Add(file, content string) error {
  536. filepath := path.Join(f.Dir, file)
  537. dirpath := path.Dir(filepath)
  538. if dirpath != "." {
  539. if err := os.MkdirAll(dirpath, 0755); err != nil {
  540. return err
  541. }
  542. }
  543. return ioutil.WriteFile(filepath, []byte(content), 0644)
  544. }
  545. func (f *FakeContext) Delete(file string) error {
  546. filepath := path.Join(f.Dir, file)
  547. return os.RemoveAll(filepath)
  548. }
  549. func (f *FakeContext) Close() error {
  550. return os.RemoveAll(f.Dir)
  551. }
  552. func fakeContextFromDir(dir string) *FakeContext {
  553. return &FakeContext{dir}
  554. }
  555. func fakeContextWithFiles(files map[string]string) (*FakeContext, error) {
  556. tmp, err := ioutil.TempDir("", "fake-context")
  557. if err != nil {
  558. return nil, err
  559. }
  560. if err := os.Chmod(tmp, 0755); err != nil {
  561. return nil, err
  562. }
  563. ctx := fakeContextFromDir(tmp)
  564. for file, content := range files {
  565. if err := ctx.Add(file, content); err != nil {
  566. ctx.Close()
  567. return nil, err
  568. }
  569. }
  570. return ctx, nil
  571. }
  572. func fakeContextAddDockerfile(ctx *FakeContext, dockerfile string) error {
  573. if err := ctx.Add("Dockerfile", dockerfile); err != nil {
  574. ctx.Close()
  575. return err
  576. }
  577. return nil
  578. }
  579. func fakeContext(dockerfile string, files map[string]string) (*FakeContext, error) {
  580. ctx, err := fakeContextWithFiles(files)
  581. if err != nil {
  582. ctx.Close()
  583. return nil, err
  584. }
  585. if err := fakeContextAddDockerfile(ctx, dockerfile); err != nil {
  586. return nil, err
  587. }
  588. return ctx, nil
  589. }
  590. // FakeStorage is a static file server. It might be running locally or remotely
  591. // on test host.
  592. type FakeStorage interface {
  593. Close() error
  594. URL() string
  595. CtxDir() string
  596. }
  597. // fakeStorage returns either a local or remote (at daemon machine) file server
  598. func fakeStorage(files map[string]string) (FakeStorage, error) {
  599. ctx, err := fakeContextWithFiles(files)
  600. if err != nil {
  601. return nil, err
  602. }
  603. return fakeStorageWithContext(ctx)
  604. }
  605. // fakeStorageWithContext returns either a local or remote (at daemon machine) file server
  606. func fakeStorageWithContext(ctx *FakeContext) (FakeStorage, error) {
  607. if isLocalDaemon {
  608. return newLocalFakeStorage(ctx)
  609. }
  610. return newRemoteFileServer(ctx)
  611. }
  612. // localFileStorage is a file storage on the running machine
  613. type localFileStorage struct {
  614. *FakeContext
  615. *httptest.Server
  616. }
  617. func (s *localFileStorage) URL() string {
  618. return s.Server.URL
  619. }
  620. func (s *localFileStorage) CtxDir() string {
  621. return s.FakeContext.Dir
  622. }
  623. func (s *localFileStorage) Close() error {
  624. defer s.Server.Close()
  625. return s.FakeContext.Close()
  626. }
  627. func newLocalFakeStorage(ctx *FakeContext) (*localFileStorage, error) {
  628. handler := http.FileServer(http.Dir(ctx.Dir))
  629. server := httptest.NewServer(handler)
  630. return &localFileStorage{
  631. FakeContext: ctx,
  632. Server: server,
  633. }, nil
  634. }
  635. // remoteFileServer is a containerized static file server started on the remote
  636. // testing machine to be used in URL-accepting docker build functionality.
  637. type remoteFileServer struct {
  638. host string // hostname/port web server is listening to on docker host e.g. 0.0.0.0:43712
  639. container string
  640. image string
  641. ctx *FakeContext
  642. }
  643. func (f *remoteFileServer) URL() string {
  644. u := url.URL{
  645. Scheme: "http",
  646. Host: f.host}
  647. return u.String()
  648. }
  649. func (f *remoteFileServer) CtxDir() string {
  650. return f.ctx.Dir
  651. }
  652. func (f *remoteFileServer) Close() error {
  653. defer func() {
  654. if f.ctx != nil {
  655. f.ctx.Close()
  656. }
  657. if f.image != "" {
  658. deleteImages(f.image)
  659. }
  660. }()
  661. if f.container == "" {
  662. return nil
  663. }
  664. return deleteContainer(f.container)
  665. }
  666. func newRemoteFileServer(ctx *FakeContext) (*remoteFileServer, error) {
  667. var (
  668. image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
  669. container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
  670. )
  671. // Build the image
  672. if err := fakeContextAddDockerfile(ctx, `FROM httpserver
  673. COPY . /static`); err != nil {
  674. return nil, fmt.Errorf("Cannot add Dockerfile to context: %v", err)
  675. }
  676. if _, err := buildImageFromContext(image, ctx, false); err != nil {
  677. return nil, fmt.Errorf("failed building file storage container image: %v", err)
  678. }
  679. // Start the container
  680. runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--name", container, image)
  681. if out, ec, err := runCommandWithOutput(runCmd); err != nil {
  682. return nil, fmt.Errorf("failed to start file storage container. ec=%v\nout=%s\nerr=%v", ec, out, err)
  683. }
  684. // Find out the system assigned port
  685. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "port", container, "80/tcp"))
  686. if err != nil {
  687. return nil, fmt.Errorf("failed to find container port: err=%v\nout=%s", err, out)
  688. }
  689. return &remoteFileServer{
  690. container: container,
  691. image: image,
  692. host: strings.Trim(out, "\n"),
  693. ctx: ctx}, nil
  694. }
  695. func inspectFieldAndMarshall(name, field string, output interface{}) error {
  696. str, err := inspectFieldJSON(name, field)
  697. if err != nil {
  698. return err
  699. }
  700. return json.Unmarshal([]byte(str), output)
  701. }
  702. func inspectFilter(name, filter string) (string, error) {
  703. format := fmt.Sprintf("{{%s}}", filter)
  704. inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
  705. out, exitCode, err := runCommandWithOutput(inspectCmd)
  706. if err != nil || exitCode != 0 {
  707. return "", fmt.Errorf("failed to inspect container %s: %s", name, out)
  708. }
  709. return strings.TrimSpace(out), nil
  710. }
  711. func inspectField(name, field string) (string, error) {
  712. return inspectFilter(name, fmt.Sprintf(".%s", field))
  713. }
  714. func inspectFieldJSON(name, field string) (string, error) {
  715. return inspectFilter(name, fmt.Sprintf("json .%s", field))
  716. }
  717. func inspectFieldMap(name, path, field string) (string, error) {
  718. return inspectFilter(name, fmt.Sprintf("index .%s %q", path, field))
  719. }
  720. func getIDByName(name string) (string, error) {
  721. return inspectField(name, "Id")
  722. }
  723. // getContainerState returns the exit code of the container
  724. // and true if it's running
  725. // the exit code should be ignored if it's running
  726. func getContainerState(c *check.C, id string) (int, bool, error) {
  727. var (
  728. exitStatus int
  729. running bool
  730. )
  731. out, exitCode := dockerCmd(c, "inspect", "--format={{.State.Running}} {{.State.ExitCode}}", id)
  732. if exitCode != 0 {
  733. return 0, false, fmt.Errorf("%q doesn't exist: %s", id, out)
  734. }
  735. out = strings.Trim(out, "\n")
  736. splitOutput := strings.Split(out, " ")
  737. if len(splitOutput) != 2 {
  738. return 0, false, fmt.Errorf("failed to get container state: output is broken")
  739. }
  740. if splitOutput[0] == "true" {
  741. running = true
  742. }
  743. if n, err := strconv.Atoi(splitOutput[1]); err == nil {
  744. exitStatus = n
  745. } else {
  746. return 0, false, fmt.Errorf("failed to get container state: couldn't parse integer")
  747. }
  748. return exitStatus, running, nil
  749. }
  750. func buildImageWithOut(name, dockerfile string, useCache bool) (string, string, error) {
  751. args := []string{"build", "-t", name}
  752. if !useCache {
  753. args = append(args, "--no-cache")
  754. }
  755. args = append(args, "-")
  756. buildCmd := exec.Command(dockerBinary, args...)
  757. buildCmd.Stdin = strings.NewReader(dockerfile)
  758. out, exitCode, err := runCommandWithOutput(buildCmd)
  759. if err != nil || exitCode != 0 {
  760. return "", out, fmt.Errorf("failed to build the image: %s", out)
  761. }
  762. id, err := getIDByName(name)
  763. if err != nil {
  764. return "", out, err
  765. }
  766. return id, out, nil
  767. }
  768. func buildImageWithStdoutStderr(name, dockerfile string, useCache bool) (string, string, string, error) {
  769. args := []string{"build", "-t", name}
  770. if !useCache {
  771. args = append(args, "--no-cache")
  772. }
  773. args = append(args, "-")
  774. buildCmd := exec.Command(dockerBinary, args...)
  775. buildCmd.Stdin = strings.NewReader(dockerfile)
  776. stdout, stderr, exitCode, err := runCommandWithStdoutStderr(buildCmd)
  777. if err != nil || exitCode != 0 {
  778. return "", stdout, stderr, fmt.Errorf("failed to build the image: %s", stdout)
  779. }
  780. id, err := getIDByName(name)
  781. if err != nil {
  782. return "", stdout, stderr, err
  783. }
  784. return id, stdout, stderr, nil
  785. }
  786. func buildImage(name, dockerfile string, useCache bool) (string, error) {
  787. id, _, err := buildImageWithOut(name, dockerfile, useCache)
  788. return id, err
  789. }
  790. func buildImageFromContext(name string, ctx *FakeContext, useCache bool) (string, error) {
  791. args := []string{"build", "-t", name}
  792. if !useCache {
  793. args = append(args, "--no-cache")
  794. }
  795. args = append(args, ".")
  796. buildCmd := exec.Command(dockerBinary, args...)
  797. buildCmd.Dir = ctx.Dir
  798. out, exitCode, err := runCommandWithOutput(buildCmd)
  799. if err != nil || exitCode != 0 {
  800. return "", fmt.Errorf("failed to build the image: %s", out)
  801. }
  802. return getIDByName(name)
  803. }
  804. func buildImageFromPath(name, path string, useCache bool) (string, error) {
  805. args := []string{"build", "-t", name}
  806. if !useCache {
  807. args = append(args, "--no-cache")
  808. }
  809. args = append(args, path)
  810. buildCmd := exec.Command(dockerBinary, args...)
  811. out, exitCode, err := runCommandWithOutput(buildCmd)
  812. if err != nil || exitCode != 0 {
  813. return "", fmt.Errorf("failed to build the image: %s", out)
  814. }
  815. return getIDByName(name)
  816. }
  817. type GitServer interface {
  818. URL() string
  819. Close() error
  820. }
  821. type localGitServer struct {
  822. *httptest.Server
  823. }
  824. func (r *localGitServer) Close() error {
  825. r.Server.Close()
  826. return nil
  827. }
  828. func (r *localGitServer) URL() string {
  829. return r.Server.URL
  830. }
  831. type FakeGIT struct {
  832. root string
  833. server GitServer
  834. RepoURL string
  835. }
  836. func (g *FakeGIT) Close() {
  837. g.server.Close()
  838. os.RemoveAll(g.root)
  839. }
  840. func fakeGIT(name string, files map[string]string, enforceLocalServer bool) (*FakeGIT, error) {
  841. ctx, err := fakeContextWithFiles(files)
  842. if err != nil {
  843. return nil, err
  844. }
  845. defer ctx.Close()
  846. curdir, err := os.Getwd()
  847. if err != nil {
  848. return nil, err
  849. }
  850. defer os.Chdir(curdir)
  851. if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil {
  852. return nil, fmt.Errorf("error trying to init repo: %s (%s)", err, output)
  853. }
  854. err = os.Chdir(ctx.Dir)
  855. if err != nil {
  856. return nil, err
  857. }
  858. if output, err := exec.Command("git", "config", "user.name", "Fake User").CombinedOutput(); err != nil {
  859. return nil, fmt.Errorf("error trying to set 'user.name': %s (%s)", err, output)
  860. }
  861. if output, err := exec.Command("git", "config", "user.email", "fake.user@example.com").CombinedOutput(); err != nil {
  862. return nil, fmt.Errorf("error trying to set 'user.email': %s (%s)", err, output)
  863. }
  864. if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil {
  865. return nil, fmt.Errorf("error trying to add files to repo: %s (%s)", err, output)
  866. }
  867. if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil {
  868. return nil, fmt.Errorf("error trying to commit to repo: %s (%s)", err, output)
  869. }
  870. root, err := ioutil.TempDir("", "docker-test-git-repo")
  871. if err != nil {
  872. return nil, err
  873. }
  874. repoPath := filepath.Join(root, name+".git")
  875. if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil {
  876. os.RemoveAll(root)
  877. return nil, fmt.Errorf("error trying to clone --bare: %s (%s)", err, output)
  878. }
  879. err = os.Chdir(repoPath)
  880. if err != nil {
  881. os.RemoveAll(root)
  882. return nil, err
  883. }
  884. if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil {
  885. os.RemoveAll(root)
  886. return nil, fmt.Errorf("error trying to git update-server-info: %s (%s)", err, output)
  887. }
  888. err = os.Chdir(curdir)
  889. if err != nil {
  890. os.RemoveAll(root)
  891. return nil, err
  892. }
  893. var server GitServer
  894. if !enforceLocalServer {
  895. // use fakeStorage server, which might be local or remote (at test daemon)
  896. server, err = fakeStorageWithContext(fakeContextFromDir(root))
  897. if err != nil {
  898. return nil, fmt.Errorf("cannot start fake storage: %v", err)
  899. }
  900. } else {
  901. // always start a local http server on CLI test machin
  902. httpServer := httptest.NewServer(http.FileServer(http.Dir(root)))
  903. server = &localGitServer{httpServer}
  904. }
  905. return &FakeGIT{
  906. root: root,
  907. server: server,
  908. RepoURL: fmt.Sprintf("%s/%s.git", server.URL(), name),
  909. }, nil
  910. }
  911. // Write `content` to the file at path `dst`, creating it if necessary,
  912. // as well as any missing directories.
  913. // The file is truncated if it already exists.
  914. // Call c.Fatal() at the first error.
  915. func writeFile(dst, content string, c *check.C) {
  916. // Create subdirectories if necessary
  917. if err := os.MkdirAll(path.Dir(dst), 0700); err != nil && !os.IsExist(err) {
  918. c.Fatal(err)
  919. }
  920. f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
  921. if err != nil {
  922. c.Fatal(err)
  923. }
  924. // Write content (truncate if it exists)
  925. if _, err := io.Copy(f, strings.NewReader(content)); err != nil {
  926. c.Fatal(err)
  927. }
  928. }
  929. // Return the contents of file at path `src`.
  930. // Call c.Fatal() at the first error (including if the file doesn't exist)
  931. func readFile(src string, c *check.C) (content string) {
  932. data, err := ioutil.ReadFile(src)
  933. if err != nil {
  934. c.Fatal(err)
  935. }
  936. return string(data)
  937. }
  938. func containerStorageFile(containerId, basename string) string {
  939. return filepath.Join("/var/lib/docker/containers", containerId, basename)
  940. }
  941. // docker commands that use this function must be run with the '-d' switch.
  942. func runCommandAndReadContainerFile(filename string, cmd *exec.Cmd) ([]byte, error) {
  943. out, _, err := runCommandWithOutput(cmd)
  944. if err != nil {
  945. return nil, fmt.Errorf("%v: %q", err, out)
  946. }
  947. time.Sleep(1 * time.Second)
  948. contID := strings.TrimSpace(out)
  949. return readContainerFile(contID, filename)
  950. }
  951. func readContainerFile(containerId, filename string) ([]byte, error) {
  952. f, err := os.Open(containerStorageFile(containerId, filename))
  953. if err != nil {
  954. return nil, err
  955. }
  956. defer f.Close()
  957. content, err := ioutil.ReadAll(f)
  958. if err != nil {
  959. return nil, err
  960. }
  961. return content, nil
  962. }
  963. func readContainerFileWithExec(containerId, filename string) ([]byte, error) {
  964. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "exec", containerId, "cat", filename))
  965. return []byte(out), err
  966. }
  967. // daemonTime provides the current time on the daemon host
  968. func daemonTime(c *check.C) time.Time {
  969. if isLocalDaemon {
  970. return time.Now()
  971. }
  972. status, body, err := sockRequest("GET", "/info", nil)
  973. c.Assert(status, check.Equals, http.StatusOK)
  974. c.Assert(err, check.IsNil)
  975. type infoJSON struct {
  976. SystemTime string
  977. }
  978. var info infoJSON
  979. if err = json.Unmarshal(body, &info); err != nil {
  980. c.Fatalf("unable to unmarshal /info response: %v", err)
  981. }
  982. dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
  983. if err != nil {
  984. c.Fatal(err)
  985. }
  986. return dt
  987. }
  988. func setupRegistry(c *check.C) *testRegistryV2 {
  989. testRequires(c, RegistryHosting)
  990. reg, err := newTestRegistryV2(c)
  991. if err != nil {
  992. c.Fatal(err)
  993. }
  994. // Wait for registry to be ready to serve requests.
  995. for i := 0; i != 5; i++ {
  996. if err = reg.Ping(); err == nil {
  997. break
  998. }
  999. time.Sleep(100 * time.Millisecond)
  1000. }
  1001. if err != nil {
  1002. c.Fatal("Timeout waiting for test registry to become available")
  1003. }
  1004. return reg
  1005. }
  1006. // appendBaseEnv appends the minimum set of environment variables to exec the
  1007. // docker cli binary for testing with correct configuration to the given env
  1008. // list.
  1009. func appendBaseEnv(env []string) []string {
  1010. preserveList := []string{
  1011. // preserve remote test host
  1012. "DOCKER_HOST",
  1013. // windows: requires preserving SystemRoot, otherwise dial tcp fails
  1014. // with "GetAddrInfoW: A non-recoverable error occurred during a database lookup."
  1015. "SystemRoot",
  1016. }
  1017. for _, key := range preserveList {
  1018. if val := os.Getenv(key); val != "" {
  1019. env = append(env, fmt.Sprintf("%s=%s", key, val))
  1020. }
  1021. }
  1022. return env
  1023. }