e2e.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package framework
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "errors"
  18. "io/ioutil"
  19. "os"
  20. "os/exec"
  21. "path/filepath"
  22. "runtime"
  23. "testing"
  24. "gotest.tools/v3/assert"
  25. is "gotest.tools/v3/assert/cmp"
  26. "gotest.tools/v3/icmd"
  27. "github.com/docker/api/containers"
  28. )
  29. var (
  30. // DockerExecutableName is the OS dependent Docker CLI binary name
  31. DockerExecutableName = "docker"
  32. existingExectuableName = "com.docker.cli"
  33. )
  34. func init() {
  35. if runtime.GOOS == "windows" {
  36. DockerExecutableName = DockerExecutableName + ".exe"
  37. existingExectuableName = existingExectuableName + ".exe"
  38. }
  39. }
  40. // E2eCLI is used to wrap the CLI for end to end testing
  41. type E2eCLI struct {
  42. BinDir string
  43. ConfigDir string
  44. }
  45. // NewParallelE2eCLI returns a configured TestE2eCLI with t.Parallel() set
  46. func NewParallelE2eCLI(t *testing.T, binDir string) *E2eCLI {
  47. t.Parallel()
  48. return newE2eCLI(t, binDir)
  49. }
  50. // NewE2eCLI returns a configured TestE2eCLI
  51. func NewE2eCLI(t *testing.T, binDir string) *E2eCLI {
  52. return newE2eCLI(t, binDir)
  53. }
  54. func newE2eCLI(t *testing.T, binDir string) *E2eCLI {
  55. d, err := ioutil.TempDir("", "")
  56. assert.Check(t, is.Nil(err))
  57. t.Cleanup(func() {
  58. if t.Failed() {
  59. conf, _ := ioutil.ReadFile(filepath.Join(d, "config.json"))
  60. t.Errorf("Config: %s\n", string(conf))
  61. t.Error("Contents of config dir:")
  62. for _, p := range dirContents(d) {
  63. t.Errorf(p)
  64. }
  65. }
  66. _ = os.RemoveAll(d)
  67. })
  68. return &E2eCLI{binDir, d}
  69. }
  70. func dirContents(dir string) []string {
  71. res := []string{}
  72. _ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  73. res = append(res, filepath.Join(dir, path))
  74. return nil
  75. })
  76. return res
  77. }
  78. // SetupExistingCLI copies the existing CLI in a temporary directory so that the
  79. // new CLI can be configured to use it
  80. func SetupExistingCLI() (string, func(), error) {
  81. p, err := exec.LookPath(existingExectuableName)
  82. if err != nil {
  83. p, err = exec.LookPath(DockerExecutableName)
  84. if err != nil {
  85. return "", nil, errors.New("existing CLI not found in PATH")
  86. }
  87. }
  88. d, err := ioutil.TempDir("", "")
  89. if err != nil {
  90. return "", nil, err
  91. }
  92. if err := CopyFile(p, filepath.Join(d, existingExectuableName)); err != nil {
  93. return "", nil, err
  94. }
  95. bin, err := filepath.Abs("../../bin/" + DockerExecutableName)
  96. if err != nil {
  97. return "", nil, err
  98. }
  99. if err := CopyFile(bin, filepath.Join(d, DockerExecutableName)); err != nil {
  100. return "", nil, err
  101. }
  102. cleanup := func() {
  103. _ = os.RemoveAll(d)
  104. }
  105. return d, cleanup, nil
  106. }
  107. // CopyFile copies a file from a path to a path setting permissions to 0777
  108. func CopyFile(sourceFile string, destinationFile string) error {
  109. input, err := ioutil.ReadFile(sourceFile)
  110. if err != nil {
  111. return err
  112. }
  113. err = ioutil.WriteFile(destinationFile, input, 0777)
  114. if err != nil {
  115. return err
  116. }
  117. return nil
  118. }
  119. // NewCmd creates a cmd object configured with the test environment set
  120. func (c *E2eCLI) NewCmd(command string, args ...string) icmd.Cmd {
  121. path := c.BinDir + ":" + os.Getenv("PATH")
  122. if runtime.GOOS == "windows" {
  123. path = c.BinDir + ";" + os.Getenv("PATH")
  124. }
  125. env := append(os.Environ(),
  126. "DOCKER_CONFIG="+c.ConfigDir,
  127. "KUBECONFIG=invalid",
  128. "PATH="+path,
  129. )
  130. return icmd.Cmd{
  131. Command: append([]string{command}, args...),
  132. Env: env,
  133. }
  134. }
  135. // NewDockerCmd creates a docker cmd without running it
  136. func (c *E2eCLI) NewDockerCmd(args ...string) icmd.Cmd {
  137. return c.NewCmd(filepath.Join(c.BinDir, DockerExecutableName), args...)
  138. }
  139. // RunDockerCmd runs a docker command and returns a result
  140. func (c *E2eCLI) RunDockerCmd(args ...string) *icmd.Result {
  141. return icmd.RunCmd(c.NewDockerCmd(args...))
  142. }
  143. // GoldenFile golden file specific to platform
  144. func GoldenFile(name string) string {
  145. if runtime.GOOS == "windows" {
  146. return name + "-windows.golden"
  147. }
  148. return name + ".golden"
  149. }
  150. // ParseContainerInspect parses the output of a `docker inspect` command for a
  151. // container
  152. func ParseContainerInspect(stdout string) (*containers.Container, error) {
  153. var res containers.Container
  154. rdr := bytes.NewReader([]byte(stdout))
  155. if err := json.NewDecoder(rdr).Decode(&res); err != nil {
  156. return nil, err
  157. }
  158. return &res, nil
  159. }