| 44 | ) |
| 45 | |
| 46 | func TestMain(m *testing.M) { |
| 47 | // uses a sensible default on windows (tcp/http) and linux/osx (socket) |
| 48 | log.Println("Starting AWS localstack docker...") |
| 49 | pool, err := dockertest.NewPool("") |
| 50 | if err != nil { |
| 51 | log.Fatalf("Could not connect to docker: %s", err) |
| 52 | } |
| 53 | |
| 54 | options := &dockertest.RunOptions{ |
| 55 | Repository: "localstack/localstack", |
| 56 | Tag: "latest", |
| 57 | PortBindings: map[dc.Port][]dc.PortBinding{ |
| 58 | "4566/tcp": { |
| 59 | {HostPort: "4566"}, |
| 60 | }, |
| 61 | }, |
| 62 | Env: []string{"SERVICES=sqs,s3"}, |
| 63 | } |
| 64 | |
| 65 | resource, err := pool.RunWithOptions(options) |
| 66 | if err != nil { |
| 67 | log.Fatalf("Could not start resource: %s", err) |
| 68 | } |
| 69 | |
| 70 | err = resource.Expire(90) |
| 71 | if err != nil { |
| 72 | log.Fatal(err) |
| 73 | } |
| 74 | |
| 75 | localStackEndpoint = fmt.Sprintf("localhost:%s", resource.GetPort("4566/tcp")) |
| 76 | |
| 77 | // exponential backoff-retry, because the application in the container might not be ready to accept connections yet |
| 78 | // the minio client does not do service discovery for you (i.e. it does not check if connection can be established), so we have to use the health check |
| 79 | if err := pool.Retry(func() error { |
| 80 | url := fmt.Sprintf("http://%s/health", localStackEndpoint) |
| 81 | resp, err := http.Get(url) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | if resp.StatusCode != http.StatusOK { |
| 86 | return fmt.Errorf("status code not OK") |
| 87 | } |
| 88 | return nil |
| 89 | }); err != nil { |
| 90 | log.Fatalf("Could not connect to docker: %s", err) |
| 91 | } |
| 92 | |
| 93 | code := m.Run() |
| 94 | |
| 95 | // You can't defer this because os.Exit doesn't care for defer |
| 96 | if err := pool.Purge(resource); err != nil { |
| 97 | log.Fatalf("Could not purge resource: %s", err) |
| 98 | } |
| 99 | |
| 100 | os.Exit(code) |
| 101 | } |
| 102 | |
| 103 | func testAWSClient(t *testing.T) *awslib.Client { |