docker_utils.go 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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. cmd := exec.Command(dockerBinary, "images")
  35. cmd.Env = appendBaseEnv(true)
  36. out, err := cmd.CombinedOutput()
  37. if err != nil {
  38. panic(fmt.Errorf("err=%v\nout=%s\n", err, out))
  39. }
  40. lines := strings.Split(string(out), "\n")[1:]
  41. for _, l := range lines {
  42. if l == "" {
  43. continue
  44. }
  45. fields := strings.Fields(l)
  46. imgTag := fields[0] + ":" + fields[1]
  47. // just for case if we have dangling images in tested daemon
  48. if imgTag != "<none>:<none>" {
  49. protectedImages[imgTag] = struct{}{}
  50. }
  51. }
  52. // Obtain the daemon platform so that it can be used by tests to make
  53. // intelligent decisions about how to configure themselves, and validate
  54. // that the target platform is valid.
  55. res, _, err := sockRequestRaw("GET", "/version", nil, "application/json")
  56. if err != nil || res == nil || (res != nil && res.StatusCode != http.StatusOK) {
  57. panic(fmt.Errorf("Init failed to get version: %v. Res=%v", err.Error(), res))
  58. }
  59. svrHeader, _ := httputils.ParseServerHeader(res.Header.Get("Server"))
  60. daemonPlatform = svrHeader.OS
  61. if daemonPlatform != "linux" && daemonPlatform != "windows" {
  62. panic("Cannot run tests against platform: " + daemonPlatform)
  63. }
  64. // On Windows, extract out the version as we need to make selective
  65. // decisions during integration testing as and when features are implemented.
  66. if daemonPlatform == "windows" {
  67. if body, err := ioutil.ReadAll(res.Body); err == nil {
  68. var server types.Version
  69. if err := json.Unmarshal(body, &server); err == nil {
  70. // eg in "10.0 10550 (10550.1000.amd64fre.branch.date-time)" we want 10550
  71. windowsDaemonKV, _ = strconv.Atoi(strings.Split(server.KernelVersion, " ")[1])
  72. }
  73. }
  74. }
  75. // Now we know the daemon platform, can set paths used by tests.
  76. _, body, err := sockRequest("GET", "/info", nil)
  77. if err != nil {
  78. panic(err)
  79. }
  80. var info types.Info
  81. err = json.Unmarshal(body, &info)
  82. dockerBasePath = info.DockerRootDir
  83. volumesConfigPath = filepath.Join(dockerBasePath, "volumes")
  84. containerStoragePath = filepath.Join(dockerBasePath, "containers")
  85. // Make sure in context of daemon, not the local platform. Note we can't
  86. // use filepath.FromSlash or ToSlash here as they are a no-op on Unix.
  87. if daemonPlatform == "windows" {
  88. volumesConfigPath = strings.Replace(volumesConfigPath, `/`, `\`, -1)
  89. containerStoragePath = strings.Replace(containerStoragePath, `/`, `\`, -1)
  90. } else {
  91. volumesConfigPath = strings.Replace(volumesConfigPath, `\`, `/`, -1)
  92. containerStoragePath = strings.Replace(containerStoragePath, `\`, `/`, -1)
  93. }
  94. }
  95. // Daemon represents a Docker daemon for the testing framework.
  96. type Daemon struct {
  97. // Defaults to "daemon"
  98. // Useful to set to --daemon or -d for checking backwards compatibility
  99. Command string
  100. GlobalFlags []string
  101. id string
  102. c *check.C
  103. logFile *os.File
  104. folder string
  105. root string
  106. stdin io.WriteCloser
  107. stdout, stderr io.ReadCloser
  108. cmd *exec.Cmd
  109. storageDriver string
  110. wait chan error
  111. userlandProxy bool
  112. useDefaultHost bool
  113. useDefaultTLSHost bool
  114. }
  115. type clientConfig struct {
  116. transport *http.Transport
  117. scheme string
  118. addr string
  119. }
  120. // NewDaemon returns a Daemon instance to be used for testing.
  121. // This will create a directory such as d123456789 in the folder specified by $DEST.
  122. // The daemon will not automatically start.
  123. func NewDaemon(c *check.C) *Daemon {
  124. dest := os.Getenv("DEST")
  125. c.Assert(dest, check.Not(check.Equals), "", check.Commentf("Please set the DEST environment variable"))
  126. id := fmt.Sprintf("d%d", time.Now().UnixNano()%100000000)
  127. dir := filepath.Join(dest, id)
  128. daemonFolder, err := filepath.Abs(dir)
  129. c.Assert(err, check.IsNil, check.Commentf("Could not make %q an absolute path", dir))
  130. daemonRoot := filepath.Join(daemonFolder, "root")
  131. c.Assert(os.MkdirAll(daemonRoot, 0755), check.IsNil, check.Commentf("Could not create daemon root %q", dir))
  132. userlandProxy := true
  133. if env := os.Getenv("DOCKER_USERLANDPROXY"); env != "" {
  134. if val, err := strconv.ParseBool(env); err != nil {
  135. userlandProxy = val
  136. }
  137. }
  138. return &Daemon{
  139. Command: "daemon",
  140. id: id,
  141. c: c,
  142. folder: daemonFolder,
  143. root: daemonRoot,
  144. storageDriver: os.Getenv("DOCKER_GRAPHDRIVER"),
  145. userlandProxy: userlandProxy,
  146. }
  147. }
  148. func (d *Daemon) getClientConfig() (*clientConfig, error) {
  149. var (
  150. transport *http.Transport
  151. scheme string
  152. addr string
  153. proto string
  154. )
  155. if d.useDefaultTLSHost {
  156. option := &tlsconfig.Options{
  157. CAFile: "fixtures/https/ca.pem",
  158. CertFile: "fixtures/https/client-cert.pem",
  159. KeyFile: "fixtures/https/client-key.pem",
  160. }
  161. tlsConfig, err := tlsconfig.Client(*option)
  162. if err != nil {
  163. return nil, err
  164. }
  165. transport = &http.Transport{
  166. TLSClientConfig: tlsConfig,
  167. }
  168. addr = fmt.Sprintf("%s:%d", opts.DefaultHTTPHost, opts.DefaultTLSHTTPPort)
  169. scheme = "https"
  170. proto = "tcp"
  171. } else if d.useDefaultHost {
  172. addr = opts.DefaultUnixSocket
  173. proto = "unix"
  174. scheme = "http"
  175. transport = &http.Transport{}
  176. } else {
  177. addr = filepath.Join(d.folder, "docker.sock")
  178. proto = "unix"
  179. scheme = "http"
  180. transport = &http.Transport{}
  181. }
  182. sockets.ConfigureTCPTransport(transport, proto, addr)
  183. return &clientConfig{
  184. transport: transport,
  185. scheme: scheme,
  186. addr: addr,
  187. }, nil
  188. }
  189. // Start will start the daemon and return once it is ready to receive requests.
  190. // You can specify additional daemon flags.
  191. func (d *Daemon) Start(arg ...string) error {
  192. dockerBinary, err := exec.LookPath(dockerBinary)
  193. d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not find docker binary in $PATH", d.id))
  194. args := append(d.GlobalFlags,
  195. d.Command,
  196. "--graph", d.root,
  197. "--pidfile", fmt.Sprintf("%s/docker.pid", d.folder),
  198. fmt.Sprintf("--userland-proxy=%t", d.userlandProxy),
  199. )
  200. if !(d.useDefaultHost || d.useDefaultTLSHost) {
  201. args = append(args, []string{"--host", d.sock()}...)
  202. }
  203. if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
  204. args = append(args, []string{"--userns-remap", root}...)
  205. }
  206. // If we don't explicitly set the log-level or debug flag(-D) then
  207. // turn on debug mode
  208. foundIt := false
  209. for _, a := range arg {
  210. if strings.Contains(a, "--log-level") || strings.Contains(a, "-D") || strings.Contains(a, "--debug") {
  211. foundIt = true
  212. }
  213. }
  214. if !foundIt {
  215. args = append(args, "--debug")
  216. }
  217. if d.storageDriver != "" {
  218. args = append(args, "--storage-driver", d.storageDriver)
  219. }
  220. args = append(args, arg...)
  221. d.cmd = exec.Command(dockerBinary, args...)
  222. d.logFile, err = os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
  223. d.c.Assert(err, check.IsNil, check.Commentf("[%s] Could not create %s/docker.log", d.id, d.folder))
  224. d.cmd.Stdout = d.logFile
  225. d.cmd.Stderr = d.logFile
  226. if err := d.cmd.Start(); err != nil {
  227. return fmt.Errorf("[%s] could not start daemon container: %v", d.id, err)
  228. }
  229. wait := make(chan error)
  230. go func() {
  231. wait <- d.cmd.Wait()
  232. d.c.Logf("[%s] exiting daemon", d.id)
  233. close(wait)
  234. }()
  235. d.wait = wait
  236. tick := time.Tick(500 * time.Millisecond)
  237. // make sure daemon is ready to receive requests
  238. startTime := time.Now().Unix()
  239. for {
  240. d.c.Logf("[%s] waiting for daemon to start", d.id)
  241. if time.Now().Unix()-startTime > 5 {
  242. // After 5 seconds, give up
  243. return fmt.Errorf("[%s] Daemon exited and never started", d.id)
  244. }
  245. select {
  246. case <-time.After(2 * time.Second):
  247. return fmt.Errorf("[%s] timeout: daemon does not respond", d.id)
  248. case <-tick:
  249. clientConfig, err := d.getClientConfig()
  250. if err != nil {
  251. return err
  252. }
  253. client := &http.Client{
  254. Transport: clientConfig.transport,
  255. }
  256. req, err := http.NewRequest("GET", "/_ping", nil)
  257. d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not create new request", d.id))
  258. req.URL.Host = clientConfig.addr
  259. req.URL.Scheme = clientConfig.scheme
  260. resp, err := client.Do(req)
  261. if err != nil {
  262. continue
  263. }
  264. if resp.StatusCode != http.StatusOK {
  265. d.c.Logf("[%s] received status != 200 OK: %s", d.id, resp.Status)
  266. }
  267. d.c.Logf("[%s] daemon started", d.id)
  268. d.root, err = d.queryRootDir()
  269. if err != nil {
  270. return fmt.Errorf("[%s] error querying daemon for root directory: %v", d.id, err)
  271. }
  272. return nil
  273. }
  274. }
  275. }
  276. // StartWithBusybox will first start the daemon with Daemon.Start()
  277. // then save the busybox image from the main daemon and load it into this Daemon instance.
  278. func (d *Daemon) StartWithBusybox(arg ...string) error {
  279. if err := d.Start(arg...); err != nil {
  280. return err
  281. }
  282. bb := filepath.Join(d.folder, "busybox.tar")
  283. if _, err := os.Stat(bb); err != nil {
  284. if !os.IsNotExist(err) {
  285. return fmt.Errorf("unexpected error on busybox.tar stat: %v", err)
  286. }
  287. // saving busybox image from main daemon
  288. if err := exec.Command(dockerBinary, "save", "--output", bb, "busybox:latest").Run(); err != nil {
  289. return fmt.Errorf("could not save busybox image: %v", err)
  290. }
  291. }
  292. // loading busybox image to this daemon
  293. if out, err := d.Cmd("load", "--input", bb); err != nil {
  294. return fmt.Errorf("could not load busybox image: %s", out)
  295. }
  296. if err := os.Remove(bb); err != nil {
  297. d.c.Logf("could not remove %s: %v", bb, err)
  298. }
  299. return nil
  300. }
  301. // Stop will send a SIGINT every second and wait for the daemon to stop.
  302. // If it timeouts, a SIGKILL is sent.
  303. // Stop will not delete the daemon directory. If a purged daemon is needed,
  304. // instantiate a new one with NewDaemon.
  305. func (d *Daemon) Stop() error {
  306. if d.cmd == nil || d.wait == nil {
  307. return errors.New("daemon not started")
  308. }
  309. defer func() {
  310. d.logFile.Close()
  311. d.cmd = nil
  312. }()
  313. i := 1
  314. tick := time.Tick(time.Second)
  315. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  316. return fmt.Errorf("could not send signal: %v", err)
  317. }
  318. out1:
  319. for {
  320. select {
  321. case err := <-d.wait:
  322. return err
  323. case <-time.After(15 * time.Second):
  324. // time for stopping jobs and run onShutdown hooks
  325. d.c.Log("timeout")
  326. break out1
  327. }
  328. }
  329. out2:
  330. for {
  331. select {
  332. case err := <-d.wait:
  333. return err
  334. case <-tick:
  335. i++
  336. if i > 4 {
  337. d.c.Logf("tried to interrupt daemon for %d times, now try to kill it", i)
  338. break out2
  339. }
  340. d.c.Logf("Attempt #%d: daemon is still running with pid %d", i, d.cmd.Process.Pid)
  341. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  342. return fmt.Errorf("could not send signal: %v", err)
  343. }
  344. }
  345. }
  346. if err := d.cmd.Process.Kill(); err != nil {
  347. d.c.Logf("Could not kill daemon: %v", err)
  348. return err
  349. }
  350. return nil
  351. }
  352. // Restart will restart the daemon by first stopping it and then starting it.
  353. func (d *Daemon) Restart(arg ...string) error {
  354. d.Stop()
  355. // in the case of tests running a user namespace-enabled daemon, we have resolved
  356. // d.root to be the actual final path of the graph dir after the "uid.gid" of
  357. // remapped root is added--we need to subtract it from the path before calling
  358. // start or else we will continue making subdirectories rather than truly restarting
  359. // with the same location/root:
  360. if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
  361. d.root = filepath.Dir(d.root)
  362. }
  363. return d.Start(arg...)
  364. }
  365. func (d *Daemon) queryRootDir() (string, error) {
  366. // update daemon root by asking /info endpoint (to support user
  367. // namespaced daemon with root remapped uid.gid directory)
  368. clientConfig, err := d.getClientConfig()
  369. if err != nil {
  370. return "", err
  371. }
  372. client := &http.Client{
  373. Transport: clientConfig.transport,
  374. }
  375. req, err := http.NewRequest("GET", "/info", nil)
  376. if err != nil {
  377. return "", err
  378. }
  379. req.Header.Set("Content-Type", "application/json")
  380. req.URL.Host = clientConfig.addr
  381. req.URL.Scheme = clientConfig.scheme
  382. resp, err := client.Do(req)
  383. if err != nil {
  384. return "", err
  385. }
  386. body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
  387. return resp.Body.Close()
  388. })
  389. type Info struct {
  390. DockerRootDir string
  391. }
  392. var b []byte
  393. var i Info
  394. b, err = readBody(body)
  395. if err == nil && resp.StatusCode == 200 {
  396. // read the docker root dir
  397. if err = json.Unmarshal(b, &i); err == nil {
  398. return i.DockerRootDir, nil
  399. }
  400. }
  401. return "", err
  402. }
  403. func (d *Daemon) sock() string {
  404. return fmt.Sprintf("unix://%s/docker.sock", d.folder)
  405. }
  406. // Cmd will execute a docker CLI command against this Daemon.
  407. // Example: d.Cmd("version") will run docker -H unix://path/to/unix.sock version
  408. func (d *Daemon) Cmd(name string, arg ...string) (string, error) {
  409. args := []string{"--host", d.sock(), name}
  410. args = append(args, arg...)
  411. c := exec.Command(dockerBinary, args...)
  412. b, err := c.CombinedOutput()
  413. return string(b), err
  414. }
  415. // CmdWithArgs will execute a docker CLI command against a daemon with the
  416. // given additional arguments
  417. func (d *Daemon) CmdWithArgs(daemonArgs []string, name string, arg ...string) (string, error) {
  418. args := append(daemonArgs, name)
  419. args = append(args, arg...)
  420. c := exec.Command(dockerBinary, args...)
  421. b, err := c.CombinedOutput()
  422. return string(b), err
  423. }
  424. // LogfileName returns the path the the daemon's log file
  425. func (d *Daemon) LogfileName() string {
  426. return d.logFile.Name()
  427. }
  428. func daemonHost() string {
  429. daemonURLStr := "unix://" + opts.DefaultUnixSocket
  430. if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" {
  431. daemonURLStr = daemonHostVar
  432. }
  433. return daemonURLStr
  434. }
  435. func getTLSConfig() (*tls.Config, error) {
  436. dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
  437. if dockerCertPath == "" {
  438. return nil, fmt.Errorf("DOCKER_TLS_VERIFY specified, but no DOCKER_CERT_PATH environment variable")
  439. }
  440. option := &tlsconfig.Options{
  441. CAFile: filepath.Join(dockerCertPath, "ca.pem"),
  442. CertFile: filepath.Join(dockerCertPath, "cert.pem"),
  443. KeyFile: filepath.Join(dockerCertPath, "key.pem"),
  444. }
  445. tlsConfig, err := tlsconfig.Client(*option)
  446. if err != nil {
  447. return nil, err
  448. }
  449. return tlsConfig, nil
  450. }
  451. func sockConn(timeout time.Duration) (net.Conn, error) {
  452. daemon := daemonHost()
  453. daemonURL, err := url.Parse(daemon)
  454. if err != nil {
  455. return nil, fmt.Errorf("could not parse url %q: %v", daemon, err)
  456. }
  457. var c net.Conn
  458. switch daemonURL.Scheme {
  459. case "unix":
  460. return net.DialTimeout(daemonURL.Scheme, daemonURL.Path, timeout)
  461. case "tcp":
  462. if os.Getenv("DOCKER_TLS_VERIFY") != "" {
  463. // Setup the socket TLS configuration.
  464. tlsConfig, err := getTLSConfig()
  465. if err != nil {
  466. return nil, err
  467. }
  468. dialer := &net.Dialer{Timeout: timeout}
  469. return tls.DialWithDialer(dialer, daemonURL.Scheme, daemonURL.Host, tlsConfig)
  470. }
  471. return net.DialTimeout(daemonURL.Scheme, daemonURL.Host, timeout)
  472. default:
  473. return c, fmt.Errorf("unknown scheme %v (%s)", daemonURL.Scheme, daemon)
  474. }
  475. }
  476. func sockRequest(method, endpoint string, data interface{}) (int, []byte, error) {
  477. jsonData := bytes.NewBuffer(nil)
  478. if err := json.NewEncoder(jsonData).Encode(data); err != nil {
  479. return -1, nil, err
  480. }
  481. res, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json")
  482. if err != nil {
  483. return -1, nil, err
  484. }
  485. b, err := readBody(body)
  486. return res.StatusCode, b, err
  487. }
  488. func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
  489. req, client, err := newRequestClient(method, endpoint, data, ct)
  490. if err != nil {
  491. return nil, nil, err
  492. }
  493. resp, err := client.Do(req)
  494. if err != nil {
  495. client.Close()
  496. return nil, nil, err
  497. }
  498. body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
  499. defer resp.Body.Close()
  500. return client.Close()
  501. })
  502. return resp, body, nil
  503. }
  504. func sockRequestHijack(method, endpoint string, data io.Reader, ct string) (net.Conn, *bufio.Reader, error) {
  505. req, client, err := newRequestClient(method, endpoint, data, ct)
  506. if err != nil {
  507. return nil, nil, err
  508. }
  509. client.Do(req)
  510. conn, br := client.Hijack()
  511. return conn, br, nil
  512. }
  513. func newRequestClient(method, endpoint string, data io.Reader, ct string) (*http.Request, *httputil.ClientConn, error) {
  514. c, err := sockConn(time.Duration(10 * time.Second))
  515. if err != nil {
  516. return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err)
  517. }
  518. client := httputil.NewClientConn(c, nil)
  519. req, err := http.NewRequest(method, endpoint, data)
  520. if err != nil {
  521. client.Close()
  522. return nil, nil, fmt.Errorf("could not create new request: %v", err)
  523. }
  524. if ct != "" {
  525. req.Header.Set("Content-Type", ct)
  526. }
  527. return req, client, nil
  528. }
  529. func readBody(b io.ReadCloser) ([]byte, error) {
  530. defer b.Close()
  531. return ioutil.ReadAll(b)
  532. }
  533. func deleteContainer(container string) error {
  534. container = strings.TrimSpace(strings.Replace(container, "\n", " ", -1))
  535. rmArgs := strings.Split(fmt.Sprintf("rm -fv %v", container), " ")
  536. exitCode, err := runCommand(exec.Command(dockerBinary, rmArgs...))
  537. // set error manually if not set
  538. if exitCode != 0 && err == nil {
  539. err = fmt.Errorf("failed to remove container: `docker rm` exit is non-zero")
  540. }
  541. return err
  542. }
  543. func getAllContainers() (string, error) {
  544. getContainersCmd := exec.Command(dockerBinary, "ps", "-q", "-a")
  545. out, exitCode, err := runCommandWithOutput(getContainersCmd)
  546. if exitCode != 0 && err == nil {
  547. err = fmt.Errorf("failed to get a list of containers: %v\n", out)
  548. }
  549. return out, err
  550. }
  551. func deleteAllContainers() error {
  552. containers, err := getAllContainers()
  553. if err != nil {
  554. fmt.Println(containers)
  555. return err
  556. }
  557. if containers != "" {
  558. if err = deleteContainer(containers); err != nil {
  559. return err
  560. }
  561. }
  562. return nil
  563. }
  564. func deleteAllNetworks() error {
  565. networks, err := getAllNetworks()
  566. if err != nil {
  567. return err
  568. }
  569. var errors []string
  570. for _, n := range networks {
  571. if n.Name == "bridge" || n.Name == "none" || n.Name == "host" {
  572. continue
  573. }
  574. status, b, err := sockRequest("DELETE", "/networks/"+n.Name, nil)
  575. if err != nil {
  576. errors = append(errors, err.Error())
  577. continue
  578. }
  579. if status != http.StatusNoContent {
  580. errors = append(errors, fmt.Sprintf("error deleting network %s: %s", n.Name, string(b)))
  581. }
  582. }
  583. if len(errors) > 0 {
  584. return fmt.Errorf(strings.Join(errors, "\n"))
  585. }
  586. return nil
  587. }
  588. func getAllNetworks() ([]types.NetworkResource, error) {
  589. var networks []types.NetworkResource
  590. _, b, err := sockRequest("GET", "/networks", nil)
  591. if err != nil {
  592. return nil, err
  593. }
  594. if err := json.Unmarshal(b, &networks); err != nil {
  595. return nil, err
  596. }
  597. return networks, nil
  598. }
  599. func deleteAllVolumes() error {
  600. volumes, err := getAllVolumes()
  601. if err != nil {
  602. return err
  603. }
  604. var errors []string
  605. for _, v := range volumes {
  606. status, b, err := sockRequest("DELETE", "/volumes/"+v.Name, nil)
  607. if err != nil {
  608. errors = append(errors, err.Error())
  609. continue
  610. }
  611. if status != http.StatusNoContent {
  612. errors = append(errors, fmt.Sprintf("error deleting volume %s: %s", v.Name, string(b)))
  613. }
  614. }
  615. if len(errors) > 0 {
  616. return fmt.Errorf(strings.Join(errors, "\n"))
  617. }
  618. return nil
  619. }
  620. func getAllVolumes() ([]*types.Volume, error) {
  621. var volumes types.VolumesListResponse
  622. _, b, err := sockRequest("GET", "/volumes", nil)
  623. if err != nil {
  624. return nil, err
  625. }
  626. if err := json.Unmarshal(b, &volumes); err != nil {
  627. return nil, err
  628. }
  629. return volumes.Volumes, nil
  630. }
  631. var protectedImages = map[string]struct{}{}
  632. func deleteAllImages() error {
  633. cmd := exec.Command(dockerBinary, "images")
  634. cmd.Env = appendBaseEnv(true)
  635. out, err := cmd.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{"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 bool) *testRegistryV2 {
  1368. testRequires(c, RegistryHosting)
  1369. reg, err := newTestRegistryV2(c, schema1)
  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"))
  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(isTLS bool, 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. if isTLS {
  1399. preserveList = append(preserveList, "DOCKER_TLS_VERIFY", "DOCKER_CERT_PATH")
  1400. }
  1401. for _, key := range preserveList {
  1402. if val := os.Getenv(key); val != "" {
  1403. env = append(env, fmt.Sprintf("%s=%s", key, val))
  1404. }
  1405. }
  1406. return env
  1407. }
  1408. func createTmpFile(c *check.C, content string) string {
  1409. f, err := ioutil.TempFile("", "testfile")
  1410. c.Assert(err, check.IsNil)
  1411. filename := f.Name()
  1412. err = ioutil.WriteFile(filename, []byte(content), 0644)
  1413. c.Assert(err, check.IsNil)
  1414. return filename
  1415. }
  1416. func buildImageWithOutInDamon(socket string, name, dockerfile string, useCache bool) (string, error) {
  1417. args := []string{"--host", socket}
  1418. buildCmd := buildImageCmdArgs(args, name, dockerfile, useCache)
  1419. out, exitCode, err := runCommandWithOutput(buildCmd)
  1420. if err != nil || exitCode != 0 {
  1421. return out, fmt.Errorf("failed to build the image: %s, error: %v", out, err)
  1422. }
  1423. return out, nil
  1424. }
  1425. func buildImageCmdArgs(args []string, name, dockerfile string, useCache bool) *exec.Cmd {
  1426. args = append(args, []string{"-D", "build", "-t", name}...)
  1427. if !useCache {
  1428. args = append(args, "--no-cache")
  1429. }
  1430. args = append(args, "-")
  1431. buildCmd := exec.Command(dockerBinary, args...)
  1432. buildCmd.Stdin = strings.NewReader(dockerfile)
  1433. return buildCmd
  1434. }
  1435. func waitForContainer(contID string, args ...string) error {
  1436. args = append([]string{"run", "--name", contID}, args...)
  1437. cmd := exec.Command(dockerBinary, args...)
  1438. if _, err := runCommand(cmd); err != nil {
  1439. return err
  1440. }
  1441. if err := waitRun(contID); err != nil {
  1442. return err
  1443. }
  1444. return nil
  1445. }
  1446. // waitRun will wait for the specified container to be running, maximum 5 seconds.
  1447. func waitRun(contID string) error {
  1448. return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)
  1449. }
  1450. // waitExited will wait for the specified container to state exit, subject
  1451. // to a maximum time limit in seconds supplied by the caller
  1452. func waitExited(contID string, duration time.Duration) error {
  1453. return waitInspect(contID, "{{.State.Status}}", "exited", duration)
  1454. }
  1455. // waitInspect will wait for the specified container to have the specified string
  1456. // in the inspect output. It will wait until the specified timeout (in seconds)
  1457. // is reached.
  1458. func waitInspect(name, expr, expected string, timeout time.Duration) error {
  1459. after := time.After(timeout)
  1460. for {
  1461. cmd := exec.Command(dockerBinary, "inspect", "-f", expr, name)
  1462. out, _, err := runCommandWithOutput(cmd)
  1463. if err != nil {
  1464. if !strings.Contains(out, "No such") {
  1465. return fmt.Errorf("error executing docker inspect: %v\n%s", err, out)
  1466. }
  1467. select {
  1468. case <-after:
  1469. return err
  1470. default:
  1471. time.Sleep(10 * time.Millisecond)
  1472. continue
  1473. }
  1474. }
  1475. out = strings.TrimSpace(out)
  1476. if out == expected {
  1477. break
  1478. }
  1479. select {
  1480. case <-after:
  1481. return fmt.Errorf("condition \"%q == %q\" not true in time", out, expected)
  1482. default:
  1483. }
  1484. time.Sleep(100 * time.Millisecond)
  1485. }
  1486. return nil
  1487. }
  1488. func getInspectBody(c *check.C, version, id string) []byte {
  1489. endpoint := fmt.Sprintf("/%s/containers/%s/json", version, id)
  1490. status, body, err := sockRequest("GET", endpoint, nil)
  1491. c.Assert(err, check.IsNil)
  1492. c.Assert(status, check.Equals, http.StatusOK)
  1493. return body
  1494. }
  1495. // Run a long running idle task in a background container using the
  1496. // system-specific default image and command.
  1497. func runSleepingContainer(c *check.C, extraArgs ...string) (string, int) {
  1498. return runSleepingContainerInImage(c, defaultSleepImage, extraArgs...)
  1499. }
  1500. // Run a long running idle task in a background container using the specified
  1501. // image and the system-specific command.
  1502. func runSleepingContainerInImage(c *check.C, image string, extraArgs ...string) (string, int) {
  1503. args := []string{"run", "-d"}
  1504. args = append(args, extraArgs...)
  1505. args = append(args, image)
  1506. args = append(args, defaultSleepCommand...)
  1507. return dockerCmd(c, args...)
  1508. }
  1509. func getRootUIDGID() (int, int, error) {
  1510. uidgid := strings.Split(filepath.Base(dockerBasePath), ".")
  1511. if len(uidgid) == 1 {
  1512. //user namespace remapping is not turned on; return 0
  1513. return 0, 0, nil
  1514. }
  1515. uid, err := strconv.Atoi(uidgid[0])
  1516. if err != nil {
  1517. return 0, 0, err
  1518. }
  1519. gid, err := strconv.Atoi(uidgid[1])
  1520. if err != nil {
  1521. return 0, 0, err
  1522. }
  1523. return uid, gid, nil
  1524. }