/
datastore.go
49 lines (42 loc) · 1.25 KB
/
datastore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package datastore
import (
"context"
"fmt"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
// datastoreContainer represents the datastore container type used in the module
type datastoreContainer struct {
testcontainers.Container
URI string
}
// setupDatastore creates an instance of the datastore container type
func setupDatastore(ctx context.Context) (*datastoreContainer, error) {
req := testcontainers.ContainerRequest{
Image: "gcr.io/google.com/cloudsdktool/cloud-sdk:367.0.0-emulators",
ExposedPorts: []string{"8081/tcp"},
WaitingFor: wait.ForHTTP("/").WithPort("8081/tcp"),
Cmd: []string{
"/bin/sh",
"-c",
"gcloud beta emulators datastore start --project test-project --host-port 0.0.0.0:8081",
},
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return nil, err
}
mappedPort, err := container.MappedPort(ctx, "8081")
if err != nil {
return nil, err
}
hostIP, err := container.Host(ctx)
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s:%s", hostIP, mappedPort.Port())
return &datastoreContainer{Container: container, URI: uri}, nil
}