Skip to content

Commit c3de192

Browse files
committed
db mount
1 parent aa81473 commit c3de192

File tree

7 files changed

+127
-18
lines changed

7 files changed

+127
-18
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [Test Configuration](./framework/test_configuration_overrides.md)
1717
- [Caching](framework/components/caching.md)
1818
- [Secrets]()
19+
- [Docker](framework/docker.md)
1920
- [Observability Stack](framework/observability/observability_stack.md)
2021
- [Metrics](framework/observability/metrics.md)
2122
- [Logs](framework/observability/logs.md)

book/src/framework/docker.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Docker
2+
3+
We are not removing volumes and images when you are working locally to allow you to debug, however, to clean up some space use:
4+
```
5+
docker volume prune -f
6+
docker system prune -f
7+
```

framework/cmd/docker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ func cleanDockerResources() error {
1414
// Bash command for removing Docker containers and networks with "framework=ctf" label
1515
cmd := exec.Command("bash", "-c", `
1616
docker ps -aq --filter "label=framework=ctf" | xargs -r docker rm -f && \
17-
docker network ls --filter "label=framework=ctf" -q | xargs -r docker network rm
17+
docker network ls --filter "label=framework=ctf" -q | xargs -r docker network rm && \
18+
docker volume rm postgresql_data
1819
`)
1920
framework.L.Debug().Msg("Running command")
2021
if framework.L.GetLevel() == zerolog.DebugLevel {

framework/components/postgres/postgres.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import (
44
"context"
55
"fmt"
66
"github.com/docker/docker/api/types/container"
7-
"github.com/docker/docker/api/types/mount"
87
"github.com/docker/go-connections/nat"
98
"github.com/smartcontractkit/chainlink-testing-framework/framework"
109
"github.com/testcontainers/testcontainers-go"
1110
tcwait "github.com/testcontainers/testcontainers-go/wait"
1211
"os"
13-
"path/filepath"
1412
"strings"
1513
"time"
1614
)
@@ -84,17 +82,19 @@ func NewPostgreSQL(in *Input) (*Output, error) {
8482
FileMode: 0644,
8583
},
8684
},
85+
Mounts: testcontainers.ContainerMounts{
86+
{
87+
Source: testcontainers.GenericVolumeMountSource{
88+
Name: DatabaseDir,
89+
},
90+
Target: "/var/lib/postgresql/data",
91+
},
92+
},
8793
WaitingFor: tcwait.ForExec([]string{"psql", "-h", "127.0.0.1",
8894
"-U", User, "-p", Port, "-c", "select", "1", "-d", Database}).
8995
WithStartupTimeout(20 * time.Second).
9096
WithPollInterval(1 * time.Second),
9197
}
92-
wd, err := os.Getwd()
93-
if err != nil {
94-
return nil, err
95-
}
96-
_ = os.RemoveAll(filepath.Join(wd, DatabaseDir))
97-
_ = os.Mkdir(DatabaseDir, os.ModePerm)
9898
req.HostConfigModifier = func(h *container.HostConfig) {
9999
h.PortBindings = nat.PortMap{
100100
nat.Port(bindPort): []nat.PortBinding{
@@ -104,17 +104,11 @@ func NewPostgreSQL(in *Input) (*Output, error) {
104104
},
105105
},
106106
}
107-
h.Mounts = []mount.Mount{
108-
{
109-
Type: mount.TypeBind,
110-
Source: filepath.Join(wd, DatabaseDir),
111-
Target: "/var/lib/postgresql/data",
112-
},
113-
}
114107
}
115108
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
116109
ContainerRequest: req,
117110
Started: true,
111+
Reuse: true,
118112
})
119113
if err != nil {
120114
return nil, err

framework/components/simple_node_set/node_set.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,18 @@ func NewSharedDBNodeSet(in *Input, bcOut *blockchain.Output, fakeUrl string) (*O
6666
}
6767

6868
func printURLs(out *Output) {
69-
httpURLs, p2pURLs := make([]string, 0), make([]string, 0)
69+
if out == nil {
70+
return
71+
}
72+
httpURLs, p2pURLs, pgURLs := make([]string, 0), make([]string, 0), make([]string, 0)
7073
for _, n := range out.CLNodes {
7174
httpURLs = append(httpURLs, n.Node.HostURL)
7275
p2pURLs = append(p2pURLs, n.Node.HostP2PURL)
76+
pgURLs = append(pgURLs, n.PostgreSQL.Url)
7377
}
7478
framework.L.Info().Any("UI", httpURLs).Send()
7579
framework.L.Debug().Any("P2P", p2pURLs).Send()
80+
framework.L.Debug().Any("DB", pgURLs).Send()
7681
}
7782

7883
func sharedDBSetup(in *Input, bcOut *blockchain.Output, fakeUrl string, overrideEach bool) (*Output, error) {
@@ -137,9 +142,10 @@ func sharedDBSetup(in *Input, bcOut *blockchain.Output, fakeUrl string, override
137142
},
138143
}
139144

145+
dbURLHost := strings.Replace(dbOut.Url, "/chainlink?sslmode=disable", fmt.Sprintf("/db_%d?sslmode=disable", i), -1)
140146
dbURL := strings.Replace(dbOut.DockerInternalURL, "/chainlink?sslmode=disable", fmt.Sprintf("/db_%d?sslmode=disable", i), -1)
141147
dbSpec := &postgres.Output{
142-
Url: dbOut.Url,
148+
Url: dbURLHost,
143149
DockerInternalURL: dbURL,
144150
}
145151

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
[blockchain_a]
3+
chain_id = "31337"
4+
image = "f4hrenh9it/foundry:latest"
5+
port = "8545"
6+
type = "anvil"
7+
8+
[data_provider]
9+
port = 9111
10+
11+
[nodeset]
12+
nodes = 5
13+
override_mode = "all"
14+
15+
[[nodeset.node_specs]]
16+
17+
[nodeset.node_specs.db]
18+
image = "postgres:15.6"
19+
pull_image = true
20+
21+
[nodeset.node_specs.node]
22+
image = "public.ecr.aws/chainlink/chainlink:v2.17.0"
23+
pull_image = false
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package examples
2+
3+
import (
4+
"fmt"
5+
"github.com/smartcontractkit/chainlink-testing-framework/framework"
6+
"github.com/smartcontractkit/chainlink-testing-framework/framework/chaos"
7+
"github.com/smartcontractkit/chainlink-testing-framework/framework/clclient"
8+
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain"
9+
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/fake"
10+
ns "github.com/smartcontractkit/chainlink-testing-framework/framework/components/simple_node_set"
11+
"github.com/stretchr/testify/require"
12+
"testing"
13+
"time"
14+
)
15+
16+
type CfgReload struct {
17+
BlockchainA *blockchain.Input `toml:"blockchain_a" validate:"required"`
18+
MockerDataProvider *fake.Input `toml:"data_provider" validate:"required"`
19+
NodeSet *ns.Input `toml:"nodeset" validate:"required"`
20+
}
21+
22+
func TestReload(t *testing.T) {
23+
in, err := framework.Load[CfgReload](t)
24+
require.NoError(t, err)
25+
26+
bc, err := blockchain.NewBlockchainNetwork(in.BlockchainA)
27+
require.NoError(t, err)
28+
dp, err := fake.NewFakeDataProvider(in.MockerDataProvider)
29+
require.NoError(t, err)
30+
31+
// deploy first time
32+
out, err := ns.NewSharedDBNodeSet(in.NodeSet, bc, dp.BaseURLDocker)
33+
require.NoError(t, err)
34+
35+
c, err := clclient.NewCLDefaultClients(out.CLNodes, framework.L)
36+
require.NoError(t, err)
37+
_, _, err = c[0].CreateJobRaw(`
38+
type = "cron"
39+
schemaVersion = 1
40+
schedule = "CRON_TZ=UTC */10 * * * * *" # every 10 secs
41+
observationSource = """
42+
// data source 2
43+
fetch [type=http method=GET url="https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD"];
44+
parse [type=jsonparse path="RAW,ETH,USD,PRICE"];
45+
multiply [type="multiply" input="$(parse)" times=100]
46+
encode_tx [type="ethabiencode"
47+
abi="submit(uint256 value)"
48+
data="{ \\"value\\": $(multiply) }"]
49+
submit_tx [type="ethtx" to="0x859AAa51961284C94d970B47E82b8771942F1980" data="$(encode_tx)"]
50+
51+
fetch -> parse -> multiply -> encode_tx -> submit_tx
52+
"""`)
53+
require.NoError(t, err)
54+
time.Sleep(20 * time.Second)
55+
56+
// deploy second time
57+
_, err = chaos.ExecPumba("rm --volumes=false re2:node.*|postgresql.*")
58+
require.NoError(t, err)
59+
60+
in.NodeSet.NodeSpecs[0].Node.UserConfigOverrides = in.NodeSet.NodeSpecs[0].Node.UserConfigOverrides + `
61+
[Log]
62+
Level = 'info'
63+
`
64+
in.NodeSet.Out = nil
65+
out, err = ns.NewSharedDBNodeSet(in.NodeSet, bc, dp.BaseURLDocker)
66+
require.NoError(t, err)
67+
jobs, _, err := c[0].ReadJobs()
68+
require.NoError(t, err)
69+
fmt.Println(jobs)
70+
71+
t.Run("test something", func(t *testing.T) {
72+
for _, n := range out.CLNodes {
73+
require.NotEmpty(t, n.Node.HostURL)
74+
require.NotEmpty(t, n.Node.HostP2PURL)
75+
}
76+
})
77+
}

0 commit comments

Comments
 (0)