suite.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright (c) 2020 Docker Inc.
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED,
  14. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH
  22. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. package framework
  25. import (
  26. "fmt"
  27. "io/ioutil"
  28. "os"
  29. "os/exec"
  30. "path/filepath"
  31. "time"
  32. "github.com/docker/api/cli/mobycli"
  33. "github.com/onsi/gomega"
  34. log "github.com/sirupsen/logrus"
  35. "github.com/stretchr/testify/suite"
  36. )
  37. // Suite is used to store context information for e2e tests
  38. type Suite struct {
  39. suite.Suite
  40. ConfigDir string
  41. BinDir string
  42. }
  43. // SetupSuite is run before running any tests
  44. func (s *Suite) SetupSuite() {
  45. d, _ := ioutil.TempDir("", "")
  46. s.BinDir = d
  47. gomega.RegisterFailHandler(func(message string, callerSkip ...int) {
  48. log.Error(message)
  49. cp := filepath.Join(s.ConfigDir, "config.json")
  50. d, _ := ioutil.ReadFile(cp)
  51. fmt.Printf("Bin dir:%s\n", s.BinDir)
  52. fmt.Printf("Contents of %s:\n%s\n\nContents of config dir:\n", cp, string(d))
  53. for _, p := range dirContents(s.ConfigDir) {
  54. fmt.Println(p)
  55. }
  56. s.T().Fail()
  57. })
  58. s.copyExecutablesInBinDir()
  59. }
  60. // TearDownSuite is run after all tests
  61. func (s *Suite) TearDownSuite() {
  62. _ = os.RemoveAll(s.BinDir)
  63. }
  64. func dirContents(dir string) []string {
  65. res := []string{}
  66. _ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  67. res = append(res, filepath.Join(dir, path))
  68. return nil
  69. })
  70. return res
  71. }
  72. func (s *Suite) copyExecutablesInBinDir() {
  73. p, err := exec.LookPath(DockerClassicExecutable())
  74. if err != nil {
  75. p, err = exec.LookPath(dockerExecutable())
  76. }
  77. gomega.Expect(err).To(gomega.BeNil())
  78. err = copyFile(p, filepath.Join(s.BinDir, DockerClassicExecutable()))
  79. gomega.Expect(err).To(gomega.BeNil())
  80. dockerPath, err := filepath.Abs("../../bin/" + dockerExecutable())
  81. gomega.Expect(err).To(gomega.BeNil())
  82. err = copyFile(dockerPath, filepath.Join(s.BinDir, dockerExecutable()))
  83. gomega.Expect(err).To(gomega.BeNil())
  84. err = os.Setenv("PATH", concatenatePath(s.BinDir))
  85. gomega.Expect(err).To(gomega.BeNil())
  86. }
  87. func concatenatePath(path string) string {
  88. if IsWindows() {
  89. return fmt.Sprintf("%s;%s", path, os.Getenv("PATH"))
  90. }
  91. return fmt.Sprintf("%s:%s", path, os.Getenv("PATH"))
  92. }
  93. func copyFile(sourceFile string, destinationFile string) error {
  94. input, err := ioutil.ReadFile(sourceFile)
  95. if err != nil {
  96. return err
  97. }
  98. err = ioutil.WriteFile(destinationFile, input, 0777)
  99. if err != nil {
  100. return err
  101. }
  102. return nil
  103. }
  104. // BeforeTest is run before each test
  105. func (s *Suite) BeforeTest(suite, test string) {
  106. d, _ := ioutil.TempDir("", "")
  107. s.ConfigDir = d
  108. _ = os.Setenv("DOCKER_CONFIG", s.ConfigDir)
  109. }
  110. // AfterTest is run after each test
  111. func (s *Suite) AfterTest(suite, test string) {
  112. _ = os.RemoveAll(s.ConfigDir)
  113. }
  114. // ListProcessesCommand creates a command to list processes, "tasklist" on windows, "ps" otherwise.
  115. func (s *Suite) ListProcessesCommand() *CmdContext {
  116. if IsWindows() {
  117. return s.NewCommand("tasklist")
  118. }
  119. return s.NewCommand("ps", "-x")
  120. }
  121. // NewCommand creates a command context.
  122. func (s *Suite) NewCommand(command string, args ...string) *CmdContext {
  123. return &CmdContext{
  124. command: command,
  125. args: args,
  126. retries: RetriesContext{interval: time.Second},
  127. }
  128. }
  129. func dockerExecutable() string {
  130. if IsWindows() {
  131. return "docker.exe"
  132. }
  133. return "docker"
  134. }
  135. // DockerClassicExecutable binary name based on platform
  136. func DockerClassicExecutable() string {
  137. if IsWindows() {
  138. return mobycli.ComDockerCli + ".exe"
  139. }
  140. return mobycli.ComDockerCli
  141. }
  142. // NewDockerCommand creates a docker builder.
  143. func (s *Suite) NewDockerCommand(args ...string) *CmdContext {
  144. return s.NewCommand(dockerExecutable(), args...)
  145. }