Skip to content

Commit

Permalink
test: add unit test on restart and dev command
Browse files Browse the repository at this point in the history
  • Loading branch information
zulkhair committed Feb 6, 2024
1 parent 66b2b6f commit 641c402
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 11 deletions.
11 changes: 11 additions & 0 deletions cmd/world/cardinal/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
// Cobra Setup //
/////////////////

func init() {
restartCmd.Flags().Bool(flagDetach, false, "Run in detached mode")
}

// restartCmd restarts your Cardinal game shard stack
// Usage: `world cardinal restart`
var restartCmd = &cobra.Command{
Expand All @@ -30,6 +34,13 @@ This will restart the following Docker services:
return err
}
cfg.Build = true
if replaceBoolWithFlag(cmd, flagDebug, &cfg.Debug); err != nil {
return err
}

if replaceBoolWithFlag(cmd, flagDetach, &cfg.Detach); err != nil {
return err
}

if cfg.Debug {
err = tea_cmd.DockerRestart(cfg, []tea_cmd.DockerService{
Expand Down
105 changes: 94 additions & 11 deletions cmd/world/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"errors"
"fmt"
"github.com/spf13/cobra"
"gotest.tools/v3/assert"
"net/http"
"os"
"pkg.world.dev/world-cli/cmd/world/cardinal"
"strings"
"testing"

"gotest.tools/v3/assert"
"time"
)

// outputFromCmd runs the rootCmd with the given cmd arguments and returns the output of the command along with
Expand Down Expand Up @@ -99,14 +100,12 @@ func TestExecuteDoctorCommand(t *testing.T) {
}
}

func TestCreateStartStopPurge(t *testing.T) {
func TestCreateStartStopRestartPurge(t *testing.T) {
// Create Cardinal
gameDir, err := os.MkdirTemp("", "game-template")
os.Chdir(gameDir)
assert.NilError(t, err)

// Remove dir
defer os.RemoveAll(gameDir)

// set tea ouput to variable
teaOut := &bytes.Buffer{}
createCmd := getCreateCmd(teaOut)
Expand All @@ -115,8 +114,17 @@ func TestCreateStartStopPurge(t *testing.T) {
err = createCmd.Execute()
assert.NilError(t, err)

defer func() {
// Purge cardinal
rootCmd.SetArgs([]string{"cardinal", "purge"})
err = rootCmd.Execute()
assert.NilError(t, err)

// Remove dir
os.RemoveAll(gameDir)
}()

// Start cardinal
os.Chdir(gameDir)
rootCmd.SetArgs([]string{"cardinal", "start", "--build", "--detach"})
err = rootCmd.Execute()
assert.NilError(t, err)
Expand All @@ -134,19 +142,94 @@ func TestCreateStartStopPurge(t *testing.T) {
assert.Assert(t, healthResponse.IsServerRunning)
assert.Assert(t, healthResponse.IsGameLoopRunning)

// Restart cardinal
rootCmd.SetArgs([]string{"cardinal", "restart", "--detach"})
err = rootCmd.Execute()
assert.NilError(t, err)

// Check cardinal health after restart
resp, err = http.Get("http://127.0.0.1:3333/health")
assert.NilError(t, err)
assert.Equal(t, resp.StatusCode, 200)
err = json.NewDecoder(resp.Body).Decode(&healthResponse)
assert.NilError(t, err)
assert.Assert(t, healthResponse.IsServerRunning)
assert.Assert(t, healthResponse.IsGameLoopRunning)

// Stop cardinal
rootCmd.SetArgs([]string{"cardinal", "stop"})
err = rootCmd.Execute()
assert.NilError(t, err)

// Check cardinal health
// Check cardinal health (expected error)
_, err = http.Get("http://127.0.0.1:3333/health")
assert.Error(t, err,
"Get \"http://127.0.0.1:3333/health\": dial tcp 127.0.0.1:3333: connect: connection refused")
}

// Purge cardinal
rootCmd.SetArgs([]string{"cardinal", "purge"})
err = rootCmd.Execute()
func TestDev(t *testing.T) {
// Create Cardinal
gameDir, err := os.MkdirTemp("", "game-template")
os.Chdir(gameDir)
assert.NilError(t, err)

//set tea ouput to variable
teaOut := &bytes.Buffer{}
createCmd := getCreateCmd(teaOut)
createCmd.SetArgs([]string{gameDir})

err = createCmd.Execute()
assert.NilError(t, err)

defer func() {
// Remove dir
os.RemoveAll(gameDir)
}()

// Start cardinal dev
rootCmd.SetArgs([]string{"cardinal", "dev"})
go func() {
err := rootCmd.Execute()
assert.NilError(t, err)
}()

// Check cardinal health for 300 second, waiting to download dependencies
isServerRunning := false
isGameLoopRunning := false
count := 0
for !(isServerRunning && isGameLoopRunning) && count < 300 {
resp, err := http.Get("http://127.0.0.1:3333/health")
if err != nil {
count++
time.Sleep(1 * time.Second)
continue
}
var healthResponse struct {
IsServerRunning bool
IsGameLoopRunning bool
}
err = json.NewDecoder(resp.Body).Decode(&healthResponse)
assert.NilError(t, err)
isServerRunning = healthResponse.IsServerRunning
isGameLoopRunning = healthResponse.IsGameLoopRunning
}
assert.Assert(t, isServerRunning)
assert.Assert(t, isGameLoopRunning)

// Shutdown the program
close(cardinal.StopChan)

// Check cardinal health (expected error), trying for 10 times
count = 0
for count < 10 {
_, err = http.Get("http://127.0.0.1:3333/health")
if err != nil {
break
}
time.Sleep(1 * time.Second)
count++
}

assert.Error(t, err,
"Get \"http://127.0.0.1:3333/health\": dial tcp 127.0.0.1:3333: connect: connection refused")
}

0 comments on commit 641c402

Please sign in to comment.