Skip to content

Commit b1ec2de

Browse files
committed
tests for node/nodeset
1 parent 3b35366 commit b1ec2de

File tree

5 files changed

+260
-11
lines changed

5 files changed

+260
-11
lines changed

framework/components/clnode/clnode.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ type Input struct {
3030

3131
// NodeInput is CL nod container inputs
3232
type NodeInput struct {
33-
Image string `toml:"image" validate:"required"`
34-
Name string `toml:"name"`
35-
PullImage bool `toml:"pull_image"`
36-
//Port string `toml:"port" validate:"required"`
37-
//P2PPort string `toml:"p2p_port" validate:"required"`
33+
Image string `toml:"image" validate:"required"`
34+
Name string `toml:"name"`
35+
PullImage bool `toml:"pull_image"`
3836
CapabilitiesBinaryPaths []string `toml:"capabilities"`
3937
CapabilityContainerDir string `toml:"capabilities_container_dir"`
4038
TestConfigOverrides string `toml:"test_config_overrides"`
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package clnode_test
2+
3+
import (
4+
"github.com/smartcontractkit/chainlink-testing-framework/framework"
5+
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/clnode"
6+
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres"
7+
"github.com/stretchr/testify/require"
8+
"sync"
9+
"testing"
10+
)
11+
12+
type testCase struct {
13+
name string
14+
input *clnode.Input
15+
assertion func(t *testing.T, output *clnode.Output)
16+
}
17+
18+
func checkBasicOutputs(t *testing.T, output *clnode.Output) {
19+
require.NotNil(t, output)
20+
require.NotNil(t, output.Node)
21+
require.Contains(t, output.Node.HostURL, "127.0.0.1")
22+
require.Contains(t, output.Node.DockerURL, "cl-node")
23+
require.Contains(t, output.Node.DockerP2PUrl, "cl-node")
24+
require.NotNil(t, output.PostgreSQL)
25+
require.Contains(t, output.PostgreSQL.Url, "postgresql://chainlink:[email protected]")
26+
require.Contains(t, output.PostgreSQL.DockerInternalURL, "postgresql://chainlink:thispasswordislongenough@postgresql-")
27+
}
28+
29+
func TestNodeWithSharedDB(t *testing.T) {
30+
testCases := []testCase{
31+
{
32+
name: "basic use case",
33+
input: &clnode.Input{
34+
DataProviderURL: "http://example.com",
35+
DbInput: &postgres.Input{
36+
Image: "postgres:15.6",
37+
PullImage: true,
38+
},
39+
Node: &clnode.NodeInput{
40+
Image: "public.ecr.aws/chainlink/chainlink:v2.17.0",
41+
Name: "cl-node-1",
42+
PullImage: true,
43+
},
44+
},
45+
assertion: func(t *testing.T, output *clnode.Output) {
46+
checkBasicOutputs(t, output)
47+
},
48+
},
49+
}
50+
51+
for _, tc := range testCases {
52+
err := framework.DefaultNetwork(t, &sync.Once{})
53+
require.NoError(t, err)
54+
55+
t.Run(tc.name, func(t *testing.T) {
56+
pgOut, err := postgres.NewPostgreSQL(tc.input.DbInput)
57+
require.NoError(t, err)
58+
output, err := clnode.NewNode(tc.input, pgOut)
59+
require.NoError(t, err)
60+
tc.assertion(t, output)
61+
})
62+
}
63+
}
64+
65+
func TestNodeWithDB(t *testing.T) {
66+
testCases := []testCase{
67+
{
68+
name: "basic use case",
69+
input: &clnode.Input{
70+
DataProviderURL: "http://example.com",
71+
DbInput: &postgres.Input{
72+
Image: "postgres:15.6",
73+
PullImage: true,
74+
},
75+
Node: &clnode.NodeInput{
76+
Image: "public.ecr.aws/chainlink/chainlink:v2.17.0",
77+
Name: "cl-node-2",
78+
PullImage: true,
79+
},
80+
},
81+
assertion: func(t *testing.T, output *clnode.Output) {
82+
checkBasicOutputs(t, output)
83+
},
84+
},
85+
}
86+
87+
for _, tc := range testCases {
88+
err := framework.DefaultNetwork(t, &sync.Once{})
89+
require.NoError(t, err)
90+
91+
t.Run(tc.name, func(t *testing.T) {
92+
output, err := clnode.NewNodeWithDB(tc.input)
93+
require.NoError(t, err)
94+
tc.assertion(t, output)
95+
})
96+
}
97+
}

framework/components/simple_node_set/node_set.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ func oneNodeSharedDBConfiguration(in *Input, bcOut *blockchain.Output, fakeUrl s
3333
for i := 0; i < in.Nodes; i++ {
3434
i := i
3535
var overrideIdx int
36+
var nodeName string
3637
if overrideEach {
3738
overrideIdx = i
3839
} else {
3940
overrideIdx = 0
4041
}
42+
if in.NodeSpecs[overrideIdx].Node.Name == "" {
43+
nodeName = fmt.Sprintf("node%d", i)
44+
}
4145
eg.Go(func() error {
4246
net, err := clnode.NewNetworkCfgOneNetworkAllNodes(bcOut)
4347
if err != nil {
@@ -49,7 +53,7 @@ func oneNodeSharedDBConfiguration(in *Input, bcOut *blockchain.Output, fakeUrl s
4953
DbInput: in.NodeSpecs[overrideIdx].DbInput,
5054
Node: &clnode.NodeInput{
5155
Image: in.NodeSpecs[overrideIdx].Node.Image,
52-
Name: fmt.Sprintf("node%d", i),
56+
Name: nodeName,
5357
PullImage: in.NodeSpecs[overrideIdx].Node.PullImage,
5458
CapabilitiesBinaryPaths: in.NodeSpecs[overrideIdx].Node.CapabilitiesBinaryPaths,
5559
CapabilityContainerDir: in.NodeSpecs[overrideIdx].Node.CapabilityContainerDir,
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package simple_node_set_test
2+
3+
import (
4+
"github.com/smartcontractkit/chainlink-testing-framework/framework"
5+
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain"
6+
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/clnode"
7+
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres"
8+
ns "github.com/smartcontractkit/chainlink-testing-framework/framework/components/simple_node_set"
9+
"github.com/stretchr/testify/require"
10+
"sync"
11+
"testing"
12+
)
13+
14+
type testCase struct {
15+
name string
16+
fakeURL string
17+
funding float64
18+
bcInput *blockchain.Input
19+
nodeSetInput *ns.Input
20+
assertion func(t *testing.T, output *ns.Output)
21+
}
22+
23+
func checkBasicOutputs(t *testing.T, output *ns.Output) {
24+
require.NotNil(t, output)
25+
require.NotNil(t, output.CLNodes)
26+
require.Len(t, output.CLNodes, 2)
27+
require.Contains(t, output.CLNodes[0].PostgreSQL.Url, "postgresql://chainlink:[email protected]")
28+
require.Contains(t, output.CLNodes[0].PostgreSQL.DockerInternalURL, "postgresql://chainlink:thispasswordislongenough@postgresql-")
29+
require.Contains(t, output.CLNodes[0].Node.HostURL, "127.0.0.1")
30+
require.Contains(t, output.CLNodes[0].Node.DockerURL, "node")
31+
require.Contains(t, output.CLNodes[0].Node.DockerP2PUrl, "node")
32+
33+
require.Contains(t, output.CLNodes[1].PostgreSQL.Url, "postgresql://chainlink:[email protected]")
34+
require.Contains(t, output.CLNodes[1].PostgreSQL.DockerInternalURL, "postgresql://chainlink:thispasswordislongenough@postgresql-")
35+
require.Contains(t, output.CLNodes[1].Node.HostURL, "127.0.0.1")
36+
require.Contains(t, output.CLNodes[1].Node.DockerURL, "node")
37+
require.Contains(t, output.CLNodes[1].Node.DockerP2PUrl, "node")
38+
}
39+
40+
func TestNodeSetSharedDB(t *testing.T) {
41+
testCases := []testCase{
42+
{
43+
name: "2 nodes cluster, override mode 'all'",
44+
fakeURL: "http://example.com",
45+
bcInput: &blockchain.Input{
46+
Type: "anvil",
47+
Image: "f4hrenh9it/foundry",
48+
PullImage: true,
49+
Port: "8545",
50+
ChainID: "31337",
51+
},
52+
nodeSetInput: &ns.Input{
53+
Nodes: 2,
54+
OverrideMode: "all",
55+
NodeSpecs: []*clnode.Input{
56+
{
57+
DataProviderURL: "http://example.com",
58+
DbInput: &postgres.Input{
59+
Image: "postgres:15.6",
60+
PullImage: true,
61+
},
62+
Node: &clnode.NodeInput{
63+
Image: "public.ecr.aws/chainlink/chainlink:v2.17.0",
64+
Name: "cl-node",
65+
PullImage: true,
66+
},
67+
},
68+
},
69+
},
70+
assertion: func(t *testing.T, output *ns.Output) {
71+
checkBasicOutputs(t, output)
72+
},
73+
},
74+
{
75+
name: "2 nodes cluster, override mode 'each'",
76+
fakeURL: "http://example.com",
77+
bcInput: &blockchain.Input{
78+
Type: "anvil",
79+
Image: "f4hrenh9it/foundry",
80+
PullImage: true,
81+
Port: "8545",
82+
ChainID: "31337",
83+
},
84+
nodeSetInput: &ns.Input{
85+
Nodes: 2,
86+
OverrideMode: "each",
87+
NodeSpecs: []*clnode.Input{
88+
{
89+
DataProviderURL: "http://example.com",
90+
DbInput: &postgres.Input{
91+
Image: "postgres:15.6",
92+
PullImage: true,
93+
},
94+
Node: &clnode.NodeInput{
95+
Image: "public.ecr.aws/chainlink/chainlink:v2.17.0",
96+
Name: "cl-node-1",
97+
PullImage: true,
98+
UserConfigOverrides: `
99+
[Log]
100+
level = 'info'
101+
`,
102+
},
103+
},
104+
{
105+
DataProviderURL: "http://example.com",
106+
DbInput: &postgres.Input{
107+
Image: "postgres:15.6",
108+
PullImage: true,
109+
},
110+
Node: &clnode.NodeInput{
111+
Image: "public.ecr.aws/chainlink/chainlink:v2.17.0",
112+
Name: "cl-node-2",
113+
PullImage: true,
114+
UserConfigOverrides: `
115+
[Log]
116+
level = 'info'
117+
`,
118+
},
119+
},
120+
},
121+
},
122+
assertion: func(t *testing.T, output *ns.Output) {
123+
checkBasicOutputs(t, output)
124+
},
125+
},
126+
}
127+
128+
for _, tc := range testCases {
129+
err := framework.DefaultNetwork(t, &sync.Once{})
130+
require.NoError(t, err)
131+
132+
t.Run(tc.name, func(t *testing.T) {
133+
bc, err := blockchain.NewBlockchainNetwork(tc.bcInput)
134+
require.NoError(t, err)
135+
output, err := ns.NewSharedDBNodeSet(tc.nodeSetInput, bc, tc.fakeURL)
136+
require.NoError(t, err)
137+
tc.assertion(t, output)
138+
})
139+
}
140+
}

framework/config.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"os"
1616
"path/filepath"
1717
"strings"
18+
"sync"
1819
"testing"
1920
"text/template"
2021
"time"
@@ -39,6 +40,7 @@ const (
3940
)
4041

4142
var (
43+
once = &sync.Once{}
4244
// Secrets is a singleton AWS Secrets Manager
4345
// Loaded once on start inside Load and is safe to call concurrently
4446
Secrets *AWSSecretsManager
@@ -136,21 +138,29 @@ func Load[X any](t *testing.T) (*X, error) {
136138
// return nil, fmt.Errorf("failed to connect AWSSecretsManager: %w", err)
137139
// }
138140
//}
139-
net, err := network.New(
140-
context.Background(),
141-
network.WithLabels(map[string]string{"framework": "ctf"}),
142-
)
141+
err = DefaultNetwork(t, once)
143142
if err != nil {
144143
return input, err
145144
}
146-
DefaultNetworkName = net.Name
147145
if os.Getenv(EnvVarLokiStream) == "true" {
148146
err = NewLokiStreamer()
149147
require.NoError(t, err)
150148
}
151149
return input, nil
152150
}
153151

152+
func DefaultNetwork(t *testing.T, once *sync.Once) error {
153+
once.Do(func() {
154+
net, err := network.New(
155+
context.Background(),
156+
network.WithLabels(map[string]string{"framework": "ctf"}),
157+
)
158+
require.NoError(t, err)
159+
DefaultNetworkName = net.Name
160+
})
161+
return nil
162+
}
163+
154164
func RenderTemplate(tmpl string, data interface{}) (string, error) {
155165
var buf bytes.Buffer
156166
err := template.Must(template.New("tmpl").Parse(tmpl)).Execute(&buf, data)

0 commit comments

Comments
 (0)