main_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package tests
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "testing"
  11. dockerConfigFile "github.com/docker/cli/cli/config/configfile"
  12. "github.com/docker/ecs-plugin/pkg/docker"
  13. "gotest.tools/v3/icmd"
  14. )
  15. var (
  16. e2ePath = flag.String("e2e-path", ".", "Set path to the e2e directory")
  17. dockerCliPath = os.Getenv("DOCKERCLI_BINARY")
  18. dockerCli dockerCliCommand
  19. testContextName = "testAwsContextToBeRemoved"
  20. )
  21. type dockerCliCommand struct {
  22. path string
  23. cliPluginDir string
  24. }
  25. type ConfigFileOperator func(configFile *dockerConfigFile.ConfigFile)
  26. func (d dockerCliCommand) createTestCmd(ops ...ConfigFileOperator) (icmd.Cmd, func(), docker.AwsContext) {
  27. configDir, err := ioutil.TempDir("", "config")
  28. if err != nil {
  29. panic(err)
  30. }
  31. configFilePath := filepath.Join(configDir, "config.json")
  32. config := dockerConfigFile.ConfigFile{
  33. CLIPluginsExtraDirs: []string{
  34. d.cliPluginDir,
  35. },
  36. Filename: configFilePath,
  37. }
  38. for _, op := range ops {
  39. op(&config)
  40. }
  41. configFile, err := os.Create(configFilePath)
  42. if err != nil {
  43. panic(err)
  44. }
  45. defer configFile.Close()
  46. err = json.NewEncoder(configFile).Encode(config)
  47. if err != nil {
  48. panic(err)
  49. }
  50. awsContext := docker.AwsContext{
  51. Profile: "sandbox.devtools.developer",
  52. Region: "eu-west-3",
  53. }
  54. testStore, err := docker.NewContextWithStore(testContextName, awsContext, filepath.Join(configDir, "contexts"))
  55. if err != nil {
  56. panic(err)
  57. }
  58. cleanup := func() {
  59. fmt.Println("cleanup")
  60. testStore.Remove(testContextName)
  61. os.RemoveAll(configDir)
  62. }
  63. env := append(os.Environ(),
  64. "DOCKER_CONFIG="+configDir,
  65. "DOCKER_CLI_EXPERIMENTAL=enabled") // TODO: Remove this once docker ecs plugin is no more experimental
  66. return icmd.Cmd{Env: env}, cleanup, awsContext
  67. }
  68. func (d dockerCliCommand) Command(args ...string) []string {
  69. return append([]string{d.path, "--context", testContextName}, args...)
  70. }
  71. func TestMain(m *testing.M) {
  72. flag.Parse()
  73. if err := os.Chdir(*e2ePath); err != nil {
  74. panic(err)
  75. }
  76. cwd, err := os.Getwd()
  77. if err != nil {
  78. panic(err)
  79. }
  80. dockerEcs := os.Getenv("DOCKERECS_BINARY")
  81. if dockerEcs == "" {
  82. dockerEcs = filepath.Join(cwd, "../dist/docker-ecs")
  83. }
  84. dockerEcs, err = filepath.Abs(dockerEcs)
  85. if err != nil {
  86. panic(err)
  87. }
  88. if dockerCliPath == "" {
  89. dockerCliPath = "docker"
  90. } else {
  91. dockerCliPath, err = filepath.Abs(dockerCliPath)
  92. if err != nil {
  93. panic(err)
  94. }
  95. }
  96. // Prepare docker cli to call the docker-ecs plugin binary:
  97. // - Create a symbolic link with the dockerEcs binary to the plugin directory
  98. cliPluginDir, err := ioutil.TempDir("", "configContent")
  99. if err != nil {
  100. panic(err)
  101. }
  102. defer os.RemoveAll(cliPluginDir)
  103. createDockerECSSymLink(dockerEcs, cliPluginDir)
  104. dockerCli = dockerCliCommand{path: dockerCliPath, cliPluginDir: cliPluginDir}
  105. os.Exit(m.Run())
  106. }
  107. func createDockerECSSymLink(dockerEcs, configDir string) {
  108. dockerEcsExecName := "docker-ecs"
  109. if runtime.GOOS == "windows" {
  110. dockerEcsExecName += ".exe"
  111. }
  112. if err := os.Symlink(dockerEcs, filepath.Join(configDir, dockerEcsExecName)); err != nil {
  113. panic(err)
  114. }
  115. }