From e3d993c3aca364d332bba96fa0d64e836615ca1c Mon Sep 17 00:00:00 2001 From: zulkhair Date: Thu, 1 Feb 2024 19:23:08 +0700 Subject: [PATCH] test: add unit test on restart and dev command --- cmd/world/cardinal/restart.go | 11 +++ cmd/world/root/root_test.go | 137 +++++++++++++++++++++++++++++----- 2 files changed, 130 insertions(+), 18 deletions(-) diff --git a/cmd/world/cardinal/restart.go b/cmd/world/cardinal/restart.go index 79690b2..0ce1032 100644 --- a/cmd/world/cardinal/restart.go +++ b/cmd/world/cardinal/restart.go @@ -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{ @@ -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{ diff --git a/cmd/world/root/root_test.go b/cmd/world/root/root_test.go index bc1d9ef..4cdef9b 100644 --- a/cmd/world/root/root_test.go +++ b/cmd/world/root/root_test.go @@ -6,14 +6,37 @@ 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" ) +type healthResponse struct { + StatusCode int + IsServerRunning bool + IsGameLoopRunning bool +} + +func getHealthCheck() (*healthResponse, error) { + var healtCheck healthResponse + + resp, err := http.Get("http://127.0.0.1:3333/health") + if err != nil { + return nil, err + } + err = json.NewDecoder(resp.Body).Decode(&healtCheck) + if err != nil { + return nil, err + } + + healtCheck.StatusCode = resp.StatusCode + return &healtCheck, nil +} + // outputFromCmd runs the rootCmd with the given cmd arguments and returns the output of the command along with // any errors. func outputFromCmd(cobra *cobra.Command, cmd string) (lines []string, err error) { @@ -99,13 +122,20 @@ func TestExecuteDoctorCommand(t *testing.T) { } } -func TestCreateStartStopPurge(t *testing.T) { +func TestCreateStartStopRestartPurge(t *testing.T) { // Create Cardinal gameDir, err := os.MkdirTemp("", "game-template") assert.NilError(t, err) // Remove dir - defer os.RemoveAll(gameDir) + defer func() { + err = os.RemoveAll(gameDir) + assert.NilError(t, err) + }() + + // Change dir + err = os.Chdir(gameDir) + assert.NilError(t, err) // set tea ouput to variable teaOut := &bytes.Buffer{} @@ -116,37 +146,108 @@ func TestCreateStartStopPurge(t *testing.T) { assert.NilError(t, err) // Start cardinal - os.Chdir(gameDir) rootCmd.SetArgs([]string{"cardinal", "start", "--build", "--detach"}) err = rootCmd.Execute() assert.NilError(t, err) + defer func() { + // Purge cardinal + rootCmd.SetArgs([]string{"cardinal", "purge"}) + err = rootCmd.Execute() + assert.NilError(t, err) + }() + // Check cardinal health - resp, err := http.Get("http://127.0.0.1:3333/health") + resp, err := getHealthCheck() assert.NilError(t, err) assert.Equal(t, resp.StatusCode, 200) - var healthResponse struct { - IsServerRunning bool - IsGameLoopRunning bool - } - err = json.NewDecoder(resp.Body).Decode(&healthResponse) + assert.Assert(t, resp.IsServerRunning) + assert.Assert(t, resp.IsGameLoopRunning) + + // Restart cardinal + rootCmd.SetArgs([]string{"cardinal", "restart", "--detach"}) + err = rootCmd.Execute() + assert.NilError(t, err) + + // Check cardinal health after restart + resp, err = getHealthCheck() assert.NilError(t, err) - assert.Assert(t, healthResponse.IsServerRunning) - assert.Assert(t, healthResponse.IsGameLoopRunning) + assert.Equal(t, resp.StatusCode, 200) + assert.Assert(t, resp.IsServerRunning) + assert.Assert(t, resp.IsGameLoopRunning) // Stop cardinal rootCmd.SetArgs([]string{"cardinal", "stop"}) err = rootCmd.Execute() assert.NilError(t, err) - // Check cardinal health - _, err = http.Get("http://127.0.0.1:3333/health") + // Check cardinal health (expected error) + _, err = getHealthCheck() 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") + assert.NilError(t, err) + + // Remove dir + defer func() { + err = os.RemoveAll(gameDir) + assert.NilError(t, err) + }() + + // Change dir + err = 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) + // 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 and building the apps + isServerRunning := false + isGameLoopRunning := false + timeout := time.Now().Add(300 * time.Second) + for !(isServerRunning && isGameLoopRunning) && time.Now().Before(timeout) { + resp, err := getHealthCheck() + if err != nil { + time.Sleep(1 * time.Second) + continue + } + assert.Equal(t, resp.StatusCode, 200) + isServerRunning = resp.IsServerRunning + isGameLoopRunning = resp.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 = getHealthCheck() + 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") }