Skip to content

Commit a05d532

Browse files
authored
use docker cmd instead of testcontainers-go (#1412)
* use docker cmd instead of testcontainers-go * update docs * remove docker image name * add cl image override * update docs * changeset
1 parent dc944c3 commit a05d532

File tree

8 files changed

+45
-31
lines changed

8 files changed

+45
-31
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [NodeSet with Capabilities](./framework/nodeset_capabilities.md)
1111
- [NodeSet (Local Docker builds)](./framework/nodeset_docker_rebuild.md)
1212
- [NodeSet Compat Environment](./framework/nodeset_compatibility.md)
13+
- [Creating your own components](./developing/developing_components.md)
1314
- [Fork Testing](./framework/fork.md)
1415
- [Quick Contracts Deployment](./framework/quick_deployment.md)
1516
- [NodeSet with External Blockchain]()
@@ -82,8 +83,6 @@
8283

8384
---
8485

85-
- [Developing](developing.md)
86-
- [Components](developing/developing_components.md)
8786
- [Releasing modules](releasing_modules.md)
8887

8988
---

book/src/developing/developing_components.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ Each component can define inputs and outputs, following these rules:
4747
- If your component is used for side effects output can be omitted.
4848
- `input.Out.UseCache` should be added if you'd like to use caching, see more [here](caching)
4949

50-
### Docker components good practices for [testcontainers-go](https://golang.testcontainers.org/):
50+
### Building Local Images
5151

52-
An example [simple component](https://github.com/smartcontractkit/chainlink-testing-framework/blob/main/framework/components/blockchain/anvil.go)
52+
Use `framework.BuildImage` or `framework.BuildImageOnce` to build the docker image.
5353

54-
An example of [complex component](https://github.com/smartcontractkit/chainlink-testing-framework/blob/main/framework/components/clnode/clnode.go)
54+
Do not use `testcontainers.NewDockerProvider()` methods, see issues: [#1](https://github.com/testcontainers/testcontainers-go/pull/2482), [#2](https://github.com/testcontainers/testcontainers-go/issues/1484)
5555

56-
An example of [composite component](https://github.com/smartcontractkit/chainlink-testing-framework/blob/main/framework/components/simple_node_set/node_set.go)
56+
### Docker components good practices for [testcontainers-go](https://golang.testcontainers.org/):
5757

5858
- Inputs should include at least `image`, `tag` and `pull_image` field
5959
```golang
@@ -94,3 +94,10 @@ An example of [composite component](https://github.com/smartcontractkit/chainlin
9494
HostURL: fmt.Sprintf("http://%s:%s", host, mp.Port()),
9595
}, nil
9696
```
97+
98+
An example [simple component](https://github.com/smartcontractkit/chainlink-testing-framework/blob/main/framework/components/blockchain/anvil.go)
99+
100+
An example of [complex component](https://github.com/smartcontractkit/chainlink-testing-framework/blob/main/framework/components/clnode/clnode.go)
101+
102+
An example of [composite component](https://github.com/smartcontractkit/chainlink-testing-framework/blob/main/framework/components/simple_node_set/node_set.go)
103+

book/src/framework/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
| LOKI_BASIC_AUTH | Basic auth in format $user:$password | `$user:$password` | - | 🚫 |
1313
| RESTY_DEBUG | Log all Resty client HTTP calls | `true`, `false` | `false` | 🚫 |
1414
| CTF_IGNORE_CRITICAL_LOGS | Ignore all logs that has CRIT,FATAL or PANIC levels (Chainlink nodes only!) | `true`, `false` | `false` | 🚫 |
15+
| CHAINLINK_IMAGE | Flag to override Chainlink Docker image in format $repository:$tag | $repository:$tag | - | 🚫 |

book/src/framework/nodeset_docker_rebuild.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ Create a configuration file `smoke.toml`
2525

2626
These paths will work for `e2e/capabilities` in our main [repository](https://github.com/smartcontractkit/chainlink/tree/ctf-v2-tests/e2e/capabilities)
2727

28+
Also check how you can add rebuild to your [components](../developing/developing_components.md#building-local-images).
29+
2830
Summary:
2931
- We learned how we can quickly re-build local docker image for CL node

framework/.changeset/v0.3.1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Use docker cmd instead of testcontainers
2+
- Add "CHAINLINK_IMAGE" flag to override NodeSet image

framework/components/clnode/clnode.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ type NodeInput struct {
4141
Name string `toml:"name"`
4242
DockerFilePath string `toml:"docker_file"`
4343
DockerContext string `toml:"docker_ctx"`
44-
DockerImageName string `toml:"docker_image_name"`
4544
PullImage bool `toml:"pull_image"`
4645
CapabilitiesBinaryPaths []string `toml:"capabilities"`
4746
CapabilityContainerDir string `toml:"capabilities_container_dir"`
@@ -259,7 +258,7 @@ func newNode(in *Input, pgOut *postgres.Output) (*NodeOut, error) {
259258
}
260259
if req.Image == "" {
261260
req.Image = TmpImageName
262-
if err := framework.BuildImageOnce(ctx, once, in.Node.DockerContext, in.Node.DockerFilePath, req.Image); err != nil {
261+
if err := framework.BuildImageOnce(once, in.Node.DockerContext, in.Node.DockerFilePath, req.Image); err != nil {
263262
return nil, err
264263
}
265264
req.KeepImage = false

framework/components/simple_node_set/node_set.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/clnode"
88
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres"
99
"golang.org/x/sync/errgroup"
10+
"os"
1011
"slices"
1112
"strings"
1213
"sync"
@@ -79,6 +80,8 @@ func sharedDBSetup(in *Input, bcOut *blockchain.Output) (*Output, error) {
7980
}
8081
nodeOuts := make([]*clnode.Output, 0)
8182

83+
envImage := os.Getenv("CHAINLINK_IMAGE")
84+
8285
// to make it easier for chaos testing we use static ports
8386
// there is no need to check them in advance since testcontainers-go returns a nice error
8487
var (
@@ -131,7 +134,6 @@ func sharedDBSetup(in *Input, bcOut *blockchain.Output) (*Output, error) {
131134
PullImage: in.NodeSpecs[overrideIdx].Node.PullImage,
132135
DockerFilePath: in.NodeSpecs[overrideIdx].Node.DockerFilePath,
133136
DockerContext: in.NodeSpecs[overrideIdx].Node.DockerContext,
134-
DockerImageName: in.NodeSpecs[overrideIdx].Node.DockerImageName,
135137
CapabilitiesBinaryPaths: in.NodeSpecs[overrideIdx].Node.CapabilitiesBinaryPaths,
136138
CapabilityContainerDir: in.NodeSpecs[overrideIdx].Node.CapabilityContainerDir,
137139
TestConfigOverrides: net,
@@ -141,6 +143,10 @@ func sharedDBSetup(in *Input, bcOut *blockchain.Output) (*Output, error) {
141143
},
142144
}
143145

146+
if envImage != "" {
147+
nodeSpec.Node.Image = envImage
148+
}
149+
144150
dbURLHost := strings.Replace(dbOut.Url, "/chainlink?sslmode=disable", fmt.Sprintf("/db_%d?sslmode=disable", i), -1)
145151
dbURL := strings.Replace(dbOut.DockerInternalURL, "/chainlink?sslmode=disable", fmt.Sprintf("/db_%d?sslmode=disable", i), -1)
146152
dbSpec := &postgres.Output{

framework/docker.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"bytes"
66
"context"
77
"encoding/binary"
8-
"errors"
98
"fmt"
109
"github.com/docker/docker/api/types/container"
1110
"github.com/docker/docker/client"
@@ -15,6 +14,7 @@ import (
1514
"golang.org/x/sync/errgroup"
1615
"io"
1716
"os"
17+
"os/exec"
1818
"path/filepath"
1919
"strings"
2020
"sync"
@@ -69,6 +69,14 @@ func DefaultTCName(name string) string {
6969
return fmt.Sprintf("%s-%s", name, uuid.NewString()[0:5])
7070
}
7171

72+
// runCommand executes a command and prints the output.
73+
func runCommand(name string, args ...string) error {
74+
cmd := exec.Command(name, args...)
75+
cmd.Stdout = os.Stdout
76+
cmd.Stderr = os.Stderr
77+
return cmd.Run()
78+
}
79+
7280
// DockerClient wraps a Docker API client and provides convenience methods
7381
type DockerClient struct {
7482
cli *client.Client
@@ -242,29 +250,19 @@ func WriteAllContainersLogs() error {
242250
return eg.Wait()
243251
}
244252

245-
func BuildImageOnce(ctx context.Context, once *sync.Once, dctx, dfile, nameAndTag string) error {
246-
var (
247-
p *tc.DockerProvider
248-
dockerCtx string
249-
err error
250-
)
253+
func BuildImageOnce(once *sync.Once, dctx, dfile, nameAndTag string) error {
254+
var err error
251255
once.Do(func() {
252-
nt := strings.Split(nameAndTag, ":")
253-
if len(nt) != 2 {
254-
err = errors.New("BuildImageOnce, tag must be in 'repo:tag' format")
255-
return
256+
dfilePath := filepath.Join(dctx, dfile)
257+
err = runCommand("docker", "build", "-t", nameAndTag, "-f", dfilePath, dctx)
258+
if err != nil {
259+
err = fmt.Errorf("failed to build Docker image: %w", err)
256260
}
257-
p, err = tc.NewDockerProvider()
258-
dockerCtx, err = filepath.Abs(dctx)
259-
_, err = p.BuildImage(ctx, &tc.ContainerRequest{
260-
FromDockerfile: tc.FromDockerfile{
261-
Repo: nt[0],
262-
Tag: nt[1],
263-
Context: dockerCtx,
264-
Dockerfile: dfile,
265-
PrintBuildLog: true,
266-
},
267-
})
268261
})
269262
return err
270263
}
264+
265+
func BuildImage(dctx, dfile, nameAndTag string) error {
266+
dfilePath := filepath.Join(dctx, dfile)
267+
return runCommand("docker", "build", "-t", nameAndTag, "-f", dfilePath, dctx)
268+
}

0 commit comments

Comments
 (0)