Skip to content

Commit

Permalink
Documents getenvoy_extension_test_test.go and starts migration of gomega
Browse files Browse the repository at this point in the history
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:

openservicemesh/osm#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 <adrian@tetrate.io>
  • Loading branch information
Adrian Cole committed Mar 26, 2021
1 parent 4f89255 commit dcfb138
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 55 deletions.
12 changes: 9 additions & 3 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
79 changes: 39 additions & 40 deletions test/e2e/getenvoy_extension_test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
21 changes: 9 additions & 12 deletions test/e2e/getenvoy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit dcfb138

Please sign in to comment.