Skip to content

Commit 4bcfe72

Browse files
committed
fix anvil miner and add docs
1 parent 279fb34 commit 4bcfe72

File tree

15 files changed

+146
-25
lines changed

15 files changed

+146
-25
lines changed

.github/workflows/framework-golden-tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ jobs:
3838
config: scalability.toml
3939
count: 1
4040
timeout: 10m
41+
- name: TestFakes
42+
config: fake.toml
43+
count: 1
44+
timeout: 10m
4145
name: ${{ matrix.test.name }}
4246
steps:
4347
- name: Checkout repo

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [NodeSet (Local Docker builds)](./framework/nodeset_docker_rebuild.md)
1212
- [NodeSet Compat Environment](./framework/nodeset_compatibility.md)
1313
- [Fork Testing](./framework/fork.md)
14+
- [Quick Contracts Deployment](./framework/quick_deployment.md)
1415
- [NodeSet with External Blockchain]()
1516
- [CLI](./framework/cli.md)
1617
- [Configuration](./framework/configuration.md)

book/src/framework/components/mocking.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ The framework aims to equip you with all the necessary tools to write end-to-end
1212
## Usage
1313

1414
See [full](https://github.com/smartcontractkit/chainlink-testing-framework/blob/main/framework/examples/myproject/fake_test.go) example.
15+
16+
<div class="warning">
17+
18+
`host.docker.internal` is docker platform dependent!
19+
20+
Use `framework.HostDockerInternal()` to reference `host.docker.internal` in your tests, so they can work in GHA CI
21+
</div>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Quick Contracts Deployment
2+
3+
You can control the mining pace to accelerate contract deployment. Start anvil with the following configuration:
4+
5+
```toml
6+
[blockchain_a]
7+
chain_id = "31337"
8+
image = "f4hrenh9it/foundry:latest"
9+
port = "8545"
10+
type = "anvil"
11+
```
12+
Set the `miner` speed,
13+
```golang
14+
// start periodic mining so nodes can receive heads (async)
15+
miner := rpc.NewRemoteAnvilMiner(bcSrc.Nodes[0].HostHTTPUrl, nil)
16+
miner.MinePeriodically(5 * time.Second)
17+
```
18+
19+
Then use this [template](https://github.com/smartcontractkit/chainlink-testing-framework/blob/main/framework/examples/myproject/quick_deploy_test.go)

framework/.changeset/v0.2.10.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Switch to "anvil_mine" instead of "evm_mine"
2+
- Test that CL nodes can see block heads after we switch the mining pace
3+
- Quick deployments example (docs)

framework/ci.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package framework
2+
3+
import (
4+
"os"
5+
)
6+
7+
// HostDockerInternal returns host.docker.internal that works both locally and in GHA
8+
func HostDockerInternal() string {
9+
if os.Getenv(EnvVarCI) == "true" {
10+
return "http://172.17.0.1"
11+
} else {
12+
return "http://host.docker.internal"
13+
}
14+
}

framework/components/fake/fake.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"github.com/gin-gonic/gin"
66
"github.com/smartcontractkit/chainlink-testing-framework/framework"
7-
"os"
87
"regexp"
98
)
109

@@ -67,12 +66,8 @@ func NewFakeDataProvider(in *Input) (*Output, error) {
6766
_ = Service.Run(fmt.Sprintf(":%d", in.Port))
6867
}()
6968
out := &Output{
70-
BaseURLHost: fmt.Sprintf("http://localhost:%d", in.Port),
71-
}
72-
if os.Getenv(framework.EnvVarCI) == "true" {
73-
out.BaseURLDocker = fmt.Sprintf("http://172.17.0.1:%d", in.Port)
74-
} else {
75-
out.BaseURLDocker = fmt.Sprintf("http://host.docker.internal:%d", in.Port)
69+
BaseURLHost: fmt.Sprintf("http://localhost:%d", in.Port),
70+
BaseURLDocker: fmt.Sprintf("%s:%d", framework.HostDockerInternal(), in.Port),
7671
}
7772
in.Out = out
7873
return out, nil

framework/docker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func runCommand(name string, args ...string) error {
123123
return cmd.Run()
124124
}
125125

126+
// TODO: use tc.NewDockerProvider().BuildImage() to skip managing the registry container
126127
// RebuildDockerImage rebuilds docker image if necessary
127128
func RebuildDockerImage(once *sync.Once, dockerfile string, buildContext string, imageName string) (string, error) {
128129
if dockerfile == "" {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package components
2+
3+
import (
4+
"bufio"
5+
"context"
6+
"fmt"
7+
"github.com/smartcontractkit/chainlink-testing-framework/framework"
8+
"github.com/testcontainers/testcontainers-go"
9+
"github.com/testcontainers/testcontainers-go/wait"
10+
)
11+
12+
// NewDockerFakeTester is a small utility to test how docker host resolves in different CI environments
13+
// it just prints logs of curl in CI when calling some URL
14+
// it is temporary and will be removed in the future
15+
func NewDockerFakeTester(url string) error {
16+
ctx := context.Background()
17+
req := testcontainers.ContainerRequest{
18+
Image: "curlimages/curl:latest",
19+
Cmd: []string{"curl", "-v", url},
20+
Labels: framework.DefaultTCLabels(),
21+
WaitingFor: wait.ForExit(),
22+
}
23+
curlContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
24+
ContainerRequest: req,
25+
Started: true,
26+
})
27+
if err != nil {
28+
return err
29+
}
30+
logs, err := curlContainer.Logs(ctx)
31+
if err != nil {
32+
return err
33+
}
34+
scanner := bufio.NewScanner(logs)
35+
fmt.Println("Container logs:")
36+
for scanner.Scan() {
37+
fmt.Println(scanner.Text())
38+
}
39+
if err := scanner.Err(); err != nil {
40+
return fmt.Errorf("error reading container logs: %w", err)
41+
}
42+
return nil
43+
}

framework/examples/myproject/fake_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain"
88
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/fake"
99
ns "github.com/smartcontractkit/chainlink-testing-framework/framework/components/simple_node_set"
10+
components "github.com/smartcontractkit/chainlink-testing-framework/framework/examples/example_components"
1011
"github.com/stretchr/testify/require"
1112
"testing"
1213
)
@@ -68,4 +69,19 @@ func TestFakes(t *testing.T) {
6869
// use docker URL and path of your fake
6970
_ = fmt.Sprintf("%s%s", fakeOut.BaseURLDocker, myFakeAPI)
7071
})
72+
73+
t.Run("verify that containers can access internally both locally and in CI", func(t *testing.T) {
74+
myFakeAPI := "/fake/api/internal"
75+
// use fake.Func if you need full control over response
76+
err = fake.JSON(
77+
"GET",
78+
myFakeAPI,
79+
map[string]any{
80+
"data": "some_data",
81+
}, 200,
82+
)
83+
require.NoError(t, err)
84+
err := components.NewDockerFakeTester(fmt.Sprintf("%s%s", fakeOut.BaseURLDocker, myFakeAPI))
85+
require.NoError(t, err)
86+
})
7187
}

0 commit comments

Comments
 (0)