-
Notifications
You must be signed in to change notification settings - Fork 19
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 test for plugin scaffold
command
#613
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0a945fd
Implement scaffold tests (wip)
zeina1i eaed72d
Fix issues
mostafa 82515d4
Add test for plugin scaffold command
mostafa 7aa210e
Fix typo
mostafa c554ba2
Add package to allow list
mostafa 07f1338
Include plugin template when testing
mostafa d7cf221
Address comments by @sinadarbouy
mostafa df4b0a7
Fix linter issues
mostafa b69f0be
Remove unnecessary os.RemoveAll
mostafa 9e4bcb2
Use latest version of golangci-lint command and action
mostafa efc8668
Each call to t.TempDir return a new directory
mostafa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/codingsince1985/checksum" | ||
"github.com/gatewayd-io/gatewayd/config" | ||
"github.com/gatewayd-io/gatewayd/plugin" | ||
"github.com/gatewayd-io/gatewayd/testhelpers" | ||
"github.com/spf13/cast" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
yamlv3 "gopkg.in/yaml.v3" | ||
) | ||
|
||
func Test_pluginScaffoldCmd(t *testing.T) { | ||
// Start the test containers. | ||
ctx := context.Background() | ||
postgresHostIP1, postgresMappedPort1 := testhelpers.SetupPostgreSQLTestContainer(ctx, t) | ||
postgresHostIP2, postgresMappedPort2 := testhelpers.SetupPostgreSQLTestContainer(ctx, t) | ||
postgresAddress1 := postgresHostIP1 + ":" + postgresMappedPort1.Port() | ||
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgresAddress1) | ||
postgresAddress2 := postgresHostIP2 + ":" + postgresMappedPort2.Port() | ||
t.Setenv("GATEWAYD_CLIENTS_TEST_WRITE_ADDRESS", postgresAddress2) | ||
|
||
globalTestConfigFile := filepath.Join("testdata", "gatewayd.yaml") | ||
plugin.IsPluginTemplateEmbedded() | ||
pluginTestScaffoldInputFile := "./testdata/scaffold_input.yaml" | ||
|
||
tempDir := t.TempDir() | ||
|
||
output, err := executeCommandC( | ||
rootCmd, "plugin", "scaffold", "-i", pluginTestScaffoldInputFile, "-o", tempDir) | ||
require.NoError(t, err, "plugin scaffold should not return an error") | ||
assert.Contains(t, output, "scaffold done") | ||
assert.Contains(t, output, "created files:") | ||
assert.Contains(t, output, "test-gatewayd-plugin/.github/issue_template.md") | ||
assert.Contains(t, output, "test-gatewayd-plugin/.github/pull_request_template.md") | ||
assert.Contains(t, output, "test-gatewayd-plugin/.github/workflows/commits-signed.yaml") | ||
|
||
pluginName := "test-gatewayd-plugin" | ||
pluginDir := filepath.Join(tempDir, pluginName) | ||
|
||
pluginsConfig, err := os.ReadFile(filepath.Join(pluginDir, "gatewayd_plugin.yaml")) | ||
require.NoError(t, err, "reading plugins config file should not return an error") | ||
|
||
var localPluginsConfig map[string]interface{} | ||
err = yamlv3.Unmarshal(pluginsConfig, &localPluginsConfig) | ||
require.NoError(t, err, "unmarshalling yaml file should not return error") | ||
|
||
err = runCommand(pluginDir, "go", "mod", "tidy") | ||
require.NoError(t, err, "running go mod tidy should not return an error") | ||
err = runCommand(pluginDir, "make", "build-dev") | ||
require.NoError(t, err, "running make build-dev should not return an error") | ||
|
||
pluginBinaryPath := filepath.Join(pluginDir, pluginName) | ||
|
||
_, err = os.Stat(pluginBinaryPath) | ||
require.NoError(t, err, "plugin binary file should exist") | ||
|
||
pluginsList := cast.ToSlice(localPluginsConfig["plugins"]) | ||
plugin := cast.ToStringMap(pluginsList[0]) | ||
plugin["localPath"] = filepath.Join("cmd", pluginDir, pluginName) | ||
sum, err := checksum.SHA256sum(pluginBinaryPath) | ||
require.NoError(t, err, "marshalling yaml file should not return error") | ||
plugin["checksum"] = sum | ||
|
||
pluginsList[0] = plugin | ||
plugins := make(map[string]interface{}) | ||
plugins["plugins"] = pluginsList | ||
|
||
updatedPluginConfig, err := yamlv3.Marshal(plugins) | ||
require.NoError(t, err, "marshalling yaml file should not return error") | ||
|
||
err = os.WriteFile( | ||
filepath.Join(pluginDir, "gatewayd_plugins.yaml"), | ||
updatedPluginConfig, FilePermissions) | ||
require.NoError(t, err, "writingh to yaml file should not return error") | ||
|
||
pluginTestConfigFile := filepath.Join(pluginDir, "gatewayd_plugins.yaml") | ||
|
||
stopChan = make(chan struct{}) | ||
|
||
var waitGroup sync.WaitGroup | ||
|
||
waitGroup.Add(1) | ||
go func(waitGroup *sync.WaitGroup) { | ||
// Test run command. | ||
output, err := executeCommandC( | ||
rootCmd, "run", "-c", globalTestConfigFile, "-p", pluginTestConfigFile) | ||
require.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") | ||
assert.Contains(t, output, "Stopped all servers") | ||
|
||
waitGroup.Done() | ||
}(&waitGroup) | ||
|
||
waitGroup.Add(1) | ||
go func(waitGroup *sync.WaitGroup) { | ||
time.Sleep(waitBeforeStop) | ||
|
||
StopGracefully( | ||
context.Background(), | ||
nil, | ||
nil, | ||
metricsServer, | ||
nil, | ||
loggers[config.Default], | ||
servers, | ||
stopChan, | ||
nil, | ||
nil, | ||
) | ||
|
||
waitGroup.Done() | ||
}(&waitGroup) | ||
|
||
waitGroup.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
remote_url: https://github.com/gatewayd-io/test-gatewayd-plugin | ||
version: 0.1 | ||
description: This is test plugin | ||
license: MIT | ||
authors: | ||
- GatewayD Team |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we use
test-gateway-plugin
multiple times, we can have a valuable for it likepluginDir := filepath.Join(t.TempDir(), "test-gatewayd-plugin")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can use t.TempDir() to avoid polluting the working directory - https://pkg.go.dev/testing#B.TempDir
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in d7cf221.