suite.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "os/exec"
  19. "path/filepath"
  20. "time"
  21. "github.com/onsi/gomega"
  22. log "github.com/sirupsen/logrus"
  23. "github.com/stretchr/testify/suite"
  24. )
  25. // Suite is used to store context information for e2e tests
  26. type Suite struct {
  27. suite.Suite
  28. ConfigDir string
  29. BinDir string
  30. }
  31. // SetupSuite is run before running any tests
  32. func (s *Suite) SetupSuite() {
  33. d, _ := ioutil.TempDir("", "")
  34. s.BinDir = d
  35. gomega.RegisterFailHandler(func(message string, callerSkip ...int) {
  36. log.Error(message)
  37. cp := filepath.Join(s.ConfigDir, "config.json")
  38. d, _ := ioutil.ReadFile(cp)
  39. fmt.Printf("Bin dir:%s\n", s.BinDir)
  40. fmt.Printf("Contents of %s:\n%s\n\nContents of config dir:\n", cp, string(d))
  41. for _, p := range dirContents(s.ConfigDir) {
  42. fmt.Println(p)
  43. }
  44. s.T().Fail()
  45. })
  46. s.copyExecutablesInBinDir()
  47. }
  48. // TearDownSuite is run after all tests
  49. func (s *Suite) TearDownSuite() {
  50. _ = os.RemoveAll(s.BinDir)
  51. }
  52. func dirContents(dir string) []string {
  53. res := []string{}
  54. _ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  55. res = append(res, filepath.Join(dir, path))
  56. return nil
  57. })
  58. return res
  59. }
  60. func (s *Suite) copyExecutablesInBinDir() {
  61. p, err := exec.LookPath(DockerClassicExecutable())
  62. if err != nil {
  63. p, err = exec.LookPath(dockerExecutable())
  64. }
  65. gomega.Expect(err).To(gomega.BeNil())
  66. err = copyFile(p, filepath.Join(s.BinDir, DockerClassicExecutable()))
  67. gomega.Expect(err).To(gomega.BeNil())
  68. dockerPath, err := filepath.Abs("../../bin/" + dockerExecutable())
  69. gomega.Expect(err).To(gomega.BeNil())
  70. err = copyFile(dockerPath, filepath.Join(s.BinDir, dockerExecutable()))
  71. gomega.Expect(err).To(gomega.BeNil())
  72. err = os.Setenv("PATH", concatenatePath(s.BinDir))
  73. gomega.Expect(err).To(gomega.BeNil())
  74. }
  75. func concatenatePath(path string) string {
  76. if IsWindows() {
  77. return fmt.Sprintf("%s;%s", path, os.Getenv("PATH"))
  78. }
  79. return fmt.Sprintf("%s:%s", path, os.Getenv("PATH"))
  80. }
  81. func copyFile(sourceFile string, destinationFile string) error {
  82. input, err := ioutil.ReadFile(sourceFile)
  83. if err != nil {
  84. return err
  85. }
  86. err = ioutil.WriteFile(destinationFile, input, 0777)
  87. if err != nil {
  88. return err
  89. }
  90. return nil
  91. }
  92. // BeforeTest is run before each test
  93. func (s *Suite) BeforeTest(suite, test string) {
  94. d, _ := ioutil.TempDir("", "")
  95. s.ConfigDir = d
  96. _ = os.Setenv("DOCKER_CONFIG", s.ConfigDir)
  97. }
  98. // AfterTest is run after each test
  99. func (s *Suite) AfterTest(suite, test string) {
  100. _ = os.RemoveAll(s.ConfigDir)
  101. }
  102. // ListProcessesCommand creates a command to list processes, "tasklist" on windows, "ps" otherwise.
  103. func (s *Suite) ListProcessesCommand() *CmdContext {
  104. if IsWindows() {
  105. return s.NewCommand("tasklist")
  106. }
  107. return s.NewCommand("ps", "-x")
  108. }
  109. // NewCommand creates a command context.
  110. func (s *Suite) NewCommand(command string, args ...string) *CmdContext {
  111. return &CmdContext{
  112. command: command,
  113. args: args,
  114. retries: RetriesContext{interval: time.Second},
  115. }
  116. }
  117. func dockerExecutable() string {
  118. if IsWindows() {
  119. return "docker.exe"
  120. }
  121. return "docker"
  122. }
  123. // DockerClassicExecutable binary name based on platform
  124. func DockerClassicExecutable() string {
  125. const comDockerCli = "com.docker.cli"
  126. if IsWindows() {
  127. return comDockerCli + ".exe"
  128. }
  129. return comDockerCli
  130. }
  131. // NewDockerCommand creates a docker builder.
  132. func (s *Suite) NewDockerCommand(args ...string) *CmdContext {
  133. return s.NewCommand(dockerExecutable(), args...)
  134. }