44 "context"
55 "fmt"
66 "os"
7+ "strconv"
78 "strings"
89 "time"
910
@@ -16,6 +17,7 @@ import (
1617
1718 "github.com/smartcontractkit/chainlink-testing-framework/framework"
1819 "github.com/smartcontractkit/chainlink-testing-framework/framework/components/dockercompose/utils"
20+ "github.com/smartcontractkit/freeport"
1921)
2022
2123const DefaultPostgresDSN = "postgres://postgres:postgres@postgres:5432/billing_platform"
@@ -26,23 +28,29 @@ type Output struct {
2628}
2729
2830type BillingPlatformServiceOutput struct {
29- BillingGRPCInternalURL string
30- BillingGRPCExternalURL string
31- CreditGRPCInternalURL string
32- CreditGRPCExternalURL string
33- OwnershipGRPCInternalURL string
34- OwnershipGRPCExternalURL string
31+ BillingGRPCInternalURL string
32+ BillingGRPCExternalURL string
33+ CreditGRPCInternalURL string
34+ CreditGRPCExternalURL string
3535}
3636
3737type PostgresOutput struct {
3838 DSN string
3939}
4040
4141type Input struct {
42- ComposeFile string `toml:"compose_file"`
43- ExtraDockerNetworks []string `toml:"extra_docker_networks"`
44- Output * Output `toml:"output"`
45- 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"`
4654}
4755
4856func defaultBillingPlatformService (in * Input ) * Input {
@@ -55,10 +63,11 @@ func defaultBillingPlatformService(in *Input) *Input {
5563const (
5664 DEFAULT_STACK_NAME = "billing-platform-service"
5765
58- DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT = "2222"
59- DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT = "2223"
60- DEFAULT_BILLING_PLATFORM_SERVICE_OWNERSHIP_GRPC_PORT = "2257"
61- 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"
6271)
6372
6473func New (in * Input ) (* Output , error ) {
@@ -96,31 +105,31 @@ func New(in *Input) (*Output, error) {
96105 // set development defaults for necessary environment variables and allow them to be overridden by the host process
97106 envVars := make (map [string ]string )
98107
99- envVars ["MAINNET_WORKFLOW_REGISTRY_CHAIN_SELECTOR" ] = "7759470850252068959" // Anvil Devnet
100- envVars ["MAINNET_WORKFLOW_REGISTRY_CONTRACT_ADDRESS" ] = "0xA15BB66138824a1c7167f5E85b957d04Dd34E468" // Deployed via Linking integration tests
101- envVars ["MAINNET_WORKFLOW_REGISTRY_RPC_URL" ] = "http://anvil:8545" // Anvil inside Docker
102- envVars ["MAINNET_WORKFLOW_REGISTRY_FINALITY_DEPTH" ] = "0" // Instant finality on devnet
103- envVars ["TESTNET_WORKFLOW_REGISTRY_CHAIN_SELECTOR " ] = "10344971235874465080" // Base Sepolia
104- envVars ["TESTNET_WORKFLOW_REGISTRY_CONTRACT_ADDRESS " ] = "0xED1D0d87706a466151d67A6a06d69534C97BE66F" // Used for Billing integration tests
105- envVars ["TESTNET_WORKFLOW_REGISTRY_RPC_URL " ] = "http://anvil:8545 " // Anvil inside Docker
106- envVars ["TESTNET_WORKFLOW_REGISTRY_FINALITY_DEPTH " ] = "10 " // Arbitrary value, adjust as needed
107- envVars [ "KMS_PROOF_SIGNING_KEY_ID" ] = "00000000-0000-0000-0000-000000000001" // provisioned via LocalStack
108- envVars ["VERIFIER_INITIAL_INTERVAL " ] = "0s" // reduced to force verifier to start immediately in integration tests
109- envVars ["VERIFIER_MAXIMUM_INTERVAL " ] = "1s" // reduced to force verifier to start immediately in integration tests
110- envVars ["LINKING_REQUEST_COOLDOWN " ] = "0s" // reduced to force consequtive linking requests to be processed immediately in integration tests
111-
112- envVars [ "MAINNET_CAPABILITIES_REGISTRY_CHAIN_SELECTOR" ] = "10344971235874465080" // Base Sepolia
113- envVars ["MAINNET_CAPABILITIES_REGISTRY_CONTRACT_ADDRESS " ] = "0x4c0a7d8f1b2e3c5f6a9b8e2d3c4f5e6b7a8b9c0d" // dummy address
114- envVars ["MAINNET_CAPABILITIES_REGISTRY_RPC_URL " ] = "http://anvil:8545" // Anvil RPC URL
115- envVars ["MAINNET_CAPABILITIES_REGISTRY_FINALITY_DEPTH " ] = "10" // Arbitrary value, adjust as needed
116- envVars ["TESTNET_CAPABILITIES_REGISTRY_CHAIN_SELECTOR " ] = "10344971235874465080" // Base Sepolia
117- envVars [ "TESTNET_CAPABILITIES_REGISTRY_CONTRACT_ADDRESS" ] = "0x4c0a7d8f1b2e3c5f6a9b8e2d3c4f5e6b7a8b9c0d" // dummy address
118- envVars [ "TESTNET_CAPABILITIES_REGISTRY_RPC_URL" ] = "http://anvil:8545" // Anvil RPC URL
119- envVars [ "TESTNET_CAPABILITIES_REGISTRY_FINALITY_DEPTH" ] = "10" // Arbitrary value, adjust as needed
120-
121- envVars [ "STREAMS_API_URL" ] = ""
122- envVars [ "STREAMS_API_KEY" ] = ""
123- envVars ["STREAMS_API_SECRET " ] = ""
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 )
124133
125134 for _ , env := range os .Environ () {
126135 pair := strings .SplitN (env , "=" , 2 )
@@ -142,7 +151,6 @@ func New(in *Input) (*Output, error) {
142151 wait .ForLog ("GRPC server is live" ).WithPollInterval (200 * time .Millisecond ),
143152 wait .ForListeningPort (DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT ),
144153 wait .ForListeningPort (DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT ),
145- wait .ForListeningPort (DEFAULT_BILLING_PLATFORM_SERVICE_OWNERSHIP_GRPC_PORT ),
146154 ).WithDeadline (1 * time .Minute ),
147155 )
148156
@@ -151,6 +159,11 @@ func New(in *Input) (*Output, error) {
151159 return nil , errors .Wrap (billingErr , "failed to get billing-platform-service container" )
152160 }
153161
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+
154167 cli , cliErr := client .NewClientWithOpts (
155168 client .FromEnv ,
156169 client .WithAPIVersionNegotiation (),
@@ -193,16 +206,27 @@ func New(in *Input) (*Output, error) {
193206 return nil , errors .Wrap (billingExternalHostErr , "failed to get host for Billing Platform Service" )
194207 }
195208
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+
196215 // get mapped ports for billing platform service
197216 serviceOutput , err := getExternalPorts (ctx , billingExternalHost , billingContainer )
198217 if err != nil {
199218 return nil , errors .Wrap (err , "failed to get mapped port for Billing Platform Service" )
200219 }
201220
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+
202226 output := & Output {
203227 BillingPlatformService : serviceOutput ,
204228 Postgres : & PostgresOutput {
205- DSN : DefaultPostgresDSN ,
229+ DSN : fmt . Sprintf ( "postgres://postgres:postgres@%s:%s/billing_platform" , postgresExternalHost , externalPostgresPort . Port ()) ,
206230 },
207231 }
208232
@@ -213,9 +237,8 @@ func New(in *Input) (*Output, error) {
213237
214238func getExternalPorts (ctx context.Context , billingExternalHost string , billingContainer * testcontainers.DockerContainer ) (* BillingPlatformServiceOutput , error ) {
215239 ports := map [string ]nat.Port {
216- "billing" : DEFAULT_BILLING_PLATFORM_SERVICE_BILLING_GRPC_PORT ,
217- "credit" : DEFAULT_BILLING_PLATFORM_SERVICE_CREDIT_GRPC_PORT ,
218- "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 ,
219242 }
220243
221244 output := BillingPlatformServiceOutput {}
@@ -236,9 +259,6 @@ func getExternalPorts(ctx context.Context, billingExternalHost string, billingCo
236259 case "credit" :
237260 output .CreditGRPCInternalURL = internal
238261 output .CreditGRPCExternalURL = external
239- case "ownership" :
240- output .OwnershipGRPCInternalURL = internal
241- output .OwnershipGRPCExternalURL = external
242262 }
243263 }
244264
0 commit comments