Skip to content

Commit

Permalink
separate JD database
Browse files Browse the repository at this point in the history
  • Loading branch information
skudasov committed Dec 11, 2024
1 parent 32e4d66 commit d688ed8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
35 changes: 26 additions & 9 deletions framework/components/jd/jd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/smartcontractkit/chainlink-testing-framework/framework"
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres"
tc "github.com/testcontainers/testcontainers-go"
tcwait "github.com/testcontainers/testcontainers-go/wait"
"os"
Expand All @@ -19,14 +20,14 @@ const (
)

type Input struct {
Image string `toml:"image"`
GRPCPort string `toml:"grpc_port"`
WSRPCPort string `toml:"wsrpc_port"`
DBURL string `toml:"db_url"`
CSAEncryptionKey string `toml:"csa_encryption_key"`
DockerFilePath string `toml:"docker_file"`
DockerContext string `toml:"docker_ctx"`
Out *Output `toml:"out"`
Image string `toml:"image"`
GRPCPort string `toml:"grpc_port"`
WSRPCPort string `toml:"wsrpc_port"`
CSAEncryptionKey string `toml:"csa_encryption_key"`
DockerFilePath string `toml:"docker_file"`
DockerContext string `toml:"docker_ctx"`
DBInput *postgres.Input `toml:"db"`
Out *Output `toml:"out"`
}

type Output struct {
Expand All @@ -49,6 +50,15 @@ func defaults(in *Input) {
}
}

func defaultJDDB() *postgres.Input {
return &postgres.Input{
Image: "postgres:12",
Port: 14000,
VolumeName: "jd",
JDDatabase: true,
}
}

func NewJD(in *Input) (*Output, error) {
if in.Out != nil && in.Out.UseCache {
return in.Out, nil
Expand All @@ -59,6 +69,13 @@ func NewJD(in *Input) (*Output, error) {
if jdImg != "" {
in.Image = jdImg
}
if in.DBInput == nil {
in.DBInput = defaultJDDB()
}
pgOut, err := postgres.NewPostgreSQL(in.DBInput)
if err != nil {
return nil, err
}
containerName := framework.DefaultTCName("jd")
bindPort := fmt.Sprintf("%s/tcp", in.GRPCPort)
req := tc.ContainerRequest{
Expand All @@ -74,7 +91,7 @@ func NewJD(in *Input) (*Output, error) {
h.PortBindings = framework.MapTheSamePort(bindPort)
},
Env: map[string]string{
"DATABASE_URL": in.DBURL,
"DATABASE_URL": pgOut.JDDockerInternalURL,
"PORT": in.GRPCPort,
"NODE_RPC_PORT": in.WSRPCPort,
"CSA_KEY_ENCRYPTION_SECRET": in.CSAEncryptionKey,
Expand Down
8 changes: 0 additions & 8 deletions framework/components/jd/jd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package jd_test
import (
"github.com/smartcontractkit/chainlink-testing-framework/framework"
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/jd"
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres"
"github.com/stretchr/testify/require"
"os"
"sync"
Expand All @@ -17,14 +16,7 @@ import (
func TestJD(t *testing.T) {
err := framework.DefaultNetwork(&sync.Once{})
require.NoError(t, err)
pgOut, err := postgres.NewPostgreSQL(&postgres.Input{
Image: "postgres:12.0",
Port: 14402,
VolumeName: "c",
})
require.NoError(t, err)
_, err = jd.NewJD(&jd.Input{
DBURL: pgOut.JDDockerInternalURL,
Image: os.Getenv("CTF_JD_IMAGE"),
})
require.NoError(t, err)
Expand Down
20 changes: 13 additions & 7 deletions framework/components/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Input struct {
Port int `toml:"port"`
VolumeName string `toml:"volume_name"`
Databases int `toml:"databases"`
JDDatabase bool `toml:"jd_database"`
PullImage bool `toml:"pull_image"`
Out *Output `toml:"out"`
}
Expand All @@ -49,7 +50,9 @@ func NewPostgreSQL(in *Input) (*Output, error) {
for i := 0; i <= in.Databases; i++ {
sqlCommands = append(sqlCommands, fmt.Sprintf("CREATE DATABASE db_%d;", i))
}
sqlCommands = append(sqlCommands, "CREATE DATABASE jd;")
if in.JDDatabase {
sqlCommands = append(sqlCommands, "CREATE DATABASE jd;")
}
sqlCommands = append(sqlCommands, "ALTER USER chainlink WITH SUPERUSER;")
initSQL := strings.Join(sqlCommands, "\n")
initFile, err := os.CreateTemp("", "init-*.sql")
Expand Down Expand Up @@ -129,7 +132,7 @@ func NewPostgreSQL(in *Input) (*Output, error) {
if err != nil {
return nil, err
}
return &Output{
o := &Output{
ContainerName: containerName,
DockerInternalURL: fmt.Sprintf(
"postgresql://%s:%s@%s:%s/%s?sslmode=disable",
Expand All @@ -147,21 +150,24 @@ func NewPostgreSQL(in *Input) (*Output, error) {
portToExpose,
Database,
),
JDDockerInternalURL: fmt.Sprintf(
}
if in.JDDatabase {
o.JDDockerInternalURL = fmt.Sprintf(
"postgresql://%s:%s@%s:%s/%s?sslmode=disable",
User,
Password,
containerName,
Port,
"jd",
),
JDUrl: fmt.Sprintf(
)
o.JDUrl = fmt.Sprintf(
"postgresql://%s:%s@%s:%d/%s?sslmode=disable",
User,
Password,
host,
portToExpose,
"jd",
),
}, nil
)
}
return o, nil
}
1 change: 0 additions & 1 deletion framework/examples/myproject_cll/jd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func TestJDAndNodeSet(t *testing.T) {
require.NoError(t, err)
out, err := ns.NewSharedDBNodeSet(in.NodeSet, bc)
require.NoError(t, err)
in.JD.DBURL = out.DBOut.JDDockerInternalURL
_, err = jd.NewJD(in.JD)
require.NoError(t, err)

Expand Down

0 comments on commit d688ed8

Please sign in to comment.