@@ -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
2223const DefaultPostgresDSN = "postgres://postgres:postgres@postgres:5432/billing_platform"
@@ -27,12 +28,10 @@ type Output struct {
2728}
2829
2930type 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
3837type PostgresOutput struct {
@@ -64,10 +63,11 @@ func defaultBillingPlatformService(in *Input) *Input {
6463const (
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
7373func 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
216238func 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
0 commit comments