e2e_deploy_services_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // +build e2e
  2. package tests
  3. import (
  4. "context"
  5. "testing"
  6. "github.com/aws/aws-sdk-go/aws"
  7. "github.com/aws/aws-sdk-go/aws/session"
  8. "github.com/docker/ecs-plugin/pkg/amazon"
  9. "github.com/docker/ecs-plugin/pkg/docker"
  10. "gotest.tools/v3/assert"
  11. "gotest.tools/v3/fs"
  12. "gotest.tools/v3/golden"
  13. "gotest.tools/v3/icmd"
  14. )
  15. const (
  16. composeFileName = "compose.yaml"
  17. )
  18. func TestE2eDeployServices(t *testing.T) {
  19. cmd, cleanup, awsContext := dockerCli.createTestCmd()
  20. defer cleanup()
  21. composeUpSimpleService(t, cmd, awsContext)
  22. }
  23. func composeUpSimpleService(t *testing.T, cmd icmd.Cmd, awsContext docker.AwsContext) {
  24. bgContext := context.Background()
  25. composeYAML := golden.Get(t, "input/simple-single-service.yaml")
  26. tmpDir := fs.NewDir(t, t.Name(),
  27. fs.WithFile(composeFileName, "", fs.WithBytes(composeYAML)),
  28. )
  29. // We can't use the file added in the tmp directory because it will drop if an assertion fails
  30. defer composeDown(t, cmd, golden.Path("input/simple-single-service.yaml"))
  31. defer tmpDir.Remove()
  32. cmd.Command = dockerCli.Command("ecs", "compose", "--file="+tmpDir.Join(composeFileName), "--project-name", t.Name(), "up")
  33. icmd.RunCmd(cmd).Assert(t, icmd.Success)
  34. session, err := session.NewSessionWithOptions(session.Options{
  35. Profile: awsContext.Profile,
  36. Config: aws.Config{
  37. Region: aws.String(awsContext.Region),
  38. },
  39. })
  40. assert.NilError(t, err)
  41. sdk := amazon.NewAPI(session)
  42. arns, err := sdk.ListTasks(bgContext, t.Name(), t.Name())
  43. assert.NilError(t, err)
  44. tasks, err := sdk.DescribeTasks(bgContext, t.Name(), arns...)
  45. publicIps, err := sdk.GetPublicIPs(context.Background(), tasks[0].NetworkInterface)
  46. assert.NilError(t, err)
  47. for _, ip := range publicIps {
  48. icmd.RunCommand("curl", "-I", "http://"+ip).Assert(t, icmd.Success)
  49. }
  50. }
  51. func composeDown(t *testing.T, cmd icmd.Cmd, composeFile string) {
  52. cmd.Command = dockerCli.Command("ecs", "compose", "--file="+composeFile, "--project-name", t.Name(), "down")
  53. icmd.RunCmd(cmd).Assert(t, icmd.Success)
  54. }