-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance.go
96 lines (80 loc) · 2.7 KB
/
instance.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package mockbigquery
import (
"context"
"testing"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
// Instance represents the underlying container that is running the mock redis instance.
type Instance struct {
t *testing.T
container testcontainers.Container
projectID string
datasetID string
}
// ProjectID returns the project id of the underlying instance.
func (r *Instance) ProjectID() string {
return r.projectID
}
// DatasetID returns the dataset id of the underlying instance.
func (r *Instance) DatasetID() string {
return r.datasetID
}
// Port returns the mapped port of the underlying container.
func (r *Instance) Port(ctx context.Context) (nat.Port, error) {
r.t.Helper()
return r.container.MappedPort(ctx, "9050")
}
// Close terminates the underlying container.
func (r *Instance) Close(ctx context.Context) {
r.t.Helper()
if err := r.container.Terminate(ctx); err != nil {
r.t.Logf("error terminating bigquery emulator: %v", err)
}
}
// NewInstance creates a new BigQuery Emulator container. It will exponentially backoff until the container is ready
// to accept connections so that you can handle throttling within CI environments
func NewInstance(ctx context.Context, t *testing.T, project string, dataset string) (*Instance, error) {
// mark this a test helper function
t.Helper()
// configure the backoff
cfg := backoff.NewExponentialBackOff()
cfg.InitialInterval = time.Second * 1
cfg.MaxElapsedTime = time.Minute * 5
policy := backoff.WithContext(cfg, ctx)
// configure our retry logic when connecting to docker, this is helpful when running tests in parallel and on ci
operation := backoff.OperationWithData[testcontainers.Container](func() (testcontainers.Container, error) {
return testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
AlwaysPullImage: true,
ImagePlatform: "linux/amd64",
Image: "ghcr.io/goccy/bigquery-emulator:latest",
Cmd: []string{
"--project=" + project,
"--dataset=" + dataset,
},
ExposedPorts: []string{"9050/tcp", "9060/tcp"},
WaitingFor: wait.ForLog("gRPC server listening at 0.0.0.0:9060"),
},
Started: true,
Reuse: false,
Logger: testcontainers.TestLogger(t),
})
})
// create the redis emulator container
bigQueryEmulator, err := backoff.RetryWithData(operation, policy)
if err != nil {
return nil, err
}
// create the mock instance type
cntr := &Instance{
t: t,
projectID: project,
datasetID: dataset,
container: bigQueryEmulator,
}
return cntr, nil
}