@@ -3,18 +3,12 @@ package billing_platform_service
33import (
44 "context"
55 "fmt"
6- "io"
7- "net/http"
8- "os"
9- "strings"
106 "time"
117
12- networkTypes "github.com/docker/docker/api/types/network"
138 "github.com/docker/docker/client"
14- "github.com/docker/go-connections/nat"
159 "github.com/pkg/errors"
1610 "github.com/smartcontractkit/chainlink-testing-framework/framework"
17- "github.com/testcontainers/testcontainers-go "
11+ "github.com/smartcontractkit/chainlink-testing-framework/framework/components/dockercompose/utils "
1812 "github.com/testcontainers/testcontainers-go/modules/compose"
1913 "github.com/testcontainers/testcontainers-go/wait"
2014)
@@ -30,6 +24,7 @@ type BillingPlatformServiceOutput struct {
3024}
3125
3226type PostgresOutput struct {
27+ DSN string
3328}
3429
3530type Input struct {
@@ -58,10 +53,8 @@ func New(in *Input) (*Output, error) {
5853 return nil , errors .New ("input is nil" )
5954 }
6055
61- if in .UseCache {
62- if in .Output != nil {
63- return in .Output , nil
64- }
56+ if in .UseCache && in .Output != nil {
57+ return in .Output , nil
6558 }
6659
6760 in = defaultBillingPlatformService (in )
@@ -70,7 +63,7 @@ func New(in *Input) (*Output, error) {
7063 Msgf ("Starting Billing Platform Service stack with identifier %s" ,
7164 framework .DefaultTCName (DEFAULT_STACK_NAME ))
7265
73- cFilePath , fileErr := composeFilePath (in .ComposeFile )
66+ cFilePath , fileErr := utils . ComposeFilePath (in .ComposeFile , DEFAULT_STACK_NAME )
7467 if fileErr != nil {
7568 return nil , errors .Wrap (fileErr , "failed to get compose file path" )
7669 }
@@ -121,9 +114,9 @@ func New(in *Input) (*Output, error) {
121114
122115 for _ , networkName := range networks {
123116 framework .L .Debug ().Msgf ("Connecting billing-platform-service to %s network" , networkName )
124- connectContex , connectCancel := context .WithTimeout (ctx , 30 * time .Second )
117+ connectCtx , connectCancel := context .WithTimeout (ctx , 30 * time .Second )
125118 defer connectCancel ()
126- if connectErr := connectNetwork ( connectContex , 30 * time .Second , cli , billingContainer .ID , networkName , identifier ); connectErr != nil {
119+ if connectErr := utils . ConnectNetwork ( connectCtx , 30 * time .Second , cli , billingContainer .ID , networkName , identifier ); connectErr != nil {
127120 return nil , errors .Wrapf (connectErr , "failed to connect billing-platform-service to %s network" , networkName )
128121 }
129122 // verify that the container is connected to framework's network
@@ -141,13 +134,13 @@ func New(in *Input) (*Output, error) {
141134 }
142135
143136 // get hosts for billing platform service
144- billingExternalHost , billingExternalHostErr := billingContainer . Host (ctx )
137+ billingExternalHost , billingExternalHostErr := utils . GetContainerHost (ctx , billingContainer )
145138 if billingExternalHostErr != nil {
146139 return nil , errors .Wrap (billingExternalHostErr , "failed to get host for Billing Platform Service" )
147140 }
148141
149142 // get mapped port for billing platform service
150- billingExternalPort , billingExternalPortErr := findMappedPort (ctx , 20 * time .Second , billingContainer , DEFAULT_BILLING_PLATFORM_SERVICE_GRPC_PORT )
143+ billingExternalPort , billingExternalPortErr := utils . FindMappedPort (ctx , 20 * time .Second , billingContainer , DEFAULT_BILLING_PLATFORM_SERVICE_GRPC_PORT )
151144 if billingExternalPortErr != nil {
152145 return nil , errors .Wrap (billingExternalPortErr , "failed to get mapped port for Chip Ingress" )
153146 }
@@ -157,92 +150,12 @@ func New(in *Input) (*Output, error) {
157150 GRPCInternalURL : fmt .Sprintf ("http://%s:%s" , DEFAULT_BILLING_PLATFORM_SERVICE_SERVICE_NAME , DEFAULT_BILLING_PLATFORM_SERVICE_GRPC_PORT ),
158151 GRPCExternalURL : fmt .Sprintf ("http://%s:%s" , billingExternalHost , billingExternalPort .Port ()),
159152 },
160- }
161-
162- framework .L .Info ().Msg ("Chip Ingress stack start" )
163-
164- return output , nil
165- }
166-
167- func composeFilePath (rawFilePath string ) (string , error ) {
168- // if it's not a URL, return it as is and assume it's a local file
169- if ! strings .HasPrefix (rawFilePath , "http" ) {
170- return rawFilePath , nil
171- }
172-
173- resp , respErr := http .Get (rawFilePath )
174- if respErr != nil {
175- return "" , errors .Wrap (respErr , "failed to download docker-compose file" )
176- }
177- defer resp .Body .Close ()
178-
179- tempFile , tempErr := os .CreateTemp ("" , "billing-platform-service-docker-compose-*.yml" )
180- if tempErr != nil {
181- return "" , errors .Wrap (tempErr , "failed to create temp file" )
182- }
183- defer tempFile .Close ()
153+ Postgres : & PostgresOutput {
184154
185- _ , copyErr := io .Copy (tempFile , resp .Body )
186- if copyErr != nil {
187- tempFile .Close ()
188- return "" , errors .Wrap (copyErr , "failed to write compose file" )
189- }
190-
191- return tempFile .Name (), nil
192- }
193-
194- func findMappedPort (ctx context.Context , timeout time.Duration , container * testcontainers.DockerContainer , port nat.Port ) (nat.Port , error ) {
195- forCtx , cancel := context .WithTimeout (ctx , timeout )
196- defer cancel ()
197-
198- tickerInterval := 5 * time .Second
199- ticker := time .NewTicker (5 * time .Second )
200- defer ticker .Stop ()
201-
202- for {
203- select {
204- case <- forCtx .Done ():
205- return "" , fmt .Errorf ("timeout while waiting for mapped port for %s" , port )
206- case <- ticker .C :
207- portCtx , portCancel := context .WithTimeout (ctx , tickerInterval )
208- defer portCancel ()
209- mappedPort , mappedPortErr := container .MappedPort (portCtx , port )
210- if mappedPortErr != nil {
211- return "" , errors .Wrapf (mappedPortErr , "failed to get mapped port for %s" , port )
212- }
213- if mappedPort .Port () == "" {
214- return "" , fmt .Errorf ("mapped port for %s is empty" , port )
215- }
216- return mappedPort , nil
217155 }
218156 }
219- }
220157
221- func connectNetwork (connCtx context.Context , timeout time.Duration , dockerClient * client.Client , containerID , networkName , stackIdentifier string ) error {
222- ticker := time .NewTicker (500 * time .Millisecond )
223- defer ticker .Stop ()
224-
225- networkCtx , networkCancel := context .WithTimeout (connCtx , timeout )
226- defer networkCancel ()
227-
228- for {
229- select {
230- case <- networkCtx .Done ():
231- return fmt .Errorf ("timeout while trying to connect billing-platform-service to default network" )
232- case <- ticker .C :
233- if networkErr := dockerClient .NetworkConnect (
234- connCtx ,
235- networkName ,
236- containerID ,
237- & networkTypes.EndpointSettings {
238- Aliases : []string {stackIdentifier },
239- },
240- ); networkErr != nil && ! strings .Contains (networkErr .Error (), "already exists in network" ) {
241- framework .L .Trace ().Msgf ("failed to connect to default network: %v" , networkErr )
242- continue
243- }
244- framework .L .Trace ().Msgf ("connected to %s network" , networkName )
245- return nil
246- }
247- }
158+ framework .L .Info ().Msg ("Billing Platform Service stack start" )
159+
160+ return output , nil
248161}
0 commit comments