From 82515d4113b9ffbad41c6c532ada9b5db890c98c Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Wed, 2 Oct 2024 23:15:14 +0200 Subject: [PATCH] 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")) }