2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2021-11-16 18:04:01 +00:00
|
|
|
//go:build integration
|
2020-05-19 18:00:00 +00:00
|
|
|
|
|
|
|
package envoy
|
|
|
|
|
|
|
|
import (
|
2023-07-21 14:56:00 +00:00
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2020-05-19 18:00:00 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-10-19 21:51:38 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-05-19 18:00:00 +00:00
|
|
|
"testing"
|
2020-10-19 21:51:38 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-05-19 18:00:00 +00:00
|
|
|
)
|
|
|
|
|
2023-07-21 14:56:00 +00:00
|
|
|
var (
|
2024-05-07 15:24:50 +00:00
|
|
|
flagWin = flag.Bool("win", false, "Execute tests on windows")
|
2023-07-21 14:56:00 +00:00
|
|
|
)
|
|
|
|
|
2020-05-19 18:00:00 +00:00
|
|
|
func TestEnvoy(t *testing.T) {
|
2023-07-21 14:56:00 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *flagWin == true {
|
|
|
|
dir := "../../../"
|
|
|
|
check_dir_files(dir)
|
|
|
|
}
|
|
|
|
|
2024-05-07 15:24:50 +00:00
|
|
|
testcases, err := discoverCases()
|
2020-10-19 21:51:38 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-19 18:00:00 +00:00
|
|
|
|
|
|
|
runCmd(t, "suite_setup")
|
2023-07-21 14:56:00 +00:00
|
|
|
|
2020-05-19 18:00:00 +00:00
|
|
|
defer runCmd(t, "suite_teardown")
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
t.Run(tc, func(t *testing.T) {
|
2020-06-02 23:24:52 +00:00
|
|
|
caseDir := "CASE_DIR=" + tc
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2023-12-21 22:18:11 +00:00
|
|
|
if t.Failed() {
|
|
|
|
runCmd(t, "capture_logs", caseDir)
|
|
|
|
}
|
2020-06-02 23:24:52 +00:00
|
|
|
|
|
|
|
runCmd(t, "test_teardown", caseDir)
|
|
|
|
})
|
|
|
|
|
|
|
|
runCmd(t, "run_tests", caseDir)
|
2020-05-19 18:00:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 14:56:00 +00:00
|
|
|
func runCmdLinux(t *testing.T, c string, env ...string) {
|
2020-05-19 18:00:00 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
cmd := exec.Command("./run-tests.sh", c)
|
|
|
|
cmd.Env = append(os.Environ(), env...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Fatalf("command failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-10-19 21:51:38 +00:00
|
|
|
|
2023-07-21 14:56:00 +00:00
|
|
|
func runCmdWindows(t *testing.T, c string, env ...string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
param_5 := "false"
|
|
|
|
if env != nil {
|
|
|
|
param_5 = strings.Join(env, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("cmd", "/C", "bash run-tests.windows.sh", c, param_5)
|
|
|
|
cmd.Env = append(os.Environ(), env...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Fatalf("command failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCmd(t *testing.T, c string, env ...string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if *flagWin == true {
|
|
|
|
runCmdWindows(t, c, env...)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
runCmdLinux(t, c, env...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:46:03 +00:00
|
|
|
// Discover the cases so we pick up both ce and ent copies.
|
2020-10-19 21:51:38 +00:00
|
|
|
func discoverCases() ([]string, error) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
dirs, err := os.ReadDir(cwd)
|
2020-10-19 21:51:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []string
|
|
|
|
for _, fi := range dirs {
|
|
|
|
if fi.IsDir() && strings.HasPrefix(fi.Name(), "case-") {
|
|
|
|
out = append(out, fi.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(out)
|
|
|
|
return out, nil
|
|
|
|
}
|
2023-07-21 14:56:00 +00:00
|
|
|
|
|
|
|
// CRLF convert functions
|
|
|
|
// Recursively iterates through the directory passed by parameter looking for the sh and bash files.
|
|
|
|
// Upon finding them, it calls crlf_file_check.
|
|
|
|
func check_dir_files(path string) {
|
|
|
|
files, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, fil := range files {
|
|
|
|
|
|
|
|
v := strings.Split(fil.Name(), ".")
|
|
|
|
file_extension := v[len(v)-1]
|
|
|
|
|
|
|
|
file_path := path + "/" + fil.Name()
|
|
|
|
|
|
|
|
if fil.IsDir() == true {
|
|
|
|
check_dir_files(file_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if file_extension == "sh" || file_extension == "bash" {
|
|
|
|
crlf_file_check(file_path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a file contains CRLF line endings if so call crlf_normalize
|
|
|
|
func crlf_file_check(file_name string) {
|
|
|
|
|
|
|
|
file, err := ioutil.ReadFile(file_name)
|
|
|
|
text := string(file)
|
|
|
|
|
|
|
|
if edit := crlf_verify(text); edit != -1 {
|
|
|
|
crlf_normalize(file_name, text)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks for the existence of CRLF line endings.
|
|
|
|
func crlf_verify(text string) int {
|
|
|
|
position := strings.Index(text, "\r\n")
|
|
|
|
return position
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace CRLF line endings with LF.
|
|
|
|
func crlf_normalize(filename, text string) {
|
|
|
|
text = strings.Replace(text, "\r\n", "\n", -1)
|
|
|
|
data := []byte(text)
|
|
|
|
|
|
|
|
ioutil.WriteFile(filename, data, 0644)
|
|
|
|
}
|