Skip to content

Commit d688ed8

Browse files
committed
separate JD database
1 parent 32e4d66 commit d688ed8

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

framework/components/jd/jd.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/docker/docker/api/types/container"
77
"github.com/docker/go-connections/nat"
88
"github.com/smartcontractkit/chainlink-testing-framework/framework"
9+
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres"
910
tc "github.com/testcontainers/testcontainers-go"
1011
tcwait "github.com/testcontainers/testcontainers-go/wait"
1112
"os"
@@ -19,14 +20,14 @@ const (
1920
)
2021

2122
type Input struct {
22-
Image string `toml:"image"`
23-
GRPCPort string `toml:"grpc_port"`
24-
WSRPCPort string `toml:"wsrpc_port"`
25-
DBURL string `toml:"db_url"`
26-
CSAEncryptionKey string `toml:"csa_encryption_key"`
27-
DockerFilePath string `toml:"docker_file"`
28-
DockerContext string `toml:"docker_ctx"`
29-
Out *Output `toml:"out"`
23+
Image string `toml:"image"`
24+
GRPCPort string `toml:"grpc_port"`
25+
WSRPCPort string `toml:"wsrpc_port"`
26+
CSAEncryptionKey string `toml:"csa_encryption_key"`
27+
DockerFilePath string `toml:"docker_file"`
28+
DockerContext string `toml:"docker_ctx"`
29+
DBInput *postgres.Input `toml:"db"`
30+
Out *Output `toml:"out"`
3031
}
3132

3233
type Output struct {
@@ -49,6 +50,15 @@ func defaults(in *Input) {
4950
}
5051
}
5152

53+
func defaultJDDB() *postgres.Input {
54+
return &postgres.Input{
55+
Image: "postgres:12",
56+
Port: 14000,
57+
VolumeName: "jd",
58+
JDDatabase: true,
59+
}
60+
}
61+
5262
func NewJD(in *Input) (*Output, error) {
5363
if in.Out != nil && in.Out.UseCache {
5464
return in.Out, nil
@@ -59,6 +69,13 @@ func NewJD(in *Input) (*Output, error) {
5969
if jdImg != "" {
6070
in.Image = jdImg
6171
}
72+
if in.DBInput == nil {
73+
in.DBInput = defaultJDDB()
74+
}
75+
pgOut, err := postgres.NewPostgreSQL(in.DBInput)
76+
if err != nil {
77+
return nil, err
78+
}
6279
containerName := framework.DefaultTCName("jd")
6380
bindPort := fmt.Sprintf("%s/tcp", in.GRPCPort)
6481
req := tc.ContainerRequest{
@@ -74,7 +91,7 @@ func NewJD(in *Input) (*Output, error) {
7491
h.PortBindings = framework.MapTheSamePort(bindPort)
7592
},
7693
Env: map[string]string{
77-
"DATABASE_URL": in.DBURL,
94+
"DATABASE_URL": pgOut.JDDockerInternalURL,
7895
"PORT": in.GRPCPort,
7996
"NODE_RPC_PORT": in.WSRPCPort,
8097
"CSA_KEY_ENCRYPTION_SECRET": in.CSAEncryptionKey,

framework/components/jd/jd_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package jd_test
33
import (
44
"github.com/smartcontractkit/chainlink-testing-framework/framework"
55
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/jd"
6-
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres"
76
"github.com/stretchr/testify/require"
87
"os"
98
"sync"
@@ -17,14 +16,7 @@ import (
1716
func TestJD(t *testing.T) {
1817
err := framework.DefaultNetwork(&sync.Once{})
1918
require.NoError(t, err)
20-
pgOut, err := postgres.NewPostgreSQL(&postgres.Input{
21-
Image: "postgres:12.0",
22-
Port: 14402,
23-
VolumeName: "c",
24-
})
25-
require.NoError(t, err)
2619
_, err = jd.NewJD(&jd.Input{
27-
DBURL: pgOut.JDDockerInternalURL,
2820
Image: os.Getenv("CTF_JD_IMAGE"),
2921
})
3022
require.NoError(t, err)

framework/components/postgres/postgres.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Input struct {
2727
Port int `toml:"port"`
2828
VolumeName string `toml:"volume_name"`
2929
Databases int `toml:"databases"`
30+
JDDatabase bool `toml:"jd_database"`
3031
PullImage bool `toml:"pull_image"`
3132
Out *Output `toml:"out"`
3233
}
@@ -49,7 +50,9 @@ func NewPostgreSQL(in *Input) (*Output, error) {
4950
for i := 0; i <= in.Databases; i++ {
5051
sqlCommands = append(sqlCommands, fmt.Sprintf("CREATE DATABASE db_%d;", i))
5152
}
52-
sqlCommands = append(sqlCommands, "CREATE DATABASE jd;")
53+
if in.JDDatabase {
54+
sqlCommands = append(sqlCommands, "CREATE DATABASE jd;")
55+
}
5356
sqlCommands = append(sqlCommands, "ALTER USER chainlink WITH SUPERUSER;")
5457
initSQL := strings.Join(sqlCommands, "\n")
5558
initFile, err := os.CreateTemp("", "init-*.sql")
@@ -129,7 +132,7 @@ func NewPostgreSQL(in *Input) (*Output, error) {
129132
if err != nil {
130133
return nil, err
131134
}
132-
return &Output{
135+
o := &Output{
133136
ContainerName: containerName,
134137
DockerInternalURL: fmt.Sprintf(
135138
"postgresql://%s:%s@%s:%s/%s?sslmode=disable",
@@ -147,21 +150,24 @@ func NewPostgreSQL(in *Input) (*Output, error) {
147150
portToExpose,
148151
Database,
149152
),
150-
JDDockerInternalURL: fmt.Sprintf(
153+
}
154+
if in.JDDatabase {
155+
o.JDDockerInternalURL = fmt.Sprintf(
151156
"postgresql://%s:%s@%s:%s/%s?sslmode=disable",
152157
User,
153158
Password,
154159
containerName,
155160
Port,
156161
"jd",
157-
),
158-
JDUrl: fmt.Sprintf(
162+
)
163+
o.JDUrl = fmt.Sprintf(
159164
"postgresql://%s:%s@%s:%d/%s?sslmode=disable",
160165
User,
161166
Password,
162167
host,
163168
portToExpose,
164169
"jd",
165-
),
166-
}, nil
170+
)
171+
}
172+
return o, nil
167173
}

framework/examples/myproject_cll/jd_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ func TestJDAndNodeSet(t *testing.T) {
2323
require.NoError(t, err)
2424
out, err := ns.NewSharedDBNodeSet(in.NodeSet, bc)
2525
require.NoError(t, err)
26-
in.JD.DBURL = out.DBOut.JDDockerInternalURL
2726
_, err = jd.NewJD(in.JD)
2827
require.NoError(t, err)
2928

0 commit comments

Comments
 (0)