main_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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()) {
  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: "TestProfile",
  52. Cluster: "TestCluster",
  53. Region: "TestRegion",
  54. }
  55. testStore, err := docker.NewContextWithStore(testContextName, &awsContext, filepath.Join(configDir, "contexts"))
  56. if err != nil {
  57. panic(err)
  58. }
  59. cleanup := func() {
  60. fmt.Println("cleanup")
  61. testStore.Remove(testContextName)
  62. os.RemoveAll(configDir)
  63. }
  64. env := append(os.Environ(),
  65. "DOCKER_CONFIG="+configDir,
  66. "DOCKER_CLI_EXPERIMENTAL=enabled") // TODO: Remove this once docker ecs plugin is no more experimental
  67. return icmd.Cmd{Env: env}, cleanup
  68. }
  69. func (d dockerCliCommand) Command(args ...string) []string {
  70. return append([]string{d.path, "--context", testContextName}, args...)
  71. }
  72. func TestMain(m *testing.M) {
  73. flag.Parse()
  74. if err := os.Chdir(*e2ePath); err != nil {
  75. panic(err)
  76. }
  77. cwd, err := os.Getwd()
  78. if err != nil {
  79. panic(err)
  80. }
  81. dockerEcs := os.Getenv("DOCKERECS_BINARY")
  82. if dockerEcs == "" {
  83. dockerEcs = filepath.Join(cwd, "../dist/docker-ecs")
  84. }
  85. dockerEcs, err = filepath.Abs(dockerEcs)
  86. if err != nil {
  87. panic(err)
  88. }
  89. if dockerCliPath == "" {
  90. dockerCliPath = "docker"
  91. } else {
  92. dockerCliPath, err = filepath.Abs(dockerCliPath)
  93. if err != nil {
  94. panic(err)
  95. }
  96. }
  97. // Prepare docker cli to call the docker-ecs plugin binary:
  98. // - Create a symbolic link with the dockerEcs binary to the plugin directory
  99. cliPluginDir, err := ioutil.TempDir("", "configContent")
  100. if err != nil {
  101. panic(err)
  102. }
  103. defer os.RemoveAll(cliPluginDir)
  104. createDockerECSSymLink(dockerEcs, cliPluginDir)
  105. dockerCli = dockerCliCommand{path: dockerCliPath, cliPluginDir: cliPluginDir}
  106. os.Exit(m.Run())
  107. }
  108. func createDockerECSSymLink(dockerEcs, configDir string) {
  109. dockerEcsExecName := "docker-ecs"
  110. if runtime.GOOS == "windows" {
  111. dockerEcsExecName += ".exe"
  112. }
  113. if err := os.Symlink(dockerEcs, filepath.Join(configDir, dockerEcsExecName)); err != nil {
  114. panic(err)
  115. }
  116. }