docker_utils.go 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. package main
  2. import (
  3. "bufio"
  4. "bytes"
  5. "crypto/tls"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "net"
  12. "net/http"
  13. "net/http/httptest"
  14. "net/http/httputil"
  15. "net/url"
  16. "os"
  17. "os/exec"
  18. "path"
  19. "path/filepath"
  20. "strconv"
  21. "strings"
  22. "time"
  23. "github.com/docker/docker/opts"
  24. "github.com/docker/docker/pkg/httputils"
  25. "github.com/docker/docker/pkg/integration"
  26. "github.com/docker/docker/pkg/ioutils"
  27. "github.com/docker/docker/pkg/stringutils"
  28. "github.com/docker/engine-api/types"
  29. "github.com/docker/go-connections/sockets"
  30. "github.com/docker/go-connections/tlsconfig"
  31. "github.com/go-check/check"
  32. )
  33. func init() {
  34. out, err := exec.Command(dockerBinary, "images").CombinedOutput()
  35. if err != nil {
  36. panic(err)
  37. }
  38. lines := strings.Split(string(out), "\n")[1:]
  39. for _, l := range lines {
  40. if l == "" {
  41. continue
  42. }
  43. fields := strings.Fields(l)
  44. imgTag := fields[0] + ":" + fields[1]
  45. // just for case if we have dangling images in tested daemon
  46. if imgTag != "<none>:<none>" {
  47. protectedImages[imgTag] = struct{}{}
  48. }
  49. }
  50. // Obtain the daemon platform so that it can be used by tests to make
  51. // intelligent decisions about how to configure themselves, and validate
  52. // that the target platform is valid.
  53. res, _, err := sockRequestRaw("GET", "/version", nil, "application/json")
  54. if err != nil || res == nil || (res != nil && res.StatusCode != http.StatusOK) {
  55. panic(fmt.Errorf("Init failed to get version: %v. Res=%v", err.Error(), res))
  56. }
  57. svrHeader, _ := httputils.ParseServerHeader(res.Header.Get("Server"))
  58. daemonPlatform = svrHeader.OS
  59. if daemonPlatform != "linux" && daemonPlatform != "windows" {
  60. panic("Cannot run tests against platform: " + daemonPlatform)
  61. }
  62. // On Windows, extract out the version as we need to make selective
  63. // decisions during integration testing as and when features are implemented.
  64. if daemonPlatform == "windows" {
  65. if body, err := ioutil.ReadAll(res.Body); err == nil {
  66. var server types.Version
  67. if err := json.Unmarshal(body, &server); err == nil {
  68. // eg in "10.0 10550 (10550.1000.amd64fre.branch.date-time)" we want 10550
  69. windowsDaemonKV, _ = strconv.Atoi(strings.Split(server.KernelVersion, " ")[1])
  70. }
  71. }
  72. }
  73. // Now we know the daemon platform, can set paths used by tests.
  74. _, body, err := sockRequest("GET", "/info", nil)
  75. if err != nil {
  76. panic(err)
  77. }
  78. var info types.Info
  79. err = json.Unmarshal(body, &info)
  80. dockerBasePath = info.DockerRootDir
  81. volumesConfigPath = filepath.Join(dockerBasePath, "volumes")
  82. containerStoragePath = filepath.Join(dockerBasePath, "containers")
  83. // Make sure in context of daemon, not the local platform. Note we can't
  84. // use filepath.FromSlash or ToSlash here as they are a no-op on Unix.
  85. if daemonPlatform == "windows" {
  86. volumesConfigPath = strings.Replace(volumesConfigPath, `/`, `\`, -1)
  87. containerStoragePath = strings.Replace(containerStoragePath, `/`, `\`, -1)
  88. } else {
  89. volumesConfigPath = strings.Replace(volumesConfigPath, `\`, `/`, -1)
  90. containerStoragePath = strings.Replace(containerStoragePath, `\`, `/`, -1)
  91. }
  92. }
  93. // Daemon represents a Docker daemon for the testing framework.
  94. type Daemon struct {
  95. // Defaults to "daemon"
  96. // Useful to set to --daemon or -d for checking backwards compatibility
  97. Command string
  98. GlobalFlags []string
  99. id string
  100. c *check.C
  101. logFile *os.File
  102. folder string
  103. root string
  104. stdin io.WriteCloser
  105. stdout, stderr io.ReadCloser
  106. cmd *exec.Cmd
  107. storageDriver string
  108. wait chan error
  109. userlandProxy bool
  110. useDefaultHost bool
  111. useDefaultTLSHost bool
  112. }
  113. type clientConfig struct {
  114. transport *http.Transport
  115. scheme string
  116. addr string
  117. }
  118. // NewDaemon returns a Daemon instance to be used for testing.
  119. // This will create a directory such as d123456789 in the folder specified by $DEST.
  120. // The daemon will not automatically start.
  121. func NewDaemon(c *check.C) *Daemon {
  122. dest := os.Getenv("DEST")
  123. c.Assert(dest, check.Not(check.Equals), "", check.Commentf("Please set the DEST environment variable"))
  124. id := fmt.Sprintf("d%d", time.Now().UnixNano()%100000000)
  125. dir := filepath.Join(dest, id)
  126. daemonFolder, err := filepath.Abs(dir)
  127. c.Assert(err, check.IsNil, check.Commentf("Could not make %q an absolute path", dir))
  128. daemonRoot := filepath.Join(daemonFolder, "root")
  129. c.Assert(os.MkdirAll(daemonRoot, 0755), check.IsNil, check.Commentf("Could not create daemon root %q", dir))
  130. userlandProxy := true
  131. if env := os.Getenv("DOCKER_USERLANDPROXY"); env != "" {
  132. if val, err := strconv.ParseBool(env); err != nil {
  133. userlandProxy = val
  134. }
  135. }
  136. return &Daemon{
  137. Command: "daemon",
  138. id: id,
  139. c: c,
  140. folder: daemonFolder,
  141. root: daemonRoot,
  142. storageDriver: os.Getenv("DOCKER_GRAPHDRIVER"),
  143. userlandProxy: userlandProxy,
  144. }
  145. }
  146. func (d *Daemon) getClientConfig() (*clientConfig, error) {
  147. var (
  148. transport *http.Transport
  149. scheme string
  150. addr string
  151. proto string
  152. )
  153. if d.useDefaultTLSHost {
  154. option := &tlsconfig.Options{
  155. CAFile: "fixtures/https/ca.pem",
  156. CertFile: "fixtures/https/client-cert.pem",
  157. KeyFile: "fixtures/https/client-key.pem",
  158. }
  159. tlsConfig, err := tlsconfig.Client(*option)
  160. if err != nil {
  161. return nil, err
  162. }
  163. transport = &http.Transport{
  164. TLSClientConfig: tlsConfig,
  165. }
  166. addr = fmt.Sprintf("%s:%d", opts.DefaultHTTPHost, opts.DefaultTLSHTTPPort)
  167. scheme = "https"
  168. proto = "tcp"
  169. } else if d.useDefaultHost {
  170. addr = opts.DefaultUnixSocket
  171. proto = "unix"
  172. scheme = "http"
  173. transport = &http.Transport{}
  174. } else {
  175. addr = filepath.Join(d.folder, "docker.sock")
  176. proto = "unix"
  177. scheme = "http"
  178. transport = &http.Transport{}
  179. }
  180. sockets.ConfigureTCPTransport(transport, proto, addr)
  181. return &clientConfig{
  182. transport: transport,
  183. scheme: scheme,
  184. addr: addr,
  185. }, nil
  186. }
  187. // Start will start the daemon and return once it is ready to receive requests.
  188. // You can specify additional daemon flags.
  189. func (d *Daemon) Start(arg ...string) error {
  190. dockerBinary, err := exec.LookPath(dockerBinary)
  191. d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not find docker binary in $PATH", d.id))
  192. args := append(d.GlobalFlags,
  193. d.Command,
  194. "--graph", d.root,
  195. "--pidfile", fmt.Sprintf("%s/docker.pid", d.folder),
  196. fmt.Sprintf("--userland-proxy=%t", d.userlandProxy),
  197. )
  198. if !(d.useDefaultHost || d.useDefaultTLSHost) {
  199. args = append(args, []string{"--host", d.sock()}...)
  200. }
  201. if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
  202. args = append(args, []string{"--userns-remap", root}...)
  203. }
  204. // If we don't explicitly set the log-level or debug flag(-D) then
  205. // turn on debug mode
  206. foundLog := false
  207. foundSd := false
  208. for _, a := range arg {
  209. if strings.Contains(a, "--log-level") || strings.Contains(a, "-D") || strings.Contains(a, "--debug") {
  210. foundLog = true
  211. }
  212. if strings.Contains(a, "--storage-driver") {
  213. foundSd = true
  214. }
  215. }
  216. if !foundLog {
  217. args = append(args, "--debug")
  218. }
  219. if d.storageDriver != "" && !foundSd {
  220. args = append(args, "--storage-driver", d.storageDriver)
  221. }
  222. args = append(args, arg...)
  223. d.cmd = exec.Command(dockerBinary, args...)
  224. d.logFile, err = os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
  225. d.c.Assert(err, check.IsNil, check.Commentf("[%s] Could not create %s/docker.log", d.id, d.folder))
  226. d.cmd.Stdout = d.logFile
  227. d.cmd.Stderr = d.logFile
  228. if err := d.cmd.Start(); err != nil {
  229. return fmt.Errorf("[%s] could not start daemon container: %v", d.id, err)
  230. }
  231. wait := make(chan error)
  232. go func() {
  233. wait <- d.cmd.Wait()
  234. d.c.Logf("[%s] exiting daemon", d.id)
  235. close(wait)
  236. }()
  237. d.wait = wait
  238. tick := time.Tick(500 * time.Millisecond)
  239. // make sure daemon is ready to receive requests
  240. startTime := time.Now().Unix()
  241. for {
  242. d.c.Logf("[%s] waiting for daemon to start", d.id)
  243. if time.Now().Unix()-startTime > 5 {
  244. // After 5 seconds, give up
  245. return fmt.Errorf("[%s] Daemon exited and never started", d.id)
  246. }
  247. select {
  248. case <-time.After(2 * time.Second):
  249. return fmt.Errorf("[%s] timeout: daemon does not respond", d.id)
  250. case <-tick:
  251. clientConfig, err := d.getClientConfig()
  252. if err != nil {
  253. return err
  254. }
  255. client := &http.Client{
  256. Transport: clientConfig.transport,
  257. }
  258. req, err := http.NewRequest("GET", "/_ping", nil)
  259. d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not create new request", d.id))
  260. req.URL.Host = clientConfig.addr
  261. req.URL.Scheme = clientConfig.scheme
  262. resp, err := client.Do(req)
  263. if err != nil {
  264. continue
  265. }
  266. if resp.StatusCode != http.StatusOK {
  267. d.c.Logf("[%s] received status != 200 OK: %s", d.id, resp.Status)
  268. }
  269. d.c.Logf("[%s] daemon started", d.id)
  270. d.root, err = d.queryRootDir()
  271. if err != nil {
  272. return fmt.Errorf("[%s] error querying daemon for root directory: %v", d.id, err)
  273. }
  274. return nil
  275. }
  276. }
  277. }
  278. // StartWithBusybox will first start the daemon with Daemon.Start()
  279. // then save the busybox image from the main daemon and load it into this Daemon instance.
  280. func (d *Daemon) StartWithBusybox(arg ...string) error {
  281. if err := d.Start(arg...); err != nil {
  282. return err
  283. }
  284. bb := filepath.Join(d.folder, "busybox.tar")
  285. if _, err := os.Stat(bb); err != nil {
  286. if !os.IsNotExist(err) {
  287. return fmt.Errorf("unexpected error on busybox.tar stat: %v", err)
  288. }
  289. // saving busybox image from main daemon
  290. if err := exec.Command(dockerBinary, "save", "--output", bb, "busybox:latest").Run(); err != nil {
  291. return fmt.Errorf("could not save busybox image: %v", err)
  292. }
  293. }
  294. // loading busybox image to this daemon
  295. if out, err := d.Cmd("load", "--input", bb); err != nil {
  296. return fmt.Errorf("could not load busybox image: %s", out)
  297. }
  298. if err := os.Remove(bb); err != nil {
  299. d.c.Logf("could not remove %s: %v", bb, err)
  300. }
  301. return nil
  302. }
  303. // Stop will send a SIGINT every second and wait for the daemon to stop.
  304. // If it timeouts, a SIGKILL is sent.
  305. // Stop will not delete the daemon directory. If a purged daemon is needed,
  306. // instantiate a new one with NewDaemon.
  307. func (d *Daemon) Stop() error {
  308. if d.cmd == nil || d.wait == nil {
  309. return errors.New("daemon not started")
  310. }
  311. defer func() {
  312. d.logFile.Close()
  313. d.cmd = nil
  314. }()
  315. i := 1
  316. tick := time.Tick(time.Second)
  317. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  318. return fmt.Errorf("could not send signal: %v", err)
  319. }
  320. out1:
  321. for {
  322. select {
  323. case err := <-d.wait:
  324. return err
  325. case <-time.After(15 * time.Second):
  326. // time for stopping jobs and run onShutdown hooks
  327. d.c.Log("timeout")
  328. break out1
  329. }
  330. }
  331. out2:
  332. for {
  333. select {
  334. case err := <-d.wait:
  335. return err
  336. case <-tick:
  337. i++
  338. if i > 4 {
  339. d.c.Logf("tried to interrupt daemon for %d times, now try to kill it", i)
  340. break out2
  341. }
  342. d.c.Logf("Attempt #%d: daemon is still running with pid %d", i, d.cmd.Process.Pid)
  343. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  344. return fmt.Errorf("could not send signal: %v", err)
  345. }
  346. }
  347. }
  348. if err := d.cmd.Process.Kill(); err != nil {
  349. d.c.Logf("Could not kill daemon: %v", err)
  350. return err
  351. }
  352. return nil
  353. }
  354. // Restart will restart the daemon by first stopping it and then starting it.
  355. func (d *Daemon) Restart(arg ...string) error {
  356. d.Stop()
  357. // in the case of tests running a user namespace-enabled daemon, we have resolved
  358. // d.root to be the actual final path of the graph dir after the "uid.gid" of
  359. // remapped root is added--we need to subtract it from the path before calling
  360. // start or else we will continue making subdirectories rather than truly restarting
  361. // with the same location/root:
  362. if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
  363. d.root = filepath.Dir(d.root)
  364. }
  365. return d.Start(arg...)
  366. }
  367. func (d *Daemon) queryRootDir() (string, error) {
  368. // update daemon root by asking /info endpoint (to support user
  369. // namespaced daemon with root remapped uid.gid directory)
  370. clientConfig, err := d.getClientConfig()
  371. if err != nil {
  372. return "", err
  373. }
  374. client := &http.Client{
  375. Transport: clientConfig.transport,
  376. }
  377. req, err := http.NewRequest("GET", "/info", nil)
  378. if err != nil {
  379. return "", err
  380. }
  381. req.Header.Set("Content-Type", "application/json")
  382. req.URL.Host = clientConfig.addr
  383. req.URL.Scheme = clientConfig.scheme
  384. resp, err := client.Do(req)
  385. if err != nil {
  386. return "", err
  387. }
  388. body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
  389. return resp.Body.Close()
  390. })
  391. type Info struct {
  392. DockerRootDir string
  393. }
  394. var b []byte
  395. var i Info
  396. b, err = readBody(body)
  397. if err == nil && resp.StatusCode == 200 {
  398. // read the docker root dir
  399. if err = json.Unmarshal(b, &i); err == nil {
  400. return i.DockerRootDir, nil
  401. }
  402. }
  403. return "", err
  404. }
  405. func (d *Daemon) sock() string {
  406. return fmt.Sprintf("unix://%s/docker.sock", d.folder)
  407. }
  408. // Cmd will execute a docker CLI command against this Daemon.
  409. // Example: d.Cmd("version") will run docker -H unix://path/to/unix.sock version
  410. func (d *Daemon) Cmd(name string, arg ...string) (string, error) {
  411. args := []string{"--host", d.sock(), name}
  412. args = append(args, arg...)
  413. c := exec.Command(dockerBinary, args...)
  414. b, err := c.CombinedOutput()
  415. return string(b), err
  416. }
  417. // CmdWithArgs will execute a docker CLI command against a daemon with the
  418. // given additional arguments
  419. func (d *Daemon) CmdWithArgs(daemonArgs []string, name string, arg ...string) (string, error) {
  420. args := append(daemonArgs, name)
  421. args = append(args, arg...)
  422. c := exec.Command(dockerBinary, args...)
  423. b, err := c.CombinedOutput()
  424. return string(b), err
  425. }
  426. // LogfileName returns the path the the daemon's log file
  427. func (d *Daemon) LogfileName() string {
  428. return d.logFile.Name()
  429. }
  430. func daemonHost() string {
  431. daemonURLStr := "unix://" + opts.DefaultUnixSocket
  432. if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" {
  433. daemonURLStr = daemonHostVar
  434. }
  435. return daemonURLStr
  436. }
  437. func getTLSConfig() (*tls.Config, error) {
  438. dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
  439. if dockerCertPath == "" {
  440. return nil, fmt.Errorf("DOCKER_TLS_VERIFY specified, but no DOCKER_CERT_PATH environment variable")
  441. }
  442. option := &tlsconfig.Options{
  443. CAFile: filepath.Join(dockerCertPath, "ca.pem"),
  444. CertFile: filepath.Join(dockerCertPath, "cert.pem"),
  445. KeyFile: filepath.Join(dockerCertPath, "key.pem"),
  446. }
  447. tlsConfig, err := tlsconfig.Client(*option)
  448. if err != nil {
  449. return nil, err
  450. }
  451. return tlsConfig, nil
  452. }
  453. func sockConn(timeout time.Duration) (net.Conn, error) {
  454. daemon := daemonHost()
  455. daemonURL, err := url.Parse(daemon)
  456. if err != nil {
  457. return nil, fmt.Errorf("could not parse url %q: %v", daemon, err)
  458. }
  459. var c net.Conn
  460. switch daemonURL.Scheme {
  461. case "unix":
  462. return net.DialTimeout(daemonURL.Scheme, daemonURL.Path, timeout)
  463. case "tcp":
  464. if os.Getenv("DOCKER_TLS_VERIFY") != "" {
  465. // Setup the socket TLS configuration.
  466. tlsConfig, err := getTLSConfig()
  467. if err != nil {
  468. return nil, err
  469. }
  470. dialer := &net.Dialer{Timeout: timeout}
  471. return tls.DialWithDialer(dialer, daemonURL.Scheme, daemonURL.Host, tlsConfig)
  472. }
  473. return net.DialTimeout(daemonURL.Scheme, daemonURL.Host, timeout)
  474. default:
  475. return c, fmt.Errorf("unknown scheme %v (%s)", daemonURL.Scheme, daemon)
  476. }
  477. }
  478. func sockRequest(method, endpoint string, data interface{}) (int, []byte, error) {
  479. jsonData := bytes.NewBuffer(nil)
  480. if err := json.NewEncoder(jsonData).Encode(data); err != nil {
  481. return -1, nil, err
  482. }
  483. res, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json")
  484. if err != nil {
  485. return -1, nil, err
  486. }
  487. b, err := readBody(body)
  488. return res.StatusCode, b, err
  489. }
  490. func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
  491. req, client, err := newRequestClient(method, endpoint, data, ct)
  492. if err != nil {
  493. return nil, nil, err
  494. }
  495. resp, err := client.Do(req)
  496. if err != nil {
  497. client.Close()
  498. return nil, nil, err
  499. }
  500. body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
  501. defer resp.Body.Close()
  502. return client.Close()
  503. })
  504. return resp, body, nil
  505. }
  506. func sockRequestHijack(method, endpoint string, data io.Reader, ct string) (net.Conn, *bufio.Reader, error) {
  507. req, client, err := newRequestClient(method, endpoint, data, ct)
  508. if err != nil {
  509. return nil, nil, err
  510. }
  511. client.Do(req)
  512. conn, br := client.Hijack()
  513. return conn, br, nil
  514. }
  515. func newRequestClient(method, endpoint string, data io.Reader, ct string) (*http.Request, *httputil.ClientConn, error) {
  516. c, err := sockConn(time.Duration(10 * time.Second))
  517. if err != nil {
  518. return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err)
  519. }
  520. client := httputil.NewClientConn(c, nil)
  521. req, err := http.NewRequest(method, endpoint, data)
  522. if err != nil {
  523. client.Close()
  524. return nil, nil, fmt.Errorf("could not create new request: %v", err)
  525. }
  526. if ct != "" {
  527. req.Header.Set("Content-Type", ct)
  528. }
  529. return req, client, nil
  530. }
  531. func readBody(b io.ReadCloser) ([]byte, error) {
  532. defer b.Close()
  533. return ioutil.ReadAll(b)
  534. }
  535. func deleteContainer(container string) error {
  536. container = strings.TrimSpace(strings.Replace(container, "\n", " ", -1))
  537. rmArgs := strings.Split(fmt.Sprintf("rm -fv %v", container), " ")
  538. exitCode, err := runCommand(exec.Command(dockerBinary, rmArgs...))
  539. // set error manually if not set
  540. if exitCode != 0 && err == nil {
  541. err = fmt.Errorf("failed to remove container: `docker rm` exit is non-zero")
  542. }
  543. return err
  544. }
  545. func getAllContainers() (string, error) {
  546. getContainersCmd := exec.Command(dockerBinary, "ps", "-q", "-a")
  547. out, exitCode, err := runCommandWithOutput(getContainersCmd)
  548. if exitCode != 0 && err == nil {
  549. err = fmt.Errorf("failed to get a list of containers: %v\n", out)
  550. }
  551. return out, err
  552. }
  553. func deleteAllContainers() error {
  554. containers, err := getAllContainers()
  555. if err != nil {
  556. fmt.Println(containers)
  557. return err
  558. }
  559. if containers != "" {
  560. if err = deleteContainer(containers); err != nil {
  561. return err
  562. }
  563. }
  564. return nil
  565. }
  566. func deleteAllNetworks() error {
  567. networks, err := getAllNetworks()
  568. if err != nil {
  569. return err
  570. }
  571. var errors []string
  572. for _, n := range networks {
  573. if n.Name == "bridge" || n.Name == "none" || n.Name == "host" {
  574. continue
  575. }
  576. status, b, err := sockRequest("DELETE", "/networks/"+n.Name, nil)
  577. if err != nil {
  578. errors = append(errors, err.Error())
  579. continue
  580. }
  581. if status != http.StatusNoContent {
  582. errors = append(errors, fmt.Sprintf("error deleting network %s: %s", n.Name, string(b)))
  583. }
  584. }
  585. if len(errors) > 0 {
  586. return fmt.Errorf(strings.Join(errors, "\n"))
  587. }
  588. return nil
  589. }
  590. func getAllNetworks() ([]types.NetworkResource, error) {
  591. var networks []types.NetworkResource
  592. _, b, err := sockRequest("GET", "/networks", nil)
  593. if err != nil {
  594. return nil, err
  595. }
  596. if err := json.Unmarshal(b, &networks); err != nil {
  597. return nil, err
  598. }
  599. return networks, nil
  600. }
  601. func deleteAllVolumes() error {
  602. volumes, err := getAllVolumes()
  603. if err != nil {
  604. return err
  605. }
  606. var errors []string
  607. for _, v := range volumes {
  608. status, b, err := sockRequest("DELETE", "/volumes/"+v.Name, nil)
  609. if err != nil {
  610. errors = append(errors, err.Error())
  611. continue
  612. }
  613. if status != http.StatusNoContent {
  614. errors = append(errors, fmt.Sprintf("error deleting volume %s: %s", v.Name, string(b)))
  615. }
  616. }
  617. if len(errors) > 0 {
  618. return fmt.Errorf(strings.Join(errors, "\n"))
  619. }
  620. return nil
  621. }
  622. func getAllVolumes() ([]*types.Volume, error) {
  623. var volumes types.VolumesListResponse
  624. _, b, err := sockRequest("GET", "/volumes", nil)
  625. if err != nil {
  626. return nil, err
  627. }
  628. if err := json.Unmarshal(b, &volumes); err != nil {
  629. return nil, err
  630. }
  631. return volumes.Volumes, nil
  632. }
  633. var protectedImages = map[string]struct{}{}
  634. func deleteAllImages() error {
  635. out, err := exec.Command(dockerBinary, "images").CombinedOutput()
  636. if err != nil {
  637. return err
  638. }
  639. lines := strings.Split(string(out), "\n")[1:]
  640. var imgs []string
  641. for _, l := range lines {
  642. if l == "" {
  643. continue
  644. }
  645. fields := strings.Fields(l)
  646. imgTag := fields[0] + ":" + fields[1]
  647. if _, ok := protectedImages[imgTag]; !ok {
  648. if fields[0] == "<none>" {
  649. imgs = append(imgs, fields[2])
  650. continue
  651. }
  652. imgs = append(imgs, imgTag)
  653. }
  654. }
  655. if len(imgs) == 0 {
  656. return nil
  657. }
  658. args := append([]string{"rmi", "-f"}, imgs...)
  659. if err := exec.Command(dockerBinary, args...).Run(); err != nil {
  660. return err
  661. }
  662. return nil
  663. }
  664. func getPausedContainers() (string, error) {
  665. getPausedContainersCmd := exec.Command(dockerBinary, "ps", "-f", "status=paused", "-q", "-a")
  666. out, exitCode, err := runCommandWithOutput(getPausedContainersCmd)
  667. if exitCode != 0 && err == nil {
  668. err = fmt.Errorf("failed to get a list of paused containers: %v\n", out)
  669. }
  670. return out, err
  671. }
  672. func getSliceOfPausedContainers() ([]string, error) {
  673. out, err := getPausedContainers()
  674. if err == nil {
  675. if len(out) == 0 {
  676. return nil, err
  677. }
  678. slice := strings.Split(strings.TrimSpace(out), "\n")
  679. return slice, err
  680. }
  681. return []string{out}, err
  682. }
  683. func unpauseContainer(container string) error {
  684. unpauseCmd := exec.Command(dockerBinary, "unpause", container)
  685. exitCode, err := runCommand(unpauseCmd)
  686. if exitCode != 0 && err == nil {
  687. err = fmt.Errorf("failed to unpause container")
  688. }
  689. return nil
  690. }
  691. func unpauseAllContainers() error {
  692. containers, err := getPausedContainers()
  693. if err != nil {
  694. fmt.Println(containers)
  695. return err
  696. }
  697. containers = strings.Replace(containers, "\n", " ", -1)
  698. containers = strings.Trim(containers, " ")
  699. containerList := strings.Split(containers, " ")
  700. for _, value := range containerList {
  701. if err = unpauseContainer(value); err != nil {
  702. return err
  703. }
  704. }
  705. return nil
  706. }
  707. func deleteImages(images ...string) error {
  708. args := []string{"rmi", "-f"}
  709. args = append(args, images...)
  710. rmiCmd := exec.Command(dockerBinary, args...)
  711. exitCode, err := runCommand(rmiCmd)
  712. // set error manually if not set
  713. if exitCode != 0 && err == nil {
  714. err = fmt.Errorf("failed to remove image: `docker rmi` exit is non-zero")
  715. }
  716. return err
  717. }
  718. func imageExists(image string) error {
  719. inspectCmd := exec.Command(dockerBinary, "inspect", image)
  720. exitCode, err := runCommand(inspectCmd)
  721. if exitCode != 0 && err == nil {
  722. err = fmt.Errorf("couldn't find image %q", image)
  723. }
  724. return err
  725. }
  726. func pullImageIfNotExist(image string) error {
  727. if err := imageExists(image); err != nil {
  728. pullCmd := exec.Command(dockerBinary, "pull", image)
  729. _, exitCode, err := runCommandWithOutput(pullCmd)
  730. if err != nil || exitCode != 0 {
  731. return fmt.Errorf("image %q wasn't found locally and it couldn't be pulled: %s", image, err)
  732. }
  733. }
  734. return nil
  735. }
  736. func dockerCmdWithError(args ...string) (string, int, error) {
  737. return integration.DockerCmdWithError(dockerBinary, args...)
  738. }
  739. func dockerCmdWithStdoutStderr(c *check.C, args ...string) (string, string, int) {
  740. return integration.DockerCmdWithStdoutStderr(dockerBinary, c, args...)
  741. }
  742. func dockerCmd(c *check.C, args ...string) (string, int) {
  743. return integration.DockerCmd(dockerBinary, c, args...)
  744. }
  745. // execute a docker command with a timeout
  746. func dockerCmdWithTimeout(timeout time.Duration, args ...string) (string, int, error) {
  747. return integration.DockerCmdWithTimeout(dockerBinary, timeout, args...)
  748. }
  749. // execute a docker command in a directory
  750. func dockerCmdInDir(c *check.C, path string, args ...string) (string, int, error) {
  751. return integration.DockerCmdInDir(dockerBinary, path, args...)
  752. }
  753. // execute a docker command in a directory with a timeout
  754. func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) (string, int, error) {
  755. return integration.DockerCmdInDirWithTimeout(dockerBinary, timeout, path, args...)
  756. }
  757. // find the State.ExitCode in container metadata
  758. func findContainerExitCode(c *check.C, name string, vargs ...string) string {
  759. args := append(vargs, "inspect", "--format='{{ .State.ExitCode }} {{ .State.Error }}'", name)
  760. cmd := exec.Command(dockerBinary, args...)
  761. out, _, err := runCommandWithOutput(cmd)
  762. if err != nil {
  763. c.Fatal(err, out)
  764. }
  765. return out
  766. }
  767. func findContainerIP(c *check.C, id string, network string) string {
  768. out, _ := dockerCmd(c, "inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.%s.IPAddress }}'", network), id)
  769. return strings.Trim(out, " \r\n'")
  770. }
  771. func (d *Daemon) findContainerIP(id string) string {
  772. out, err := d.Cmd("inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.bridge.IPAddress }}'"), id)
  773. if err != nil {
  774. d.c.Log(err)
  775. }
  776. return strings.Trim(out, " \r\n'")
  777. }
  778. func getContainerCount() (int, error) {
  779. const containers = "Containers:"
  780. cmd := exec.Command(dockerBinary, "info")
  781. out, _, err := runCommandWithOutput(cmd)
  782. if err != nil {
  783. return 0, err
  784. }
  785. lines := strings.Split(out, "\n")
  786. for _, line := range lines {
  787. if strings.Contains(line, containers) {
  788. output := strings.TrimSpace(line)
  789. output = strings.TrimLeft(output, containers)
  790. output = strings.Trim(output, " ")
  791. containerCount, err := strconv.Atoi(output)
  792. if err != nil {
  793. return 0, err
  794. }
  795. return containerCount, nil
  796. }
  797. }
  798. return 0, fmt.Errorf("couldn't find the Container count in the output")
  799. }
  800. // FakeContext creates directories that can be used as a build context
  801. type FakeContext struct {
  802. Dir string
  803. }
  804. // Add a file at a path, creating directories where necessary
  805. func (f *FakeContext) Add(file, content string) error {
  806. return f.addFile(file, []byte(content))
  807. }
  808. func (f *FakeContext) addFile(file string, content []byte) error {
  809. filepath := path.Join(f.Dir, file)
  810. dirpath := path.Dir(filepath)
  811. if dirpath != "." {
  812. if err := os.MkdirAll(dirpath, 0755); err != nil {
  813. return err
  814. }
  815. }
  816. return ioutil.WriteFile(filepath, content, 0644)
  817. }
  818. // Delete a file at a path
  819. func (f *FakeContext) Delete(file string) error {
  820. filepath := path.Join(f.Dir, file)
  821. return os.RemoveAll(filepath)
  822. }
  823. // Close deletes the context
  824. func (f *FakeContext) Close() error {
  825. return os.RemoveAll(f.Dir)
  826. }
  827. func fakeContextFromNewTempDir() (*FakeContext, error) {
  828. tmp, err := ioutil.TempDir("", "fake-context")
  829. if err != nil {
  830. return nil, err
  831. }
  832. if err := os.Chmod(tmp, 0755); err != nil {
  833. return nil, err
  834. }
  835. return fakeContextFromDir(tmp), nil
  836. }
  837. func fakeContextFromDir(dir string) *FakeContext {
  838. return &FakeContext{dir}
  839. }
  840. func fakeContextWithFiles(files map[string]string) (*FakeContext, error) {
  841. ctx, err := fakeContextFromNewTempDir()
  842. if err != nil {
  843. return nil, err
  844. }
  845. for file, content := range files {
  846. if err := ctx.Add(file, content); err != nil {
  847. ctx.Close()
  848. return nil, err
  849. }
  850. }
  851. return ctx, nil
  852. }
  853. func fakeContextAddDockerfile(ctx *FakeContext, dockerfile string) error {
  854. if err := ctx.Add("Dockerfile", dockerfile); err != nil {
  855. ctx.Close()
  856. return err
  857. }
  858. return nil
  859. }
  860. func fakeContext(dockerfile string, files map[string]string) (*FakeContext, error) {
  861. ctx, err := fakeContextWithFiles(files)
  862. if err != nil {
  863. return nil, err
  864. }
  865. if err := fakeContextAddDockerfile(ctx, dockerfile); err != nil {
  866. return nil, err
  867. }
  868. return ctx, nil
  869. }
  870. // FakeStorage is a static file server. It might be running locally or remotely
  871. // on test host.
  872. type FakeStorage interface {
  873. Close() error
  874. URL() string
  875. CtxDir() string
  876. }
  877. func fakeBinaryStorage(archives map[string]*bytes.Buffer) (FakeStorage, error) {
  878. ctx, err := fakeContextFromNewTempDir()
  879. if err != nil {
  880. return nil, err
  881. }
  882. for name, content := range archives {
  883. if err := ctx.addFile(name, content.Bytes()); err != nil {
  884. return nil, err
  885. }
  886. }
  887. return fakeStorageWithContext(ctx)
  888. }
  889. // fakeStorage returns either a local or remote (at daemon machine) file server
  890. func fakeStorage(files map[string]string) (FakeStorage, error) {
  891. ctx, err := fakeContextWithFiles(files)
  892. if err != nil {
  893. return nil, err
  894. }
  895. return fakeStorageWithContext(ctx)
  896. }
  897. // fakeStorageWithContext returns either a local or remote (at daemon machine) file server
  898. func fakeStorageWithContext(ctx *FakeContext) (FakeStorage, error) {
  899. if isLocalDaemon {
  900. return newLocalFakeStorage(ctx)
  901. }
  902. return newRemoteFileServer(ctx)
  903. }
  904. // localFileStorage is a file storage on the running machine
  905. type localFileStorage struct {
  906. *FakeContext
  907. *httptest.Server
  908. }
  909. func (s *localFileStorage) URL() string {
  910. return s.Server.URL
  911. }
  912. func (s *localFileStorage) CtxDir() string {
  913. return s.FakeContext.Dir
  914. }
  915. func (s *localFileStorage) Close() error {
  916. defer s.Server.Close()
  917. return s.FakeContext.Close()
  918. }
  919. func newLocalFakeStorage(ctx *FakeContext) (*localFileStorage, error) {
  920. handler := http.FileServer(http.Dir(ctx.Dir))
  921. server := httptest.NewServer(handler)
  922. return &localFileStorage{
  923. FakeContext: ctx,
  924. Server: server,
  925. }, nil
  926. }
  927. // remoteFileServer is a containerized static file server started on the remote
  928. // testing machine to be used in URL-accepting docker build functionality.
  929. type remoteFileServer struct {
  930. host string // hostname/port web server is listening to on docker host e.g. 0.0.0.0:43712
  931. container string
  932. image string
  933. ctx *FakeContext
  934. }
  935. func (f *remoteFileServer) URL() string {
  936. u := url.URL{
  937. Scheme: "http",
  938. Host: f.host}
  939. return u.String()
  940. }
  941. func (f *remoteFileServer) CtxDir() string {
  942. return f.ctx.Dir
  943. }
  944. func (f *remoteFileServer) Close() error {
  945. defer func() {
  946. if f.ctx != nil {
  947. f.ctx.Close()
  948. }
  949. if f.image != "" {
  950. deleteImages(f.image)
  951. }
  952. }()
  953. if f.container == "" {
  954. return nil
  955. }
  956. return deleteContainer(f.container)
  957. }
  958. func newRemoteFileServer(ctx *FakeContext) (*remoteFileServer, error) {
  959. var (
  960. image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
  961. container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
  962. )
  963. // Build the image
  964. if err := fakeContextAddDockerfile(ctx, `FROM httpserver
  965. COPY . /static`); err != nil {
  966. return nil, fmt.Errorf("Cannot add Dockerfile to context: %v", err)
  967. }
  968. if _, err := buildImageFromContext(image, ctx, false); err != nil {
  969. return nil, fmt.Errorf("failed building file storage container image: %v", err)
  970. }
  971. // Start the container
  972. runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--name", container, image)
  973. if out, ec, err := runCommandWithOutput(runCmd); err != nil {
  974. return nil, fmt.Errorf("failed to start file storage container. ec=%v\nout=%s\nerr=%v", ec, out, err)
  975. }
  976. // Find out the system assigned port
  977. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "port", container, "80/tcp"))
  978. if err != nil {
  979. return nil, fmt.Errorf("failed to find container port: err=%v\nout=%s", err, out)
  980. }
  981. fileserverHostPort := strings.Trim(out, "\n")
  982. _, port, err := net.SplitHostPort(fileserverHostPort)
  983. if err != nil {
  984. return nil, fmt.Errorf("unable to parse file server host:port: %v", err)
  985. }
  986. dockerHostURL, err := url.Parse(daemonHost())
  987. if err != nil {
  988. return nil, fmt.Errorf("unable to parse daemon host URL: %v", err)
  989. }
  990. host, _, err := net.SplitHostPort(dockerHostURL.Host)
  991. if err != nil {
  992. return nil, fmt.Errorf("unable to parse docker daemon host:port: %v", err)
  993. }
  994. return &remoteFileServer{
  995. container: container,
  996. image: image,
  997. host: fmt.Sprintf("%s:%s", host, port),
  998. ctx: ctx}, nil
  999. }
  1000. func inspectFieldAndMarshall(name, field string, output interface{}) error {
  1001. str, err := inspectFieldJSON(name, field)
  1002. if err != nil {
  1003. return err
  1004. }
  1005. return json.Unmarshal([]byte(str), output)
  1006. }
  1007. func inspectFilter(name, filter string) (string, error) {
  1008. format := fmt.Sprintf("{{%s}}", filter)
  1009. inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
  1010. out, exitCode, err := runCommandWithOutput(inspectCmd)
  1011. if err != nil || exitCode != 0 {
  1012. return "", fmt.Errorf("failed to inspect container %s: %s", name, out)
  1013. }
  1014. return strings.TrimSpace(out), nil
  1015. }
  1016. func inspectField(name, field string) (string, error) {
  1017. return inspectFilter(name, fmt.Sprintf(".%s", field))
  1018. }
  1019. func inspectFieldJSON(name, field string) (string, error) {
  1020. return inspectFilter(name, fmt.Sprintf("json .%s", field))
  1021. }
  1022. func inspectFieldMap(name, path, field string) (string, error) {
  1023. return inspectFilter(name, fmt.Sprintf("index .%s %q", path, field))
  1024. }
  1025. func inspectMountSourceField(name, destination string) (string, error) {
  1026. m, err := inspectMountPoint(name, destination)
  1027. if err != nil {
  1028. return "", err
  1029. }
  1030. return m.Source, nil
  1031. }
  1032. func inspectMountPoint(name, destination string) (types.MountPoint, error) {
  1033. out, err := inspectFieldJSON(name, "Mounts")
  1034. if err != nil {
  1035. return types.MountPoint{}, err
  1036. }
  1037. return inspectMountPointJSON(out, destination)
  1038. }
  1039. var errMountNotFound = errors.New("mount point not found")
  1040. func inspectMountPointJSON(j, destination string) (types.MountPoint, error) {
  1041. var mp []types.MountPoint
  1042. if err := unmarshalJSON([]byte(j), &mp); err != nil {
  1043. return types.MountPoint{}, err
  1044. }
  1045. var m *types.MountPoint
  1046. for _, c := range mp {
  1047. if c.Destination == destination {
  1048. m = &c
  1049. break
  1050. }
  1051. }
  1052. if m == nil {
  1053. return types.MountPoint{}, errMountNotFound
  1054. }
  1055. return *m, nil
  1056. }
  1057. func getIDByName(name string) (string, error) {
  1058. return inspectField(name, "Id")
  1059. }
  1060. // getContainerState returns the exit code of the container
  1061. // and true if it's running
  1062. // the exit code should be ignored if it's running
  1063. func getContainerState(c *check.C, id string) (int, bool, error) {
  1064. var (
  1065. exitStatus int
  1066. running bool
  1067. )
  1068. out, exitCode := dockerCmd(c, "inspect", "--format={{.State.Running}} {{.State.ExitCode}}", id)
  1069. if exitCode != 0 {
  1070. return 0, false, fmt.Errorf("%q doesn't exist: %s", id, out)
  1071. }
  1072. out = strings.Trim(out, "\n")
  1073. splitOutput := strings.Split(out, " ")
  1074. if len(splitOutput) != 2 {
  1075. return 0, false, fmt.Errorf("failed to get container state: output is broken")
  1076. }
  1077. if splitOutput[0] == "true" {
  1078. running = true
  1079. }
  1080. if n, err := strconv.Atoi(splitOutput[1]); err == nil {
  1081. exitStatus = n
  1082. } else {
  1083. return 0, false, fmt.Errorf("failed to get container state: couldn't parse integer")
  1084. }
  1085. return exitStatus, running, nil
  1086. }
  1087. func buildImageCmd(name, dockerfile string, useCache bool, buildFlags ...string) *exec.Cmd {
  1088. args := []string{"-D", "build", "-t", name}
  1089. if !useCache {
  1090. args = append(args, "--no-cache")
  1091. }
  1092. args = append(args, buildFlags...)
  1093. args = append(args, "-")
  1094. buildCmd := exec.Command(dockerBinary, args...)
  1095. buildCmd.Stdin = strings.NewReader(dockerfile)
  1096. return buildCmd
  1097. }
  1098. func buildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...string) (string, string, error) {
  1099. buildCmd := buildImageCmd(name, dockerfile, useCache, buildFlags...)
  1100. out, exitCode, err := runCommandWithOutput(buildCmd)
  1101. if err != nil || exitCode != 0 {
  1102. return "", out, fmt.Errorf("failed to build the image: %s", out)
  1103. }
  1104. id, err := getIDByName(name)
  1105. if err != nil {
  1106. return "", out, err
  1107. }
  1108. return id, out, nil
  1109. }
  1110. func buildImageWithStdoutStderr(name, dockerfile string, useCache bool, buildFlags ...string) (string, string, string, error) {
  1111. buildCmd := buildImageCmd(name, dockerfile, useCache, buildFlags...)
  1112. stdout, stderr, exitCode, err := runCommandWithStdoutStderr(buildCmd)
  1113. if err != nil || exitCode != 0 {
  1114. return "", stdout, stderr, fmt.Errorf("failed to build the image: %s", stdout)
  1115. }
  1116. id, err := getIDByName(name)
  1117. if err != nil {
  1118. return "", stdout, stderr, err
  1119. }
  1120. return id, stdout, stderr, nil
  1121. }
  1122. func buildImage(name, dockerfile string, useCache bool, buildFlags ...string) (string, error) {
  1123. id, _, err := buildImageWithOut(name, dockerfile, useCache, buildFlags...)
  1124. return id, err
  1125. }
  1126. func buildImageFromContext(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, error) {
  1127. id, _, err := buildImageFromContextWithOut(name, ctx, useCache, buildFlags...)
  1128. if err != nil {
  1129. return "", err
  1130. }
  1131. return id, nil
  1132. }
  1133. func buildImageFromContextWithOut(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, string, error) {
  1134. args := []string{"build", "-t", name}
  1135. if !useCache {
  1136. args = append(args, "--no-cache")
  1137. }
  1138. args = append(args, buildFlags...)
  1139. args = append(args, ".")
  1140. buildCmd := exec.Command(dockerBinary, args...)
  1141. buildCmd.Dir = ctx.Dir
  1142. out, exitCode, err := runCommandWithOutput(buildCmd)
  1143. if err != nil || exitCode != 0 {
  1144. return "", "", fmt.Errorf("failed to build the image: %s", out)
  1145. }
  1146. id, err := getIDByName(name)
  1147. if err != nil {
  1148. return "", "", err
  1149. }
  1150. return id, out, nil
  1151. }
  1152. func buildImageFromContextWithStdoutStderr(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, string, string, error) {
  1153. args := []string{"build", "-t", name}
  1154. if !useCache {
  1155. args = append(args, "--no-cache")
  1156. }
  1157. args = append(args, buildFlags...)
  1158. args = append(args, ".")
  1159. buildCmd := exec.Command(dockerBinary, args...)
  1160. buildCmd.Dir = ctx.Dir
  1161. stdout, stderr, exitCode, err := runCommandWithStdoutStderr(buildCmd)
  1162. if err != nil || exitCode != 0 {
  1163. return "", stdout, stderr, fmt.Errorf("failed to build the image: %s", stdout)
  1164. }
  1165. id, err := getIDByName(name)
  1166. if err != nil {
  1167. return "", stdout, stderr, err
  1168. }
  1169. return id, stdout, stderr, nil
  1170. }
  1171. func buildImageFromGitWithStdoutStderr(name string, ctx *fakeGit, useCache bool, buildFlags ...string) (string, string, string, error) {
  1172. args := []string{"build", "-t", name}
  1173. if !useCache {
  1174. args = append(args, "--no-cache")
  1175. }
  1176. args = append(args, buildFlags...)
  1177. args = append(args, ctx.RepoURL)
  1178. buildCmd := exec.Command(dockerBinary, args...)
  1179. stdout, stderr, exitCode, err := runCommandWithStdoutStderr(buildCmd)
  1180. if err != nil || exitCode != 0 {
  1181. return "", stdout, stderr, fmt.Errorf("failed to build the image: %s", stdout)
  1182. }
  1183. id, err := getIDByName(name)
  1184. if err != nil {
  1185. return "", stdout, stderr, err
  1186. }
  1187. return id, stdout, stderr, nil
  1188. }
  1189. func buildImageFromPath(name, path string, useCache bool, buildFlags ...string) (string, error) {
  1190. args := []string{"build", "-t", name}
  1191. if !useCache {
  1192. args = append(args, "--no-cache")
  1193. }
  1194. args = append(args, buildFlags...)
  1195. args = append(args, path)
  1196. buildCmd := exec.Command(dockerBinary, args...)
  1197. out, exitCode, err := runCommandWithOutput(buildCmd)
  1198. if err != nil || exitCode != 0 {
  1199. return "", fmt.Errorf("failed to build the image: %s", out)
  1200. }
  1201. return getIDByName(name)
  1202. }
  1203. type gitServer interface {
  1204. URL() string
  1205. Close() error
  1206. }
  1207. type localGitServer struct {
  1208. *httptest.Server
  1209. }
  1210. func (r *localGitServer) Close() error {
  1211. r.Server.Close()
  1212. return nil
  1213. }
  1214. func (r *localGitServer) URL() string {
  1215. return r.Server.URL
  1216. }
  1217. type fakeGit struct {
  1218. root string
  1219. server gitServer
  1220. RepoURL string
  1221. }
  1222. func (g *fakeGit) Close() {
  1223. g.server.Close()
  1224. os.RemoveAll(g.root)
  1225. }
  1226. func newFakeGit(name string, files map[string]string, enforceLocalServer bool) (*fakeGit, error) {
  1227. ctx, err := fakeContextWithFiles(files)
  1228. if err != nil {
  1229. return nil, err
  1230. }
  1231. defer ctx.Close()
  1232. curdir, err := os.Getwd()
  1233. if err != nil {
  1234. return nil, err
  1235. }
  1236. defer os.Chdir(curdir)
  1237. if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil {
  1238. return nil, fmt.Errorf("error trying to init repo: %s (%s)", err, output)
  1239. }
  1240. err = os.Chdir(ctx.Dir)
  1241. if err != nil {
  1242. return nil, err
  1243. }
  1244. if output, err := exec.Command("git", "config", "user.name", "Fake User").CombinedOutput(); err != nil {
  1245. return nil, fmt.Errorf("error trying to set 'user.name': %s (%s)", err, output)
  1246. }
  1247. if output, err := exec.Command("git", "config", "user.email", "fake.user@example.com").CombinedOutput(); err != nil {
  1248. return nil, fmt.Errorf("error trying to set 'user.email': %s (%s)", err, output)
  1249. }
  1250. if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil {
  1251. return nil, fmt.Errorf("error trying to add files to repo: %s (%s)", err, output)
  1252. }
  1253. if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil {
  1254. return nil, fmt.Errorf("error trying to commit to repo: %s (%s)", err, output)
  1255. }
  1256. root, err := ioutil.TempDir("", "docker-test-git-repo")
  1257. if err != nil {
  1258. return nil, err
  1259. }
  1260. repoPath := filepath.Join(root, name+".git")
  1261. if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil {
  1262. os.RemoveAll(root)
  1263. return nil, fmt.Errorf("error trying to clone --bare: %s (%s)", err, output)
  1264. }
  1265. err = os.Chdir(repoPath)
  1266. if err != nil {
  1267. os.RemoveAll(root)
  1268. return nil, err
  1269. }
  1270. if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil {
  1271. os.RemoveAll(root)
  1272. return nil, fmt.Errorf("error trying to git update-server-info: %s (%s)", err, output)
  1273. }
  1274. err = os.Chdir(curdir)
  1275. if err != nil {
  1276. os.RemoveAll(root)
  1277. return nil, err
  1278. }
  1279. var server gitServer
  1280. if !enforceLocalServer {
  1281. // use fakeStorage server, which might be local or remote (at test daemon)
  1282. server, err = fakeStorageWithContext(fakeContextFromDir(root))
  1283. if err != nil {
  1284. return nil, fmt.Errorf("cannot start fake storage: %v", err)
  1285. }
  1286. } else {
  1287. // always start a local http server on CLI test machine
  1288. httpServer := httptest.NewServer(http.FileServer(http.Dir(root)))
  1289. server = &localGitServer{httpServer}
  1290. }
  1291. return &fakeGit{
  1292. root: root,
  1293. server: server,
  1294. RepoURL: fmt.Sprintf("%s/%s.git", server.URL(), name),
  1295. }, nil
  1296. }
  1297. // Write `content` to the file at path `dst`, creating it if necessary,
  1298. // as well as any missing directories.
  1299. // The file is truncated if it already exists.
  1300. // Fail the test when error occurs.
  1301. func writeFile(dst, content string, c *check.C) {
  1302. // Create subdirectories if necessary
  1303. c.Assert(os.MkdirAll(path.Dir(dst), 0700), check.IsNil)
  1304. f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
  1305. c.Assert(err, check.IsNil)
  1306. defer f.Close()
  1307. // Write content (truncate if it exists)
  1308. _, err = io.Copy(f, strings.NewReader(content))
  1309. c.Assert(err, check.IsNil)
  1310. }
  1311. // Return the contents of file at path `src`.
  1312. // Fail the test when error occurs.
  1313. func readFile(src string, c *check.C) (content string) {
  1314. data, err := ioutil.ReadFile(src)
  1315. c.Assert(err, check.IsNil)
  1316. return string(data)
  1317. }
  1318. func containerStorageFile(containerID, basename string) string {
  1319. return filepath.Join(containerStoragePath, containerID, basename)
  1320. }
  1321. // docker commands that use this function must be run with the '-d' switch.
  1322. func runCommandAndReadContainerFile(filename string, cmd *exec.Cmd) ([]byte, error) {
  1323. out, _, err := runCommandWithOutput(cmd)
  1324. if err != nil {
  1325. return nil, fmt.Errorf("%v: %q", err, out)
  1326. }
  1327. contID := strings.TrimSpace(out)
  1328. if err := waitRun(contID); err != nil {
  1329. return nil, fmt.Errorf("%v: %q", contID, err)
  1330. }
  1331. return readContainerFile(contID, filename)
  1332. }
  1333. func readContainerFile(containerID, filename string) ([]byte, error) {
  1334. f, err := os.Open(containerStorageFile(containerID, filename))
  1335. if err != nil {
  1336. return nil, err
  1337. }
  1338. defer f.Close()
  1339. content, err := ioutil.ReadAll(f)
  1340. if err != nil {
  1341. return nil, err
  1342. }
  1343. return content, nil
  1344. }
  1345. func readContainerFileWithExec(containerID, filename string) ([]byte, error) {
  1346. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "exec", containerID, "cat", filename))
  1347. return []byte(out), err
  1348. }
  1349. // daemonTime provides the current time on the daemon host
  1350. func daemonTime(c *check.C) time.Time {
  1351. if isLocalDaemon {
  1352. return time.Now()
  1353. }
  1354. status, body, err := sockRequest("GET", "/info", nil)
  1355. c.Assert(err, check.IsNil)
  1356. c.Assert(status, check.Equals, http.StatusOK)
  1357. type infoJSON struct {
  1358. SystemTime string
  1359. }
  1360. var info infoJSON
  1361. err = json.Unmarshal(body, &info)
  1362. c.Assert(err, check.IsNil, check.Commentf("unable to unmarshal GET /info response"))
  1363. dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
  1364. c.Assert(err, check.IsNil, check.Commentf("invalid time format in GET /info response"))
  1365. return dt
  1366. }
  1367. func setupRegistry(c *check.C, schema1, auth bool) *testRegistryV2 {
  1368. testRequires(c, RegistryHosting)
  1369. reg, err := newTestRegistryV2(c, schema1, auth)
  1370. c.Assert(err, check.IsNil)
  1371. // Wait for registry to be ready to serve requests.
  1372. for i := 0; i != 50; i++ {
  1373. if err = reg.Ping(); err == nil {
  1374. break
  1375. }
  1376. time.Sleep(100 * time.Millisecond)
  1377. }
  1378. c.Assert(err, check.IsNil, check.Commentf("Timeout waiting for test registry to become available: %v", err))
  1379. return reg
  1380. }
  1381. func setupNotary(c *check.C) *testNotary {
  1382. testRequires(c, NotaryHosting)
  1383. ts, err := newTestNotary(c)
  1384. c.Assert(err, check.IsNil)
  1385. return ts
  1386. }
  1387. // appendBaseEnv appends the minimum set of environment variables to exec the
  1388. // docker cli binary for testing with correct configuration to the given env
  1389. // list.
  1390. func appendBaseEnv(env []string) []string {
  1391. preserveList := []string{
  1392. // preserve remote test host
  1393. "DOCKER_HOST",
  1394. // windows: requires preserving SystemRoot, otherwise dial tcp fails
  1395. // with "GetAddrInfoW: A non-recoverable error occurred during a database lookup."
  1396. "SystemRoot",
  1397. }
  1398. for _, key := range preserveList {
  1399. if val := os.Getenv(key); val != "" {
  1400. env = append(env, fmt.Sprintf("%s=%s", key, val))
  1401. }
  1402. }
  1403. return env
  1404. }
  1405. func createTmpFile(c *check.C, content string) string {
  1406. f, err := ioutil.TempFile("", "testfile")
  1407. c.Assert(err, check.IsNil)
  1408. filename := f.Name()
  1409. err = ioutil.WriteFile(filename, []byte(content), 0644)
  1410. c.Assert(err, check.IsNil)
  1411. return filename
  1412. }
  1413. func buildImageWithOutInDamon(socket string, name, dockerfile string, useCache bool) (string, error) {
  1414. args := []string{"--host", socket}
  1415. buildCmd := buildImageCmdArgs(args, name, dockerfile, useCache)
  1416. out, exitCode, err := runCommandWithOutput(buildCmd)
  1417. if err != nil || exitCode != 0 {
  1418. return out, fmt.Errorf("failed to build the image: %s, error: %v", out, err)
  1419. }
  1420. return out, nil
  1421. }
  1422. func buildImageCmdArgs(args []string, name, dockerfile string, useCache bool) *exec.Cmd {
  1423. args = append(args, []string{"-D", "build", "-t", name}...)
  1424. if !useCache {
  1425. args = append(args, "--no-cache")
  1426. }
  1427. args = append(args, "-")
  1428. buildCmd := exec.Command(dockerBinary, args...)
  1429. buildCmd.Stdin = strings.NewReader(dockerfile)
  1430. return buildCmd
  1431. }
  1432. func waitForContainer(contID string, args ...string) error {
  1433. args = append([]string{"run", "--name", contID}, args...)
  1434. cmd := exec.Command(dockerBinary, args...)
  1435. if _, err := runCommand(cmd); err != nil {
  1436. return err
  1437. }
  1438. if err := waitRun(contID); err != nil {
  1439. return err
  1440. }
  1441. return nil
  1442. }
  1443. // waitRun will wait for the specified container to be running, maximum 5 seconds.
  1444. func waitRun(contID string) error {
  1445. return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)
  1446. }
  1447. // waitExited will wait for the specified container to state exit, subject
  1448. // to a maximum time limit in seconds supplied by the caller
  1449. func waitExited(contID string, duration time.Duration) error {
  1450. return waitInspect(contID, "{{.State.Status}}", "exited", duration)
  1451. }
  1452. // waitInspect will wait for the specified container to have the specified string
  1453. // in the inspect output. It will wait until the specified timeout (in seconds)
  1454. // is reached.
  1455. func waitInspect(name, expr, expected string, timeout time.Duration) error {
  1456. after := time.After(timeout)
  1457. for {
  1458. cmd := exec.Command(dockerBinary, "inspect", "-f", expr, name)
  1459. out, _, err := runCommandWithOutput(cmd)
  1460. if err != nil {
  1461. if !strings.Contains(out, "No such") {
  1462. return fmt.Errorf("error executing docker inspect: %v\n%s", err, out)
  1463. }
  1464. select {
  1465. case <-after:
  1466. return err
  1467. default:
  1468. time.Sleep(10 * time.Millisecond)
  1469. continue
  1470. }
  1471. }
  1472. out = strings.TrimSpace(out)
  1473. if out == expected {
  1474. break
  1475. }
  1476. select {
  1477. case <-after:
  1478. return fmt.Errorf("condition \"%q == %q\" not true in time", out, expected)
  1479. default:
  1480. }
  1481. time.Sleep(100 * time.Millisecond)
  1482. }
  1483. return nil
  1484. }
  1485. func getInspectBody(c *check.C, version, id string) []byte {
  1486. endpoint := fmt.Sprintf("/%s/containers/%s/json", version, id)
  1487. status, body, err := sockRequest("GET", endpoint, nil)
  1488. c.Assert(err, check.IsNil)
  1489. c.Assert(status, check.Equals, http.StatusOK)
  1490. return body
  1491. }
  1492. // Run a long running idle task in a background container using the
  1493. // system-specific default image and command.
  1494. func runSleepingContainer(c *check.C, extraArgs ...string) (string, int) {
  1495. return runSleepingContainerInImage(c, defaultSleepImage, extraArgs...)
  1496. }
  1497. // Run a long running idle task in a background container using the specified
  1498. // image and the system-specific command.
  1499. func runSleepingContainerInImage(c *check.C, image string, extraArgs ...string) (string, int) {
  1500. args := []string{"run", "-d"}
  1501. args = append(args, extraArgs...)
  1502. args = append(args, image)
  1503. args = append(args, defaultSleepCommand...)
  1504. return dockerCmd(c, args...)
  1505. }