Skip to content

Commit 14888c6

Browse files
Atrax1EasterTheBunny
authored andcommitted
refactor
1 parent 6b896fa commit 14888c6

File tree

2 files changed

+118
-100
lines changed

2 files changed

+118
-100
lines changed

framework/components/dockercompose/billing_platform_service/billing_platform_service.go

Lines changed: 13 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ package billing_platform_service
33
import (
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

3226
type PostgresOutput struct {
27+
DSN string
3328
}
3429

3530
type 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
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"os"
9+
"strings"
10+
"time"
11+
12+
networkTypes "github.com/docker/docker/api/types/network"
13+
"github.com/docker/docker/client"
14+
"github.com/docker/go-connections/nat"
15+
"github.com/pkg/errors"
16+
"github.com/smartcontractkit/chainlink-testing-framework/framework"
17+
"github.com/testcontainers/testcontainers-go"
18+
)
19+
20+
func ComposeFilePath(rawFilePath string, serviceName string) (string, error) {
21+
// if it's not a URL, return it as is and assume it's a local file
22+
if !strings.HasPrefix(rawFilePath, "http") {
23+
return rawFilePath, nil
24+
}
25+
26+
resp, respErr := http.Get(rawFilePath)
27+
if respErr != nil {
28+
return "", errors.Wrap(respErr, "failed to download docker-compose file")
29+
}
30+
defer resp.Body.Close()
31+
32+
tempFile, tempErr := os.CreateTemp("", serviceName+"-docker-compose-*.yml")
33+
if tempErr != nil {
34+
return "", errors.Wrap(tempErr, "failed to create temp file")
35+
}
36+
defer tempFile.Close()
37+
38+
_, copyErr := io.Copy(tempFile, resp.Body)
39+
if copyErr != nil {
40+
tempFile.Close()
41+
return "", errors.Wrap(copyErr, "failed to write compose file")
42+
}
43+
44+
return tempFile.Name(), nil
45+
}
46+
47+
func GetContainerHost(ctx context.Context, container *testcontainers.DockerContainer) (string, error) {
48+
return container.Host(ctx)
49+
}
50+
51+
func FindMappedPort(ctx context.Context, timeout time.Duration, container *testcontainers.DockerContainer, port nat.Port) (nat.Port, error) {
52+
forCtx, cancel := context.WithTimeout(ctx, timeout)
53+
defer cancel()
54+
55+
tickerInterval := 5 * time.Second
56+
ticker := time.NewTicker(5 * time.Second)
57+
defer ticker.Stop()
58+
59+
for {
60+
select {
61+
case <-forCtx.Done():
62+
return "", fmt.Errorf("timeout while waiting for mapped port for %s", port)
63+
case <-ticker.C:
64+
portCtx, portCancel := context.WithTimeout(ctx, tickerInterval)
65+
defer portCancel()
66+
mappedPort, mappedPortErr := container.MappedPort(portCtx, port)
67+
if mappedPortErr != nil {
68+
return "", errors.Wrapf(mappedPortErr, "failed to get mapped port for %s", port)
69+
}
70+
if mappedPort.Port() == "" {
71+
return "", fmt.Errorf("mapped port for %s is empty", port)
72+
}
73+
return mappedPort, nil
74+
}
75+
}
76+
}
77+
78+
func ConnectNetwork(connCtx context.Context, timeout time.Duration, dockerClient *client.Client, containerID, networkName, stackIdentifier string) error {
79+
ticker := time.NewTicker(500 * time.Millisecond)
80+
defer ticker.Stop()
81+
82+
networkCtx, networkCancel := context.WithTimeout(connCtx, timeout)
83+
defer networkCancel()
84+
85+
for {
86+
select {
87+
case <-networkCtx.Done():
88+
return fmt.Errorf("timeout while trying to connect billing-platform-service to default network")
89+
case <-ticker.C:
90+
if networkErr := dockerClient.NetworkConnect(
91+
connCtx,
92+
networkName,
93+
containerID,
94+
&networkTypes.EndpointSettings{
95+
Aliases: []string{stackIdentifier},
96+
},
97+
); networkErr != nil && !strings.Contains(networkErr.Error(), "already exists in network") {
98+
framework.L.Trace().Msgf("failed to connect to default network: %v", networkErr)
99+
continue
100+
}
101+
framework.L.Trace().Msgf("connected to %s network", networkName)
102+
return nil
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)