Files
nigiri/cli/cmd/start_stop_test.go
Francis 88fca5db00 Add faucet command (#93)
* added faucet command

* chopsticks ports imported from env

* changed order of assignments

* test: sleep after start nigiri

* sleep after each strat and each stop

* small changes on faucet

* added check for txId

* json err control

* correcting json err control

* small fixes

* fixes on faucet command

* http.StatusOK

* try with 0.0.0.0 interface

* try with 127.0.0.1 interface

* fixes in travis

Co-authored-by: Marco Argentieri <3596602+tiero@users.noreply.github.com>
2020-09-11 19:13:43 +01:00

169 lines
3.9 KiB
Go

package cmd
import (
"fmt"
"testing"
"time"
"github.com/vulpemventures/nigiri/cli/constants"
"github.com/vulpemventures/nigiri/cli/controller"
)
const (
liquid = true
bitcoin = false
delete = true
)
var (
stopCmd = []string{"stop"}
// deleteCmd = append(stopCmd, "--delete")
startCmd = []string{"start"}
// liquidStartCmd = append(startCmd, "--liquid")
)
func TestStartStopLiquid(t *testing.T) {
// Start/Stop
testStart(t, liquid)
testStop(t)
// Start/Delete
testStart(t, liquid)
testDelete(t)
}
func TestStartStopBitcoin(t *testing.T) {
// Start/Stop
testStart(t, bitcoin)
testStop(t)
// Start/Delete
testStart(t, bitcoin)
testDelete(t)
}
func TestStopBeforeStartShouldFail(t *testing.T) {
expectedError := constants.ErrNigiriNotRunning.Error()
err := testCommand("stop", "", !delete)
if err == nil {
t.Fatal("Should return error when trying to stop before starting")
}
if err.Error() != expectedError {
t.Fatalf("Expected error: %s, got: %s", expectedError, err)
}
expectedError = constants.ErrNigiriNotExisting.Error()
err = testCommand("stop", "", delete)
if err == nil {
t.Fatal("Should return error when trying to delete before starting")
}
if err.Error() != expectedError {
t.Fatalf("Expected error: %s, got: %s", expectedError, err)
}
}
func TestStartAfterStartShouldFail(t *testing.T) {
expectedError := constants.ErrNigiriAlreadyRunning.Error()
if err := testCommand("start", "", bitcoin); err != nil {
t.Fatal(err)
}
err := testCommand("start", "", bitcoin)
if err == nil {
t.Fatal("Should return error when trying to start Nigiri if already started")
}
if err.Error() != expectedError {
t.Fatalf("Expected error: %s, got: %s", expectedError, err)
}
err = testCommand("start", "", liquid)
if err == nil {
t.Fatal("Should return error when trying to start Nigiri if already started")
}
if err.Error() != expectedError {
t.Fatalf("Expected error: %s, got: %s", expectedError, err)
}
if err := testCommand("stop", "", delete); err != nil {
t.Fatal(err)
}
}
func testStart(t *testing.T, flag bool) {
ctl, err := controller.NewController()
if err != nil {
t.Fatal(err)
}
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 := ctl.IsNigiriRunning(); 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) {
ctl, err := controller.NewController()
if err != nil {
t.Fatal(err)
}
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 isStopped, err := ctl.IsNigiriStopped(); err != nil {
t.Fatal(err)
} else if !isStopped {
t.Fatal("Nigiri should have been stopped but services have not been found among stopped containers")
}
}
func testDelete(t *testing.T) {
ctl, err := controller.NewController()
if err != nil {
t.Fatal(err)
}
if err := testCommand("stop", "", delete); err != nil {
t.Fatal(err)
}
if isStopped, err := ctl.IsNigiriStopped(); err != nil {
t.Fatal(err)
} else if isStopped {
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 := RootCmd
cmd.SetArgs(nil)
if command == "start" {
args := append(startCmd, fmt.Sprintf("--liquid=%t", flag))
cmd.SetArgs(args)
}
if command == "stop" {
args := append(stopCmd, fmt.Sprintf("--delete=%t", flag))
cmd.SetArgs(args)
}
if command == "logs" {
logsCmd := []string{command, arg, fmt.Sprintf("--liquid=%t", flag)}
cmd.SetArgs(logsCmd)
}
if command == "faucet" {
faucetCmd := []string{command, arg, fmt.Sprintf("--liquid=%t", flag)}
cmd.SetArgs(faucetCmd)
}
if err := cmd.Execute(); err != nil {
return err
}
return nil
}