Files
nigiri/test/start_stop_test.go
Marco Argentieri 70e22ba402 refactor codebase with urfave/cli (#120)
* clean

* load datadir with resources and start compose

* start --liquid

* check if is running already

* Add rpc command

* Add logs and faucet commands

* Add mint command

* Add push command

* Move state into dedicated pkg

* less code for isRunning check

* Add --ci flag to start command

* Add NIGIRI_DATADIR env var

* add update command

* Update readme, makefile and goreleaser

* update test gh action

* skip tests

* print endpoint in start command

* Add a default network to docker compose file.

Containers started through this docker compose file will be in this isolated network.
Note: the minimum version for the docker compose file format is 3.5 which introduces `name` for networks. This file format requires a docker engine version of 17.12.0+.

* add prerelease: auto

* makefile: release commands

* go version

* use global flag --datadir for the folder

* wrap datadir in variable

* use single compose file & move state to internal

* remove unused state keys

* return err in test

* docker-compose: use latest version

* print endpoint of services based on --liquid flag

Co-authored-by: Philipp Hoenisch <philipp@hoenisch.at>
2021-08-06 18:09:13 +02:00

116 lines
2.6 KiB
Go

package test
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/vulpemventures/nigiri/internal/config"
"github.com/vulpemventures/nigiri/internal/state"
)
const (
liquid = true
bitcoin = false
delete = true
)
var (
stopCmd = "stop"
// deleteCmd = append(stopCmd, "--delete")
startCmd = "start"
// liquidStartCmd = append(startCmd, "--liquid")
tmpDatadir = filepath.Join(os.TempDir(), "nigiri-tmp")
nigiriState = state.New(filepath.Join(tmpDatadir, config.DefaultName), config.InitialState)
)
func TestStartStopLiquid(t *testing.T) {
if testing.Short() {
t.Skip("skipping testing in short mode")
}
// Start/Stop
testStart(t, liquid)
testStop(t)
// Start/Delete
testStart(t, liquid)
testDelete(t)
}
func TestStartStopBitcoin(t *testing.T) {
if testing.Short() {
t.Skip("skipping testing in short mode")
}
// Start/Stop
testStart(t, bitcoin)
testStop(t)
// Start/Delete
testStart(t, bitcoin)
testDelete(t)
}
func testStart(t *testing.T, flag bool) {
if err := testCommand("start", "", flag); err != nil {
t.Fatal(err)
}
//Give some time to nigiri to be ready before calling
time.Sleep(5 * time.Second)
if isRunning, err := nigiriState.GetBool("running"); err != nil {
t.Fatal(err)
} else if !isRunning {
t.Fatal("Nigiri should have been started but services have not been found among running containers")
}
}
func testStop(t *testing.T) {
if err := testCommand("stop", "", !delete); err != nil {
t.Fatal(err)
}
//Give some time to nigiri to be ready before calling
time.Sleep(5 * time.Second)
if isRunning, err := nigiriState.GetBool("running"); err != nil {
t.Fatal(err)
} else if isRunning {
t.Fatal("Nigiri should have been stopped but services have not been found among stopped containers")
}
}
func testDelete(t *testing.T) {
if err := testCommand("stop", "", delete); err != nil {
t.Fatal(err)
}
if isRunning, err := nigiriState.GetBool("running"); err != nil {
t.Fatal(err)
} else if isRunning {
t.Fatal("Nigiri should have been terminated at this point but services have been found among stopped containers")
}
}
func testCommand(command, arg string, flag bool) error {
cmd := exec.Command("go", "run", "./cmd/nigiri")
env := "NIGIRI_DATADIR=" + tmpDatadir
cmd.Env = []string{env}
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if command == "start" {
cmd.Args = append(cmd.Args, startCmd, fmt.Sprintf("--liquid=%t", flag))
}
if command == "stop" {
cmd.Args = append(cmd.Args, stopCmd, fmt.Sprintf("--delete=%t", flag))
}
err := cmd.Start()
if err != nil {
return fmt.Errorf("name: %v, args: %v, err: %v", command, arg, err.Error())
}
return nil
}