Skip to content

Commit

Permalink
Add test for run command
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Sep 18, 2023
1 parent 94ca796 commit 6764208
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func StopGracefully(
}
for name, server := range servers {
logger.Info().Str("name", name).Msg("Stopping server")
server.Shutdown()
server.Shutdown() //nolint:contextcheck
span.AddEvent("Stopped server")
}
logger.Info().Msg("Stopped all servers")
Expand Down
72 changes: 72 additions & 0 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
"os"
"sync"
"testing"
"time"

"github.com/gatewayd-io/gatewayd/config"
"github.com/stretchr/testify/assert"
"github.com/zenizh/go-capturer"
)

func Test_runCmd(t *testing.T) {
// Create a test plugins config file.
_, err := executeCommandC(rootCmd, "plugin", "init", "--force", "-p", pluginTestConfigFile)
assert.NoError(t, err, "plugin init command should not have returned an error")
assert.FileExists(t, pluginTestConfigFile, "plugin init command should have created a config file")

// Create a test config file.
_, err = executeCommandC(rootCmd, "config", "init", "--force", "-c", globalTestConfigFile)
assert.NoError(t, err, "configInitCmd should not return an error")
// Check that the config file was created.
assert.FileExists(t, globalTestConfigFile, "configInitCmd should create a config file")

var waitGroup sync.WaitGroup
waitGroup.Add(1)
go func(waitGroup *sync.WaitGroup) {
time.Sleep(100 * time.Millisecond)

StopGracefully(
runCmd.Context(),
runCmd.Context(),
nil,
nil,
nil,
loggers[config.Default],
servers,
stopChan,
)

waitGroup.Done()
}(&waitGroup)

waitGroup.Add(1)
go func(waitGroup *sync.WaitGroup) {
// Test run command.
output := capturer.CaptureOutput(func() {
_, err := executeCommandC(rootCmd, "run", "-c", globalTestConfigFile, "-p", pluginTestConfigFile)
assert.NoError(t, err, "run command should not have returned an error")
})
// Print the output for debugging purposes.
runCmd.Print(output)
// Check if GatewayD started and stopped correctly.
assert.Contains(t,
output,
"GatewayD is running",
"run command should have returned the correct output")
assert.Contains(t,
output,
"Stopped all servers\n",
"run command should have returned the correct output")

waitGroup.Done()
}(&waitGroup)

waitGroup.Wait()

// Clean up.
assert.NoError(t, os.Remove(pluginTestConfigFile))
assert.NoError(t, os.Remove(globalTestConfigFile))
}

0 comments on commit 6764208

Please sign in to comment.