Skip to content

Commit f3ab7f3

Browse files
committed
WIP: shared nodeset
1 parent fbc8083 commit f3ab7f3

File tree

5 files changed

+101
-6
lines changed

5 files changed

+101
-6
lines changed

framework/components/blockchain/anvil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
// deployAnvil deploy foundry anvil node
1818
func deployAnvil(in *Input) (*Output, error) {
1919
ctx := context.Background()
20-
entryPoint := []string{"anvil", "--host", "0.0.0.0", "--port", in.Port, "--chain-id", in.ChainID}
20+
entryPoint := []string{"anvil", "--host", "0.0.0.0", "--port", in.Port, "--chain-id", in.ChainID, "-b", "1"}
2121
entryPoint = append(entryPoint, in.DockerCmdParamsOverrides...)
2222
bindPort := fmt.Sprintf("%s/tcp", in.Port)
2323
containerName := framework.DefaultTCName("anvil")

framework/components/clnode/clnode.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ type NodeOut struct {
5353
DockerP2PUrl string `toml:"p2p_docker_internal_url"`
5454
}
5555

56-
// NewNode create a new Chainlink node with some image:tag and one or several configs
56+
// NewNodeWithDB create a new Chainlink node with some image:tag and one or several configs
5757
// see config params: TestConfigOverrides, UserConfigOverrides, etc
58-
func NewNode(in *Input) (*Output, error) {
58+
func NewNodeWithDB(in *Input) (*Output, error) {
5959
if in.Out != nil && in.Out.UseCache {
6060
return in.Out, nil
6161
}
@@ -76,6 +76,23 @@ func NewNode(in *Input) (*Output, error) {
7676
return out, nil
7777
}
7878

79+
func NewNode(in *Input, pgOut *postgres.Output) (*Output, error) {
80+
if in.Out != nil && in.Out.UseCache {
81+
return in.Out, nil
82+
}
83+
nodeOut, err := newNode(in, pgOut)
84+
if err != nil {
85+
return nil, err
86+
}
87+
out := &Output{
88+
UseCache: true,
89+
Node: nodeOut,
90+
PostgreSQL: pgOut,
91+
}
92+
in.Out = out
93+
return out, nil
94+
}
95+
7996
func newNode(in *Input, pgOut *postgres.Output) (*NodeOut, error) {
8097
ctx := context.Background()
8198

framework/components/node_set_extended/node_set_extended.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func NewExtendedNodeSet(in *Input, bcOut *blockchain.Output, fakeUrl string) (*O
2727
}
2828
n.Node.TestConfigOverrides = net
2929
n.DataProviderURL = fakeUrl
30-
o, err := clnode.NewNode(n)
30+
o, err := clnode.NewNodeWithDB(n)
3131
if err != nil {
3232
return nil, err
3333
}

framework/components/postgres/postgres.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/smartcontractkit/chainlink-testing-framework/framework"
88
"github.com/testcontainers/testcontainers-go"
99
tcwait "github.com/testcontainers/testcontainers-go/wait"
10+
"os"
11+
"strings"
1012
"time"
1113
)
1214

@@ -17,6 +19,7 @@ type Input struct {
1719
User string `toml:"user" validate:"required" default:"chainlink"`
1820
Password string `toml:"password" validate:"required" default:"thispasswordislongenough"`
1921
Database string `toml:"database" validate:"required" default:"chainlink"`
22+
Databases int `toml:"databases" validate:"required" default:"20"`
2023
Port string `toml:"port" validate:"required" default:"5432"`
2124
Out *Output `toml:"out"`
2225
}
@@ -33,6 +36,23 @@ func NewPostgreSQL(in *Input) (*Output, error) {
3336

3437
containerName := framework.DefaultTCName("postgresql")
3538

39+
var sqlCommands []string
40+
for i := 0; i <= in.Databases; i++ {
41+
sqlCommands = append(sqlCommands, fmt.Sprintf("CREATE DATABASE db_%d;", i))
42+
}
43+
sqlCommands = append(sqlCommands, "ALTER USER chainlink WITH SUPERUSER;")
44+
initSQL := strings.Join(sqlCommands, "\n")
45+
initFile, err := os.CreateTemp("", "init-*.sql")
46+
if err != nil {
47+
return nil, err
48+
}
49+
if _, err := initFile.WriteString(initSQL); err != nil {
50+
return nil, err
51+
}
52+
if err := initFile.Close(); err != nil {
53+
return nil, err
54+
}
55+
3656
req := testcontainers.ContainerRequest{
3757
AlwaysPullImage: in.PullImage,
3858
Image: fmt.Sprintf("%s:%s", in.Image, in.Tag),
@@ -51,9 +71,16 @@ func NewPostgreSQL(in *Input) (*Output, error) {
5171
Cmd: []string{
5272
"postgres", "-c", fmt.Sprintf("port=%s", in.Port),
5373
},
74+
Files: []testcontainers.ContainerFile{
75+
{
76+
HostFilePath: initFile.Name(),
77+
ContainerFilePath: "/docker-entrypoint-initdb.d/init.sql",
78+
FileMode: 0644,
79+
},
80+
},
5481
WaitingFor: tcwait.ForExec([]string{"psql", "-h", "127.0.0.1",
5582
"-U", in.User, "-p", in.Port, "-c", "select", "1", "-d", in.Database}).
56-
WithStartupTimeout(10 * time.Second),
83+
WithStartupTimeout(20 * time.Second),
5784
}
5885
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
5986
ContainerRequest: req,

framework/components/simple_node_set/node_set.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package simple_node_set
22

33
import (
4+
"fmt"
45
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain"
56
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/clnode"
7+
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres"
8+
"golang.org/x/sync/errgroup"
9+
"strings"
10+
"sync"
611
)
712

813
type Input struct {
@@ -31,7 +36,7 @@ func NewNodeSet(in *Input, bcOut *blockchain.Output, fakeUrl string) (*Output, e
3136
newIn.Node.TestConfigOverrides = net
3237
newIn.DataProviderURL = fakeUrl
3338
newIn.Out = nil
34-
o, err := clnode.NewNode(newIn)
39+
o, err := clnode.NewNodeWithDB(newIn)
3540
if err != nil {
3641
return nil, err
3742
}
@@ -44,3 +49,49 @@ func NewNodeSet(in *Input, bcOut *blockchain.Output, fakeUrl string) (*Output, e
4449
in.Out = out
4550
return out, nil
4651
}
52+
53+
func NewSharedDBNodeSet(in *Input, bcOut *blockchain.Output, fakeUrl string) (*Output, error) {
54+
if in.Out.UseCache {
55+
return in.Out, nil
56+
}
57+
dbOut, err := postgres.NewPostgreSQL(in.NodeSpec.DbInput)
58+
if err != nil {
59+
return nil, err
60+
}
61+
nodeOuts := make([]*clnode.Output, 0)
62+
eg := &errgroup.Group{}
63+
mu := &sync.Mutex{}
64+
for i := 0; i < in.Nodes; i++ {
65+
eg.Go(func() error {
66+
net, err := clnode.NewNetworkCfgOneNetworkAllNodes(bcOut)
67+
if err != nil {
68+
return err
69+
}
70+
in.NodeSpec.Node.TestConfigOverrides = net
71+
in.NodeSpec.DataProviderURL = fakeUrl
72+
in.NodeSpec.Out = nil
73+
74+
dbURL := strings.Replace(dbOut.DockerInternalURL, "/chainlink?sslmode=disable", fmt.Sprintf("/db_%d?sslmode=disable", i), -1)
75+
o, err := clnode.NewNode(in.NodeSpec, &postgres.Output{
76+
Url: dbOut.Url,
77+
DockerInternalURL: dbURL,
78+
})
79+
if err != nil {
80+
return err
81+
}
82+
mu.Lock()
83+
nodeOuts = append(nodeOuts, o)
84+
mu.Unlock()
85+
return nil
86+
})
87+
}
88+
if err := eg.Wait(); err != nil {
89+
return nil, err
90+
}
91+
out := &Output{
92+
UseCache: true,
93+
CLNodes: nodeOuts,
94+
}
95+
in.Out = out
96+
return out, nil
97+
}

0 commit comments

Comments
 (0)