suite.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "runtime"
  32. "time"
  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("Contents of %s:\n%s\n\nContents of config dir:\n", cp, string(d))
  52. for _, p := range dirContents(s.ConfigDir) {
  53. fmt.Println(p)
  54. }
  55. s.T().Fail()
  56. })
  57. s.linkClassicDocker()
  58. }
  59. // TearDownSuite is run after all tests
  60. func (s *Suite) TearDownSuite() {
  61. _ = os.RemoveAll(s.BinDir)
  62. }
  63. func dirContents(dir string) []string {
  64. res := []string{}
  65. _ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  66. res = append(res, filepath.Join(dir, path))
  67. return nil
  68. })
  69. return res
  70. }
  71. func (s *Suite) linkClassicDocker() {
  72. p, err := exec.LookPath("docker")
  73. gomega.Expect(err).To(gomega.BeNil())
  74. err = os.Symlink(p, filepath.Join(s.BinDir, "docker-classic"))
  75. gomega.Expect(err).To(gomega.BeNil())
  76. err = os.Setenv("PATH", fmt.Sprintf("%s:%s", s.BinDir, os.Getenv("PATH")))
  77. gomega.Expect(err).To(gomega.BeNil())
  78. }
  79. // BeforeTest is run before each test
  80. func (s *Suite) BeforeTest(suite, test string) {
  81. d, _ := ioutil.TempDir("", "")
  82. s.ConfigDir = d
  83. _ = os.Setenv("DOCKER_CONFIG", s.ConfigDir)
  84. }
  85. // AfterTest is run after each test
  86. func (s *Suite) AfterTest(suite, test string) {
  87. _ = os.RemoveAll(s.ConfigDir)
  88. }
  89. // NewCommand creates a command context.
  90. func (s *Suite) NewCommand(command string, args ...string) *CmdContext {
  91. return &CmdContext{
  92. command: command,
  93. args: args,
  94. retries: RetriesContext{interval: time.Second},
  95. }
  96. }
  97. func dockerExecutable() string {
  98. if runtime.GOOS == "windows" {
  99. return "../../bin/docker.exe"
  100. }
  101. return "../../bin/docker"
  102. }
  103. // NewDockerCommand creates a docker builder.
  104. func (s *Suite) NewDockerCommand(args ...string) *CmdContext {
  105. return s.NewCommand(dockerExecutable(), args...)
  106. }