Skip to content

Commit e2b9456

Browse files
committed
clean use of pg host networking mode
1 parent df7023a commit e2b9456

File tree

9 files changed

+223
-160
lines changed

9 files changed

+223
-160
lines changed

framedata/search_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ func (s *SearchTestSuite) runStableSearchTests(t *testing.T, depOpt *definition.
403403

404404
// Create a service with the test dependencies
405405
ctx, svc := frame.NewServiceWithContext(ctx, "search-test",
406-
frame.WithDatastoreConnection(depOpt.Database()[0].GetDS().String(), false),
407-
frame.WithRegisterPublisher("test-queue", depOpt.Queue()[0].GetDS().String()),
406+
frame.WithDatastoreConnection(depOpt.Database(ctx)[0].GetDS(ctx).String(), false),
407+
frame.WithRegisterPublisher("test-queue", depOpt.Queue(ctx)[0].GetDS(ctx).String()),
408408
)
409409
defer svc.Stop(ctx)
410410

@@ -612,8 +612,8 @@ func (s *SearchTestSuite) TestStableSearchConcurrency() {
612612

613613
// Create a service with the test dependencies
614614
ctx, svc := frame.NewServiceWithContext(ctx, "search-test",
615-
frame.WithDatastoreConnection(depOpt.Database()[0].GetDS().String(), false),
616-
frame.WithRegisterPublisher("test-queue", depOpt.Queue()[0].GetDS().String()),
615+
frame.WithDatastoreConnection(depOpt.Database(ctx)[0].GetDS(ctx).String(), false),
616+
frame.WithRegisterPublisher("test-queue", depOpt.Queue(ctx)[0].GetDS(ctx).String()),
617617
)
618618
defer svc.Stop(ctx)
619619

@@ -691,8 +691,8 @@ func (s *SearchTestSuite) TestStableSearchMemoryManagement() {
691691

692692
// Create a service with the test dependencies
693693
ctx, svc := frame.NewServiceWithContext(ctx, "search-test",
694-
frame.WithDatastoreConnection(depOpt.Database()[0].GetDS().String(), false),
695-
frame.WithRegisterPublisher("test-queue", depOpt.Queue()[0].GetDS().String()),
694+
frame.WithDatastoreConnection(depOpt.Database(ctx)[0].GetDS(ctx).String(), false),
695+
frame.WithRegisterPublisher("test-queue", depOpt.Queue(ctx)[0].GetDS(ctx).String()),
696696
)
697697
defer svc.Stop(ctx)
698698

@@ -922,8 +922,8 @@ func (s *SearchTestSuite) runErrorRecoveryTests(t *testing.T, depOpt *definition
922922

923923
// Create a service with the test dependencies
924924
ctx, svc := frame.NewServiceWithContext(ctx, "search-test",
925-
frame.WithDatastoreConnection(depOpt.Database()[0].GetDS().String(), false),
926-
frame.WithRegisterPublisher("test-queue", depOpt.Queue()[0].GetDS().String()),
925+
frame.WithDatastoreConnection(depOpt.Database(ctx)[0].GetDS(ctx).String(), false),
926+
frame.WithRegisterPublisher("test-queue", depOpt.Queue(ctx)[0].GetDS(ctx).String()),
927927
)
928928
defer svc.Stop(ctx)
929929

frametests/definition/options.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func (o *ContainerOpts) Configure(
5151
}
5252
} else {
5353
containerRequest.ExposedPorts = o.Ports
54-
5554
containerRequest.Networks = []string{ntwk.Name}
5655
containerRequest.NetworkAliases = map[string][]string{
5756
ntwk.Name: o.NetworkAliases,
@@ -79,14 +78,22 @@ func (o *ContainerOpts) ConfigurationExtend(
7978
}))
8079
} else {
8180
containerCustomize = append(containerCustomize,
82-
testcontainers.WithExposedPorts(o.Ports...),
81+
withExposePorts(o.Ports...),
8382
network.WithNetwork([]string{ntwk.Name}, ntwk),
8483
network.WithNetworkName(o.NetworkAliases, ntwk.Name))
8584
}
8685

8786
return containerCustomize
8887
}
8988

89+
// withExposePorts appends the ports to the exposed ports for a container.
90+
func withExposePorts(ports ...string) testcontainers.CustomizeRequestOption {
91+
return func(req *testcontainers.GenericContainerRequest) error {
92+
req.ExposedPorts = ports
93+
return nil
94+
}
95+
}
96+
9097
// ContainerOption is a type that can be used to configure the container creation request.
9198
type ContainerOption func(req *ContainerOpts)
9299

@@ -132,10 +139,10 @@ func WithNetworkAliases(networkAliases []string) ContainerOption {
132139
}
133140
}
134141

135-
// WithDisableLogging allows to set the disable logging to use for testing.
136-
func WithDisableLogging(disableLogging bool) ContainerOption {
142+
// WithEnableLogging allows to enable logging to use for testing.
143+
func WithEnableLogging(enableLogging bool) ContainerOption {
137144
return func(original *ContainerOpts) {
138-
original.EnableLogging = disableLogging
145+
original.EnableLogging = enableLogging
139146
}
140147
}
141148

frametests/definition/setup.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type DependancyRes interface {
1919

2020
type DependancyConn interface {
2121
Name() string
22-
GetDS() frame.DataSource
23-
GetInternalDS() frame.DataSource
22+
GetDS(ctx context.Context) frame.DataSource
23+
GetInternalDS(ctx context.Context) frame.DataSource
2424
GetRandomisedDS(ctx context.Context, randomisedPrefix string) (frame.DataSource, func(context.Context), error)
2525
}
2626

@@ -53,28 +53,28 @@ func (opt *DependancyOption) All() []DependancyConn {
5353
return opt.deps
5454
}
5555

56-
func (opt *DependancyOption) Database() []DependancyConn {
56+
func (opt *DependancyOption) Database(ctx context.Context) []DependancyConn {
5757
var deps []DependancyConn
5858
for _, dep := range opt.deps {
59-
if dep.GetDS().IsDB() {
59+
if dep.GetDS(ctx).IsDB() {
6060
deps = append(deps, dep)
6161
}
6262
}
6363
return deps
6464
}
65-
func (opt *DependancyOption) Cache() []DependancyConn {
65+
func (opt *DependancyOption) Cache(ctx context.Context) []DependancyConn {
6666
var deps []DependancyConn
6767
for _, dep := range opt.deps {
68-
if dep.GetDS().IsCache() {
68+
if dep.GetDS(ctx).IsCache() {
6969
deps = append(deps, dep)
7070
}
7171
}
7272
return deps
7373
}
74-
func (opt *DependancyOption) Queue() []DependancyConn {
74+
func (opt *DependancyOption) Queue(ctx context.Context) []DependancyConn {
7575
var deps []DependancyConn
7676
for _, dep := range opt.deps {
77-
if dep.GetDS().IsQueue() {
77+
if dep.GetDS(ctx).IsQueue() {
7878
deps = append(deps, dep)
7979
}
8080
}

frametests/deps/testnats/nats.go

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55
"fmt"
66
"net"
7+
"strconv"
78

9+
"github.com/docker/go-connections/nat"
810
"github.com/pitabwire/util"
911
"github.com/testcontainers/testcontainers-go"
1012
tcNats "github.com/testcontainers/testcontainers-go/modules/nats"
@@ -21,13 +23,10 @@ const (
2123
NatsCluster = "frame_test"
2224
)
2325

24-
type natsDependancy struct {
26+
type dependancy struct {
2527
opts definition.ContainerOpts
2628
cluster string
2729

28-
conn frame.DataSource
29-
internalConn frame.DataSource
30-
3130
container *tcNats.NATSContainer
3231
}
3332

@@ -45,20 +44,20 @@ func NewWithOpts(cluster string, containerOpts ...definition.ContainerOption) de
4544
}
4645
opts.Setup(containerOpts...)
4746

48-
return &natsDependancy{
47+
return &dependancy{
4948
cluster: cluster,
5049
opts: opts,
5150
}
5251
}
5352

54-
func (d *natsDependancy) Name() string {
53+
func (d *dependancy) Name() string {
5554
return d.opts.ImageName
5655
}
57-
func (d *natsDependancy) Container() testcontainers.Container {
56+
func (d *dependancy) Container() testcontainers.Container {
5857
return d.container
5958
}
6059

61-
func (d *natsDependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerNetwork) error {
60+
func (d *dependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerNetwork) error {
6261
containerCustomize := d.opts.ConfigurationExtend(ctx, ntwk, []testcontainers.ContainerCustomizer{
6362

6463
testcontainers.WithCmdArgs("--js", "-DVV"),
@@ -73,38 +72,50 @@ func (d *natsDependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerN
7372

7473
d.container = natsContainer
7574

76-
conn, err := natsContainer.ConnectionString(ctx)
75+
return nil
76+
}
77+
78+
func (d *dependancy) GetDS(ctx context.Context) frame.DataSource {
79+
port := nat.Port(d.opts.Ports[0])
80+
conn, err := d.container.PortEndpoint(ctx, port, "nats")
7781
if err != nil {
78-
return fmt.Errorf("failed to get connection string for container: %w", err)
82+
logger := util.Log(ctx).WithField("image", d.opts.ImageName)
83+
logger.WithError(err).Error("failed to get connection for Container")
7984
}
8085

81-
d.conn = frame.DataSource(conn)
86+
return frame.DataSource(conn)
87+
}
8288

83-
internalIP, err := natsContainer.ContainerIP(ctx)
89+
func (d *dependancy) GetInternalDS(ctx context.Context) frame.DataSource {
90+
internalIP, err := d.container.ContainerIP(ctx)
8491
if err != nil {
85-
return fmt.Errorf("failed to get internal host ip for container: %w", err)
92+
logger := util.Log(ctx).WithField("image", d.opts.ImageName)
93+
logger.WithError(err).Error("failed to get internal host ip for Container")
94+
return ""
8695
}
87-
d.internalConn = frame.DataSource(fmt.Sprintf("nats://%s", net.JoinHostPort(internalIP, d.opts.Ports[0])))
8896

89-
return nil
90-
}
97+
if internalIP == "" && d.opts.UseHostMode {
98+
internalIP, err = d.container.Host(ctx)
99+
if err != nil {
100+
logger := util.Log(ctx).WithField("image", d.opts.ImageName)
101+
logger.WithError(err).Error("failed to get host ip for Container")
102+
return ""
103+
}
104+
}
105+
port := nat.Port(d.opts.Ports[0])
91106

92-
func (d *natsDependancy) GetDS() frame.DataSource {
93-
return d.conn
94-
}
95-
func (d *natsDependancy) GetInternalDS() frame.DataSource {
96-
return d.internalConn
107+
return frame.DataSource(fmt.Sprintf("nats://%s", net.JoinHostPort(internalIP, strconv.Itoa(port.Int()))))
97108
}
98109

99-
func (d *natsDependancy) GetRandomisedDS(
100-
_ context.Context,
110+
func (d *dependancy) GetRandomisedDS(
111+
ctx context.Context,
101112
_ string,
102113
) (frame.DataSource, func(context.Context), error) {
103-
return d.GetDS(), func(_ context.Context) {
114+
return d.GetDS(ctx), func(_ context.Context) {
104115
}, nil
105116
}
106117

107-
func (d *natsDependancy) Cleanup(ctx context.Context) {
118+
func (d *dependancy) Cleanup(ctx context.Context) {
108119
if d.container != nil {
109120
if err := d.container.Terminate(ctx); err != nil {
110121
log := util.Log(ctx)

frametests/deps/testoryhydra/hydra.go

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"net"
8+
"strconv"
89
"strings"
910

1011
"github.com/docker/go-connections/nat"
@@ -54,13 +55,10 @@ strategies:
5455
`
5556
)
5657

57-
type hydraDependancy struct {
58+
type dependancy struct {
5859
opts definition.ContainerOpts
5960
configuration string
6061

61-
conn frame.DataSource
62-
internalConn frame.DataSource
63-
6462
container testcontainers.Container
6563
}
6664

@@ -76,21 +74,21 @@ func NewWithOpts(configuration string, containerOpts ...definition.ContainerOpti
7674
}
7775
opts.Setup(containerOpts...)
7876

79-
return &hydraDependancy{
77+
return &dependancy{
8078
opts: opts,
8179
configuration: configuration,
8280
}
8381
}
8482

85-
func (d *hydraDependancy) Name() string {
83+
func (d *dependancy) Name() string {
8684
return d.opts.ImageName
8785
}
8886

89-
func (d *hydraDependancy) Container() testcontainers.Container {
87+
func (d *dependancy) Container() testcontainers.Container {
9088
return d.container
9189
}
9290

93-
func (d *hydraDependancy) migrateContainer(
91+
func (d *dependancy) migrateContainer(
9492
ctx context.Context,
9593
ntwk *testcontainers.DockerNetwork,
9694
databaseURL string,
@@ -130,12 +128,12 @@ func (d *hydraDependancy) migrateContainer(
130128
return nil
131129
}
132130

133-
func (d *hydraDependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerNetwork) error {
134-
if len(d.opts.Dependencies) == 0 || !d.opts.Dependencies[0].GetDS().IsDB() {
131+
func (d *dependancy) Setup(ctx context.Context, ntwk *testcontainers.DockerNetwork) error {
132+
if len(d.opts.Dependencies) == 0 || !d.opts.Dependencies[0].GetDS(ctx).IsDB() {
135133
return errors.New("no Database dependencies was supplied")
136134
}
137135

138-
databaseURL := d.opts.Dependencies[0].GetInternalDS().String()
136+
databaseURL := d.opts.Dependencies[0].GetInternalDS(ctx).String()
139137
err := d.migrateContainer(ctx, ntwk, databaseURL)
140138
if err != nil {
141139
return err
@@ -175,51 +173,51 @@ func (d *hydraDependancy) Setup(ctx context.Context, ntwk *testcontainers.Docker
175173
return fmt.Errorf("failed to start hydraContainer: %w", err)
176174
}
177175

178-
port, err := hydraContainer.MappedPort(ctx, adminPort)
179-
if err != nil {
180-
return fmt.Errorf("failed to get connection string for hydraContainer: %w", err)
181-
}
176+
d.container = hydraContainer
177+
return nil
178+
}
182179

183-
host, err := hydraContainer.Host(ctx)
180+
func (d *dependancy) GetDS(ctx context.Context) frame.DataSource {
181+
port := nat.Port(d.opts.Ports[1])
182+
conn, err := d.container.PortEndpoint(ctx, port, "http")
184183
if err != nil {
185-
return fmt.Errorf("failed to get connection string for hydraContainer: %w", err)
184+
logger := util.Log(ctx).WithField("image", d.opts.ImageName)
185+
logger.WithError(err).Error("failed to get connection for Container")
186186
}
187187

188-
d.conn = frame.DataSource(fmt.Sprintf("http://%s", net.JoinHostPort(host, port.Port())))
188+
return frame.DataSource(conn)
189+
}
189190

190-
internalIP, err := hydraContainer.ContainerIP(ctx)
191+
func (d *dependancy) GetInternalDS(ctx context.Context) frame.DataSource {
192+
internalIP, err := d.container.ContainerIP(ctx)
191193
if err != nil {
192-
return fmt.Errorf("failed to get internal host ip for hydraContainer: %w", err)
194+
logger := util.Log(ctx).WithField("image", d.opts.ImageName)
195+
logger.WithError(err).Error("failed to get internal host ip for Container")
196+
return ""
193197
}
194198

195-
if d.opts.UseHostMode && internalIP == "" {
196-
internalIP = host
199+
if internalIP == "" && d.opts.UseHostMode {
200+
internalIP, err = d.container.Host(ctx)
201+
if err != nil {
202+
logger := util.Log(ctx).WithField("image", d.opts.ImageName)
203+
logger.WithError(err).Error("failed to get host ip for Container")
204+
return ""
205+
}
197206
}
207+
port := nat.Port(d.opts.Ports[1])
198208

199-
d.internalConn = frame.DataSource(
200-
fmt.Sprintf("http://%s", net.JoinHostPort(internalIP, adminPort.Port())),
201-
)
202-
203-
d.container = hydraContainer
204-
return nil
209+
return frame.DataSource(fmt.Sprintf("http://%s", net.JoinHostPort(internalIP, strconv.Itoa(port.Int()))))
205210
}
206211

207-
func (d *hydraDependancy) GetDS() frame.DataSource {
208-
return d.conn
209-
}
210-
func (d *hydraDependancy) GetInternalDS() frame.DataSource {
211-
return d.internalConn
212-
}
213-
214-
func (d *hydraDependancy) GetRandomisedDS(
215-
_ context.Context,
212+
func (d *dependancy) GetRandomisedDS(
213+
ctx context.Context,
216214
_ string,
217215
) (frame.DataSource, func(context.Context), error) {
218-
return d.GetDS(), func(_ context.Context) {
216+
return d.GetDS(ctx), func(_ context.Context) {
219217
}, nil
220218
}
221219

222-
func (d *hydraDependancy) Cleanup(ctx context.Context) {
220+
func (d *dependancy) Cleanup(ctx context.Context) {
223221
if d.container != nil {
224222
if err := d.container.Terminate(ctx); err != nil {
225223
log := util.Log(ctx)

0 commit comments

Comments
 (0)