Skip to content

Commit f795c80

Browse files
update billing with env vars and input configs
1 parent 14888c6 commit f795c80

File tree

4 files changed

+181
-52
lines changed

4 files changed

+181
-52
lines changed

framework/components/dockercompose/billing_platform_service/billing_platform_service.go

Lines changed: 129 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,54 @@ package billing_platform_service
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"strconv"
8+
"strings"
69
"time"
710

811
"github.com/docker/docker/client"
12+
"github.com/docker/go-connections/nat"
913
"github.com/pkg/errors"
10-
"github.com/smartcontractkit/chainlink-testing-framework/framework"
11-
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/dockercompose/utils"
14+
"github.com/testcontainers/testcontainers-go"
1215
"github.com/testcontainers/testcontainers-go/modules/compose"
1316
"github.com/testcontainers/testcontainers-go/wait"
17+
18+
"github.com/smartcontractkit/chainlink-testing-framework/framework"
19+
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/dockercompose/utils"
20+
"github.com/smartcontractkit/freeport"
1421
)
1522

23+
const DefaultPostgresDSN = "postgres://postgres:postgres@postgres:5432/billing_platform?sslmode=disable"
24+
1625
type Output struct {
1726
BillingPlatformService *BillingPlatformServiceOutput
1827
Postgres *PostgresOutput
1928
}
2029

2130
type BillingPlatformServiceOutput struct {
22-
GRPCInternalURL string
23-
GRPCExternalURL string
31+
BillingGRPCInternalURL string
32+
BillingGRPCExternalURL string
33+
CreditGRPCInternalURL string
34+
CreditGRPCExternalURL string
2435
}
2536

2637
type PostgresOutput struct {
2738
DSN string
2839
}
2940

3041
type Input struct {
31-
ComposeFile string `toml:"compose_file"`
32-
ExtraDockerNetworks []string `toml:"extra_docker_networks"`
33-
Output *Output `toml:"output"`
34-
UseCache bool `toml:"use_cache"`
42+
ComposeFile string `toml:"compose_file"`
43+
ExtraDockerNetworks []string `toml:"extra_docker_networks"`
44+
Output *Output `toml:"output"`
45+
UseCache bool `toml:"use_cache"`
46+
ChainSelector uint64 `toml:"chain_selector"`
47+
StreamsAPIURL string `toml:"streams_api_url"`
48+
StreamsAPIKey string `toml:"streams_api_key"`
49+
StreamsAPISecret string `toml:"streams_api_secret"`
50+
RPCURL string `toml:"rpc_url"`
51+
WorkflowRegistryAddress string `toml:"workflow_registry_address"`
52+
CapabilitiesRegistryAddress string `toml:"capabilities_registry_address"`
53+
WorkflowOwners []string `toml:"workflow_owners"`
3554
}
3655

3756
func defaultBillingPlatformService(in *Input) *Input {
@@ -44,8 +63,11 @@ func defaultBillingPlatformService(in *Input) *Input {
4463
const (
4564
DEFAULT_STACK_NAME = "billing-platform-service"
4665

47-
DEFAULT_BILLING_PLATFORM_SERVICE_GRPC_PORT = "2022"
48-
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"
4971
)
5072

5173
func New(in *Input) (*Output, error) {
@@ -79,7 +101,48 @@ func New(in *Input) (*Output, error) {
79101
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
80102
defer cancel()
81103

82-
upErr := stack.Up(ctx)
104+
// Start the stackwith all environment variables from the host process
105+
// set development defaults for necessary environment variables and allow them to be overridden by the host process
106+
envVars := make(map[string]string)
107+
108+
envVars["MAINNET_WORKFLOW_REGISTRY_CHAIN_SELECTOR"] = strconv.FormatUint(in.ChainSelector, 10)
109+
envVars["MAINNET_WORKFLOW_REGISTRY_CONTRACT_ADDRESS"] = in.WorkflowRegistryAddress
110+
envVars["MAINNET_WORKFLOW_REGISTRY_RPC_URL"] = in.RPCURL
111+
envVars["MAINNET_WORKFLOW_REGISTRY_FINALITY_DEPTH"] = "0" // Instant finality on devnet
112+
envVars["KMS_PROOF_SIGNING_KEY_ID"] = "00000000-0000-0000-0000-000000000001" // provisioned via LocalStack
113+
envVars["VERIFIER_INITIAL_INTERVAL"] = "0s" // reduced to force verifier to start immediately in integration tests
114+
envVars["VERIFIER_MAXIMUM_INTERVAL"] = "1s" // reduced to force verifier to start immediately in integration tests
115+
envVars["LINKING_REQUEST_COOLDOWN"] = "0s" // reduced to force consequtive linking requests to be processed immediately in integration tests
116+
117+
envVars["MAINNET_CAPABILITIES_REGISTRY_CHAIN_SELECTOR"] = strconv.FormatUint(in.ChainSelector, 10)
118+
envVars["MAINNET_CAPABILITIES_REGISTRY_CONTRACT_ADDRESS"] = in.CapabilitiesRegistryAddress
119+
envVars["MAINNET_CAPABILITIES_REGISTRY_RPC_URL"] = in.RPCURL
120+
envVars["MAINNET_CAPABILITIES_REGISTRY_FINALITY_DEPTH"] = "10" // Arbitrary value, adjust as needed
121+
122+
envVars["TEST_OWNERS"] = strings.Join(in.WorkflowOwners, ",")
123+
envVars["STREAMS_API_URL"] = in.StreamsAPIURL
124+
envVars["STREAMS_API_KEY"] = in.StreamsAPIKey
125+
envVars["STREAMS_API_SECRET"] = in.StreamsAPISecret
126+
127+
for _, env := range os.Environ() {
128+
pair := strings.SplitN(env, "=", 2)
129+
if len(pair) == 2 {
130+
envVars[pair[0]] = pair[1]
131+
}
132+
}
133+
134+
// set these env vars after reading env vars from host
135+
port, err := freeport.Take(1)
136+
if err != nil {
137+
return nil, errors.Wrap(err, "failed to get free port for Billing Platform Service postgres")
138+
}
139+
140+
envVars["POSTGRES_PORT"] = strconv.FormatInt(int64(port[0]), 10)
141+
envVars["DEFAULT_DSN"] = DefaultPostgresDSN
142+
143+
upErr := stack.
144+
WithEnv(envVars).
145+
Up(ctx)
83146

84147
if upErr != nil {
85148
return nil, errors.Wrap(upErr, "failed to start stack for Billing Platform Service")
@@ -88,7 +151,8 @@ func New(in *Input) (*Output, error) {
88151
stack.WaitForService(DEFAULT_BILLING_PLATFORM_SERVICE_SERVICE_NAME,
89152
wait.ForAll(
90153
wait.ForLog("GRPC server is live").WithPollInterval(200*time.Millisecond),
91-
wait.ForListeningPort(DEFAULT_BILLING_PLATFORM_SERVICE_GRPC_PORT),
154+
wait.ForListeningPort(DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT),
155+
wait.ForListeningPort(DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT),
92156
).WithDeadline(1*time.Minute),
93157
)
94158

@@ -97,6 +161,11 @@ func New(in *Input) (*Output, error) {
97161
return nil, errors.Wrap(billingErr, "failed to get billing-platform-service container")
98162
}
99163

164+
postgresContainer, postgresErr := stack.ServiceContainer(ctx, DEFAULT_POSTGRES_SERVICE_NAME)
165+
if postgresErr != nil {
166+
return nil, errors.Wrap(postgresErr, "failed to get postgres container")
167+
}
168+
100169
cli, cliErr := client.NewClientWithOpts(
101170
client.FromEnv,
102171
client.WithAPIVersionNegotiation(),
@@ -139,23 +208,61 @@ func New(in *Input) (*Output, error) {
139208
return nil, errors.Wrap(billingExternalHostErr, "failed to get host for Billing Platform Service")
140209
}
141210

142-
// get mapped port for billing platform service
143-
billingExternalPort, billingExternalPortErr := utils.FindMappedPort(ctx, 20*time.Second, billingContainer, DEFAULT_BILLING_PLATFORM_SERVICE_GRPC_PORT)
144-
if billingExternalPortErr != nil {
145-
return nil, errors.Wrap(billingExternalPortErr, "failed to get mapped port for Chip Ingress")
211+
// get hosts for billing platform service
212+
postgresExternalHost, postgresExternalHostErr := utils.GetContainerHost(ctx, postgresContainer)
213+
if postgresExternalHostErr != nil {
214+
return nil, errors.Wrap(postgresExternalHostErr, "failed to get host for postgres")
215+
}
216+
217+
// get mapped ports for billing platform service
218+
serviceOutput, err := getExternalPorts(ctx, billingExternalHost, billingContainer)
219+
if err != nil {
220+
return nil, errors.Wrap(err, "failed to get mapped port for Billing Platform Service")
221+
}
222+
223+
externalPostgresPort, err := utils.FindMappedPort(ctx, 20*time.Second, postgresContainer, nat.Port(DEFAULT_POSTGRES_PORT+"/tcp"))
224+
if err != nil {
225+
return nil, errors.Wrap(err, "failed to get mapped port for postgres")
146226
}
147227

148228
output := &Output{
149-
BillingPlatformService: &BillingPlatformServiceOutput{
150-
GRPCInternalURL: fmt.Sprintf("http://%s:%s", DEFAULT_BILLING_PLATFORM_SERVICE_SERVICE_NAME, DEFAULT_BILLING_PLATFORM_SERVICE_GRPC_PORT),
151-
GRPCExternalURL: fmt.Sprintf("http://%s:%s", billingExternalHost, billingExternalPort.Port()),
152-
},
229+
BillingPlatformService: serviceOutput,
153230
Postgres: &PostgresOutput{
154-
155-
}
231+
DSN: fmt.Sprintf("postgres://postgres:postgres@%s:%s/billing_platform", postgresExternalHost, externalPostgresPort.Port()),
232+
},
156233
}
157234

158235
framework.L.Info().Msg("Billing Platform Service stack start")
159236

160237
return output, nil
161238
}
239+
240+
func getExternalPorts(ctx context.Context, billingExternalHost string, billingContainer *testcontainers.DockerContainer) (*BillingPlatformServiceOutput, error) {
241+
ports := map[string]nat.Port{
242+
"billing": DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT,
243+
"credit": DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT,
244+
}
245+
246+
output := BillingPlatformServiceOutput{}
247+
248+
for name, defaultPort := range ports {
249+
externalPort, err := utils.FindMappedPort(ctx, 20*time.Second, billingContainer, defaultPort)
250+
if err != nil {
251+
return nil, errors.Wrap(err, "failed to get mapped port for Billing Platform Service")
252+
}
253+
254+
internal := fmt.Sprintf("http://%s:%s", DEFAULT_BILLING_PLATFORM_SERVICE_SERVICE_NAME, defaultPort)
255+
external := fmt.Sprintf("http://%s:%s", billingExternalHost, externalPort.Port())
256+
257+
switch name {
258+
case "billing":
259+
output.BillingGRPCInternalURL = internal
260+
output.BillingGRPCExternalURL = external
261+
case "credit":
262+
output.CreditGRPCInternalURL = internal
263+
output.CreditGRPCExternalURL = external
264+
}
265+
}
266+
267+
return &output, nil
268+
}
Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
11
services:
2+
23
billing-platform-service:
3-
restart: on-failure
4-
tty: true
4+
image: ${BILLING_PLATFORM_SERVICE_IMAGE:-billing-platform-service:local-cre}
55
container_name: billing-platform-service
6-
image: ${BILLING_PLATFORM_SERVICE_IMAGE:-}
7-
command: ["grpc-service"]
86
depends_on:
97
postgres:
108
condition: service_healthy
119
migrations:
1210
condition: service_started
13-
ports:
14-
- "2112:2112"
15-
- "2222:2222"
16-
- "2223:2223"
17-
- "2257:2257"
11+
restart: on-failure
12+
tty: true
13+
command: ["grpc", "billing", "reserve"]
1814
environment:
15+
DISABLE_AUTH: true
1916
PROMETHEUS_PORT: 2112
2017
BILLING_SERVER_PORT: 2222
2118
CREDIT_RESERVATION_SERVER_PORT: 2223
22-
WORKFLOW_OWNERSHIP_PROOF_SERVER_PORT: 2257
2319
WORKFLOW_OWNERSHIP_PROOF_SERVER_HOST: 0.0.0.0
24-
STREAMS_API_URL: ${STREAMS_API_URL}
25-
STREAMS_API_KEY: ${STREAMS_API_KEY}
26-
STREAMS_API_SECRET: ${STREAMS_API_SECRET}
20+
MAINNET_WORKFLOW_REGISTRY_CHAIN_SELECTOR: ${MAINNET_WORKFLOW_REGISTRY_CHAIN_SELECTOR:-}
21+
TESTNET_WORKFLOW_REGISTRY_CHAIN_SELECTOR: ${MAINNET_WORKFLOW_REGISTRY_CHAIN_SELECTOR:-}
22+
MAINNET_WORKFLOW_REGISTRY_CONTRACT_ADDRESS: ${MAINNET_WORKFLOW_REGISTRY_CONTRACT_ADDRESS:-}
23+
TESTNET_WORKFLOW_REGISTRY_CONTRACT_ADDRESS: ${MAINNET_WORKFLOW_REGISTRY_CONTRACT_ADDRESS:-}
24+
MAINNET_WORKFLOW_REGISTRY_RPC_URL: ${MAINNET_WORKFLOW_REGISTRY_RPC_URL:-}
25+
TESTNET_WORKFLOW_REGISTRY_RPC_URL: ${MAINNET_WORKFLOW_REGISTRY_RPC_URL:-}
26+
MAINNET_WORKFLOW_REGISTRY_FINALITY_DEPTH: ${MAINNET_WORKFLOW_REGISTRY_FINALITY_DEPTH:-}
27+
KMS_PROOF_SIGNING_KEY_ID: ${KMS_PROOF_SIGNING_KEY_ID:-}
28+
VERIFIER_INITIAL_INTERVAL: ${VERIFIER_INITIAL_INTERVAL:-}
29+
VERIFIER_MAXIMUM_INTERVAL: ${VERIFIER_MAXIMUM_INTERVAL:-}
30+
LINKING_REQUEST_COOLDOWN: ${LINKING_REQUEST_COOLDOWN:-}
31+
MAINNET_CAPABILITIES_REGISTRY_CHAIN_SELECTOR: ${MAINNET_CAPABILITIES_REGISTRY_CHAIN_SELECTOR:-}
32+
TESTNET_CAPABILITIES_REGISTRY_CHAIN_SELECTOR: ${MAINNET_CAPABILITIES_REGISTRY_CHAIN_SELECTOR:-}
33+
MAINNET_CAPABILITIES_REGISTRY_CONTRACT_ADDRESS: ${MAINNET_CAPABILITIES_REGISTRY_CONTRACT_ADDRESS:-}
34+
MAINNET_CAPABILITIES_REGISTRY_RPC_URL: ${MAINNET_CAPABILITIES_REGISTRY_RPC_URL:-}
35+
TESTNET_CAPABILITIES_REGISTRY_RPC_URL: ${MAINNET_CAPABILITIES_REGISTRY_RPC_URL:-}
36+
MAINNET_CAPABILITIES_REGISTRY_FINALITY_DEPTH: ${MAINNET_CAPABILITIES_REGISTRY_FINALITY_DEPTH:-}
37+
TEST_OWNERS: ${TEST_OWNERS:-}
38+
STREAMS_API_URL: ${STREAMS_API_URL:-}
39+
STREAMS_API_KEY: ${STREAMS_API_KEY:-}
40+
STREAMS_API_SECRET: ${STREAMS_API_SECRET:-}
2741
DB_HOST: postgres
2842
DB_PORT: 5432
2943
DB_NAME: billing_platform
3044
DB_USERNAME: postgres
3145
DB_PASSWORD: postgres
46+
ports:
47+
- "2112:2112"
48+
- "2222:2222"
49+
- "2223:2223"
3250
healthcheck:
3351
test: ["CMD", "grpc_health_probe", "-addr=localhost:2222"]
3452
interval: 200ms
@@ -45,30 +63,31 @@ services:
4563
POSTGRES_HOST: postgres
4664
POSTGRES_USER: postgres
4765
POSTGRES_PASSWORD: postgres
66+
ports:
67+
- "${POSTGRES_PORT:-5432}:5432"
4868
healthcheck:
4969
test: ["CMD","pg_isready","-U","${POSTGRES_USER}","-d","${POSTGRES_DB}","-h","${POSTGRES_HOST}"]
5070
interval: 5s
5171
timeout: 10s
5272
retries: 3
53-
ports:
54-
- "5432:5432"
5573

5674
migrations:
57-
image: ${BILLING_PLATFORM_SERVICE_MIGRATION_IMAGE:-}
75+
image: ${BILLING_PLATFORM_SERVICE_IMAGE:-billing-platform-service:local-cre}
5876
container_name: db-migrations-billing-platform
59-
restart: on-failure
60-
environment:
61-
DBMATE_NO_DUMP_SCHEMA: true
62-
DBMATE_WAIT: true
63-
DATABASE_URL: postgres://postgres:postgres@postgres:postgres/billing_platform
64-
networks:
65-
- backend
6677
depends_on:
6778
postgres:
6879
condition: service_healthy
69-
entrypoint:
70-
- dbmate
71-
- up
72-
- --
73-
- --url=${DATABASE_URL}
74-
- --migrations-dir=/db
80+
restart: on-failure
81+
command: ["db", "create-and-migrate", "--url", "${DEFAULT_DSN:-}"]
82+
83+
populate_test_data:
84+
image: ${BILLING_PLATFORM_SERVICE_IMAGE:-billing-platform-service:local-cre}
85+
container_name: populate-data-billing-platform
86+
depends_on:
87+
billing-platform-service:
88+
condition: service_started
89+
restart: on-failure
90+
command: ["db", "populate-data", "--environment=local", "--billing-client-url=billing-platform-service:2222", "--simple-linking", "--dsn=${DEFAULT_DSN:-}"]
91+
environment:
92+
TEST_OWNERS: ${TEST_OWNERS:-}
93+
MAINNET_CAPABILITIES_REGISTRY_CHAIN_SELECTOR: ${MAINNET_CAPABILITIES_REGISTRY_CHAIN_SELECTOR:-}

framework/components/dockercompose/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ require (
88
github.com/avast/retry-go/v4 v4.6.1
99
github.com/confluentinc/confluent-kafka-go v1.9.2
1010
github.com/docker/docker v28.0.4+incompatible
11+
github.com/docker/go-connections v0.5.0
1112
github.com/google/go-github/v72 v72.0.0
1213
github.com/pkg/errors v0.9.1
1314
github.com/smartcontractkit/chainlink-testing-framework/framework v0.0.0-00010101000000-000000000000
15+
github.com/smartcontractkit/freeport v0.1.2
1416
github.com/testcontainers/testcontainers-go v0.37.0
1517
github.com/testcontainers/testcontainers-go/modules/compose v0.37.0
1618
golang.org/x/oauth2 v0.25.0
@@ -66,7 +68,6 @@ require (
6668
github.com/docker/distribution v2.8.3+incompatible // indirect
6769
github.com/docker/docker-credential-helpers v0.8.2 // indirect
6870
github.com/docker/go v1.5.1-1.0.20160303222718-d30aec9fd63c // indirect
69-
github.com/docker/go-connections v0.5.0 // indirect
7071
github.com/docker/go-metrics v0.0.1 // indirect
7172
github.com/docker/go-units v0.5.0 // indirect
7273
github.com/ebitengine/purego v0.8.2 // indirect

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)