Skip to content

Commit

Permalink
feat: add integration tests (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton authored Sep 25, 2023
1 parent ab8c600 commit df9744d
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 7 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: integration

on:
pull_request:
paths:
- "testdata/**"
- "go.sum"
push:
branches:
- master
- main

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 'stable'
- run: go test -v -run='TestIntegration/.*' .
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ z_use_remote_gno: ## Use the remote 'github.com/gnolang/gno' module and remove a
z_test_realm: ## Test the realm.
go run github.com/gnolang/gno/gnovm/cmd/gno test --verbose ./realm

z_test_integration: ## Test the realm.
go test -v -run='TestIntegration/.*' .

z_build_realm: ## Precompile and build the generated Go files. Assumes a working clone of gno in ../gno.
mkdir -p ../gno/examples/gno.land/r/gnochess
cp -rf realm/*.gno ../gno/examples/gno.land/r/gnochess
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ module github.com/gnolang/gnochess

go 1.20

require github.com/gnolang/gno v0.0.0-20230922075615-67b75bafbe3c
require (
github.com/gnolang/gno v0.0.0-20230925211159-9c7c0a577eb6
github.com/jaekwon/testify v1.6.1
github.com/rogpeppe/go-internal v1.11.0
)

require (
github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c // indirect
Expand Down Expand Up @@ -32,6 +36,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.10.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.opencensus.io v0.22.5 // indirect
Expand All @@ -44,4 +49,5 @@ require (
golang.org/x/term v0.12.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 11 additions & 6 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"time"

integration "github.com/gnolang/gno/gno.land/pkg/integration"
"github.com/jaekwon/testify/require"
"github.com/rogpeppe/go-internal/testscript"
)

var balances = []string{
"g1plqds6kxnfaqcpky0gtt6fpntfhjgcfx8r73a0=100000000000000000ugnot",
"g1sgy2zhqg2wecuz3qt8th63d539afagjnhs4zj3=100000000000000000ugnot",
"g1unk9a8yt595p4yxpfpejewvf9lx6yrvd2ylgtm=100000000000000000ugnot",
"g17x4qwuhmc6fyp6ut2qtscc9265xe5jnj83s8c6=100000000000000000ugnot",
"g1agq8t3289xxmm63z55axykmmve2pz87yqgyn5n=100000000000000000ugnot",
"g153xesqpfvr5y35l0aykew3796kz452zttp0xt2=100000000000000000ugnot",
"g18epncd7avkhmdlf930e4t2p7c7j9qdv3yda93f=100000000000000000ugnot",
"g1elguymy8sjjy246u09qddtx587934k6uzf8mc4=100000000000000000ugnot",
"g1sl70rzvu49mp0lstxaptmvle8h2a8rx8pu56uk=100000000000000000ugnot",
"g18dgugclk93v65qtxxus82eg30af59fgk246nqy=100000000000000000ugnot",
}

func generateGenesisFile(genesispath, target string) error {
genesis, err := os.ReadFile(genesispath)
if err != nil {
return fmt.Errorf("Error reading genesis file: %w", err)
}

outputFile, err := os.Create(target)
if err != nil {
return fmt.Errorf("Error creating output file: %w", err)
}
defer outputFile.Close()

writer := bufio.NewWriter(outputFile)
writer.Write(genesis)
writer.WriteRune('\n')
writer.WriteRune('\n')

for _, line := range balances {
writer.Write([]byte(line))
writer.WriteRune('\n')
}

return nil
}

func TestIntegration(t *testing.T) {
ts := integration.SetupGnolandTestScript(t, "testdata")

goModPath, err := exec.Command("go", "env", "GOMOD").CombinedOutput()
require.NoError(t, err)

oldsetup := ts.Setup
ts.Setup = func(e *testscript.Env) error {
oldsetup(e)
e.Setenv("ROOTDIR", filepath.Dir(string(goModPath)))

rootdir := e.Getenv("GNOROOT")
tmpdir := e.Getenv("TMPDIR")

outpout_genesis := filepath.Join(tmpdir, "genesis_balances.txt")
input_genesis := filepath.Join(rootdir, "gno.land/genesis/genesis_balances.txt")
if err := generateGenesisFile(input_genesis, outpout_genesis); err != nil {
return fmt.Errorf("unable to generate genesis file: %w", err)
}

return nil
}

ts.Cmds["sleep"] = func(ts *testscript.TestScript, neg bool, args []string) {
d := time.Second
if len(args) > 0 {
var err error
if d, err = time.ParseDuration(args[0]); err != nil {
ts.Fatalf("uanble to parse duration %q: %s", args[1], err)
}
}

time.Sleep(d)
}

testscript.Run(t, ts)
}
26 changes: 26 additions & 0 deletions testdata/addpkg.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# test for add package

## start a new node
gnoland start

## add bar.gno package located in $WORK directory as gno.land/r/foobar/bar
gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/foobar/bar -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1

## execute Render
gnokey maketx call -pkgpath gno.land/r/foobar/bar -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1

## compare render
cmp stdout stdout.golden

-- bar.gno --
package bar

func Render(path string) string {
return "hello from foo"
}

-- stdout.golden --
("hello from foo" string)
OK!
GAS WANTED: 2000000
GAS USED: 69163
5 changes: 5 additions & 0 deletions testdata/addpkg_gnochess.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gnoland start -genesis-balances-file=${TMPDIR}/genesis_balances.txt -genesis-max-vm-cycles 100000000

sleep 500ms

gnokey maketx addpkg -pkgdir ${ROOTDIR}/realm -pkgpath gno.land/r/demo/chess -gas-wanted 20000000 -gas-fee 1ugnot -chainid=tendermint_test -broadcast test1
22 changes: 22 additions & 0 deletions testdata/basic_commands.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# test basic gnoland integrations commands

## no arguments should fail
! gnoland

## should be able to start
gnoland start

## should not be able to start a node twice
! gnoland start

## test1 account should be available on default
gnokey query auth/accounts/${USER_ADDR_test1}

## invalid gnokey command should raise an error
! gnokey query foo/bar

## should be able to stop default
gnoland stop

## should not be able to stop a node twice
! gnoland stop

0 comments on commit df9744d

Please sign in to comment.