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 e4c8497
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -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

Check failure on line 27 in cmd/run_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

variable name 'wg' is too short for the scope of its usage (varnamelen)
wg.Add(1)
go func(wg *sync.WaitGroup) {

Check failure on line 29 in cmd/run_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

parameter name 'wg' is too short for the scope of its usage (varnamelen)
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) {

Check failure on line 47 in cmd/run_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

parameter name 'wg' is too short for the scope of its usage (varnamelen)
// 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 failure on line 54 in cmd/run_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

use of `fmt.Print` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
// 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))
}

0 comments on commit e4c8497

Please sign in to comment.