@@ -3,6 +3,9 @@ package billing_platform_service
33import (
44 "context"
55 "fmt"
6+ "os"
7+ "strconv"
8+ "strings"
69 "time"
710
811 "github.com/docker/docker/client"
@@ -14,6 +17,7 @@ import (
1417
1518 "github.com/smartcontractkit/chainlink-testing-framework/framework"
1619 "github.com/smartcontractkit/chainlink-testing-framework/framework/components/dockercompose/utils"
20+ "github.com/smartcontractkit/freeport"
1721)
1822
1923const DefaultPostgresDSN = "postgres://postgres:postgres@postgres:5432/billing_platform"
@@ -24,23 +28,29 @@ type Output struct {
2428}
2529
2630type BillingPlatformServiceOutput struct {
27- BillingGRPCInternalURL string
28- BillingGRPCExternalURL string
29- CreditGRPCInternalURL string
30- CreditGRPCExternalURL string
31- OwnershipGRPCInternalURL string
32- OwnershipGRPCExternalURL string
31+ BillingGRPCInternalURL string
32+ BillingGRPCExternalURL string
33+ CreditGRPCInternalURL string
34+ CreditGRPCExternalURL string
3335}
3436
3537type PostgresOutput struct {
3638 DSN string
3739}
3840
3941type Input struct {
40- ComposeFile string `toml:"compose_file"`
41- ExtraDockerNetworks []string `toml:"extra_docker_networks"`
42- Output * Output `toml:"output"`
43- 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"`
4454}
4555
4656func defaultBillingPlatformService (in * Input ) * Input {
@@ -53,10 +63,11 @@ func defaultBillingPlatformService(in *Input) *Input {
5363const (
5464 DEFAULT_STACK_NAME = "billing-platform-service"
5565
56- DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT = "2222"
57- DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT = "2223"
58- DEFAULT_BILLING_PLATFORM_SERVICE_OWNERSHIP_GRPC_PORT = "2257"
59- 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"
6071)
6172
6273func New (in * Input ) (* Output , error ) {
@@ -90,7 +101,46 @@ func New(in *Input) (*Output, error) {
90101 ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Minute )
91102 defer cancel ()
92103
93- 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+ 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+
134+ for _ , env := range os .Environ () {
135+ pair := strings .SplitN (env , "=" , 2 )
136+ if len (pair ) == 2 {
137+ envVars [pair [0 ]] = pair [1 ]
138+ }
139+ }
140+
141+ upErr := stack .
142+ WithEnv (envVars ).
143+ Up (ctx )
94144
95145 if upErr != nil {
96146 return nil , errors .Wrap (upErr , "failed to start stack for Billing Platform Service" )
@@ -101,7 +151,6 @@ func New(in *Input) (*Output, error) {
101151 wait .ForLog ("GRPC server is live" ).WithPollInterval (200 * time .Millisecond ),
102152 wait .ForListeningPort (DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT ),
103153 wait .ForListeningPort (DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT ),
104- wait .ForListeningPort (DEFAULT_BILLING_PLATFORM_SERVICE_OWNERSHIP_GRPC_PORT ),
105154 ).WithDeadline (1 * time .Minute ),
106155 )
107156
@@ -110,6 +159,11 @@ func New(in *Input) (*Output, error) {
110159 return nil , errors .Wrap (billingErr , "failed to get billing-platform-service container" )
111160 }
112161
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+
113167 cli , cliErr := client .NewClientWithOpts (
114168 client .FromEnv ,
115169 client .WithAPIVersionNegotiation (),
@@ -152,16 +206,27 @@ func New(in *Input) (*Output, error) {
152206 return nil , errors .Wrap (billingExternalHostErr , "failed to get host for Billing Platform Service" )
153207 }
154208
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+
155215 // get mapped ports for billing platform service
156216 serviceOutput , err := getExternalPorts (ctx , billingExternalHost , billingContainer )
157217 if err != nil {
158218 return nil , errors .Wrap (err , "failed to get mapped port for Billing Platform Service" )
159219 }
160220
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+
161226 output := & Output {
162227 BillingPlatformService : serviceOutput ,
163228 Postgres : & PostgresOutput {
164- DSN : DefaultPostgresDSN ,
229+ DSN : fmt . Sprintf ( "postgres://postgres:postgres@%s:%s/billing_platform" , postgresExternalHost , externalPostgresPort . Port ()) ,
165230 },
166231 }
167232
@@ -172,9 +237,8 @@ func New(in *Input) (*Output, error) {
172237
173238func getExternalPorts (ctx context.Context , billingExternalHost string , billingContainer * testcontainers.DockerContainer ) (* BillingPlatformServiceOutput , error ) {
174239 ports := map [string ]nat.Port {
175- "billing" : DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT ,
176- "credit" : DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT ,
177- "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 ,
178242 }
179243
180244 output := BillingPlatformServiceOutput {}
@@ -195,9 +259,6 @@ func getExternalPorts(ctx context.Context, billingExternalHost string, billingCo
195259 case "credit" :
196260 output .CreditGRPCInternalURL = internal
197261 output .CreditGRPCExternalURL = external
198- case "ownership" :
199- output .OwnershipGRPCInternalURL = internal
200- output .OwnershipGRPCExternalURL = external
201262 }
202263 }
203264
0 commit comments