daemon.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. package daemon // import "github.com/docker/docker/internal/test/daemon"
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/docker/docker/api/types"
  15. "github.com/docker/docker/api/types/events"
  16. "github.com/docker/docker/client"
  17. "github.com/docker/docker/internal/test/request"
  18. "github.com/docker/docker/opts"
  19. "github.com/docker/docker/pkg/ioutils"
  20. "github.com/docker/docker/pkg/stringid"
  21. "github.com/docker/go-connections/sockets"
  22. "github.com/docker/go-connections/tlsconfig"
  23. "github.com/gotestyourself/gotestyourself/assert"
  24. "github.com/pkg/errors"
  25. )
  26. type testingT interface {
  27. assert.TestingT
  28. logT
  29. Fatalf(string, ...interface{})
  30. }
  31. type logT interface {
  32. Logf(string, ...interface{})
  33. }
  34. const defaultDockerdBinary = "dockerd"
  35. var errDaemonNotStarted = errors.New("daemon not started")
  36. // SockRoot holds the path of the default docker integration daemon socket
  37. var SockRoot = filepath.Join(os.TempDir(), "docker-integration")
  38. type clientConfig struct {
  39. transport *http.Transport
  40. scheme string
  41. addr string
  42. }
  43. // Daemon represents a Docker daemon for the testing framework
  44. type Daemon struct {
  45. GlobalFlags []string
  46. Root string
  47. Folder string
  48. Wait chan error
  49. UseDefaultHost bool
  50. UseDefaultTLSHost bool
  51. id string
  52. logFile *os.File
  53. cmd *exec.Cmd
  54. storageDriver string
  55. userlandProxy bool
  56. execRoot string
  57. experimental bool
  58. dockerdBinary string
  59. log logT
  60. // swarm related field
  61. swarmListenAddr string
  62. SwarmPort int // FIXME(vdemeester) should probably not be exported
  63. // cached information
  64. CachedInfo types.Info
  65. }
  66. // New returns a Daemon instance to be used for testing.
  67. // This will create a directory such as d123456789 in the folder specified by $DOCKER_INTEGRATION_DAEMON_DEST or $DEST.
  68. // The daemon will not automatically start.
  69. func New(t testingT, ops ...func(*Daemon)) *Daemon {
  70. dest := os.Getenv("DOCKER_INTEGRATION_DAEMON_DEST")
  71. if dest == "" {
  72. dest = os.Getenv("DEST")
  73. }
  74. assert.Check(t, dest != "", "Please set the DOCKER_INTEGRATION_DAEMON_DEST or the DEST environment variable")
  75. storageDriver := os.Getenv("DOCKER_GRAPHDRIVER")
  76. assert.NilError(t, os.MkdirAll(SockRoot, 0700), "could not create daemon socket root")
  77. id := fmt.Sprintf("d%s", stringid.TruncateID(stringid.GenerateRandomID()))
  78. dir := filepath.Join(dest, id)
  79. daemonFolder, err := filepath.Abs(dir)
  80. assert.NilError(t, err, "Could not make %q an absolute path", dir)
  81. daemonRoot := filepath.Join(daemonFolder, "root")
  82. assert.NilError(t, os.MkdirAll(daemonRoot, 0755), "Could not create daemon root %q", dir)
  83. userlandProxy := true
  84. if env := os.Getenv("DOCKER_USERLANDPROXY"); env != "" {
  85. if val, err := strconv.ParseBool(env); err != nil {
  86. userlandProxy = val
  87. }
  88. }
  89. d := &Daemon{
  90. id: id,
  91. Folder: daemonFolder,
  92. Root: daemonRoot,
  93. storageDriver: storageDriver,
  94. userlandProxy: userlandProxy,
  95. execRoot: filepath.Join(os.TempDir(), "docker-execroot", id),
  96. dockerdBinary: defaultDockerdBinary,
  97. swarmListenAddr: defaultSwarmListenAddr,
  98. SwarmPort: DefaultSwarmPort,
  99. log: t,
  100. }
  101. for _, op := range ops {
  102. op(d)
  103. }
  104. return d
  105. }
  106. // RootDir returns the root directory of the daemon.
  107. func (d *Daemon) RootDir() string {
  108. return d.Root
  109. }
  110. // ID returns the generated id of the daemon
  111. func (d *Daemon) ID() string {
  112. return d.id
  113. }
  114. // StorageDriver returns the configured storage driver of the daemon
  115. func (d *Daemon) StorageDriver() string {
  116. return d.storageDriver
  117. }
  118. // Sock returns the socket path of the daemon
  119. func (d *Daemon) Sock() string {
  120. return fmt.Sprintf("unix://" + d.sockPath())
  121. }
  122. func (d *Daemon) sockPath() string {
  123. return filepath.Join(SockRoot, d.id+".sock")
  124. }
  125. // LogFileName returns the path the daemon's log file
  126. func (d *Daemon) LogFileName() string {
  127. return d.logFile.Name()
  128. }
  129. // ReadLogFile returns the content of the daemon log file
  130. func (d *Daemon) ReadLogFile() ([]byte, error) {
  131. return ioutil.ReadFile(d.logFile.Name())
  132. }
  133. // NewClient creates new client based on daemon's socket path
  134. // FIXME(vdemeester): replace NewClient with NewClientT
  135. func (d *Daemon) NewClient() (*client.Client, error) {
  136. return client.NewClientWithOpts(
  137. client.FromEnv,
  138. client.WithHost(d.Sock()))
  139. }
  140. // NewClientT creates new client based on daemon's socket path
  141. // FIXME(vdemeester): replace NewClient with NewClientT
  142. func (d *Daemon) NewClientT(t assert.TestingT) *client.Client {
  143. c, err := client.NewClientWithOpts(
  144. client.FromEnv,
  145. client.WithHost(d.Sock()))
  146. assert.NilError(t, err, "cannot create daemon client")
  147. return c
  148. }
  149. // CleanupExecRoot cleans the daemon exec root (network namespaces, ...)
  150. func (d *Daemon) CleanupExecRoot(t testingT) {
  151. cleanupExecRoot(t, d.execRoot)
  152. }
  153. // Start starts the daemon and return once it is ready to receive requests.
  154. func (d *Daemon) Start(t testingT, args ...string) {
  155. if err := d.StartWithError(args...); err != nil {
  156. t.Fatalf("Error starting daemon with arguments: %v", args)
  157. }
  158. }
  159. // StartWithError starts the daemon and return once it is ready to receive requests.
  160. // It returns an error in case it couldn't start.
  161. func (d *Daemon) StartWithError(args ...string) error {
  162. logFile, err := os.OpenFile(filepath.Join(d.Folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
  163. if err != nil {
  164. return errors.Wrapf(err, "[%s] Could not create %s/docker.log", d.id, d.Folder)
  165. }
  166. return d.StartWithLogFile(logFile, args...)
  167. }
  168. // StartWithLogFile will start the daemon and attach its streams to a given file.
  169. func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
  170. dockerdBinary, err := exec.LookPath(d.dockerdBinary)
  171. if err != nil {
  172. return errors.Wrapf(err, "[%s] could not find docker binary in $PATH", d.id)
  173. }
  174. args := append(d.GlobalFlags,
  175. "--containerd", "/var/run/docker/containerd/docker-containerd.sock",
  176. "--data-root", d.Root,
  177. "--exec-root", d.execRoot,
  178. "--pidfile", fmt.Sprintf("%s/docker.pid", d.Folder),
  179. fmt.Sprintf("--userland-proxy=%t", d.userlandProxy),
  180. )
  181. if d.experimental {
  182. args = append(args, "--experimental", "--init")
  183. }
  184. if !(d.UseDefaultHost || d.UseDefaultTLSHost) {
  185. args = append(args, []string{"--host", d.Sock()}...)
  186. }
  187. if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
  188. args = append(args, []string{"--userns-remap", root}...)
  189. }
  190. // If we don't explicitly set the log-level or debug flag(-D) then
  191. // turn on debug mode
  192. foundLog := false
  193. foundSd := false
  194. for _, a := range providedArgs {
  195. if strings.Contains(a, "--log-level") || strings.Contains(a, "-D") || strings.Contains(a, "--debug") {
  196. foundLog = true
  197. }
  198. if strings.Contains(a, "--storage-driver") {
  199. foundSd = true
  200. }
  201. }
  202. if !foundLog {
  203. args = append(args, "--debug")
  204. }
  205. if d.storageDriver != "" && !foundSd {
  206. args = append(args, "--storage-driver", d.storageDriver)
  207. }
  208. args = append(args, providedArgs...)
  209. d.cmd = exec.Command(dockerdBinary, args...)
  210. d.cmd.Env = append(os.Environ(), "DOCKER_SERVICE_PREFER_OFFLINE_IMAGE=1")
  211. d.cmd.Stdout = out
  212. d.cmd.Stderr = out
  213. d.logFile = out
  214. if err := d.cmd.Start(); err != nil {
  215. return errors.Errorf("[%s] could not start daemon container: %v", d.id, err)
  216. }
  217. wait := make(chan error)
  218. go func() {
  219. wait <- d.cmd.Wait()
  220. d.log.Logf("[%s] exiting daemon", d.id)
  221. close(wait)
  222. }()
  223. d.Wait = wait
  224. tick := time.Tick(500 * time.Millisecond)
  225. // make sure daemon is ready to receive requests
  226. startTime := time.Now().Unix()
  227. for {
  228. d.log.Logf("[%s] waiting for daemon to start", d.id)
  229. if time.Now().Unix()-startTime > 5 {
  230. // After 5 seconds, give up
  231. return errors.Errorf("[%s] Daemon exited and never started", d.id)
  232. }
  233. select {
  234. case <-time.After(2 * time.Second):
  235. return errors.Errorf("[%s] timeout: daemon does not respond", d.id)
  236. case <-tick:
  237. clientConfig, err := d.getClientConfig()
  238. if err != nil {
  239. return err
  240. }
  241. client := &http.Client{
  242. Transport: clientConfig.transport,
  243. }
  244. req, err := http.NewRequest("GET", "/_ping", nil)
  245. if err != nil {
  246. return errors.Wrapf(err, "[%s] could not create new request", d.id)
  247. }
  248. req.URL.Host = clientConfig.addr
  249. req.URL.Scheme = clientConfig.scheme
  250. resp, err := client.Do(req)
  251. if err != nil {
  252. continue
  253. }
  254. resp.Body.Close()
  255. if resp.StatusCode != http.StatusOK {
  256. d.log.Logf("[%s] received status != 200 OK: %s\n", d.id, resp.Status)
  257. }
  258. d.log.Logf("[%s] daemon started\n", d.id)
  259. d.Root, err = d.queryRootDir()
  260. if err != nil {
  261. return errors.Errorf("[%s] error querying daemon for root directory: %v", d.id, err)
  262. }
  263. return nil
  264. case <-d.Wait:
  265. return errors.Errorf("[%s] Daemon exited during startup", d.id)
  266. }
  267. }
  268. }
  269. // StartWithBusybox will first start the daemon with Daemon.Start()
  270. // then save the busybox image from the main daemon and load it into this Daemon instance.
  271. func (d *Daemon) StartWithBusybox(t testingT, arg ...string) {
  272. d.Start(t, arg...)
  273. d.LoadBusybox(t)
  274. }
  275. // Kill will send a SIGKILL to the daemon
  276. func (d *Daemon) Kill() error {
  277. if d.cmd == nil || d.Wait == nil {
  278. return errDaemonNotStarted
  279. }
  280. defer func() {
  281. d.logFile.Close()
  282. d.cmd = nil
  283. }()
  284. if err := d.cmd.Process.Kill(); err != nil {
  285. return err
  286. }
  287. return os.Remove(fmt.Sprintf("%s/docker.pid", d.Folder))
  288. }
  289. // Pid returns the pid of the daemon
  290. func (d *Daemon) Pid() int {
  291. return d.cmd.Process.Pid
  292. }
  293. // Interrupt stops the daemon by sending it an Interrupt signal
  294. func (d *Daemon) Interrupt() error {
  295. return d.Signal(os.Interrupt)
  296. }
  297. // Signal sends the specified signal to the daemon if running
  298. func (d *Daemon) Signal(signal os.Signal) error {
  299. if d.cmd == nil || d.Wait == nil {
  300. return errDaemonNotStarted
  301. }
  302. return d.cmd.Process.Signal(signal)
  303. }
  304. // DumpStackAndQuit sends SIGQUIT to the daemon, which triggers it to dump its
  305. // stack to its log file and exit
  306. // This is used primarily for gathering debug information on test timeout
  307. func (d *Daemon) DumpStackAndQuit() {
  308. if d.cmd == nil || d.cmd.Process == nil {
  309. return
  310. }
  311. SignalDaemonDump(d.cmd.Process.Pid)
  312. }
  313. // Stop will send a SIGINT every second and wait for the daemon to stop.
  314. // If it times out, a SIGKILL is sent.
  315. // Stop will not delete the daemon directory. If a purged daemon is needed,
  316. // instantiate a new one with NewDaemon.
  317. // If an error occurs while starting the daemon, the test will fail.
  318. func (d *Daemon) Stop(t testingT) {
  319. err := d.StopWithError()
  320. if err != nil {
  321. if err != errDaemonNotStarted {
  322. t.Fatalf("Error while stopping the daemon %s : %v", d.id, err)
  323. } else {
  324. t.Logf("Daemon %s is not started", d.id)
  325. }
  326. }
  327. }
  328. // StopWithError will send a SIGINT every second and wait for the daemon to stop.
  329. // If it timeouts, a SIGKILL is sent.
  330. // Stop will not delete the daemon directory. If a purged daemon is needed,
  331. // instantiate a new one with NewDaemon.
  332. func (d *Daemon) StopWithError() error {
  333. if d.cmd == nil || d.Wait == nil {
  334. return errDaemonNotStarted
  335. }
  336. defer func() {
  337. d.logFile.Close()
  338. d.cmd = nil
  339. }()
  340. i := 1
  341. tick := time.Tick(time.Second)
  342. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  343. if strings.Contains(err.Error(), "os: process already finished") {
  344. return errDaemonNotStarted
  345. }
  346. return errors.Errorf("could not send signal: %v", err)
  347. }
  348. out1:
  349. for {
  350. select {
  351. case err := <-d.Wait:
  352. return err
  353. case <-time.After(20 * time.Second):
  354. // time for stopping jobs and run onShutdown hooks
  355. d.log.Logf("[%s] daemon started", d.id)
  356. break out1
  357. }
  358. }
  359. out2:
  360. for {
  361. select {
  362. case err := <-d.Wait:
  363. return err
  364. case <-tick:
  365. i++
  366. if i > 5 {
  367. d.log.Logf("tried to interrupt daemon for %d times, now try to kill it", i)
  368. break out2
  369. }
  370. d.log.Logf("Attempt #%d: daemon is still running with pid %d", i, d.cmd.Process.Pid)
  371. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  372. return errors.Errorf("could not send signal: %v", err)
  373. }
  374. }
  375. }
  376. if err := d.cmd.Process.Kill(); err != nil {
  377. d.log.Logf("Could not kill daemon: %v", err)
  378. return err
  379. }
  380. d.cmd.Wait()
  381. return os.Remove(fmt.Sprintf("%s/docker.pid", d.Folder))
  382. }
  383. // Restart will restart the daemon by first stopping it and the starting it.
  384. // If an error occurs while starting the daemon, the test will fail.
  385. func (d *Daemon) Restart(t testingT, args ...string) {
  386. d.Stop(t)
  387. d.handleUserns()
  388. d.Start(t, args...)
  389. }
  390. // RestartWithError will restart the daemon by first stopping it and then starting it.
  391. func (d *Daemon) RestartWithError(arg ...string) error {
  392. if err := d.StopWithError(); err != nil {
  393. return err
  394. }
  395. d.handleUserns()
  396. return d.StartWithError(arg...)
  397. }
  398. func (d *Daemon) handleUserns() {
  399. // in the case of tests running a user namespace-enabled daemon, we have resolved
  400. // d.Root to be the actual final path of the graph dir after the "uid.gid" of
  401. // remapped root is added--we need to subtract it from the path before calling
  402. // start or else we will continue making subdirectories rather than truly restarting
  403. // with the same location/root:
  404. if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
  405. d.Root = filepath.Dir(d.Root)
  406. }
  407. }
  408. // ReloadConfig asks the daemon to reload its configuration
  409. func (d *Daemon) ReloadConfig() error {
  410. if d.cmd == nil || d.cmd.Process == nil {
  411. return errors.New("daemon is not running")
  412. }
  413. errCh := make(chan error)
  414. started := make(chan struct{})
  415. go func() {
  416. _, body, err := request.Get("/events", request.Host(d.Sock()))
  417. close(started)
  418. if err != nil {
  419. errCh <- err
  420. }
  421. defer body.Close()
  422. dec := json.NewDecoder(body)
  423. for {
  424. var e events.Message
  425. if err := dec.Decode(&e); err != nil {
  426. errCh <- err
  427. return
  428. }
  429. if e.Type != events.DaemonEventType {
  430. continue
  431. }
  432. if e.Action != "reload" {
  433. continue
  434. }
  435. close(errCh) // notify that we are done
  436. return
  437. }
  438. }()
  439. <-started
  440. if err := signalDaemonReload(d.cmd.Process.Pid); err != nil {
  441. return errors.Errorf("error signaling daemon reload: %v", err)
  442. }
  443. select {
  444. case err := <-errCh:
  445. if err != nil {
  446. return errors.Errorf("error waiting for daemon reload event: %v", err)
  447. }
  448. case <-time.After(30 * time.Second):
  449. return errors.New("timeout waiting for daemon reload event")
  450. }
  451. return nil
  452. }
  453. // LoadBusybox image into the daemon
  454. func (d *Daemon) LoadBusybox(t assert.TestingT) {
  455. clientHost, err := client.NewEnvClient()
  456. assert.NilError(t, err, "failed to create client")
  457. defer clientHost.Close()
  458. ctx := context.Background()
  459. reader, err := clientHost.ImageSave(ctx, []string{"busybox:latest"})
  460. assert.NilError(t, err, "failed to download busybox")
  461. defer reader.Close()
  462. client, err := d.NewClient()
  463. assert.NilError(t, err, "failed to create client")
  464. defer client.Close()
  465. resp, err := client.ImageLoad(ctx, reader, true)
  466. assert.NilError(t, err, "failed to load busybox")
  467. defer resp.Body.Close()
  468. }
  469. func (d *Daemon) getClientConfig() (*clientConfig, error) {
  470. var (
  471. transport *http.Transport
  472. scheme string
  473. addr string
  474. proto string
  475. )
  476. if d.UseDefaultTLSHost {
  477. option := &tlsconfig.Options{
  478. CAFile: "fixtures/https/ca.pem",
  479. CertFile: "fixtures/https/client-cert.pem",
  480. KeyFile: "fixtures/https/client-key.pem",
  481. }
  482. tlsConfig, err := tlsconfig.Client(*option)
  483. if err != nil {
  484. return nil, err
  485. }
  486. transport = &http.Transport{
  487. TLSClientConfig: tlsConfig,
  488. }
  489. addr = fmt.Sprintf("%s:%d", opts.DefaultHTTPHost, opts.DefaultTLSHTTPPort)
  490. scheme = "https"
  491. proto = "tcp"
  492. } else if d.UseDefaultHost {
  493. addr = opts.DefaultUnixSocket
  494. proto = "unix"
  495. scheme = "http"
  496. transport = &http.Transport{}
  497. } else {
  498. addr = d.sockPath()
  499. proto = "unix"
  500. scheme = "http"
  501. transport = &http.Transport{}
  502. }
  503. if err := sockets.ConfigureTransport(transport, proto, addr); err != nil {
  504. return nil, err
  505. }
  506. transport.DisableKeepAlives = true
  507. return &clientConfig{
  508. transport: transport,
  509. scheme: scheme,
  510. addr: addr,
  511. }, nil
  512. }
  513. func (d *Daemon) queryRootDir() (string, error) {
  514. // update daemon root by asking /info endpoint (to support user
  515. // namespaced daemon with root remapped uid.gid directory)
  516. clientConfig, err := d.getClientConfig()
  517. if err != nil {
  518. return "", err
  519. }
  520. client := &http.Client{
  521. Transport: clientConfig.transport,
  522. }
  523. req, err := http.NewRequest("GET", "/info", nil)
  524. if err != nil {
  525. return "", err
  526. }
  527. req.Header.Set("Content-Type", "application/json")
  528. req.URL.Host = clientConfig.addr
  529. req.URL.Scheme = clientConfig.scheme
  530. resp, err := client.Do(req)
  531. if err != nil {
  532. return "", err
  533. }
  534. body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
  535. return resp.Body.Close()
  536. })
  537. type Info struct {
  538. DockerRootDir string
  539. }
  540. var b []byte
  541. var i Info
  542. b, err = request.ReadBody(body)
  543. if err == nil && resp.StatusCode == http.StatusOK {
  544. // read the docker root dir
  545. if err = json.Unmarshal(b, &i); err == nil {
  546. return i.DockerRootDir, nil
  547. }
  548. }
  549. return "", err
  550. }
  551. // Info returns the info struct for this daemon
  552. func (d *Daemon) Info(t assert.TestingT) types.Info {
  553. apiclient, err := d.NewClient()
  554. assert.NilError(t, err)
  555. info, err := apiclient.Info(context.Background())
  556. assert.NilError(t, err)
  557. return info
  558. }