Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests #322

Merged
merged 20 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ linters-settings:
- "github.com/prometheus/client_model/go"
- "github.com/prometheus/common/expfmt"
- "github.com/panjf2000/gnet/v2"
- "github.com/spf13/cobra"
tagalign:
align: false
sort: false
Expand Down
25 changes: 25 additions & 0 deletions cmd/cmd_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"bytes"

"github.com/spf13/cobra"
)

var (
globalTestConfigFile = "./test_global.yaml"
pluginTestConfigFile = "./test_plugins.yaml"
)

// executeCommandC executes a cobra command and returns the command, output, and error.
// Taken from https://github.com/spf13/cobra/blob/0c72800b8dba637092b57a955ecee75949e79a73/command_test.go#L48.
func executeCommandC(root *cobra.Command, args ...string) (string, error) {
buf := new(bytes.Buffer)
root.SetOut(buf)
root.SetErr(buf)
root.SetArgs(args)

_, err := root.ExecuteC()

return buf.String(), err
}
33 changes: 33 additions & 0 deletions cmd/config_init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_configInitCmd(t *testing.T) {
// Test configInitCmd.
output, err := executeCommandC(rootCmd, "config", "init", "-c", globalTestConfigFile)
assert.NoError(t, err, "configInitCmd should not return an error")
assert.Equal(t,
fmt.Sprintf("Config file '%s' was created successfully.", globalTestConfigFile),
output,
"configInitCmd should print the correct output")
// Check that the config file was created.
assert.FileExists(t, globalTestConfigFile, "configInitCmd should create a config file")

// Test configInitCmd with the --force flag to overwrite the config file.
output, err = executeCommandC(rootCmd, "config", "init", "--force")
assert.NoError(t, err, "configInitCmd should not return an error")
assert.Equal(t,
fmt.Sprintf("Config file '%s' was overwritten successfully.", globalTestConfigFile),
output,
"configInitCmd should print the correct output")

// Clean up.
err = os.Remove(globalTestConfigFile)
assert.NoError(t, err)
}
33 changes: 33 additions & 0 deletions cmd/config_lint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_configLintCmd(t *testing.T) {
// Test configInitCmd.
output, err := executeCommandC(rootCmd, "config", "init", "-c", globalTestConfigFile)
assert.NoError(t, err, "configInitCmd should not return an error")
assert.Equal(t,
fmt.Sprintf("Config file '%s' was created successfully.", globalTestConfigFile),
output,
"configInitCmd should print the correct output")
// Check that the config file was created.
assert.FileExists(t, globalTestConfigFile, "configInitCmd should create a config file")

// Test configLintCmd.
output, err = executeCommandC(rootCmd, "config", "lint", "-c", globalTestConfigFile)
assert.NoError(t, err, "configLintCmd should not return an error")
assert.Equal(t,
"global config is valid\n",
output,
"configLintCmd should print the correct output")

// Clean up.
err = os.Remove(globalTestConfigFile)
assert.NoError(t, err)
}
31 changes: 31 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_configCmd(t *testing.T) {
// Test configCmd with no arguments.
output, err := executeCommandC(rootCmd, "config")
assert.NoError(t, err, "configCmd should not return an error")
assert.Equal(t,
`Manage GatewayD global configuration

Usage:
gatewayd config [flags]
gatewayd config [command]

Available Commands:
init Create or overwrite the GatewayD global config
lint Lint the GatewayD global config

Flags:
-h, --help help for config

Use "gatewayd config [command] --help" for more information about a command.
`,
output,
"configCmd should print the correct output")
}
24 changes: 24 additions & 0 deletions cmd/plugin_init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_pluginInitCmd(t *testing.T) {
// Test plugin init command.
output, err := executeCommandC(rootCmd, "plugin", "init", "-p", pluginTestConfigFile)
assert.NoError(t, err, "plugin init command should not have returned an error")
assert.Equal(t,
fmt.Sprintf("Config file '%s' was created successfully.", pluginTestConfigFile),
output,
"plugin init command should have returned the correct output")
assert.FileExists(t, pluginTestConfigFile, "plugin init command should have created a config file")

// Clean up.
err = os.Remove(pluginTestConfigFile)
assert.NoError(t, err)
}
18 changes: 11 additions & 7 deletions cmd/plugin_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ var pluginInstallCmd = &cobra.Command{
splittedURL := strings.Split(args[0], "@")
// If the version is not specified, use the latest version.
if len(splittedURL) < NumParts {
log.Println("Version not specified. Using latest version")
cmd.Println("Version not specified. Using latest version")
}
if len(splittedURL) >= NumParts {
pluginVersion = splittedURL[1]
Expand Down Expand Up @@ -138,7 +138,9 @@ var pluginInstallCmd = &cobra.Command{
strings.Contains(name, archiveExt)
})
if downloadURL != "" && releaseID != 0 {
downloadFile(client, account, pluginName, downloadURL, releaseID, pluginFilename)
cmd.Println("Downloading", downloadURL)
downloadFile(client, account, pluginName, releaseID, pluginFilename)
cmd.Println("Download completed successfully")
} else {
log.Panic("The plugin file could not be found in the release assets")
}
Expand All @@ -148,7 +150,9 @@ var pluginInstallCmd = &cobra.Command{
return strings.Contains(name, "checksums.txt")
})
if checksumsFilename != "" && downloadURL != "" && releaseID != 0 {
downloadFile(client, account, pluginName, downloadURL, releaseID, checksumsFilename)
cmd.Println("Downloading", downloadURL)
downloadFile(client, account, pluginName, releaseID, checksumsFilename)
cmd.Println("Download completed successfully")
} else {
log.Panic("The checksum file could not be found in the release assets")
}
Expand All @@ -174,13 +178,13 @@ var pluginInstallCmd = &cobra.Command{
log.Panic("Checksum verification failed")
}

log.Println("Checksum verification passed")
cmd.Println("Checksum verification passed")
break
}
}

if pullOnly {
log.Println("Plugin binary downloaded to", pluginFilename)
cmd.Println("Plugin binary downloaded to", pluginFilename)
return
}
} else {
Expand All @@ -204,7 +208,7 @@ var pluginInstallCmd = &cobra.Command{
pluginFileSum := ""
for _, filename := range filenames {
if strings.Contains(filename, pluginName) {
log.Println("Plugin binary extracted to", filename)
cmd.Println("Plugin binary extracted to", filename)
localPath = filename
// Get the checksum for the extracted plugin binary.
// TODO: Should we verify the checksum using the checksum.txt file instead?
Expand Down Expand Up @@ -303,7 +307,7 @@ var pluginInstallCmd = &cobra.Command{
}

// TODO: Add a rollback mechanism.
log.Println("Plugin installed successfully")
cmd.Println("Plugin installed successfully")
},
}

Expand Down
43 changes: 43 additions & 0 deletions cmd/plugin_install_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_pluginInstallCmd(t *testing.T) {
// Create a test plugin config file.
output, err := executeCommandC(rootCmd, "plugin", "init", "-p", pluginTestConfigFile)
assert.NoError(t, err, "plugin init should not return an error")
assert.Equal(t,
fmt.Sprintf("Config file '%s' was created successfully.", pluginTestConfigFile),
output,
"plugin init command should have returned the correct output")
assert.FileExists(t, pluginTestConfigFile, "plugin init command should have created a config file")

// Test plugin install command.
output, err = executeCommandC(
rootCmd, "plugin", "install",
"github.com/gatewayd-io/gatewayd-plugin-cache@v0.2.4", "-p", pluginTestConfigFile)
assert.NoError(t, err, "plugin install should not return an error")
assert.Contains(t, output, "Downloading https://github.com/gatewayd-io/gatewayd-plugin-cache/releases/download/v0.2.4/gatewayd-plugin-cache-linux-amd64-v0.2.4.tar.gz") //nolint:lll
assert.Contains(t, output, "Downloading https://github.com/gatewayd-io/gatewayd-plugin-cache/releases/download/v0.2.4/checksums.txt") //nolint:lll
assert.Contains(t, output, "Download completed successfully")
assert.Contains(t, output, "Checksum verification passed")
assert.Contains(t, output, "Plugin binary extracted to plugins/gatewayd-plugin-cache")
assert.Contains(t, output, "Plugin installed successfully")

// See if the plugin was actually installed.
output, err = executeCommandC(rootCmd, "plugin", "list", "-p", pluginTestConfigFile)
assert.NoError(t, err, "plugin list should not return an error")
assert.Contains(t, output, "Name: gatewayd-plugin-cache")

// Clean up.
assert.NoError(t, os.RemoveAll("plugins/"))
assert.NoError(t, os.Remove("checksums.txt"))
assert.NoError(t, os.Remove("gatewayd-plugin-cache-linux-amd64-v0.2.4.tar.gz"))
assert.NoError(t, os.Remove(pluginTestConfigFile))
}
17 changes: 17 additions & 0 deletions cmd/plugin_lint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_pluginLintCmd(t *testing.T) {
// Test plugin lint command.
output, err := executeCommandC(rootCmd, "plugin", "lint", "-p", "../gatewayd_plugins.yaml")
assert.NoError(t, err, "plugin lint command should not have returned an error")
assert.Equal(t,
"plugins config is valid\n",
output,
"plugin lint command should have returned the correct output")
}
63 changes: 63 additions & 0 deletions cmd/plugin_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_pluginListCmd(t *testing.T) {
// Test plugin list command.
output, err := executeCommandC(rootCmd, "plugin", "init", "-p", pluginTestConfigFile)
assert.NoError(t, err, "plugin init command should not have returned an error")
assert.Equal(t,
fmt.Sprintf("Config file '%s' was created successfully.", pluginTestConfigFile),
output,
"plugin init command should have returned the correct output")
assert.FileExists(t, pluginTestConfigFile, "plugin init command should have created a config file")

output, err = executeCommandC(rootCmd, "plugin", "list", "-p", pluginTestConfigFile)
assert.NoError(t, err, "plugin list command should not have returned an error")
assert.Equal(t,
"No plugins found\n",
output,
"plugin list command should have returned empty output")

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

func Test_pluginListCmdWithPlugins(t *testing.T) {
// Test plugin list command.
// Read the plugin config file from the root directory.
pluginTestConfigFile := "../gatewayd_plugins.yaml"
output, err := executeCommandC(rootCmd, "plugin", "list", "-p", pluginTestConfigFile)
assert.NoError(t, err, "plugin list command should not have returned an error")
assert.Equal(t, `Total plugins: 1
Plugins:
Name: gatewayd-plugin-cache
Enabled: true
Path: ../gatewayd-plugin-cache/gatewayd-plugin-cache
Args: --log-level debug
Env:
MAGIC_COOKIE_KEY=GATEWAYD_PLUGIN
MAGIC_COOKIE_VALUE=5712b87aa5d7e9f9e9ab643e6603181c5b796015cb1c09d6f5ada882bf2a1872
REDIS_URL=redis://localhost:6379/0
EXPIRY=1h
METRICS_ENABLED=True
METRICS_UNIX_DOMAIN_SOCKET=/tmp/gatewayd-plugin-cache.sock
METRICS_PATH=/metrics
PERIODIC_INVALIDATOR_ENABLED=True
PERIODIC_INVALIDATOR_INTERVAL=1m
PERIODIC_INVALIDATOR_START_DELAY=1m
API_ADDRESS=localhost:18080
EXIT_ON_STARTUP_ERROR=False
SENTRY_DSN=https://70eb1abcd32e41acbdfc17bc3407a543@o4504550475038720.ingest.sentry.io/4505342961123328
Checksum: 054e7dba9c1e3e3910f4928a000d35c8a6199719fad505c66527f3e9b1993833
`,
output,
"plugin list command should have returned the correct output")
}
32 changes: 32 additions & 0 deletions cmd/plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_pluginCmd(t *testing.T) {
// Test pluginCmd with no arguments.
output, err := executeCommandC(rootCmd, "plugin")
assert.NoError(t, err, "pluginCmd should not return an error")
assert.Equal(t, `Manage plugins and their configuration

Usage:
gatewayd plugin [flags]
gatewayd plugin [command]

Available Commands:
init Create or overwrite the GatewayD plugins config
install Install a plugin from a local archive or a GitHub repository
lint Lint the GatewayD plugins config
list List the GatewayD plugins

Flags:
-h, --help help for plugin

Use "gatewayd plugin [command] --help" for more information about a command.
`,
output,
"pluginCmd should print the correct output")
}
Loading