Skip to content

Commit 1e8da23

Browse files
pick random postgres port and return dsn
1 parent a2b316c commit 1e8da23

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

framework/components/dockercompose/billing_platform_service/billing_platform_service.go

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/smartcontractkit/chainlink-testing-framework/framework"
1919
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/dockercompose/utils"
20+
"github.com/smartcontractkit/freeport"
2021
)
2122

2223
const DefaultPostgresDSN = "postgres://postgres:postgres@postgres:5432/billing_platform"
@@ -27,12 +28,10 @@ type Output struct {
2728
}
2829

2930
type BillingPlatformServiceOutput struct {
30-
BillingGRPCInternalURL string
31-
BillingGRPCExternalURL string
32-
CreditGRPCInternalURL string
33-
CreditGRPCExternalURL string
34-
OwnershipGRPCInternalURL string
35-
OwnershipGRPCExternalURL string
31+
BillingGRPCInternalURL string
32+
BillingGRPCExternalURL string
33+
CreditGRPCInternalURL string
34+
CreditGRPCExternalURL string
3635
}
3736

3837
type PostgresOutput struct {
@@ -64,10 +63,11 @@ func defaultBillingPlatformService(in *Input) *Input {
6463
const (
6564
DEFAULT_STACK_NAME = "billing-platform-service"
6665

67-
DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT = "2222"
68-
DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT = "2223"
69-
DEFAULT_BILLING_PLATFORM_SERVICE_OWNERSHIP_GRPC_PORT = "2257"
70-
DEFAULT_BILLING_PLATFORM_SERVICE_SERVICE_NAME = "billing-platform-service"
66+
DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT = "2222"
67+
DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT = "2223"
68+
DEFAULT_POSTGRES_PORT = "5432"
69+
DEFAULT_BILLING_PLATFORM_SERVICE_SERVICE_NAME = "billing-platform-service"
70+
DEFAULT_POSTGRES_SERVICE_NAME = "postgres"
7171
)
7272

7373
func New(in *Input) (*Output, error) {
@@ -124,6 +124,13 @@ func New(in *Input) (*Output, error) {
124124
envVars["STREAMS_API_KEY"] = in.StreamsAPIKey
125125
envVars["STREAMS_API_SECRET"] = in.StreamsAPISecret
126126

127+
port, err := freeport.Take(1)
128+
if err != nil {
129+
return nil, errors.Wrap(err, "failed to get free port for Billing Platform Service postgres")
130+
}
131+
132+
envVars["POSTGRES_PORT"] = strconv.FormatInt(int64(port[0]), 10)
133+
127134
for _, env := range os.Environ() {
128135
pair := strings.SplitN(env, "=", 2)
129136
if len(pair) == 2 {
@@ -144,7 +151,6 @@ func New(in *Input) (*Output, error) {
144151
wait.ForLog("GRPC server is live").WithPollInterval(200*time.Millisecond),
145152
wait.ForListeningPort(DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT),
146153
wait.ForListeningPort(DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT),
147-
wait.ForListeningPort(DEFAULT_BILLING_PLATFORM_SERVICE_OWNERSHIP_GRPC_PORT),
148154
).WithDeadline(1*time.Minute),
149155
)
150156

@@ -153,6 +159,11 @@ func New(in *Input) (*Output, error) {
153159
return nil, errors.Wrap(billingErr, "failed to get billing-platform-service container")
154160
}
155161

162+
postgresContainer, postgresErr := stack.ServiceContainer(ctx, DEFAULT_POSTGRES_SERVICE_NAME)
163+
if postgresErr != nil {
164+
return nil, errors.Wrap(postgresErr, "failed to get postgres container")
165+
}
166+
156167
cli, cliErr := client.NewClientWithOpts(
157168
client.FromEnv,
158169
client.WithAPIVersionNegotiation(),
@@ -195,16 +206,27 @@ func New(in *Input) (*Output, error) {
195206
return nil, errors.Wrap(billingExternalHostErr, "failed to get host for Billing Platform Service")
196207
}
197208

209+
// get hosts for billing platform service
210+
postgresExternalHost, postgresExternalHostErr := utils.GetContainerHost(ctx, postgresContainer)
211+
if postgresExternalHostErr != nil {
212+
return nil, errors.Wrap(postgresExternalHostErr, "failed to get host for postgres")
213+
}
214+
198215
// get mapped ports for billing platform service
199216
serviceOutput, err := getExternalPorts(ctx, billingExternalHost, billingContainer)
200217
if err != nil {
201218
return nil, errors.Wrap(err, "failed to get mapped port for Billing Platform Service")
202219
}
203220

221+
externalPostgresPort, err := utils.FindMappedPort(ctx, 20*time.Second, postgresContainer, nat.Port(DEFAULT_POSTGRES_PORT+"/tcp"))
222+
if err != nil {
223+
return nil, errors.Wrap(err, "failed to get mapped port for postgres")
224+
}
225+
204226
output := &Output{
205227
BillingPlatformService: serviceOutput,
206228
Postgres: &PostgresOutput{
207-
DSN: DefaultPostgresDSN,
229+
DSN: fmt.Sprintf("postgres://postgres:postgres@%s:%s/billing_platform", postgresExternalHost, externalPostgresPort.Port()),
208230
},
209231
}
210232

@@ -215,9 +237,8 @@ func New(in *Input) (*Output, error) {
215237

216238
func getExternalPorts(ctx context.Context, billingExternalHost string, billingContainer *testcontainers.DockerContainer) (*BillingPlatformServiceOutput, error) {
217239
ports := map[string]nat.Port{
218-
"billing": DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT,
219-
"credit": DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT,
220-
"ownership": DEFAULT_BILLING_PLATFORM_SERVICE_OWNERSHIP_GRPC_PORT,
240+
"billing": DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT,
241+
"credit": DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT,
221242
}
222243

223244
output := BillingPlatformServiceOutput{}
@@ -238,9 +259,6 @@ func getExternalPorts(ctx context.Context, billingExternalHost string, billingCo
238259
case "credit":
239260
output.CreditGRPCInternalURL = internal
240261
output.CreditGRPCExternalURL = external
241-
case "ownership":
242-
output.OwnershipGRPCInternalURL = internal
243-
output.OwnershipGRPCExternalURL = external
244262
}
245263
}
246264

framework/components/dockercompose/billing_platform_service/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ services:
6464
POSTGRES_USER: postgres
6565
POSTGRES_PASSWORD: postgres
6666
ports:
67-
- "5432:5432"
67+
- "${POSTGRES_PORT:-5432}:5432"
6868
healthcheck:
6969
test: ["CMD","pg_isready","-U","${POSTGRES_USER}","-d","${POSTGRES_DB}","-h","${POSTGRES_HOST}"]
7070
interval: 5s

framework/components/dockercompose/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/google/go-github/v72 v72.0.0
1313
github.com/pkg/errors v0.9.1
1414
github.com/smartcontractkit/chainlink-testing-framework/framework v0.0.0-00010101000000-000000000000
15+
github.com/smartcontractkit/freeport v0.1.2
1516
github.com/testcontainers/testcontainers-go v0.37.0
1617
github.com/testcontainers/testcontainers-go/modules/compose v0.37.0
1718
golang.org/x/oauth2 v0.25.0

framework/components/dockercompose/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,8 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
567567
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
568568
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
569569
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
570+
github.com/smartcontractkit/freeport v0.1.2 h1:xMZ0UFHmjfB4MwbDANae3RS7UKt7OJ0JVqhjPSXdKVk=
571+
github.com/smartcontractkit/freeport v0.1.2/go.mod h1:T4zH9R8R8lVWKfU7tUvYz2o2jMv1OpGCdpY2j2QZXzU=
570572
github.com/spdx/tools-golang v0.5.3 h1:ialnHeEYUC4+hkm5vJm4qz2x+oEJbS0mAMFrNXdQraY=
571573
github.com/spdx/tools-golang v0.5.3/go.mod h1:/ETOahiAo96Ob0/RAIBmFZw6XN0yTnyr/uFZm2NTMhI=
572574
github.com/spf13/cast v0.0.0-20150508191742-4d07383ffe94 h1:JmfC365KywYwHB946TTiQWEb8kqPY+pybPLoGE9GgVk=

0 commit comments

Comments
 (0)