Skip to content

Commit 907496b

Browse files
committed
JD fork test example
1 parent 2943192 commit 907496b

File tree

10 files changed

+100
-83
lines changed

10 files changed

+100
-83
lines changed

framework/components/blockchain/aptos.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,18 @@ func newAptos(in *Input) (*Output, error) {
103103
if err != nil {
104104
return nil, err
105105
}
106+
107+
dc, err := framework.NewDockerClient()
108+
if err != nil {
109+
return nil, err
110+
}
106111
cmdStr := []string{"aptos", "init", "--network=local", "--assume-yes", fmt.Sprintf("--private-key=%s", DefaultAptosPrivateKey)}
107-
_, err = framework.ExecContainer(containerName, cmdStr)
112+
_, err = dc.ExecContainer(containerName, cmdStr)
108113
if err != nil {
109114
return nil, err
110115
}
111116
fundCmd := []string{"aptos", "account", "fund-with-faucet", "--account", DefaultAptosAccount, "--amount", "1000000000000"}
112-
_, err = framework.ExecContainer(containerName, fundCmd)
117+
_, err = dc.ExecContainer(containerName, fundCmd)
113118
if err != nil {
114119
return nil, err
115120
}

framework/components/blockchain/sui.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ func fundAccount(url string, address string) error {
5555
// generateKeyData generates a wallet and returns all the data
5656
func generateKeyData(containerName string, keyCipherType string) (*SuiWalletInfo, error) {
5757
cmdStr := []string{"sui", "keytool", "generate", keyCipherType, "--json"}
58-
keyOut, err := framework.ExecContainer(containerName, cmdStr)
58+
dc, err := framework.NewDockerClient()
59+
if err != nil {
60+
return nil, err
61+
}
62+
keyOut, err := dc.ExecContainer(containerName, cmdStr)
5963
if err != nil {
6064
return nil, err
6165
}

framework/components/jd/jd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type Input struct {
3434

3535
type Output struct {
3636
UseCache bool `toml:"use_cache"`
37+
ContainerName string `toml:"container_name"`
38+
DBContainerName string `toml:"db_container_name"`
3739
ExternalGRPCUrl string `toml:"grpc_url"`
3840
InternalGRPCUrl string `toml:"internal_grpc_url"`
3941
ExternalWSRPCUrl string `toml:"wsrpc_url"`
@@ -123,6 +125,8 @@ func NewJD(in *Input) (*Output, error) {
123125
}
124126
out := &Output{
125127
UseCache: true,
128+
ContainerName: containerName,
129+
DBContainerName: pgOut.ContainerName,
126130
ExternalGRPCUrl: fmt.Sprintf("%s:%s", host, in.GRPCPort),
127131
InternalGRPCUrl: fmt.Sprintf("%s:%s", containerName, in.GRPCPort),
128132
ExternalWSRPCUrl: fmt.Sprintf("%s:%s", host, in.WSRPCPort),

framework/docker.go

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,51 @@ func NewDockerClient() (*DockerClient, error) {
112112
return &DockerClient{cli: cli}, nil
113113
}
114114

115+
// ExecContainer executes a command inside a running container by name and returns the combined stdout/stderr.
116+
func (dc *DockerClient) ExecContainer(containerName string, command []string) (string, error) {
117+
L.Info().Strs("Command", command).Str("ContainerName", containerName).Msg("Executing command")
118+
ctx := context.Background()
119+
containers, err := dc.cli.ContainerList(ctx, container.ListOptions{
120+
All: true,
121+
})
122+
if err != nil {
123+
return "", fmt.Errorf("failed to list containers: %w", err)
124+
}
125+
var containerID string
126+
for _, cont := range containers {
127+
for _, name := range cont.Names {
128+
if name == "/"+containerName {
129+
containerID = cont.ID
130+
break
131+
}
132+
}
133+
}
134+
if containerID == "" {
135+
return "", fmt.Errorf("container with name '%s' not found", containerName)
136+
}
137+
138+
execConfig := container.ExecOptions{
139+
Cmd: command,
140+
AttachStdout: true,
141+
AttachStderr: true,
142+
}
143+
execID, err := dc.cli.ContainerExecCreate(ctx, containerID, execConfig)
144+
if err != nil {
145+
return "", fmt.Errorf("failed to create exec instance: %w", err)
146+
}
147+
resp, err := dc.cli.ContainerExecAttach(ctx, execID.ID, container.ExecStartOptions{})
148+
if err != nil {
149+
return "", fmt.Errorf("failed to attach to exec instance: %w", err)
150+
}
151+
defer resp.Close()
152+
output, err := io.ReadAll(resp.Reader)
153+
if err != nil {
154+
return "", fmt.Errorf("failed to read exec output: %w", err)
155+
}
156+
L.Info().Str("Output", string(output)).Msg("Command output")
157+
return string(output), nil
158+
}
159+
115160
// CopyFile copies a file into a container by name
116161
func (dc *DockerClient) CopyFile(containerName, sourceFile, targetPath string) error {
117162
ctx := context.Background()
@@ -340,55 +385,6 @@ func RemoveTestContainers() error {
340385
return nil
341386
}
342387

343-
// ExecContainer executes a command inside a running container by name and returns the combined stdout/stderr.
344-
func ExecContainer(containerName string, command []string) (string, error) {
345-
L.Info().Strs("Command", command).Str("ContainerName", containerName).Msg("Executing command")
346-
p, err := tc.NewDockerProvider()
347-
if err != nil {
348-
return "", err
349-
}
350-
ctx := context.Background()
351-
containers, err := p.Client().ContainerList(ctx, container.ListOptions{
352-
All: true,
353-
})
354-
if err != nil {
355-
return "", fmt.Errorf("failed to list containers: %w", err)
356-
}
357-
var containerID string
358-
for _, cont := range containers {
359-
for _, name := range cont.Names {
360-
if name == "/"+containerName {
361-
containerID = cont.ID
362-
break
363-
}
364-
}
365-
}
366-
if containerID == "" {
367-
return "", fmt.Errorf("container with name '%s' not found", containerName)
368-
}
369-
370-
execConfig := container.ExecOptions{
371-
Cmd: command,
372-
AttachStdout: true,
373-
AttachStderr: true,
374-
}
375-
execID, err := p.Client().ContainerExecCreate(ctx, containerID, execConfig)
376-
if err != nil {
377-
return "", fmt.Errorf("failed to create exec instance: %w", err)
378-
}
379-
resp, err := p.Client().ContainerExecAttach(ctx, execID.ID, container.ExecStartOptions{})
380-
if err != nil {
381-
return "", fmt.Errorf("failed to attach to exec instance: %w", err)
382-
}
383-
defer resp.Close()
384-
output, err := io.ReadAll(resp.Reader)
385-
if err != nil {
386-
return "", fmt.Errorf("failed to read exec output: %w", err)
387-
}
388-
L.Info().Str("Output", string(output)).Msg("Command output")
389-
return string(output), nil
390-
}
391-
392388
type ContainerResources struct {
393389
CPUs float64 `toml:"cpus" validate:"gte=0"`
394390
MemoryMb uint `toml:"memory_mb"`

framework/examples/myproject/smoke_aptos_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ func TestAptosSmoke(t *testing.T) {
2626
_ = blockchain.DefaultAptosAccount
2727
_ = blockchain.DefaultAptosPrivateKey
2828

29-
_, err = framework.ExecContainer(bc.ContainerName, []string{"ls", "-lah"})
29+
dc, err := framework.NewDockerClient()
30+
require.NoError(t, err)
31+
32+
_, err = dc.ExecContainer(bc.ContainerName, []string{"ls", "-lah"})
3033
require.NoError(t, err)
3134

3235
t.Run("test something", func(t *testing.T) {

framework/examples/myproject/smoke_sui_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ func TestSuiSmoke(t *testing.T) {
3636
_ = bc.NetworkSpecificData.SuiAccount.SuiAddress
3737

3838
// execute any additional commands, to deploy contracts or set up
39-
_, err = framework.ExecContainer(bc.ContainerName, []string{"ls", "-lah"})
39+
dc, err := framework.NewDockerClient()
40+
require.NoError(t, err)
41+
_, err = dc.ExecContainer(bc.ContainerName, []string{"ls", "-lah"})
4042
require.NoError(t, err)
4143

4244
_, err = fake.NewFakeDataProvider(in.MockerDataProvider)

framework/examples/myproject_cll/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
These examples require either internal images or access to our repositories.
44

55

6-
## Developing
6+
## Job Distributor
7+
Checkout Job Distributor repository and build an image
8+
```
9+
docker build -t job-distributor:0.9.0 -f e2e/Dockerfile.e2e .
10+
```
11+
712
Run the tests locally
813
```
9-
CTF_CONFIGS=jd.toml go test -v -run TestJD
14+
CTF_CONFIGS=jd.toml go test -v -count 1 -run TestJD
1015
```
889 Bytes
Binary file not shown.
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
2-
[blockchain_a]
1+
[blockchain]
32
type = "anvil"
4-
docker_cmd_params = ["-b", "1"]
3+
chain_id = "1337"
4+
docker_cmd_params = ["--fork-url", "wss://avalanche-fuji-c-chain-rpc.publicnode.com", "--auto-impersonate", "-b", "1"]
55

66
[jd]
7-
8-
[[nodesets]]
9-
nodes = 5
10-
override_mode = "all"
11-
12-
[nodesets.db]
13-
image = "postgres:12.0"
14-
15-
[[nodesets.node_specs]]
16-
17-
[nodesets.node_specs.node]
18-
image = "public.ecr.aws/chainlink/chainlink:v2.17.0"
7+
image = "job-distributor:0.9.0"

framework/examples/myproject_cll/jd_test.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,38 @@ import (
88
"github.com/smartcontractkit/chainlink-testing-framework/framework"
99
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain"
1010
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/jd"
11-
ns "github.com/smartcontractkit/chainlink-testing-framework/framework/components/simple_node_set"
1211
)
1312

1413
type CfgJD struct {
15-
BlockchainA *blockchain.Input `toml:"blockchain_a" validate:"required"`
16-
JD *jd.Input `toml:"jd" validate:"required"`
17-
NodeSet *ns.Input `toml:"nodeset" validate:"required"`
14+
Blockchain *blockchain.Input `toml:"blockchain" validate:"required"`
15+
JD *jd.Input `toml:"jd" validate:"required"`
1816
}
1917

20-
func TestJDAndNodeSet(t *testing.T) {
18+
func TestJD(t *testing.T) {
2119
in, err := framework.Load[CfgJD](t)
2220
require.NoError(t, err)
2321

24-
bc, err := blockchain.NewBlockchainNetwork(in.BlockchainA)
22+
bcOut, err := blockchain.NewBlockchainNetwork(in.Blockchain)
2523
require.NoError(t, err)
26-
out, err := ns.NewSharedDBNodeSet(in.NodeSet, bc)
24+
25+
jdOut, err := jd.NewJD(in.JD)
26+
require.NoError(t, err)
27+
dc, err := framework.NewDockerClient()
28+
require.NoError(t, err)
29+
// find what to dump, RDS API here instead?
30+
_, err = dc.ExecContainer(jdOut.DBContainerName, []string{"pg_dump", "-U", "chainlink", "-h", "localhost", "-p", "5432", "-d", "chainlink", "-F", "c", "-f", "jd.dump"})
2731
require.NoError(t, err)
28-
_, err = jd.NewJD(in.JD)
32+
33+
// copy your dump
34+
err = dc.CopyFile(jdOut.DBContainerName, "jd.dump", "/")
35+
require.NoError(t, err)
36+
37+
// restore
38+
_, err = dc.ExecContainer(jdOut.DBContainerName, []string{"pg_restore", "-U", "chainlink", "-d", "chainlink", "jd.dump"})
2939
require.NoError(t, err)
3040

31-
t.Run("test something", func(t *testing.T) {
32-
for _, n := range out.CLNodes {
33-
require.NotEmpty(t, n.Node.ExternalURL)
34-
}
41+
t.Run("test changesets with forked network and real JD state", func(t *testing.T) {
42+
_ = bcOut
43+
_ = jdOut
3544
})
3645
}

0 commit comments

Comments
 (0)