e2e.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. "fmt"
  19. "io/ioutil"
  20. "net/http"
  21. "os"
  22. "os/exec"
  23. "path/filepath"
  24. "runtime"
  25. "strings"
  26. "testing"
  27. "time"
  28. "gotest.tools/v3/assert"
  29. is "gotest.tools/v3/assert/cmp"
  30. "gotest.tools/v3/icmd"
  31. "github.com/docker/compose-cli/api/containers"
  32. )
  33. var (
  34. // DockerExecutableName is the OS dependent Docker CLI binary name
  35. DockerExecutableName = "docker"
  36. existingExectuableName = "com.docker.cli"
  37. )
  38. func init() {
  39. if runtime.GOOS == "windows" {
  40. DockerExecutableName = DockerExecutableName + ".exe"
  41. existingExectuableName = existingExectuableName + ".exe"
  42. }
  43. }
  44. // E2eCLI is used to wrap the CLI for end to end testing
  45. type E2eCLI struct {
  46. BinDir string
  47. ConfigDir string
  48. test *testing.T
  49. }
  50. // NewParallelE2eCLI returns a configured TestE2eCLI with t.Parallel() set
  51. func NewParallelE2eCLI(t *testing.T, binDir string) *E2eCLI {
  52. t.Parallel()
  53. return newE2eCLI(t, binDir)
  54. }
  55. // NewE2eCLI returns a configured TestE2eCLI
  56. func NewE2eCLI(t *testing.T, binDir string) *E2eCLI {
  57. return newE2eCLI(t, binDir)
  58. }
  59. func newE2eCLI(t *testing.T, binDir string) *E2eCLI {
  60. d, err := ioutil.TempDir("", "")
  61. assert.Check(t, is.Nil(err))
  62. t.Cleanup(func() {
  63. if t.Failed() {
  64. conf, _ := ioutil.ReadFile(filepath.Join(d, "config.json"))
  65. t.Errorf("Config: %s\n", string(conf))
  66. t.Error("Contents of config dir:")
  67. for _, p := range dirContents(d) {
  68. t.Errorf(p)
  69. }
  70. }
  71. _ = os.RemoveAll(d)
  72. })
  73. return &E2eCLI{binDir, d, t}
  74. }
  75. func dirContents(dir string) []string {
  76. res := []string{}
  77. _ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  78. res = append(res, filepath.Join(dir, path))
  79. return nil
  80. })
  81. return res
  82. }
  83. // SetupExistingCLI copies the existing CLI in a temporary directory so that the
  84. // new CLI can be configured to use it
  85. func SetupExistingCLI() (string, func(), error) {
  86. p, err := exec.LookPath(existingExectuableName)
  87. if err != nil {
  88. p, err = exec.LookPath(DockerExecutableName)
  89. if err != nil {
  90. return "", nil, errors.New("existing CLI not found in PATH")
  91. }
  92. }
  93. d, err := ioutil.TempDir("", "")
  94. if err != nil {
  95. return "", nil, err
  96. }
  97. if err := CopyFile(p, filepath.Join(d, existingExectuableName)); err != nil {
  98. return "", nil, err
  99. }
  100. bin, err := filepath.Abs("../../bin/" + DockerExecutableName)
  101. if err != nil {
  102. return "", nil, err
  103. }
  104. if err := CopyFile(bin, filepath.Join(d, DockerExecutableName)); err != nil {
  105. return "", nil, err
  106. }
  107. cleanup := func() {
  108. _ = os.RemoveAll(d)
  109. }
  110. return d, cleanup, nil
  111. }
  112. // CopyFile copies a file from a path to a path setting permissions to 0777
  113. func CopyFile(sourceFile string, destinationFile string) error {
  114. input, err := ioutil.ReadFile(sourceFile)
  115. if err != nil {
  116. return err
  117. }
  118. err = ioutil.WriteFile(destinationFile, input, 0777)
  119. if err != nil {
  120. return err
  121. }
  122. return nil
  123. }
  124. // NewCmd creates a cmd object configured with the test environment set
  125. func (c *E2eCLI) NewCmd(command string, args ...string) icmd.Cmd {
  126. env := append(os.Environ(),
  127. "DOCKER_CONFIG="+c.ConfigDir,
  128. "KUBECONFIG=invalid",
  129. "PATH="+c.PathEnvVar(),
  130. )
  131. return icmd.Cmd{
  132. Command: append([]string{command}, args...),
  133. Env: env,
  134. }
  135. }
  136. // NewDockerCmd creates a docker cmd without running it
  137. func (c *E2eCLI) NewDockerCmd(args ...string) icmd.Cmd {
  138. return c.NewCmd(filepath.Join(c.BinDir, DockerExecutableName), args...)
  139. }
  140. // RunDockerOrExitError runs a docker command and returns a result
  141. func (c *E2eCLI) RunDockerOrExitError(args ...string) *icmd.Result {
  142. fmt.Printf(" [%s] docker %s\n", c.test.Name(), strings.Join(args, " "))
  143. return icmd.RunCmd(c.NewDockerCmd(args...))
  144. }
  145. // RunDockerCmd runs a docker command, expects no error and returns a result
  146. func (c *E2eCLI) RunDockerCmd(args ...string) *icmd.Result {
  147. res := c.RunDockerOrExitError(args...)
  148. res.Assert(c.test, icmd.Success)
  149. return res
  150. }
  151. // PathEnvVar returns path (os sensitive) for running test
  152. func (c *E2eCLI) PathEnvVar() string {
  153. path := c.BinDir + ":" + os.Getenv("PATH")
  154. if runtime.GOOS == "windows" {
  155. path = c.BinDir + ";" + os.Getenv("PATH")
  156. }
  157. return path
  158. }
  159. // GoldenFile golden file specific to platform
  160. func GoldenFile(name string) string {
  161. if runtime.GOOS == "windows" {
  162. return name + "-windows.golden"
  163. }
  164. return name + ".golden"
  165. }
  166. // ParseContainerInspect parses the output of a `docker inspect` command for a
  167. // container
  168. func ParseContainerInspect(stdout string) (*containers.Container, error) {
  169. var res containers.Container
  170. rdr := bytes.NewReader([]byte(stdout))
  171. if err := json.NewDecoder(rdr).Decode(&res); err != nil {
  172. return nil, err
  173. }
  174. return &res, nil
  175. }
  176. // HTTPGetWithRetry performs an HTTP GET on an `endpoint`.
  177. // In the case of an error it retries the same request after a 5 second sleep,
  178. // returning the error if count of `tries` is reached
  179. func HTTPGetWithRetry(endpoint string, tries int) (*http.Response, error) {
  180. var (
  181. r *http.Response
  182. err error
  183. )
  184. for t := 0; t < tries; t++ {
  185. r, err = http.Get(endpoint)
  186. if err == nil || t == tries-1 {
  187. break
  188. }
  189. time.Sleep(5 * time.Second)
  190. }
  191. return r, err
  192. }