From dcfb13824e378e50c2b8570514162a49ec39281b Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 24 Mar 2021 15:51:55 +0800 Subject: [PATCH] Documents getenvoy_extension_test_test.go and starts migration of gomega To call gomega indirect would be putting things lightly. A combination of an indirect testing library and long-running tests are a recipe for frustration. I've begun a process to remove gomega with a couple tests that I had been looking at. While the rationale is fairly straightforward, the following issue captures a lot of it nicely: https://github.com/openservicemesh/osm/issues/1704 If doing this is ok, I don't mind finishing the e2e suite including improving the docs. See #127 Signed-off-by: Adrian Cole --- test/e2e/e2e_suite_test.go | 12 +++- test/e2e/getenvoy_extension_test_test.go | 79 ++++++++++++------------ test/e2e/getenvoy_test.go | 21 +++---- 3 files changed, 57 insertions(+), 55 deletions(-) diff --git a/test/e2e/e2e_suite_test.go b/test/e2e/e2e_suite_test.go index 74d629d2..9a973208 100644 --- a/test/e2e/e2e_suite_test.go +++ b/test/e2e/e2e_suite_test.go @@ -46,13 +46,19 @@ var ( var tempDir string var _ = BeforeEach(func() { - dir, err := ioutil.TempDir("", "") - Expect(err).NotTo(HaveOccurred()) - dir, err = filepath.EvalSymlinks(dir) + dir, err := newTempDir("") Expect(err).NotTo(HaveOccurred()) tempDir = dir }) +func newTempDir(pattern string) (string, error) { + dir, err := ioutil.TempDir("", pattern) + if err != nil { + return "", err + } + return filepath.EvalSymlinks(dir) +} + var _ = AfterEach(func() { if tempDir != "" { Expect(os.RemoveAll(tempDir)).To(Succeed()) diff --git a/test/e2e/getenvoy_extension_test_test.go b/test/e2e/getenvoy_extension_test_test.go index 9bdc2ea5..f24cb737 100644 --- a/test/e2e/getenvoy_extension_test_test.go +++ b/test/e2e/getenvoy_extension_test_test.go @@ -16,59 +16,58 @@ package e2e_test import ( "os" - "path/filepath" + "regexp" + "testing" - . "github.com/onsi/ginkgo" - . "github.com/onsi/ginkgo/extensions/table" - . "github.com/onsi/gomega" + "github.com/stretchr/testify/require" + "github.com/tetratelabs/getenvoy/pkg/extension/workspace/config/extension" e2e "github.com/tetratelabs/getenvoy/test/e2e/util" ) -var _ = Describe("getenvoy extension test", func() { - - type testCase e2e.CategoryLanguageTuple - - testCases := func() []TableEntry { - testCases := make([]TableEntry, 0) - for _, combination := range e2e.GetCategoryLanguageCombinations() { - testCases = append(testCases, Entry(combination.String(), testCase(combination))) - } - return testCases +// TestGetEnvoyExtensionTest runs the equivalent of "getenvoy extension test" for a matrix of extension.Categories and +// extension.Languages. +// +// "getenvoy extension init" is a prerequisite, so run first. Note: "getenvoy extension test" can be extremely slow due +// to implicit responsibilities such as downloading modules or compilation. This is ultimately a Docker command, so +// changes to the Dockerfile or contents like "commands.sh" effect performance. +func TestGetEnvoyExtensionTest(t *testing.T) { + const extensionName = "my.extension" + var languageToTestOutputRegexp = map[extension.Language]*regexp.Regexp{ + extension.LanguageRust: regexp.MustCompile(`(?s)^.*test result: ok.*$`), + extension.LanguageTinyGo: regexp.MustCompile(`(?s)^.*ok my.extension.*$`), } - const extensionName = "my.extension" + for _, test := range e2e.GetCategoryLanguageCombinations() { + testOutputRegexp := languageToTestOutputRegexp[test.Language] + outputDir, err := newTempDir("getenvoy_extension_test") + require.NoError(t, err) - DescribeTable("should run unit tests", - func(given testCase) { - By("choosing the output directory") - outputDir := filepath.Join(tempDir, "new") - defer CleanUpExtensionDir(outputDir) + // "getenvoy extension init" is a prerequisite, so we don't test the output in this test. + if _, _, err = GetEnvoy("extension init"). + Arg(outputDir). + Arg("--category").Arg(test.Category.String()). + Arg("--language").Arg(test.Language.String()). + Arg("--name").Arg(extensionName). + Exec(); err != nil { + os.RemoveAll(outputDir) + t.Fatal(err) + } - By("running `extension init` command") - _, _, err := GetEnvoy("extension init"). - Arg(outputDir). - Arg("--category").Arg(given.Category.String()). - Arg("--language").Arg(given.Language.String()). - Arg("--name").Arg(extensionName). - Exec() - Expect(err).NotTo(HaveOccurred()) + t.Run(test.String(), func(t *testing.T) { + defer os.RemoveAll(outputDir) - By("changing to the output directory") + // Change to the initialized directory and run "getenvoy extension test" err = os.Chdir(outputDir) - Expect(err).NotTo(HaveOccurred()) - - By("running `extension test` command") + require.NoError(t, err) stdout, stderr, err := GetEnvoy("extension test"). Args(e2e.Env.GetBuiltinContainerOptions()...). Exec() - Expect(err).NotTo(HaveOccurred()) - By("verifying stdout/stderr") // apparently, use of `-t` option in `docker run` causes stderr to be incorporated into stdout - Expect(stdout).NotTo(BeEmpty()) - Expect(stderr).To(BeEmpty()) - }, - testCases()..., - ) -}) + require.Regexp(t, testOutputRegexp, stdout) + require.Equal(t, ``, stderr) + require.NoError(t, err) + }) + } +} diff --git a/test/e2e/getenvoy_test.go b/test/e2e/getenvoy_test.go index e3fe246d..254be814 100644 --- a/test/e2e/getenvoy_test.go +++ b/test/e2e/getenvoy_test.go @@ -15,18 +15,15 @@ package e2e_test import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" + "testing" + + "github.com/stretchr/testify/require" ) -var _ = Describe("getenvoy", func() { - It("should support '--version' flag", func() { - By("running the command") - stdout, stderr, err := GetEnvoy("--version").Exec() - Expect(err).NotTo(HaveOccurred()) +func TestGetEnvoyVersion(t *testing.T) { + stdout, stderr, err := GetEnvoy("--version").Exec() - By("verifying stdout/stderr") - Expect(stdout).To(MatchRegexp(`^getenvoy version ([^\s]+)\n$`)) - Expect(stderr).To(Equal(``)) - }) -}) + require.Regexp(t, `^getenvoy version ([^\s]+)\n$`, stdout) + require.Equal(t, ``, stderr) + require.NoError(t, err) +}