diff --git a/cmd/run_test.go b/cmd/run_test.go new file mode 100644 index 00000000..b9f60eef --- /dev/null +++ b/cmd/run_test.go @@ -0,0 +1,73 @@ +package cmd + +import ( + "fmt" + "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 wg sync.WaitGroup + wg.Add(1) + go func(wg *sync.WaitGroup) { + time.Sleep(100 * time.Millisecond) + + StopGracefully( + runCmd.Context(), + runCmd.Context(), + nil, + nil, + nil, + loggers[config.Default], + servers, + stopChan, + ) + + wg.Done() + }(&wg) + + wg.Add(1) + go func(wg *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. + fmt.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") + + wg.Done() + }(&wg) + + wg.Wait() + + // Clean up. + assert.NoError(t, os.Remove(pluginTestConfigFile)) + assert.NoError(t, os.Remove(globalTestConfigFile)) +}