From 0a945fdfaddc66cc295c71e0dff64bfcefb219d3 Mon Sep 17 00:00:00 2001 From: zeina1i Date: Thu, 9 May 2024 23:54:08 +0200 Subject: [PATCH 01/11] Implement scaffold tests (wip) --- cmd/plugin_scaffold_test.go | 105 ++++++++++++++++++++++++++++ cmd/testdata/scaffold_input.yaml | 6 ++ plugin/.template/input.example.yaml | 2 +- 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 cmd/plugin_scaffold_test.go create mode 100644 cmd/testdata/scaffold_input.yaml diff --git a/cmd/plugin_scaffold_test.go b/cmd/plugin_scaffold_test.go new file mode 100644 index 00000000..388b8f98 --- /dev/null +++ b/cmd/plugin_scaffold_test.go @@ -0,0 +1,105 @@ +package cmd + +import ( + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/codingsince1985/checksum" + "github.com/gatewayd-io/gatewayd/plugin" + "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) { + globalTestConfigFile := filepath.Join("testdata", "gatewayd.yaml") + plugin.IsPluginTemplateEmbedded() + pluginTestScaffoldInputFile := "./testdata/scaffold_input.yaml" + + output, err := executeCommandC( + rootCmd, "plugin", "scaffold", + "-i", pluginTestScaffoldInputFile) + 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") + + pluginsConfig, err := os.ReadFile(filepath.Join("plugins", "test-gatewayd-plugin", "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 = os.ReadFile(filepath.Join("plugins", "test-gatewayd-plugin", "test-gatewayd-plugin")) + require.NoError(t, err, "reading plugin binary file should not return an error") + + pluginsList := cast.ToSlice(localPluginsConfig["plugins"]) + plugin := cast.ToStringMap(pluginsList[0]) + pluginsList[0] = plugin + plugin["localPath"] = filepath.Join("cmd", "plugins", "test-gatewayd-plugin", "test-gatewayd-plugin") + sum, err := checksum.SHA256sum(filepath.Join("plugins", "test-gatewayd-plugin", "test-gatewayd-plugin")) + 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("plugins", "test-gatewayd-plugin", "gatewayd_plugins.yaml"), updatedPluginConfig, FilePermissions) + require.NoError(t, err, "writingh to yaml file should not return error") + + output, err = executeCommandC(rootCmd, "run", "-p", filepath.Join("plugins", "test-gatewayd-plugin", "gatewayd_plugins.yaml"), "-c", globalTestConfigFile) + require.NoError(t, err, "run command should not have returned an error") + fmt.Println(output) + + // var waitGroup sync.WaitGroup + // waitGroup.Add(1) + // go func(waitGroup *sync.WaitGroup) { + // fmt.Println("sasasaas") + // // Test run command. + // _, err := executeCommandC(rootCmd, "run", "-p", filepath.Join("plugins", "test-gatewayd-plugin", "gatewayd_plugins.yaml"), "-c", globalTestConfigFile) + // require.NoError(t, err, "run command should not have returned an error") + // fmt.Println("2121") + + // // // 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.Wait() + + // waitGroup.Add(1) + // go func(waitGroup *sync.WaitGroup) { + // time.Sleep(waitBeforeStop * 2) + + // StopGracefully( + // context.Background(), + // nil, + // nil, + // metricsServer, + // nil, + // loggers[config.Default], + // servers, + // stopChan, + // nil, + // nil, + // ) + + // waitGroup.Done() + // }(&waitGroup) + + // waitGroup.Wait() +} diff --git a/cmd/testdata/scaffold_input.yaml b/cmd/testdata/scaffold_input.yaml new file mode 100644 index 00000000..db04542d --- /dev/null +++ b/cmd/testdata/scaffold_input.yaml @@ -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 \ No newline at end of file diff --git a/plugin/.template/input.example.yaml b/plugin/.template/input.example.yaml index 517e4bb5..db04542d 100644 --- a/plugin/.template/input.example.yaml +++ b/plugin/.template/input.example.yaml @@ -1,4 +1,4 @@ -remote_url: https://github.com/gatewayd/test-gatewayd-plugin +remote_url: https://github.com/gatewayd-io/test-gatewayd-plugin version: 0.1 description: This is test plugin license: MIT From eaed72d3d32d0bbec8ab18aba79a5d05ceac20cc Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Fri, 10 May 2024 22:44:10 +0200 Subject: [PATCH 02/11] Fix issues --- Makefile | 2 +- cmd/plugin_scaffold_test.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b3bf09f1..78fe74d5 100644 --- a/Makefile +++ b/Makefile @@ -82,7 +82,7 @@ clean: @rm -rf dist test: - @go test -v -cover -coverprofile=c.out ./... + @go test -tags embed_plugin_template -v -cover -coverprofile=c.out ./... test-race: @go test -race -v ./... diff --git a/cmd/plugin_scaffold_test.go b/cmd/plugin_scaffold_test.go index 388b8f98..6a77315f 100644 --- a/cmd/plugin_scaffold_test.go +++ b/cmd/plugin_scaffold_test.go @@ -36,6 +36,8 @@ func Test_pluginScaffoldCmd(t *testing.T) { err = yamlv3.Unmarshal(pluginsConfig, &localPluginsConfig) require.NoError(t, err, "unmarshalling yaml file should not return error") + // TODO: build the plugin binary and check that it is valid. + _, err = os.ReadFile(filepath.Join("plugins", "test-gatewayd-plugin", "test-gatewayd-plugin")) require.NoError(t, err, "reading plugin binary file should not return an error") From 82515d4113b9ffbad41c6c532ada9b5db890c98c Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Wed, 2 Oct 2024 23:15:14 +0200 Subject: [PATCH 03/11] Add test for plugin scaffold command --- cmd/plugin_scaffold_test.go | 127 +++++++++++++++++++++--------------- 1 file changed, 76 insertions(+), 51 deletions(-) diff --git a/cmd/plugin_scaffold_test.go b/cmd/plugin_scaffold_test.go index 6a77315f..fe0478e5 100644 --- a/cmd/plugin_scaffold_test.go +++ b/cmd/plugin_scaffold_test.go @@ -1,13 +1,18 @@ package cmd import ( - "fmt" + "context" "os" + "os/exec" "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" @@ -15,13 +20,21 @@ import ( ) 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" output, err := executeCommandC( - rootCmd, "plugin", "scaffold", - "-i", pluginTestScaffoldInputFile) + rootCmd, "plugin", "scaffold", "-i", pluginTestScaffoldInputFile) require.NoError(t, err, "plugin scaffold should not return an error") assert.Contains(t, output, "scaffold done") assert.Contains(t, output, "created files:") @@ -29,14 +42,23 @@ func Test_pluginScaffoldCmd(t *testing.T) { assert.Contains(t, output, "test-gatewayd-plugin/.github/pull_request_template.md") assert.Contains(t, output, "test-gatewayd-plugin/.github/workflows/commits-signed.yaml") - pluginsConfig, err := os.ReadFile(filepath.Join("plugins", "test-gatewayd-plugin", "gatewayd_plugin.yaml")) + pluginsConfig, err := os.ReadFile( + filepath.Join("plugins", "test-gatewayd-plugin", "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") - // TODO: build the plugin binary and check that it is valid. + tidy := exec.Command("go", "mod", "tidy") + tidy.Dir = filepath.Join("plugins", "test-gatewayd-plugin") + err = tidy.Run() + assert.NoError(t, err) + + build := exec.Command("make", "build-dev") + build.Dir = filepath.Join("plugins", "test-gatewayd-plugin") + err = build.Run() + assert.NoError(t, err) _, err = os.ReadFile(filepath.Join("plugins", "test-gatewayd-plugin", "test-gatewayd-plugin")) require.NoError(t, err, "reading plugin binary file should not return an error") @@ -56,52 +78,55 @@ func Test_pluginScaffoldCmd(t *testing.T) { updatedPluginConfig, err := yamlv3.Marshal(plugins) require.NoError(t, err, "marshalling yaml file should not return error") - err = os.WriteFile(filepath.Join("plugins", "test-gatewayd-plugin", "gatewayd_plugins.yaml"), updatedPluginConfig, FilePermissions) + err = os.WriteFile( + filepath.Join("plugins", "test-gatewayd-plugin", "gatewayd_plugins.yaml"), + updatedPluginConfig, FilePermissions) require.NoError(t, err, "writingh to yaml file should not return error") - output, err = executeCommandC(rootCmd, "run", "-p", filepath.Join("plugins", "test-gatewayd-plugin", "gatewayd_plugins.yaml"), "-c", globalTestConfigFile) - require.NoError(t, err, "run command should not have returned an error") - fmt.Println(output) - - // var waitGroup sync.WaitGroup - // waitGroup.Add(1) - // go func(waitGroup *sync.WaitGroup) { - // fmt.Println("sasasaas") - // // Test run command. - // _, err := executeCommandC(rootCmd, "run", "-p", filepath.Join("plugins", "test-gatewayd-plugin", "gatewayd_plugins.yaml"), "-c", globalTestConfigFile) - // require.NoError(t, err, "run command should not have returned an error") - // fmt.Println("2121") - - // // // 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.Wait() - - // waitGroup.Add(1) - // go func(waitGroup *sync.WaitGroup) { - // time.Sleep(waitBeforeStop * 2) - - // StopGracefully( - // context.Background(), - // nil, - // nil, - // metricsServer, - // nil, - // loggers[config.Default], - // servers, - // stopChan, - // nil, - // nil, - // ) - - // waitGroup.Done() - // }(&waitGroup) - - // waitGroup.Wait() + pluginTestConfigFile := filepath.Join( + "plugins", "test-gatewayd-plugin", "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() + + assert.NoError(t, os.RemoveAll("plugins")) } From 7aa210e37d189ee5b716e4afc13490de7666c2b6 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Wed, 2 Oct 2024 23:15:38 +0200 Subject: [PATCH 04/11] Fix typo --- cmd/run_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/run_test.go b/cmd/run_test.go index 823189cc..534d203a 100644 --- a/cmd/run_test.go +++ b/cmd/run_test.go @@ -20,8 +20,8 @@ var ( func Test_runCmd(t *testing.T) { postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t) - postgredAddress := postgresHostIP + ":" + postgresMappedPort.Port() - t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgredAddress) + postgresAddress := postgresHostIP + ":" + postgresMappedPort.Port() + t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgresAddress) globalTestConfigFile := "./test_global_runCmd.yaml" pluginTestConfigFile := "./test_plugins_runCmd.yaml" @@ -84,8 +84,8 @@ func Test_runCmd(t *testing.T) { // Test_runCmdWithTLS tests the run command with TLS enabled on the server. func Test_runCmdWithTLS(t *testing.T) { postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t) - postgredAddress := postgresHostIP + ":" + postgresMappedPort.Port() - t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgredAddress) + postgresAddress := postgresHostIP + ":" + postgresMappedPort.Port() + t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgresAddress) globalTLSTestConfigFile := "./testdata/gatewayd_tls.yaml" pluginTestConfigFile := "./test_plugins_runCmdWithTLS.yaml" @@ -144,11 +144,11 @@ func Test_runCmdWithTLS(t *testing.T) { // Test_runCmdWithMultiTenancy tests the run command with multi-tenancy enabled. func Test_runCmdWithMultiTenancy(t *testing.T) { postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t) - postgredAddress := postgresHostIP + ":" + postgresMappedPort.Port() - t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgredAddress) + postgresAddress := postgresHostIP + ":" + postgresMappedPort.Port() + t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgresAddress) postgresHostIP2, postgresMappedPort2 := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t) - postgredAddress2 := postgresHostIP2 + ":" + postgresMappedPort2.Port() - t.Setenv("GATEWAYD_CLIENTS_TEST_WRITE_ADDRESS", postgredAddress2) + postgresAddress2 := postgresHostIP2 + ":" + postgresMappedPort2.Port() + t.Setenv("GATEWAYD_CLIENTS_TEST_WRITE_ADDRESS", postgresAddress2) globalTestConfigFile := "./testdata/gatewayd.yaml" pluginTestConfigFile := "./test_plugins_runCmdWithMultiTenancy.yaml" @@ -208,8 +208,8 @@ func Test_runCmdWithMultiTenancy(t *testing.T) { func Test_runCmdWithCachePlugin(t *testing.T) { postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t) - postgredAddress := postgresHostIP + ":" + postgresMappedPort.Port() - t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgredAddress) + postgresAddress := postgresHostIP + ":" + postgresMappedPort.Port() + t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgresAddress) globalTestConfigFile := "./test_global_runCmdWithCachePlugin.yaml" pluginTestConfigFile := "./test_plugins_runCmdWithCachePlugin.yaml" From c554ba2ee25abf229c98375d617af41db01cb340 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Wed, 2 Oct 2024 23:22:07 +0200 Subject: [PATCH 05/11] Add package to allow list --- .golangci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yaml b/.golangci.yaml index 4a617b33..ddab1d5b 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -91,6 +91,7 @@ linters-settings: - "github.com/testcontainers/testcontainers-go" - "github.com/redis/go-redis/v9" - "github.com/docker/go-connections/nat" + - "github.com/codingsince1985/checksum" tagalign: align: false sort: false From 07f1338de3398f5b24d82126e6b118bb422dfdce Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Wed, 2 Oct 2024 23:25:38 +0200 Subject: [PATCH 06/11] Include plugin template when testing --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index ee6d355d..0c13fb5f 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -82,7 +82,7 @@ jobs: dockerfile: Dockerfile - name: Run Go tests 🔬 - run: go test -p 1 -cover -covermode atomic -coverprofile=profile.cov -v ./... + run: go test -tags embed_plugin_template -p 1 -cover -covermode atomic -coverprofile=profile.cov -v ./... env: GITHUB_AUTH_TOKEN: ${{ secrets.INTEGRATION }} From d7cf22171a6ca097fd3fcd32b5a889727c9bdf0d Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Thu, 3 Oct 2024 17:17:53 +0200 Subject: [PATCH 07/11] Address comments by @sinadarbouy --- cmd/cmd_helpers_test.go | 8 ++++++++ cmd/plugin_scaffold_test.go | 36 ++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/cmd/cmd_helpers_test.go b/cmd/cmd_helpers_test.go index 375d87a3..f5fc15dc 100644 --- a/cmd/cmd_helpers_test.go +++ b/cmd/cmd_helpers_test.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "os" + "os/exec" "path/filepath" "runtime" @@ -44,3 +45,10 @@ func mustPullPlugin() (string, error) { return filepath.Abs(fileName) //nolint:wrapcheck } + +// runCommand runs a command in a given directory. +func runCommand(dir string, command string, args ...string) error { + cmd := exec.Command(command, args...) + cmd.Dir = dir + return cmd.Run() +} diff --git a/cmd/plugin_scaffold_test.go b/cmd/plugin_scaffold_test.go index fe0478e5..d3c5f6c7 100644 --- a/cmd/plugin_scaffold_test.go +++ b/cmd/plugin_scaffold_test.go @@ -3,7 +3,6 @@ package cmd import ( "context" "os" - "os/exec" "path/filepath" "sync" "testing" @@ -34,7 +33,7 @@ func Test_pluginScaffoldCmd(t *testing.T) { pluginTestScaffoldInputFile := "./testdata/scaffold_input.yaml" output, err := executeCommandC( - rootCmd, "plugin", "scaffold", "-i", pluginTestScaffoldInputFile) + rootCmd, "plugin", "scaffold", "-i", pluginTestScaffoldInputFile, "-o", t.TempDir()) require.NoError(t, err, "plugin scaffold should not return an error") assert.Contains(t, output, "scaffold done") assert.Contains(t, output, "created files:") @@ -42,32 +41,30 @@ func Test_pluginScaffoldCmd(t *testing.T) { assert.Contains(t, output, "test-gatewayd-plugin/.github/pull_request_template.md") assert.Contains(t, output, "test-gatewayd-plugin/.github/workflows/commits-signed.yaml") - pluginsConfig, err := os.ReadFile( - filepath.Join("plugins", "test-gatewayd-plugin", "gatewayd_plugin.yaml")) + pluginName := "test-gatewayd-plugin" + pluginDir := filepath.Join(t.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") - tidy := exec.Command("go", "mod", "tidy") - tidy.Dir = filepath.Join("plugins", "test-gatewayd-plugin") - err = tidy.Run() - assert.NoError(t, err) + err = runCommand(pluginDir, "go", "mod", "tidy") + require.NoError(t, err, "running go mod tidy should not return an error") + runCommand(pluginDir, "make", "build-dev") + require.NoError(t, err, "running make build-dev should not return an error") - build := exec.Command("make", "build-dev") - build.Dir = filepath.Join("plugins", "test-gatewayd-plugin") - err = build.Run() - assert.NoError(t, err) + pluginBinaryPath := filepath.Join(pluginDir, pluginName) - _, err = os.ReadFile(filepath.Join("plugins", "test-gatewayd-plugin", "test-gatewayd-plugin")) - require.NoError(t, err, "reading plugin binary file should not return an error") + _, err = os.Stat(pluginBinaryPath) + require.NoError(t, err, "plugin binary file should exist") pluginsList := cast.ToSlice(localPluginsConfig["plugins"]) plugin := cast.ToStringMap(pluginsList[0]) - pluginsList[0] = plugin - plugin["localPath"] = filepath.Join("cmd", "plugins", "test-gatewayd-plugin", "test-gatewayd-plugin") - sum, err := checksum.SHA256sum(filepath.Join("plugins", "test-gatewayd-plugin", "test-gatewayd-plugin")) + 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 @@ -79,12 +76,11 @@ func Test_pluginScaffoldCmd(t *testing.T) { require.NoError(t, err, "marshalling yaml file should not return error") err = os.WriteFile( - filepath.Join("plugins", "test-gatewayd-plugin", "gatewayd_plugins.yaml"), + filepath.Join(pluginDir, "gatewayd_plugins.yaml"), updatedPluginConfig, FilePermissions) require.NoError(t, err, "writingh to yaml file should not return error") - pluginTestConfigFile := filepath.Join( - "plugins", "test-gatewayd-plugin", "gatewayd_plugins.yaml") + pluginTestConfigFile := filepath.Join(pluginDir, "gatewayd_plugins.yaml") stopChan = make(chan struct{}) From df4b0a79f52e74ee4ffe9226e7fe824a85d0e605 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Thu, 3 Oct 2024 17:29:58 +0200 Subject: [PATCH 08/11] Fix linter issues --- cmd/cmd_helpers_test.go | 2 +- cmd/plugin_scaffold_test.go | 2 +- config/constants.go | 2 +- network/roundrobin_test.go | 2 +- network/server.go | 2 +- plugin/plugin_registry.go | 3 ++- plugin/plugin_registry_test.go | 2 +- 7 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cmd/cmd_helpers_test.go b/cmd/cmd_helpers_test.go index f5fc15dc..a3fbc0ac 100644 --- a/cmd/cmd_helpers_test.go +++ b/cmd/cmd_helpers_test.go @@ -50,5 +50,5 @@ func mustPullPlugin() (string, error) { func runCommand(dir string, command string, args ...string) error { cmd := exec.Command(command, args...) cmd.Dir = dir - return cmd.Run() + return cmd.Run() //nolint:wrapcheck } diff --git a/cmd/plugin_scaffold_test.go b/cmd/plugin_scaffold_test.go index d3c5f6c7..1c6d9bfd 100644 --- a/cmd/plugin_scaffold_test.go +++ b/cmd/plugin_scaffold_test.go @@ -53,7 +53,7 @@ func Test_pluginScaffoldCmd(t *testing.T) { err = runCommand(pluginDir, "go", "mod", "tidy") require.NoError(t, err, "running go mod tidy should not return an error") - runCommand(pluginDir, "make", "build-dev") + err = runCommand(pluginDir, "make", "build-dev") require.NoError(t, err, "running make build-dev should not return an error") pluginBinaryPath := filepath.Join(pluginDir, pluginName) diff --git a/config/constants.go b/config/constants.go index 28dffe83..0fbbef89 100644 --- a/config/constants.go +++ b/config/constants.go @@ -61,7 +61,7 @@ const ( // Plugin constants. DefaultMinPort = 50000 DefaultMaxPort = 60000 - PluginPriorityStart = 1000 + PluginPriorityStart = uint(1000) DefaultPluginAddress = "http://plugins/metrics" DefaultMetricsMergerPeriod = 5 * time.Second DefaultPluginHealthCheckPeriod = 5 * time.Second diff --git a/network/roundrobin_test.go b/network/roundrobin_test.go index 259e8c56..9f98d6ac 100644 --- a/network/roundrobin_test.go +++ b/network/roundrobin_test.go @@ -74,7 +74,7 @@ func TestRoundRobin_ConcurrentAccess(t *testing.T) { waitGroup.Wait() nextIndex := roundRobin.next.Load() - if nextIndex != uint32(numGoroutines) { //nolint:gosec + if nextIndex != uint32(numGoroutines) { t.Errorf("expected next index to be %d, got %d", numGoroutines, nextIndex) } } diff --git a/network/server.go b/network/server.go index ec919d72..0858976c 100644 --- a/network/server.go +++ b/network/server.go @@ -241,7 +241,7 @@ func (s *Server) OnClose(conn *ConnWrapper, err error) Action { // Shutdown the server if there are no more connections and the server is stopped. // This is used to shut down the server gracefully. - if uint64(s.CountConnections()) == 0 && !s.IsRunning() { + if s.CountConnections() == 0 && !s.IsRunning() { span.AddEvent("Shutting down the server") return Shutdown } diff --git a/plugin/plugin_registry.go b/plugin/plugin_registry.go index f486bfe2..b5130d52 100644 --- a/plugin/plugin_registry.go +++ b/plugin/plugin_registry.go @@ -477,7 +477,8 @@ func (reg *Registry) LoadPlugins( // in the config file. Built-in plugins are loaded first, followed by user-defined // plugins. Built-in plugins have a priority of 0 to 999, and user-defined plugins // have a priority of 1000 or greater. - plugin.Priority = sdkPlugin.Priority(config.PluginPriorityStart + uint(priority)) + plugin.Priority = sdkPlugin.Priority( + config.PluginPriorityStart + uint(priority)) //nolint:gosec logAdapter := logging.NewHcLogAdapter(®.Logger, pCfg.Name) diff --git a/plugin/plugin_registry_test.go b/plugin/plugin_registry_test.go index 4a33d910..fcd66294 100644 --- a/plugin/plugin_registry_test.go +++ b/plugin/plugin_registry_test.go @@ -167,7 +167,7 @@ func BenchmarkHookRun(b *testing.B) { args.Fields["test"] = v1.NewStringValue("test1") return args, nil } - for priority := range 1000 { + for priority := range uint(1000) { reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, sdkPlugin.Priority(priority), hookFunction, From b69f0be0686cf20e1ed6cedc458919116d97b1db Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Thu, 3 Oct 2024 17:31:48 +0200 Subject: [PATCH 09/11] Remove unnecessary os.RemoveAll --- cmd/plugin_scaffold_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/plugin_scaffold_test.go b/cmd/plugin_scaffold_test.go index 1c6d9bfd..d04eda2a 100644 --- a/cmd/plugin_scaffold_test.go +++ b/cmd/plugin_scaffold_test.go @@ -123,6 +123,4 @@ func Test_pluginScaffoldCmd(t *testing.T) { }(&waitGroup) waitGroup.Wait() - - assert.NoError(t, os.RemoveAll("plugins")) } From 9e4bcb2f0be73be4c7fda9b8b7261963511eefec Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Thu, 3 Oct 2024 17:37:39 +0200 Subject: [PATCH 10/11] Use latest version of golangci-lint command and action --- .github/workflows/test.yaml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 0c13fb5f..de55a843 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -67,10 +67,9 @@ jobs: go-version: "1.23" - name: Lint code with golangci-lint 🚨 - uses: golangci/golangci-lint-action@v4 + uses: golangci/golangci-lint-action@v6 with: - version: "v1.60.3" - skip-pkg-cache: true + version: "latest" install-mode: "goinstall" - name: Lint Bash script with shellcheck 🚨 From efc8668dde558d6eb1720ad16ca96e6f08bd3f1e Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Thu, 3 Oct 2024 17:45:51 +0200 Subject: [PATCH 11/11] Each call to t.TempDir return a new directory --- cmd/plugin_scaffold_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmd/plugin_scaffold_test.go b/cmd/plugin_scaffold_test.go index d04eda2a..3a3cc183 100644 --- a/cmd/plugin_scaffold_test.go +++ b/cmd/plugin_scaffold_test.go @@ -32,8 +32,10 @@ func Test_pluginScaffoldCmd(t *testing.T) { plugin.IsPluginTemplateEmbedded() pluginTestScaffoldInputFile := "./testdata/scaffold_input.yaml" + tempDir := t.TempDir() + output, err := executeCommandC( - rootCmd, "plugin", "scaffold", "-i", pluginTestScaffoldInputFile, "-o", t.TempDir()) + 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:") @@ -42,7 +44,7 @@ func Test_pluginScaffoldCmd(t *testing.T) { assert.Contains(t, output, "test-gatewayd-plugin/.github/workflows/commits-signed.yaml") pluginName := "test-gatewayd-plugin" - pluginDir := filepath.Join(t.TempDir(), pluginName) + 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")