daemon.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. // Cleanup cleans the daemon files : exec root (network namespaces, ...), swarmkit files
  150. func (d *Daemon) Cleanup(t testingT) {
  151. // Cleanup swarmkit wal files if present
  152. cleanupRaftDir(t, d.Root)
  153. cleanupNetworkNamespace(t, d.execRoot)
  154. }
  155. // Start starts the daemon and return once it is ready to receive requests.
  156. func (d *Daemon) Start(t testingT, args ...string) {
  157. if err := d.StartWithError(args...); err != nil {
  158. t.Fatalf("Error starting daemon with arguments: %v", args)
  159. }
  160. }
  161. // StartWithError starts the daemon and return once it is ready to receive requests.
  162. // It returns an error in case it couldn't start.
  163. func (d *Daemon) StartWithError(args ...string) error {
  164. logFile, err := os.OpenFile(filepath.Join(d.Folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
  165. if err != nil {
  166. return errors.Wrapf(err, "[%s] Could not create %s/docker.log", d.id, d.Folder)
  167. }
  168. return d.StartWithLogFile(logFile, args...)
  169. }
  170. // StartWithLogFile will start the daemon and attach its streams to a given file.
  171. func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
  172. d.handleUserns()
  173. dockerdBinary, err := exec.LookPath(d.dockerdBinary)
  174. if err != nil {
  175. return errors.Wrapf(err, "[%s] could not find docker binary in $PATH", d.id)
  176. }
  177. args := append(d.GlobalFlags,
  178. "--containerd", "/var/run/docker/containerd/docker-containerd.sock",
  179. "--data-root", d.Root,
  180. "--exec-root", d.execRoot,
  181. "--pidfile", fmt.Sprintf("%s/docker.pid", d.Folder),
  182. fmt.Sprintf("--userland-proxy=%t", d.userlandProxy),
  183. )
  184. if d.experimental {
  185. args = append(args, "--experimental", "--init")
  186. }
  187. if !(d.UseDefaultHost || d.UseDefaultTLSHost) {
  188. args = append(args, []string{"--host", d.Sock()}...)
  189. }
  190. if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
  191. args = append(args, []string{"--userns-remap", root}...)
  192. }
  193. // If we don't explicitly set the log-level or debug flag(-D) then
  194. // turn on debug mode
  195. foundLog := false
  196. foundSd := false
  197. for _, a := range providedArgs {
  198. if strings.Contains(a, "--log-level") || strings.Contains(a, "-D") || strings.Contains(a, "--debug") {
  199. foundLog = true
  200. }
  201. if strings.Contains(a, "--storage-driver") {
  202. foundSd = true
  203. }
  204. }
  205. if !foundLog {
  206. args = append(args, "--debug")
  207. }
  208. if d.storageDriver != "" && !foundSd {
  209. args = append(args, "--storage-driver", d.storageDriver)
  210. }
  211. args = append(args, providedArgs...)
  212. d.cmd = exec.Command(dockerdBinary, args...)
  213. d.cmd.Env = append(os.Environ(), "DOCKER_SERVICE_PREFER_OFFLINE_IMAGE=1")
  214. d.cmd.Stdout = out
  215. d.cmd.Stderr = out
  216. d.logFile = out
  217. if err := d.cmd.Start(); err != nil {
  218. return errors.Errorf("[%s] could not start daemon container: %v", d.id, err)
  219. }
  220. wait := make(chan error)
  221. go func() {
  222. wait <- d.cmd.Wait()
  223. d.log.Logf("[%s] exiting daemon", d.id)
  224. close(wait)
  225. }()
  226. d.Wait = wait
  227. tick := time.Tick(500 * time.Millisecond)
  228. // make sure daemon is ready to receive requests
  229. startTime := time.Now().Unix()
  230. for {
  231. d.log.Logf("[%s] waiting for daemon to start", d.id)
  232. if time.Now().Unix()-startTime > 5 {
  233. // After 5 seconds, give up
  234. return errors.Errorf("[%s] Daemon exited and never started", d.id)
  235. }
  236. select {
  237. case <-time.After(2 * time.Second):
  238. return errors.Errorf("[%s] timeout: daemon does not respond", d.id)
  239. case <-tick:
  240. clientConfig, err := d.getClientConfig()
  241. if err != nil {
  242. return err
  243. }
  244. client := &http.Client{
  245. Transport: clientConfig.transport,
  246. }
  247. req, err := http.NewRequest("GET", "/_ping", nil)
  248. if err != nil {
  249. return errors.Wrapf(err, "[%s] could not create new request", d.id)
  250. }
  251. req.URL.Host = clientConfig.addr
  252. req.URL.Scheme = clientConfig.scheme
  253. resp, err := client.Do(req)
  254. if err != nil {
  255. continue
  256. }
  257. resp.Body.Close()
  258. if resp.StatusCode != http.StatusOK {
  259. d.log.Logf("[%s] received status != 200 OK: %s\n", d.id, resp.Status)
  260. }
  261. d.log.Logf("[%s] daemon started\n", d.id)
  262. d.Root, err = d.queryRootDir()
  263. if err != nil {
  264. return errors.Errorf("[%s] error querying daemon for root directory: %v", d.id, err)
  265. }
  266. return nil
  267. case <-d.Wait:
  268. return errors.Errorf("[%s] Daemon exited during startup", d.id)
  269. }
  270. }
  271. }
  272. // StartWithBusybox will first start the daemon with Daemon.Start()
  273. // then save the busybox image from the main daemon and load it into this Daemon instance.
  274. func (d *Daemon) StartWithBusybox(t testingT, arg ...string) {
  275. d.Start(t, arg...)
  276. d.LoadBusybox(t)
  277. }
  278. // Kill will send a SIGKILL to the daemon
  279. func (d *Daemon) Kill() error {
  280. if d.cmd == nil || d.Wait == nil {
  281. return errDaemonNotStarted
  282. }
  283. defer func() {
  284. d.logFile.Close()
  285. d.cmd = nil
  286. }()
  287. if err := d.cmd.Process.Kill(); err != nil {
  288. return err
  289. }
  290. return os.Remove(fmt.Sprintf("%s/docker.pid", d.Folder))
  291. }
  292. // Pid returns the pid of the daemon
  293. func (d *Daemon) Pid() int {
  294. return d.cmd.Process.Pid
  295. }
  296. // Interrupt stops the daemon by sending it an Interrupt signal
  297. func (d *Daemon) Interrupt() error {
  298. return d.Signal(os.Interrupt)
  299. }
  300. // Signal sends the specified signal to the daemon if running
  301. func (d *Daemon) Signal(signal os.Signal) error {
  302. if d.cmd == nil || d.Wait == nil {
  303. return errDaemonNotStarted
  304. }
  305. return d.cmd.Process.Signal(signal)
  306. }
  307. // DumpStackAndQuit sends SIGQUIT to the daemon, which triggers it to dump its
  308. // stack to its log file and exit
  309. // This is used primarily for gathering debug information on test timeout
  310. func (d *Daemon) DumpStackAndQuit() {
  311. if d.cmd == nil || d.cmd.Process == nil {
  312. return
  313. }
  314. SignalDaemonDump(d.cmd.Process.Pid)
  315. }
  316. // Stop will send a SIGINT every second and wait for the daemon to stop.
  317. // If it times out, a SIGKILL is sent.
  318. // Stop will not delete the daemon directory. If a purged daemon is needed,
  319. // instantiate a new one with NewDaemon.
  320. // If an error occurs while starting the daemon, the test will fail.
  321. func (d *Daemon) Stop(t testingT) {
  322. err := d.StopWithError()
  323. if err != nil {
  324. if err != errDaemonNotStarted {
  325. t.Fatalf("Error while stopping the daemon %s : %v", d.id, err)
  326. } else {
  327. t.Logf("Daemon %s is not started", d.id)
  328. }
  329. }
  330. }
  331. // StopWithError will send a SIGINT every second and wait for the daemon to stop.
  332. // If it timeouts, a SIGKILL is sent.
  333. // Stop will not delete the daemon directory. If a purged daemon is needed,
  334. // instantiate a new one with NewDaemon.
  335. func (d *Daemon) StopWithError() error {
  336. if d.cmd == nil || d.Wait == nil {
  337. return errDaemonNotStarted
  338. }
  339. defer func() {
  340. d.logFile.Close()
  341. d.cmd = nil
  342. }()
  343. i := 1
  344. tick := time.Tick(time.Second)
  345. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  346. if strings.Contains(err.Error(), "os: process already finished") {
  347. return errDaemonNotStarted
  348. }
  349. return errors.Errorf("could not send signal: %v", err)
  350. }
  351. out1:
  352. for {
  353. select {
  354. case err := <-d.Wait:
  355. return err
  356. case <-time.After(20 * time.Second):
  357. // time for stopping jobs and run onShutdown hooks
  358. d.log.Logf("[%s] daemon started", d.id)
  359. break out1
  360. }
  361. }
  362. out2:
  363. for {
  364. select {
  365. case err := <-d.Wait:
  366. return err
  367. case <-tick:
  368. i++
  369. if i > 5 {
  370. d.log.Logf("tried to interrupt daemon for %d times, now try to kill it", i)
  371. break out2
  372. }
  373. d.log.Logf("Attempt #%d: daemon is still running with pid %d", i, d.cmd.Process.Pid)
  374. if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
  375. return errors.Errorf("could not send signal: %v", err)
  376. }
  377. }
  378. }
  379. if err := d.cmd.Process.Kill(); err != nil {
  380. d.log.Logf("Could not kill daemon: %v", err)
  381. return err
  382. }
  383. d.cmd.Wait()
  384. return os.Remove(fmt.Sprintf("%s/docker.pid", d.Folder))
  385. }
  386. // Restart will restart the daemon by first stopping it and the starting it.
  387. // If an error occurs while starting the daemon, the test will fail.
  388. func (d *Daemon) Restart(t testingT, args ...string) {
  389. d.Stop(t)
  390. d.Start(t, args...)
  391. }
  392. // RestartWithError will restart the daemon by first stopping it and then starting it.
  393. func (d *Daemon) RestartWithError(arg ...string) error {
  394. if err := d.StopWithError(); err != nil {
  395. return err
  396. }
  397. return d.StartWithError(arg...)
  398. }
  399. func (d *Daemon) handleUserns() {
  400. // in the case of tests running a user namespace-enabled daemon, we have resolved
  401. // d.Root to be the actual final path of the graph dir after the "uid.gid" of
  402. // remapped root is added--we need to subtract it from the path before calling
  403. // start or else we will continue making subdirectories rather than truly restarting
  404. // with the same location/root:
  405. if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
  406. d.Root = filepath.Dir(d.Root)
  407. }
  408. }
  409. // ReloadConfig asks the daemon to reload its configuration
  410. func (d *Daemon) ReloadConfig() error {
  411. if d.cmd == nil || d.cmd.Process == nil {
  412. return errors.New("daemon is not running")
  413. }
  414. errCh := make(chan error)
  415. started := make(chan struct{})
  416. go func() {
  417. _, body, err := request.Get("/events", request.Host(d.Sock()))
  418. close(started)
  419. if err != nil {
  420. errCh <- err
  421. }
  422. defer body.Close()
  423. dec := json.NewDecoder(body)
  424. for {
  425. var e events.Message
  426. if err := dec.Decode(&e); err != nil {
  427. errCh <- err
  428. return
  429. }
  430. if e.Type != events.DaemonEventType {
  431. continue
  432. }
  433. if e.Action != "reload" {
  434. continue
  435. }
  436. close(errCh) // notify that we are done
  437. return
  438. }
  439. }()
  440. <-started
  441. if err := signalDaemonReload(d.cmd.Process.Pid); err != nil {
  442. return errors.Errorf("error signaling daemon reload: %v", err)
  443. }
  444. select {
  445. case err := <-errCh:
  446. if err != nil {
  447. return errors.Errorf("error waiting for daemon reload event: %v", err)
  448. }
  449. case <-time.After(30 * time.Second):
  450. return errors.New("timeout waiting for daemon reload event")
  451. }
  452. return nil
  453. }
  454. // LoadBusybox image into the daemon
  455. func (d *Daemon) LoadBusybox(t assert.TestingT) {
  456. clientHost, err := client.NewEnvClient()
  457. assert.NilError(t, err, "failed to create client")
  458. defer clientHost.Close()
  459. ctx := context.Background()
  460. reader, err := clientHost.ImageSave(ctx, []string{"busybox:latest"})
  461. assert.NilError(t, err, "failed to download busybox")
  462. defer reader.Close()
  463. client, err := d.NewClient()
  464. assert.NilError(t, err, "failed to create client")
  465. defer client.Close()
  466. resp, err := client.ImageLoad(ctx, reader, true)
  467. assert.NilError(t, err, "failed to load busybox")
  468. defer resp.Body.Close()
  469. }
  470. func (d *Daemon) getClientConfig() (*clientConfig, error) {
  471. var (
  472. transport *http.Transport
  473. scheme string
  474. addr string
  475. proto string
  476. )
  477. if d.UseDefaultTLSHost {
  478. option := &tlsconfig.Options{
  479. CAFile: "fixtures/https/ca.pem",
  480. CertFile: "fixtures/https/client-cert.pem",
  481. KeyFile: "fixtures/https/client-key.pem",
  482. }
  483. tlsConfig, err := tlsconfig.Client(*option)
  484. if err != nil {
  485. return nil, err
  486. }
  487. transport = &http.Transport{
  488. TLSClientConfig: tlsConfig,
  489. }
  490. addr = fmt.Sprintf("%s:%d", opts.DefaultHTTPHost, opts.DefaultTLSHTTPPort)
  491. scheme = "https"
  492. proto = "tcp"
  493. } else if d.UseDefaultHost {
  494. addr = opts.DefaultUnixSocket
  495. proto = "unix"
  496. scheme = "http"
  497. transport = &http.Transport{}
  498. } else {
  499. addr = d.sockPath()
  500. proto = "unix"
  501. scheme = "http"
  502. transport = &http.Transport{}
  503. }
  504. if err := sockets.ConfigureTransport(transport, proto, addr); err != nil {
  505. return nil, err
  506. }
  507. transport.DisableKeepAlives = true
  508. return &clientConfig{
  509. transport: transport,
  510. scheme: scheme,
  511. addr: addr,
  512. }, nil
  513. }
  514. func (d *Daemon) queryRootDir() (string, error) {
  515. // update daemon root by asking /info endpoint (to support user
  516. // namespaced daemon with root remapped uid.gid directory)
  517. clientConfig, err := d.getClientConfig()
  518. if err != nil {
  519. return "", err
  520. }
  521. client := &http.Client{
  522. Transport: clientConfig.transport,
  523. }
  524. req, err := http.NewRequest("GET", "/info", nil)
  525. if err != nil {
  526. return "", err
  527. }
  528. req.Header.Set("Content-Type", "application/json")
  529. req.URL.Host = clientConfig.addr
  530. req.URL.Scheme = clientConfig.scheme
  531. resp, err := client.Do(req)
  532. if err != nil {
  533. return "", err
  534. }
  535. body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
  536. return resp.Body.Close()
  537. })
  538. type Info struct {
  539. DockerRootDir string
  540. }
  541. var b []byte
  542. var i Info
  543. b, err = request.ReadBody(body)
  544. if err == nil && resp.StatusCode == http.StatusOK {
  545. // read the docker root dir
  546. if err = json.Unmarshal(b, &i); err == nil {
  547. return i.DockerRootDir, nil
  548. }
  549. }
  550. return "", err
  551. }
  552. // Info returns the info struct for this daemon
  553. func (d *Daemon) Info(t assert.TestingT) types.Info {
  554. apiclient, err := d.NewClient()
  555. assert.NilError(t, err)
  556. info, err := apiclient.Info(context.Background())
  557. assert.NilError(t, err)
  558. return info
  559. }
  560. func cleanupRaftDir(t testingT, rootPath string) {
  561. walDir := filepath.Join(rootPath, "swarm/raft/wal")
  562. if err := os.RemoveAll(walDir); err != nil {
  563. t.Logf("error removing %v: %v", walDir, err)
  564. }
  565. }