suite.go 4.1 KB

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