Skip to content

Commit 041dc62

Browse files
mostafazeina1i
andauthored
Add test for plugin scaffold command (#613)
* Implement scaffold tests (wip) * Fix issues * Add test for plugin scaffold command * Fix typo * Add package to allow list * Include plugin template when testing * Address comments by @sinadarbouy * Fix linter issues * Remove unnecessary os.RemoveAll * Use latest version of golangci-lint command and action * Each call to t.TempDir return a new directory --------- Co-authored-by: zeina1i <hosseinzeinalii@gmail.com>
1 parent ffd13ea commit 041dc62

File tree

13 files changed

+164
-21
lines changed

13 files changed

+164
-21
lines changed

.github/workflows/test.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ jobs:
6767
go-version: "1.23"
6868

6969
- name: Lint code with golangci-lint 🚨
70-
uses: golangci/golangci-lint-action@v4
70+
uses: golangci/golangci-lint-action@v6
7171
with:
72-
version: "v1.60.3"
73-
skip-pkg-cache: true
72+
version: "latest"
7473
install-mode: "goinstall"
7574

7675
- name: Lint Bash script with shellcheck 🚨
@@ -82,7 +81,7 @@ jobs:
8281
dockerfile: Dockerfile
8382

8483
- name: Run Go tests 🔬
85-
run: go test -p 1 -cover -covermode atomic -coverprofile=profile.cov -v ./...
84+
run: go test -tags embed_plugin_template -p 1 -cover -covermode atomic -coverprofile=profile.cov -v ./...
8685
env:
8786
GITHUB_AUTH_TOKEN: ${{ secrets.INTEGRATION }}
8887

.golangci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ linters-settings:
9191
- "github.com/testcontainers/testcontainers-go"
9292
- "github.com/redis/go-redis/v9"
9393
- "github.com/docker/go-connections/nat"
94+
- "github.com/codingsince1985/checksum"
9495
tagalign:
9596
align: false
9697
sort: false

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ clean:
8282
@rm -rf dist
8383

8484
test:
85-
@go test -v -cover -coverprofile=c.out ./...
85+
@go test -tags embed_plugin_template -v -cover -coverprofile=c.out ./...
8686

8787
test-race:
8888
@go test -race -v ./...

cmd/cmd_helpers_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"os"
7+
"os/exec"
78
"path/filepath"
89
"runtime"
910

@@ -44,3 +45,10 @@ func mustPullPlugin() (string, error) {
4445

4546
return filepath.Abs(fileName) //nolint:wrapcheck
4647
}
48+
49+
// runCommand runs a command in a given directory.
50+
func runCommand(dir string, command string, args ...string) error {
51+
cmd := exec.Command(command, args...)
52+
cmd.Dir = dir
53+
return cmd.Run() //nolint:wrapcheck
54+
}

cmd/plugin_scaffold_test.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"os"
6+
"path/filepath"
7+
"sync"
8+
"testing"
9+
"time"
10+
11+
"github.com/codingsince1985/checksum"
12+
"github.com/gatewayd-io/gatewayd/config"
13+
"github.com/gatewayd-io/gatewayd/plugin"
14+
"github.com/gatewayd-io/gatewayd/testhelpers"
15+
"github.com/spf13/cast"
16+
"github.com/stretchr/testify/assert"
17+
"github.com/stretchr/testify/require"
18+
yamlv3 "gopkg.in/yaml.v3"
19+
)
20+
21+
func Test_pluginScaffoldCmd(t *testing.T) {
22+
// Start the test containers.
23+
ctx := context.Background()
24+
postgresHostIP1, postgresMappedPort1 := testhelpers.SetupPostgreSQLTestContainer(ctx, t)
25+
postgresHostIP2, postgresMappedPort2 := testhelpers.SetupPostgreSQLTestContainer(ctx, t)
26+
postgresAddress1 := postgresHostIP1 + ":" + postgresMappedPort1.Port()
27+
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgresAddress1)
28+
postgresAddress2 := postgresHostIP2 + ":" + postgresMappedPort2.Port()
29+
t.Setenv("GATEWAYD_CLIENTS_TEST_WRITE_ADDRESS", postgresAddress2)
30+
31+
globalTestConfigFile := filepath.Join("testdata", "gatewayd.yaml")
32+
plugin.IsPluginTemplateEmbedded()
33+
pluginTestScaffoldInputFile := "./testdata/scaffold_input.yaml"
34+
35+
tempDir := t.TempDir()
36+
37+
output, err := executeCommandC(
38+
rootCmd, "plugin", "scaffold", "-i", pluginTestScaffoldInputFile, "-o", tempDir)
39+
require.NoError(t, err, "plugin scaffold should not return an error")
40+
assert.Contains(t, output, "scaffold done")
41+
assert.Contains(t, output, "created files:")
42+
assert.Contains(t, output, "test-gatewayd-plugin/.github/issue_template.md")
43+
assert.Contains(t, output, "test-gatewayd-plugin/.github/pull_request_template.md")
44+
assert.Contains(t, output, "test-gatewayd-plugin/.github/workflows/commits-signed.yaml")
45+
46+
pluginName := "test-gatewayd-plugin"
47+
pluginDir := filepath.Join(tempDir, pluginName)
48+
49+
pluginsConfig, err := os.ReadFile(filepath.Join(pluginDir, "gatewayd_plugin.yaml"))
50+
require.NoError(t, err, "reading plugins config file should not return an error")
51+
52+
var localPluginsConfig map[string]interface{}
53+
err = yamlv3.Unmarshal(pluginsConfig, &localPluginsConfig)
54+
require.NoError(t, err, "unmarshalling yaml file should not return error")
55+
56+
err = runCommand(pluginDir, "go", "mod", "tidy")
57+
require.NoError(t, err, "running go mod tidy should not return an error")
58+
err = runCommand(pluginDir, "make", "build-dev")
59+
require.NoError(t, err, "running make build-dev should not return an error")
60+
61+
pluginBinaryPath := filepath.Join(pluginDir, pluginName)
62+
63+
_, err = os.Stat(pluginBinaryPath)
64+
require.NoError(t, err, "plugin binary file should exist")
65+
66+
pluginsList := cast.ToSlice(localPluginsConfig["plugins"])
67+
plugin := cast.ToStringMap(pluginsList[0])
68+
plugin["localPath"] = filepath.Join("cmd", pluginDir, pluginName)
69+
sum, err := checksum.SHA256sum(pluginBinaryPath)
70+
require.NoError(t, err, "marshalling yaml file should not return error")
71+
plugin["checksum"] = sum
72+
73+
pluginsList[0] = plugin
74+
plugins := make(map[string]interface{})
75+
plugins["plugins"] = pluginsList
76+
77+
updatedPluginConfig, err := yamlv3.Marshal(plugins)
78+
require.NoError(t, err, "marshalling yaml file should not return error")
79+
80+
err = os.WriteFile(
81+
filepath.Join(pluginDir, "gatewayd_plugins.yaml"),
82+
updatedPluginConfig, FilePermissions)
83+
require.NoError(t, err, "writingh to yaml file should not return error")
84+
85+
pluginTestConfigFile := filepath.Join(pluginDir, "gatewayd_plugins.yaml")
86+
87+
stopChan = make(chan struct{})
88+
89+
var waitGroup sync.WaitGroup
90+
91+
waitGroup.Add(1)
92+
go func(waitGroup *sync.WaitGroup) {
93+
// Test run command.
94+
output, err := executeCommandC(
95+
rootCmd, "run", "-c", globalTestConfigFile, "-p", pluginTestConfigFile)
96+
require.NoError(t, err, "run command should not have returned an error")
97+
98+
// Print the output for debugging purposes.
99+
runCmd.Print(output)
100+
// Check if GatewayD started and stopped correctly.
101+
assert.Contains(t, output, "GatewayD is running")
102+
assert.Contains(t, output, "Stopped all servers")
103+
104+
waitGroup.Done()
105+
}(&waitGroup)
106+
107+
waitGroup.Add(1)
108+
go func(waitGroup *sync.WaitGroup) {
109+
time.Sleep(waitBeforeStop)
110+
111+
StopGracefully(
112+
context.Background(),
113+
nil,
114+
nil,
115+
metricsServer,
116+
nil,
117+
loggers[config.Default],
118+
servers,
119+
stopChan,
120+
nil,
121+
nil,
122+
)
123+
124+
waitGroup.Done()
125+
}(&waitGroup)
126+
127+
waitGroup.Wait()
128+
}

cmd/run_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var (
2020

2121
func Test_runCmd(t *testing.T) {
2222
postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
23-
postgredAddress := postgresHostIP + ":" + postgresMappedPort.Port()
24-
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgredAddress)
23+
postgresAddress := postgresHostIP + ":" + postgresMappedPort.Port()
24+
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgresAddress)
2525

2626
globalTestConfigFile := "./test_global_runCmd.yaml"
2727
pluginTestConfigFile := "./test_plugins_runCmd.yaml"
@@ -84,8 +84,8 @@ func Test_runCmd(t *testing.T) {
8484
// Test_runCmdWithTLS tests the run command with TLS enabled on the server.
8585
func Test_runCmdWithTLS(t *testing.T) {
8686
postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
87-
postgredAddress := postgresHostIP + ":" + postgresMappedPort.Port()
88-
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgredAddress)
87+
postgresAddress := postgresHostIP + ":" + postgresMappedPort.Port()
88+
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgresAddress)
8989

9090
globalTLSTestConfigFile := "./testdata/gatewayd_tls.yaml"
9191
pluginTestConfigFile := "./test_plugins_runCmdWithTLS.yaml"
@@ -144,11 +144,11 @@ func Test_runCmdWithTLS(t *testing.T) {
144144
// Test_runCmdWithMultiTenancy tests the run command with multi-tenancy enabled.
145145
func Test_runCmdWithMultiTenancy(t *testing.T) {
146146
postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
147-
postgredAddress := postgresHostIP + ":" + postgresMappedPort.Port()
148-
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgredAddress)
147+
postgresAddress := postgresHostIP + ":" + postgresMappedPort.Port()
148+
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgresAddress)
149149
postgresHostIP2, postgresMappedPort2 := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
150-
postgredAddress2 := postgresHostIP2 + ":" + postgresMappedPort2.Port()
151-
t.Setenv("GATEWAYD_CLIENTS_TEST_WRITE_ADDRESS", postgredAddress2)
150+
postgresAddress2 := postgresHostIP2 + ":" + postgresMappedPort2.Port()
151+
t.Setenv("GATEWAYD_CLIENTS_TEST_WRITE_ADDRESS", postgresAddress2)
152152

153153
globalTestConfigFile := "./testdata/gatewayd.yaml"
154154
pluginTestConfigFile := "./test_plugins_runCmdWithMultiTenancy.yaml"
@@ -208,8 +208,8 @@ func Test_runCmdWithMultiTenancy(t *testing.T) {
208208

209209
func Test_runCmdWithCachePlugin(t *testing.T) {
210210
postgresHostIP, postgresMappedPort := testhelpers.SetupPostgreSQLTestContainer(context.Background(), t)
211-
postgredAddress := postgresHostIP + ":" + postgresMappedPort.Port()
212-
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgredAddress)
211+
postgresAddress := postgresHostIP + ":" + postgresMappedPort.Port()
212+
t.Setenv("GATEWAYD_CLIENTS_DEFAULT_WRITES_ADDRESS", postgresAddress)
213213

214214
globalTestConfigFile := "./test_global_runCmdWithCachePlugin.yaml"
215215
pluginTestConfigFile := "./test_plugins_runCmdWithCachePlugin.yaml"

cmd/testdata/scaffold_input.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
remote_url: https://github.com/gatewayd-io/test-gatewayd-plugin
2+
version: 0.1
3+
description: This is test plugin
4+
license: MIT
5+
authors:
6+
- GatewayD Team

config/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const (
6161
// Plugin constants.
6262
DefaultMinPort = 50000
6363
DefaultMaxPort = 60000
64-
PluginPriorityStart = 1000
64+
PluginPriorityStart = uint(1000)
6565
DefaultPluginAddress = "http://plugins/metrics"
6666
DefaultMetricsMergerPeriod = 5 * time.Second
6767
DefaultPluginHealthCheckPeriod = 5 * time.Second

network/roundrobin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestRoundRobin_ConcurrentAccess(t *testing.T) {
7474

7575
waitGroup.Wait()
7676
nextIndex := roundRobin.next.Load()
77-
if nextIndex != uint32(numGoroutines) { //nolint:gosec
77+
if nextIndex != uint32(numGoroutines) {
7878
t.Errorf("expected next index to be %d, got %d", numGoroutines, nextIndex)
7979
}
8080
}

network/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func (s *Server) OnClose(conn *ConnWrapper, err error) Action {
241241

242242
// Shutdown the server if there are no more connections and the server is stopped.
243243
// This is used to shut down the server gracefully.
244-
if uint64(s.CountConnections()) == 0 && !s.IsRunning() {
244+
if s.CountConnections() == 0 && !s.IsRunning() {
245245
span.AddEvent("Shutting down the server")
246246
return Shutdown
247247
}

0 commit comments

Comments
 (0)