|
| 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 | +} |
0 commit comments