Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating e2e tests in go #637

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@ GIT_TAG = $(shell git tag -l --points-at HEAD)
THOR_VERSION = $(shell cat cmd/thor/VERSION)
DISCO_VERSION = $(shell cat cmd/disco/VERSION)

PACKAGES = `go list ./... | grep -v '/vendor/'`
UNIT_TEST_PACKAGES = `go list ./... | grep -v '/vendor/'` | grep -v 'e2e'

MAJOR = $(shell go version | cut -d' ' -f3 | cut -b 3- | cut -d. -f1)
MINOR = $(shell go version | cut -d' ' -f3 | cut -b 3- | cut -d. -f2)
export GO111MODULE=on

.PHONY: thor disco all clean test

thor:| go_version_check
help: #@ Show a list of available commands
@egrep -h '\s#@\s' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?#@ "}; {printf "\033[36m %-30s\033[0m %s\n", $$1, $$2}'

thor:| go_version_check #@ Build the `thor` executable
@echo "building $@..."
@go build -v -o $(CURDIR)/bin/$@ -ldflags "-X main.version=$(THOR_VERSION) -X main.gitCommit=$(GIT_COMMIT) -X main.gitTag=$(GIT_TAG)" ./cmd/thor
@echo "done. executable created at 'bin/$@'"

disco:| go_version_check
disco:| go_version_check #@ Build the `disco` executable
@echo "building $@..."
@go build -v -o $(CURDIR)/bin/$@ -ldflags "-X main.version=$(DISCO_VERSION) -X main.gitCommit=$(GIT_COMMIT) -X main.gitTag=$(GIT_TAG)" ./cmd/disco
@echo "done. executable created at 'bin/$@'"

dep:| go_version_check
dep:| go_version_check #@ Download dependencies
@go mod download

go_version_check:
go_version_check: #@ Check go version
@if test $(MAJOR) -lt 1; then \
echo "Go 1.16 or higher required"; \
exit 1; \
Expand All @@ -37,13 +40,16 @@ go_version_check:
fi \
fi

all: thor disco
all: thor disco #@ Build all executables

clean:
clean: #@ Clean the executables
-rm -rf \
$(CURDIR)/bin/thor \
$(CURDIR)/bin/disco
$(CURDIR)/bin/disco

test:| go_version_check #@ Run unit tests
@go test -cover $(UNIT_TEST_PACKAGES)

test:| go_version_check
@go test -cover $(PACKAGES)
test-e2e:| go_version_check #@ Run end-to-end tests
GOMAXPROCS=1 go test github.com/vechain/thor/v2/tests/e2e

24 changes: 24 additions & 0 deletions tests/e2e/accounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package e2e

import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/vechain/thor/v2/tests/e2e/client"
"github.com/vechain/thor/v2/tests/e2e/network"
"testing"

"github.com/stretchr/testify/assert"
)

func TestAccountBalance(t *testing.T) {
network.StartCompose(t)

account, err := client.Default.GetAccount("0xf077b491b355E64048cE21E3A6Fc4751eEeA77fa")

assert.NoError(t, err, "GetAccount()")

balance, err := account.Balance.MarshalText()

assert.NoError(t, err, "MarshalText()")

assert.Equal(t, hexutil.Encode(balance), "0x307831346164663462373332303333346239303030303030")
}
18 changes: 18 additions & 0 deletions tests/e2e/blocks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package e2e

import (
"github.com/vechain/thor/v2/tests/e2e/client"
"github.com/vechain/thor/v2/tests/e2e/network"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetBlock(t *testing.T) {
network.StartCompose(t)

block, err := client.Default.GetCompressedBlock(1)

assert.NoError(t, err, "GetCompressedBlock()")
assert.Greater(t, block.Number, uint32(0), "GetCompressedBlock()")
}
54 changes: 54 additions & 0 deletions tests/e2e/client/thor_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package client

import (
"encoding/json"
"github.com/vechain/thor/v2/api/accounts"
"github.com/vechain/thor/v2/api/blocks"
"net/http"
"strconv"
)

var (
Default = NewThorClient("http://localhost:8669")
Node1 = NewThorClient("http://localhost:8669")
Node2 = NewThorClient("http://localhost:8679")
Node3 = NewThorClient("http://localhost:8689")
)

type ThorClient struct {
baseUrl string
}

func NewThorClient(baseUrl string) *ThorClient {
return &ThorClient{baseUrl: baseUrl}
}

func (c *ThorClient) GetAccount(address string) (*accounts.Account, error) {
return Get(c, "/accounts/"+address, new(accounts.Account))
}

func (c *ThorClient) GetExpandedBlock(number int32) (*blocks.JSONExpandedBlock, error) {
return Get(c, "/blocks/"+strconv.Itoa(int(number))+"?expanded=true", new(blocks.JSONExpandedBlock))
}

func (c *ThorClient) GetCompressedBlock(number int32) (*blocks.JSONCollapsedBlock, error) {
return Get(c, "/blocks/"+strconv.Itoa(int(number)), new(blocks.JSONCollapsedBlock))
}

func Get[T any](c *ThorClient, endpoint string, v *T) (*T, error) {

resp, err := http.Get(c.baseUrl + endpoint)

if err != nil {
return nil, err
}

defer resp.Body.Close()

err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
return nil, err
}

return v, nil
}
87 changes: 87 additions & 0 deletions tests/e2e/network/config/genesis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"launchTime": 1703180212,
"gasLimit": 10000000,
"extraData": "My custom VeChain",
"accounts": [
{
"address": "0x7567d83b7b8d80addcb281a71d54fc7b3364ffed",
"balance": "0x14ADF4B7320334B9000000",
"energy": 0,
"code": "0x6060604052600256",
"storage": {
"0x0000000000000000000000000000000000000000000000000000000000000001": "0x0000000000000000000000000000000000000000000000000000000000000002"
}
},
{
"address": "0xf077b491b355e64048ce21e3a6fc4751eeea77fa",
"balance": "0x14ADF4B7320334B9000000",
"energy": "0x14ADF4B7320334B9000000"
},
{
"address": "0x435933c8064b4ae76be665428e0307ef2ccfbd68",
"balance": "0x14ADF4B7320334B9000000",
"energy": "0x14ADF4B7320334B9000000"
},
{
"address": "0x0f872421dc479f3c11edd89512731814d0598db5",
"balance": "0x14ADF4B7320334B9000000",
"energy": "0x14ADF4B7320334B9000000"
},
{
"address": "0xf370940abdbd2583bc80bfc19d19bc216c88ccf0",
"balance": "0x14ADF4B7320334B9000000",
"energy": "0x14ADF4B7320334B9000000"
},
{
"address": "0x99602e4bbc0503b8ff4432bb1857f916c3653b85",
"balance": "0x14ADF4B7320334B9000000",
"energy": "0x14ADF4B7320334B9000000"
},
{
"address": "0x61e7d0c2b25706be3485980f39a3a994a8207acf",
"balance": "0x14ADF4B7320334B9000000",
"energy": "0x14ADF4B7320334B9000000"
},
{
"address": "0x361277d1b27504f36a3b33d3a52d1f8270331b8c",
"balance": "0x14ADF4B7320334B9000000",
"energy": "0x14ADF4B7320334B9000000"
},
{
"address": "0xd7f75a0a1287ab2916848909c8531a0ea9412800",
"balance": "0x14ADF4B7320334B9000000",
"energy": "0x14ADF4B7320334B9000000"
}
],
"authority": [
{
"masterAddress": "0x435933c8064b4ae76be665428e0307ef2ccfbd68",
"endorsorAddress": "0x7567d83b7b8d80addcb281a71d54fc7b3364ffed",
"identity": "0x000000000000000068747470733a2f2f636f6e6e65782e76656368612e696e2f"
},
{
"masterAddress": "0xf370940abdbd2583bc80bfc19d19bc216c88ccf0",
"endorsorAddress": "0x7567d83b7b8d80addcb281a71d54fc7b3364ffed",
"identity": "0x000000000000000068747470733a2f2f656e762e7665636861696e2e6f72672f"
},
{
"masterAddress": "0x0f872421dc479f3c11edd89512731814d0598db5",
"endorsorAddress": "0x7567d83b7b8d80addcb281a71d54fc7b3364ffed",
"identity": "0x0000000000000068747470733a2f2f617070732e7665636861696e2e6f72672f"
}
],
"params": {
"rewardRatio": 300000000000000000,
"baseGasPrice": 1000000000000000,
"proposerEndorsement": "0x14ADF4B7320334B9000000",
"executorAddress": "0x0000000000000000000000004578656375746f72"
},
"executor": {
"approvers": [
{
"address": "0x199b836d8a57365baccd4f371c1fabb7be77d389",
"identity": "0x00000000000067656e6572616c20707572706f736520626c6f636b636861696e"
}
]
}
}
22 changes: 22 additions & 0 deletions tests/e2e/network/config/health_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

# Set the URL
url="http://localhost:8669/blocks/best"

# Make the HTTP request and store the response in a variable
response=$(wget -qO- $url)

echo $response

# Extract the value of "number" from the JSON response using grep
number=$(echo $response | grep -o '"number":[^,}]*' | awk -F: '{print $2}' | tr -d '[:space:]')

# Check if the number is greater than 0
if [ $number -gt 0 ]; then
echo "Health check passed! Number is greater than 0."
exit 0
else
echo "Health check failed! Unexpected response or number is not greater than 0:"
echo $response
exit 1
fi
13 changes: 13 additions & 0 deletions tests/e2e/network/config/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

if [[ -n $MASTER_KEY_ADDRESS ]]; then
echo "Starting node with master key address $MASTER_KEY_ADDRESS"

cp /node/keys/$MASTER_KEY_ADDRESS/master.key /tmp
fi

BOOTNODE_IP=$(ping -c 1 thor-disco | awk -F'[()]' '/PING/{print $2}')

echo $BOOTNODE_IP

thor --config-dir=/tmp --network /node/config/genesis.json --api-addr="0.0.0.0:8669" --api-cors="*" --bootnode "enode://e32e5960781ce0b43d8c2952eeea4b95e286b1bb5f8c1f0c9f09983ba7141d2fdd7dfbec798aefb30dcd8c3b9b7cda8e9a94396a0192bfa54ab285c2cec515ab@$BOOTNODE_IP:55555"
18 changes: 18 additions & 0 deletions tests/e2e/network/docker-compose-base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: "3.8"

services:
base-authority-node:
image: ${THOR_IMAGE:-vechain/thor:latest}
entrypoint: "/node/config/start.sh"
volumes:
- type: bind
source: ./config
target: /node/config
- type: bind
source: ./keys
target: /node/keys
healthcheck:
test: ["CMD", "/node/config/health_check.sh"]
interval: 1s
timeout: 5s
retries: 120
38 changes: 38 additions & 0 deletions tests/e2e/network/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: "3.8"

services:

thor-disco:
image: ${THOR_IMAGE:-vechain/thor:latest}
container_name: thor-disco
entrypoint: "disco --keyhex=99f0500549792796c14fed62011a51081dc5b5e68fe8bd8a13b86be829c4fd36"

authority-node-1:
extends:
file: docker-compose-base.yaml
service: base-authority-node
container_name: authority-node-1
environment:
MASTER_KEY_ADDRESS: "435933c8064b4ae76be665428e0307ef2ccfbd68"
ports:
- "8669:8669"

authority-node-2:
extends:
file: docker-compose-base.yaml
service: base-authority-node
container_name: authority-node-2
environment:
MASTER_KEY_ADDRESS: "f370940abdbd2583bc80bfc19d19bc216c88ccf0"
ports:
- "8679:8669"

authority-node-3:
extends:
file: docker-compose-base.yaml
service: base-authority-node
container_name: authority-node-3
environment:
MASTER_KEY_ADDRESS: "0f872421dc479f3c11edd89512731814d0598db5"
ports:
- "8689:8669"
27 changes: 27 additions & 0 deletions tests/e2e/network/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package network

import (
"context"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go/modules/compose"

Check failure on line 6 in tests/e2e/network/docker.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.21.x, ubuntu-latest)

no required module provides package github.com/testcontainers/testcontainers-go/modules/compose; to add it:

Check failure on line 6 in tests/e2e/network/docker.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.21.x, ubuntu-latest)

no required module provides package github.com/testcontainers/testcontainers-go/modules/compose; to add it:

Check failure on line 6 in tests/e2e/network/docker.go

View workflow job for this annotation

GitHub Actions / unit_tests (1.20.x, ubuntu-latest)

no required module provides package github.com/testcontainers/testcontainers-go/modules/compose; to add it:
"testing"
)

type Network struct {
stack *compose.ComposeStack
}

// StartCompose starts the docker-compose network and destroys it after the test
func StartCompose(t *testing.T) {
dc, err := compose.NewDockerCompose("network/docker-compose.yaml")
assert.NoError(t, err, "NewDockerComposeAPI()")

t.Cleanup(func() {
assert.NoError(t, dc.Down(context.Background(), compose.RemoveOrphans(true), compose.RemoveImagesLocal), "compose.Down()")
})

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

assert.NoError(t, dc.Up(ctx, compose.Wait(true)), "compose.Up()")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"address": "0f872421dc479f3c11edd89512731814d0598db5",
"id": "33eda5b7-140d-441d-b450-07ca28fb0105",
"version": 3,
"crypto": {
"cipher": "aes-128-ctr",
"cipherparams": {
"iv": "182b783ea15a49b815042dd3a8bbc80e"
},
"ciphertext": "2084fac13c2d3b3e45dd743766abcffe5c48e2d8169a895e7891235949fd3506",
"kdf": "scrypt",
"kdfparams": {
"salt": "e5256c7b430ffefda1b1bcbef44af363eddf01ff8dc9112eb97324bfa6d388c6",
"n": 131072,
"dklen": 32,
"p": 1,
"r": 8
},
"mac": "b27043563629497c167a756eed7130079e7a4e7c5aa05fde67ed064a14e6f536"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f4a1a17039216f535d42ec23732c79943ffb45a089fbb78a14daad0dae93e991
Loading
Loading