|
| 1 | +// Copyright 2021 Tetrate |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package e2e_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "io/ioutil" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "regexp" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + "github.com/tetratelabs/log" |
| 26 | + |
| 27 | + "github.com/tetratelabs/getenvoy/pkg/extension/workspace/config/extension" |
| 28 | + e2e "github.com/tetratelabs/getenvoy/test/e2e/util" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + // GetEnvoy is a convenient alias. |
| 33 | + GetEnvoy = e2e.GetEnvoy |
| 34 | +) |
| 35 | + |
| 36 | +// stripAnsiEscapeRegexp is a regular expression to clean ANSI Control sequences |
| 37 | +// feat https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python#33925425 |
| 38 | +var stripAnsiEscapeRegexp = regexp.MustCompile(`(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]`) |
| 39 | + |
| 40 | +func requireEnvoyBinaryPath(t *testing.T) { |
| 41 | + path, err := e2e.Env.GetEnvoyBinary() |
| 42 | + require.NoError(t, err, `error reading path to getenvoy binary`) |
| 43 | + e2e.GetEnvoyBinaryPath = path |
| 44 | +} |
| 45 | + |
| 46 | +// requireNewTempDir creates a new directory. The function returned cleans it up. |
| 47 | +func requireNewTempDir(t *testing.T) (string, func()) { |
| 48 | + d, err := ioutil.TempDir("", "") |
| 49 | + if err != nil { |
| 50 | + require.NoError(t, err, `ioutil.TempDir("", "") erred`) |
| 51 | + } |
| 52 | + dir := requireAbsDir(t, d) |
| 53 | + return dir, func() { |
| 54 | + e := os.RemoveAll(dir) |
| 55 | + require.NoError(t, e, `error removing directory: %v`, dir) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// RequireChDir will os.Chdir into the indicated dir, panicing on any problem. |
| 60 | +// The function returned reverts to the original. |
| 61 | +func requireChDir(t *testing.T, d string) func() { |
| 62 | + // Save previous working directory to that it can be reverted later. |
| 63 | + previous, err := os.Getwd() |
| 64 | + require.NoError(t, err, `error determining current directory`) |
| 65 | + |
| 66 | + // Now, actually change to the directory. |
| 67 | + err = os.Chdir(d) |
| 68 | + require.NoError(t, err, `error changing to directory: %v`, d) |
| 69 | + return func() { |
| 70 | + e := os.Chdir(previous) |
| 71 | + require.NoError(t, e, `error changing to directory: %v`, previous) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// requireAbsDir runs filepath.Abs and ensures there are no errors and the input is a directory. |
| 76 | +func requireAbsDir(t *testing.T, d string) string { |
| 77 | + dir, err := filepath.Abs(d) |
| 78 | + require.NoError(t, err, `error determining absolute directory: %v`, d) |
| 79 | + require.DirExists(t, dir, `directory doesn't exist': %v`, dir) |
| 80 | + dir, err = filepath.EvalSymlinks(dir) |
| 81 | + require.NoError(t, err, `filepath.EvalSymlinks(%s) erred`, dir) |
| 82 | + require.NotEmpty(t, dir, `filepath.EvalSymlinks(%s) returned ""`) |
| 83 | + return dir |
| 84 | +} |
| 85 | + |
| 86 | +// Command gives us an interface needed for testing GetEnvoy |
| 87 | +type Command interface { |
| 88 | + Exec() (string, string, error) |
| 89 | +} |
| 90 | + |
| 91 | +// requireExecNoStdout invokes the command and returns its stderr if successful and stdout is empty. |
| 92 | +func requireExecNoStdout(t *testing.T, cmd Command) string { |
| 93 | + stdout, stderr := requireExec(t, cmd) |
| 94 | + require.Empty(t, stdout, `expected no stdout running [%v]`, cmd) |
| 95 | + require.NotEmpty(t, stderr, `expected stderr running [%v]`, cmd) |
| 96 | + return stderr |
| 97 | +} |
| 98 | + |
| 99 | +// requireExecNoStderr invokes the command and returns its stdout if successful and stderr is empty. |
| 100 | +func requireExecNoStderr(t *testing.T, cmd Command) string { |
| 101 | + stdout, stderr := requireExec(t, cmd) |
| 102 | + require.NotEmpty(t, stdout, `expected stdout running [%v]`, cmd) |
| 103 | + require.Empty(t, stderr, `expected no stderr running [%v]`, cmd) |
| 104 | + return stdout |
| 105 | +} |
| 106 | + |
| 107 | +// requireExec invokes the command and returns its (stdout, stderr) if successful. |
| 108 | +func requireExec(t *testing.T, cmd Command) (string, string) { |
| 109 | + log.Infof(`running [%v]`, cmd) |
| 110 | + stdout, stderr, err := cmd.Exec() |
| 111 | + |
| 112 | + require.NoError(t, err, `error running [%v]`, cmd) |
| 113 | + return stdout, stderr |
| 114 | +} |
| 115 | + |
| 116 | +// requireExtensionInit is useful for tests that depend on "getenvoy extension init" as a prerequisite. |
| 117 | +func requireExtensionInit(t *testing.T, workDir string, category extension.Category, language extension.Language, name string) { |
| 118 | + cmd := GetEnvoy("extension init"). |
| 119 | + Arg(workDir). |
| 120 | + Arg("--category").Arg(string(category)). |
| 121 | + Arg("--language").Arg(string(language)). |
| 122 | + Arg("--name").Arg(name) |
| 123 | + // stderr returned is not tested because doing so is redundant to TestGetEnvoyExtensionInit. |
| 124 | + _ = requireExecNoStdout(t, cmd) |
| 125 | +} |
| 126 | + |
| 127 | +// extensionWasmPath returns the language-specific location of the extension.wasm. |
| 128 | +func extensionWasmPath(language extension.Language) string { |
| 129 | + switch language { |
| 130 | + case extension.LanguageRust: |
| 131 | + return filepath.Join("target", "getenvoy", "extension.wasm") |
| 132 | + case extension.LanguageTinyGo: |
| 133 | + return filepath.Join("build", "extension.wasm") |
| 134 | + } |
| 135 | + panic("unsupported language " + language) |
| 136 | +} |
| 137 | + |
| 138 | +// requireExtensionInit is useful for tests that depend on "getenvoy extension build" as a prerequisite. |
| 139 | +// The result of calling this is the bytes representing the built wasm |
| 140 | +func requireExtensionBuild(t *testing.T, language extension.Language, workDir string) []byte { |
| 141 | + cmd := GetEnvoy("extension build").Args(e2e.Env.GetBuiltinContainerOptions()...) |
| 142 | + // stderr returned is not tested because doing so is redundant to TestGetEnvoyExtensionInit. |
| 143 | + _ = requireExecNoStderr(t, cmd) |
| 144 | + |
| 145 | + extensionWasmFile := filepath.Join(workDir, extensionWasmPath(language)) |
| 146 | + require.FileExists(t, extensionWasmFile, `extension wasm file %s missing after running [%v]`, extensionWasmFile, cmd) |
| 147 | + |
| 148 | + wasmBytes, err := ioutil.ReadFile(extensionWasmFile) |
| 149 | + require.NoError(t, err, `error reading %s after running [%v]: %s`, extensionWasmFile, cmd) |
| 150 | + require.NotEmpty(t, wasmBytes, `%s empty after running [%v]`, extensionWasmFile, cmd) |
| 151 | + return wasmBytes |
| 152 | +} |
| 153 | + |
| 154 | +// requireExtensionClean is useful for tests that depend on "getenvoy extension clean" on completion. |
| 155 | +// (stdout, stderr) returned are not tested because they can both be empty. |
| 156 | +func requireExtensionClean(t *testing.T, workDir string) { |
| 157 | + err := os.Chdir(workDir) |
| 158 | + require.NoError(t, err, `error changing to directory: %v`, workDir) |
| 159 | + |
| 160 | + cmd := GetEnvoy("extension clean") |
| 161 | + _, _ = requireExec(t, cmd) |
| 162 | +} |
0 commit comments