Skip to content

Commit c27bd9a

Browse files
committed
add e2e test
1 parent b67404a commit c27bd9a

File tree

15 files changed

+361
-26
lines changed

15 files changed

+361
-26
lines changed

.github/workflows/e2e-test.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: E2E tests
2+
3+
on:
4+
push:
5+
branches: [ "develop"]
6+
pull_request:
7+
branches: [ "develop"]
8+
9+
jobs:
10+
build:
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
matrix:
14+
os: [ ubuntu-latest ]
15+
permissions:
16+
contents: read
17+
packages: write
18+
id-token: write
19+
20+
steps:
21+
- uses: actions/checkout@v3
22+
with:
23+
ref: ${{ github.event.pull_request.head.sha }}
24+
fetch-depth: 0
25+
26+
- name: Setup Node.js
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: '21.x'
30+
31+
- name: Run e2e tests
32+
run: make e2e-test

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ unit_test:
4242
clean:
4343
@rm -rf $(BUILD_DIR)
4444

45+
.PHONY: contract-test
4546
contract-test:
4647
@cd smartcontracts && npm install --save-dev hardhat
4748
@cd smartcontracts && npx hardhat test
49+
50+
.PHONY: e2e-test
51+
e2e-test:
52+
@cd smartcontracts && yarn install
53+
@TEST_E2E=true go test ./e2e -v
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

e2e/e2e_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package e2e
2+
3+
import (
4+
"context"
5+
"log"
6+
"os"
7+
"runtime"
8+
"testing"
9+
10+
"github.com/iotexproject/w3bstream/e2e/utils"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestE2E(t *testing.T) {
15+
if os.Getenv("TEST_E2E") != "true" {
16+
t.Skip("Skipping E2E tests.")
17+
}
18+
if runtime.GOARCH == "arm64" {
19+
log.Println("Skipping tests: Unsupported architecture (arm64)")
20+
t.Skip()
21+
}
22+
23+
chainContainer, endpoint, err := utils.SetupLocalChain(context.Background())
24+
t.Cleanup(func() {
25+
if err := chainContainer.Terminate(context.Background()); err != nil {
26+
t.Logf("failed to terminate chain container: %v", err)
27+
}
28+
})
29+
require.NoError(t, err)
30+
31+
log.Printf("Chain endpoint: %s\n", endpoint)
32+
33+
err = utils.DeployContract(endpoint)
34+
require.NoError(t, err)
35+
}

e2e/utils/chain.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/ethereum/go-ethereum/ethclient"
8+
"github.com/testcontainers/testcontainers-go"
9+
"github.com/testcontainers/testcontainers-go/wait"
10+
)
11+
12+
type chainContainer struct {
13+
testcontainers.Container
14+
}
15+
16+
func SetupLocalChain(ctx context.Context) (*chainContainer, string, error) {
17+
req := testcontainers.ContainerRequest{
18+
Image: "ghcr.io/foundry-rs/foundry:latest",
19+
ExposedPorts: []string{"8545/tcp"},
20+
Entrypoint: []string{"anvil", "--block-time", "5", "--host", "0.0.0.0"},
21+
WaitingFor: wait.ForListeningPort("8545"),
22+
}
23+
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
24+
ContainerRequest: req,
25+
Started: true,
26+
Reuse: false,
27+
})
28+
if err != nil {
29+
return nil, "", err
30+
}
31+
32+
mapPort, err := container.MappedPort(ctx, "8545")
33+
if err != nil {
34+
return nil, "", err
35+
}
36+
37+
ip, err := container.Host(ctx)
38+
if err != nil {
39+
return nil, "", err
40+
}
41+
42+
endpoint := fmt.Sprintf("http://%s:%s", ip, mapPort.Port())
43+
44+
ethClient, err := ethclient.Dial(endpoint)
45+
if err != nil {
46+
return nil, "", err
47+
}
48+
_, err = ethClient.NetworkID(ctx)
49+
if err != nil {
50+
return nil, "", err
51+
52+
}
53+
54+
return &chainContainer{Container: container}, endpoint, nil
55+
}

e2e/utils/contractdeploy.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package utils
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"fmt"
7+
"log"
8+
"os"
9+
"os/exec"
10+
)
11+
12+
const (
13+
smartcontractsPath = "../smartcontracts/"
14+
)
15+
16+
func DeployContract(endpoint string) error {
17+
// A private key in Anvil local chain
18+
os.Setenv("PRIVATE_KEY", "0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6")
19+
os.Setenv("URL", endpoint)
20+
21+
cmd := exec.Command("bash", "-c", "./deploy.sh")
22+
cmd.Dir = smartcontractsPath
23+
24+
stdout, err := cmd.StdoutPipe()
25+
if err != nil {
26+
return err
27+
}
28+
29+
// Start the command asynchronously
30+
if err := cmd.Start(); err != nil {
31+
return err
32+
}
33+
34+
scanner := bufio.NewScanner(stdout)
35+
scanner.Split(bufio.ScanLines)
36+
37+
var outputBuffer bytes.Buffer
38+
39+
for scanner.Scan() {
40+
line := scanner.Text()
41+
log.Println(line)
42+
outputBuffer.WriteString(line + "\n")
43+
}
44+
45+
// Check for any scanning errors
46+
if err := scanner.Err(); err != nil {
47+
return err
48+
}
49+
50+
if err := cmd.Wait(); err != nil {
51+
return err
52+
}
53+
54+
// reprint outputbuffer
55+
fmt.Println("Output:")
56+
fmt.Println(outputBuffer.String())
57+
return nil
58+
}

go.mod

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ require (
2121
github.com/pkg/errors v0.9.1
2222
github.com/prometheus/client_golang v1.18.0
2323
github.com/spf13/viper v1.17.0
24-
github.com/stretchr/testify v1.8.4
24+
github.com/stretchr/testify v1.9.0
25+
github.com/testcontainers/testcontainers-go v0.33.0
2526
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
2627
google.golang.org/grpc v1.65.0
2728
google.golang.org/protobuf v1.34.1
@@ -34,30 +35,42 @@ require (
3435
require github.com/cespare/cp v1.1.1 // indirect
3536

3637
require (
38+
dario.cat/mergo v1.0.0 // indirect
39+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
3740
github.com/DataDog/zstd v1.5.2 // indirect
41+
github.com/Microsoft/go-winio v0.6.2 // indirect
3842
github.com/StackExchange/wmi v1.2.1 // indirect
3943
github.com/benbjohnson/clock v1.3.5 // indirect
4044
github.com/beorn7/perks v1.0.1 // indirect
4145
github.com/blang/semver/v4 v4.0.0 // indirect
4246
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
4347
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
4448
github.com/bytedance/sonic v1.9.1 // indirect
49+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
4550
github.com/cespare/xxhash/v2 v2.3.0 // indirect
4651
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
4752
github.com/cockroachdb/errors v1.11.1 // indirect
4853
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
4954
github.com/cockroachdb/redact v1.1.5 // indirect
5055
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
5156
github.com/containerd/cgroups v1.1.0 // indirect
57+
github.com/containerd/containerd v1.7.18 // indirect
58+
github.com/containerd/log v0.1.0 // indirect
59+
github.com/containerd/platforms v0.2.1 // indirect
5260
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
61+
github.com/cpuguy83/dockercfg v0.3.1 // indirect
5362
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
5463
github.com/crackcomm/go-gitignore v0.0.0-20231225121904-e25f5bc08668 // indirect
5564
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
5665
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
5766
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
5867
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
68+
github.com/distribution/reference v0.6.0 // indirect
69+
github.com/docker/docker v27.1.1+incompatible // indirect
70+
github.com/docker/go-connections v0.5.0 // indirect
5971
github.com/docker/go-units v0.5.0 // indirect
6072
github.com/elastic/gosigar v0.14.2 // indirect
73+
github.com/felixge/httpsnoop v1.0.4 // indirect
6174
github.com/flynn/noise v1.0.1 // indirect
6275
github.com/francoispqt/gojay v1.2.13 // indirect
6376
github.com/fsnotify/fsnotify v1.6.0 // indirect
@@ -66,7 +79,7 @@ require (
6679
github.com/gin-contrib/sse v0.1.0 // indirect
6780
github.com/go-logr/logr v1.4.1 // indirect
6881
github.com/go-logr/stdr v1.2.2 // indirect
69-
github.com/go-ole/go-ole v1.2.5 // indirect
82+
github.com/go-ole/go-ole v1.2.6 // indirect
7083
github.com/go-playground/locales v0.14.1 // indirect
7184
github.com/go-playground/universal-translator v0.18.1 // indirect
7285
github.com/go-playground/validator/v10 v10.14.0 // indirect
@@ -122,6 +135,7 @@ require (
122135
github.com/libp2p/go-netroute v0.2.1 // indirect
123136
github.com/libp2p/go-reuseport v0.4.0 // indirect
124137
github.com/libp2p/go-yamux/v4 v4.0.1 // indirect
138+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
125139
github.com/magiconair/properties v1.8.7 // indirect
126140
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
127141
github.com/mattn/go-colorable v0.1.13 // indirect
@@ -135,8 +149,14 @@ require (
135149
github.com/minio/sha256-simd v1.0.1 // indirect
136150
github.com/mitchellh/go-homedir v1.1.0 // indirect
137151
github.com/mitchellh/mapstructure v1.5.0 // indirect
152+
github.com/moby/docker-image-spec v1.3.1 // indirect
153+
github.com/moby/patternmatcher v0.6.0 // indirect
154+
github.com/moby/sys/sequential v0.5.0 // indirect
155+
github.com/moby/sys/user v0.1.0 // indirect
156+
github.com/moby/term v0.5.0 // indirect
138157
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
139158
github.com/modern-go/reflect2 v1.0.2 // indirect
159+
github.com/morikuni/aec v1.0.0 // indirect
140160
github.com/mr-tron/base58 v1.2.0 // indirect
141161
github.com/multiformats/go-base32 v0.1.0 // indirect
142162
github.com/multiformats/go-base36 v0.2.0 // indirect
@@ -148,12 +168,15 @@ require (
148168
github.com/multiformats/go-multistream v0.5.0 // indirect
149169
github.com/multiformats/go-varint v0.0.7 // indirect
150170
github.com/onsi/ginkgo/v2 v2.13.2 // indirect
171+
github.com/opencontainers/go-digest v1.0.0 // indirect
172+
github.com/opencontainers/image-spec v1.1.0 // indirect
151173
github.com/opencontainers/runtime-spec v1.1.0 // indirect
152174
github.com/opentracing/opentracing-go v1.2.0 // indirect
153175
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
154176
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
155177
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
156178
github.com/polydawn/refmt v0.89.0 // indirect
179+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
157180
github.com/prometheus/client_model v0.5.0 // indirect
158181
github.com/prometheus/common v0.45.0 // indirect
159182
github.com/prometheus/procfs v0.12.0 // indirect
@@ -166,6 +189,9 @@ require (
166189
github.com/sagikazarmark/locafero v0.3.0 // indirect
167190
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
168191
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
192+
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
193+
github.com/shoenig/go-m1cpu v0.1.6 // indirect
194+
github.com/sirupsen/logrus v1.9.3 // indirect
169195
github.com/smarty/assertions v1.15.1 // indirect
170196
github.com/smartystreets/goconvey v1.8.1 // indirect
171197
github.com/sourcegraph/conc v0.3.0 // indirect
@@ -180,10 +206,12 @@ require (
180206
github.com/ugorji/go/codec v1.2.11 // indirect
181207
github.com/urfave/cli/v2 v2.25.7 // indirect
182208
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
209+
github.com/yusufpapurcu/wmi v1.2.3 // indirect
183210
go.opencensus.io v0.24.0 // indirect
184-
go.opentelemetry.io/otel v1.21.0 // indirect
185-
go.opentelemetry.io/otel/metric v1.21.0 // indirect
186-
go.opentelemetry.io/otel/trace v1.21.0 // indirect
211+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
212+
go.opentelemetry.io/otel v1.24.0 // indirect
213+
go.opentelemetry.io/otel/metric v1.24.0 // indirect
214+
go.opentelemetry.io/otel/trace v1.24.0 // indirect
187215
go.uber.org/dig v1.17.1 // indirect
188216
go.uber.org/fx v1.20.1 // indirect
189217
go.uber.org/mock v0.4.0 // indirect

0 commit comments

Comments
 (0)