2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:12:30 +00:00
|
|
|
|
2021-11-16 18:04:01 +00:00
|
|
|
//go:build linux || darwin
|
2018-10-05 20:08:01 +00:00
|
|
|
// +build linux darwin
|
|
|
|
|
|
|
|
package envoy
|
|
|
|
|
|
|
|
import (
|
2021-05-31 22:10:58 +00:00
|
|
|
"bytes"
|
2018-10-05 20:08:01 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-05-31 22:10:58 +00:00
|
|
|
"io"
|
2018-10-05 20:08:01 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-10-09 09:57:26 +00:00
|
|
|
"strings"
|
2018-10-05 20:08:01 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-12-20 17:58:19 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-05 20:08:01 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExecEnvoy(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
2019-06-21 15:06:25 +00:00
|
|
|
|
2018-10-09 09:57:26 +00:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Args []string
|
|
|
|
WantArgs []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "default",
|
|
|
|
Args: []string{},
|
|
|
|
WantArgs: []string{
|
|
|
|
"--config-path",
|
|
|
|
"{{ got.ConfigPath }}",
|
|
|
|
"--disable-hot-restart",
|
|
|
|
"--fake-envoy-arg",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "hot-restart-epoch",
|
|
|
|
Args: []string{"--restart-epoch", "1"},
|
|
|
|
WantArgs: []string{
|
|
|
|
"--config-path",
|
|
|
|
// Different platforms produce different file descriptors here so we use the
|
|
|
|
// value we got back. This is somewhat tautological but we do sanity check
|
|
|
|
// that value further below.
|
|
|
|
"{{ got.ConfigPath }}",
|
|
|
|
// No --disable-hot-restart
|
|
|
|
"--fake-envoy-arg",
|
|
|
|
"--restart-epoch",
|
|
|
|
"1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "hot-restart-version",
|
|
|
|
Args: []string{"--drain-time-s", "10"},
|
|
|
|
WantArgs: []string{
|
|
|
|
"--config-path",
|
|
|
|
// Different platforms produce different file descriptors here so we use the
|
|
|
|
// value we got back. This is somewhat tautological but we do sanity check
|
|
|
|
// that value further below.
|
|
|
|
"{{ got.ConfigPath }}",
|
|
|
|
// No --disable-hot-restart
|
|
|
|
"--fake-envoy-arg",
|
|
|
|
// Restart epoch defaults to 0 if not given and not disabled.
|
|
|
|
"--drain-time-s",
|
|
|
|
"10",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "hot-restart-version",
|
|
|
|
Args: []string{"--parent-shutdown-time-s", "20"},
|
|
|
|
WantArgs: []string{
|
|
|
|
"--config-path",
|
|
|
|
// Different platforms produce different file descriptors here so we use the
|
|
|
|
// value we got back. This is somewhat tautological but we do sanity check
|
|
|
|
// that value further below.
|
|
|
|
"{{ got.ConfigPath }}",
|
|
|
|
// No --disable-hot-restart
|
|
|
|
"--fake-envoy-arg",
|
|
|
|
// Restart epoch defaults to 0 if not given and not disabled.
|
|
|
|
"--parent-shutdown-time-s",
|
|
|
|
"20",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "hot-restart-version",
|
|
|
|
Args: []string{"--hot-restart-version", "foobar1"},
|
|
|
|
WantArgs: []string{
|
|
|
|
"--config-path",
|
|
|
|
// Different platforms produce different file descriptors here so we use the
|
|
|
|
// value we got back. This is somewhat tautological but we do sanity check
|
|
|
|
// that value further below.
|
|
|
|
"{{ got.ConfigPath }}",
|
|
|
|
// No --disable-hot-restart
|
|
|
|
"--fake-envoy-arg",
|
|
|
|
// Restart epoch defaults to 0 if not given and not disabled.
|
|
|
|
"--hot-restart-version",
|
|
|
|
"foobar1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-10-05 20:08:01 +00:00
|
|
|
|
2018-10-09 09:57:26 +00:00
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
2018-10-05 20:08:01 +00:00
|
|
|
|
2018-10-09 09:57:26 +00:00
|
|
|
args := append([]string{"exec-fake-envoy"}, tc.Args...)
|
|
|
|
cmd, destroy := helperProcess(args...)
|
|
|
|
defer destroy()
|
2018-10-05 20:08:01 +00:00
|
|
|
|
2018-10-09 09:57:26 +00:00
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
outBytes, err := cmd.Output()
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2018-10-05 20:08:01 +00:00
|
|
|
|
2018-10-09 09:57:26 +00:00
|
|
|
var got FakeEnvoyExecData
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, json.Unmarshal(outBytes, &got))
|
2018-10-09 09:57:26 +00:00
|
|
|
|
|
|
|
expectConfigData := fakeEnvoyTestData
|
2018-10-05 20:08:01 +00:00
|
|
|
|
2018-10-09 09:57:26 +00:00
|
|
|
// Substitute the right FD path
|
|
|
|
for idx := range tc.WantArgs {
|
|
|
|
tc.WantArgs[idx] = strings.Replace(tc.WantArgs[idx],
|
|
|
|
"{{ got.ConfigPath }}", got.ConfigPath, 1)
|
|
|
|
}
|
|
|
|
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Equal(t, tc.WantArgs, got.Args)
|
|
|
|
require.Equal(t, expectConfigData, got.ConfigData)
|
2018-10-09 09:57:26 +00:00
|
|
|
// Sanity check the config path in a non-brittle way since we used it to
|
|
|
|
// generate expectation for the args.
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Regexp(t, `-bootstrap.json$`, got.ConfigPath)
|
2018-10-09 09:57:26 +00:00
|
|
|
})
|
|
|
|
}
|
2018-10-05 20:08:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 17:58:19 +00:00
|
|
|
func TestExecEnvoyVersion(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
cmdOutput string
|
|
|
|
expectedOutput string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "actual-version-output-1-24-1",
|
|
|
|
cmdOutput: `envoy version: 69958e4fe32da561376d8b1d367b5e6942dfba24/1.24.1/Distribution/RELEASE/BoringSSL`,
|
|
|
|
expectedOutput: "1.24.1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "format-change",
|
|
|
|
cmdOutput: `envoy version: (69958e4fe32da561376d8b1d367b5e6942dfba24)__(1.24.1)/Distribution/RELEASE/BoringSSL`,
|
|
|
|
expectedOutput: "1.24.1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "zeroes",
|
|
|
|
cmdOutput: `envoy version: 69958e4fe32da561376d8b1d367b5e6942dfba24/0.0.0/Distribution/RELEASE/BoringSSL`,
|
|
|
|
expectedOutput: "0.0.0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test-multi-digit",
|
|
|
|
cmdOutput: `envoy version: 69958e4fe32da561376d8b1d367b5e6942dfba24/1246390.9401081.1238495/Distribution/RELEASE/BoringSSL`,
|
|
|
|
expectedOutput: "1246390.9401081.1238495",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
fe := fakeEnvoy{
|
|
|
|
desiredOutput: tc.cmdOutput,
|
|
|
|
}
|
|
|
|
execCommand = fe.ExecCommand
|
|
|
|
// Reset back to base exec.Command
|
|
|
|
defer func() { execCommand = exec.Command }()
|
|
|
|
version, err := execEnvoyVersion("fake-envoy")
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.expectedOutput, version)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeEnvoy struct {
|
|
|
|
desiredOutput string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fe fakeEnvoy) ExecCommand(command string, args ...string) *exec.Cmd {
|
|
|
|
cs := []string{"-test.run=TestEnvoyExecHelperProcess", "--", command}
|
|
|
|
cs = append(cs, args...)
|
|
|
|
// last argument will be the output
|
|
|
|
cs = append(cs, fe.desiredOutput)
|
|
|
|
cmd := exec.Command(os.Args[0], cs...)
|
|
|
|
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvoyExecHelperProcess(t *testing.T) {
|
|
|
|
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
output := os.Args[len(os.Args)-1]
|
|
|
|
fmt.Fprint(os.Stdout, output)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2018-10-05 20:08:01 +00:00
|
|
|
type FakeEnvoyExecData struct {
|
|
|
|
Args []string `json:"args"`
|
|
|
|
ConfigPath string `json:"configPath"`
|
|
|
|
ConfigData string `json:"configData"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// helperProcessSentinel is a sentinel value that is put as the first
|
|
|
|
// argument following "--" and is used to determine if TestHelperProcess
|
|
|
|
// should run.
|
|
|
|
const helperProcessSentinel = "GO_WANT_HELPER_PROCESS"
|
|
|
|
|
|
|
|
// helperProcess returns an *exec.Cmd that can be used to execute the
|
|
|
|
// TestHelperProcess function below. This can be used to test multi-process
|
|
|
|
// interactions.
|
|
|
|
func helperProcess(s ...string) (*exec.Cmd, func()) {
|
|
|
|
cs := []string{"-test.run=TestHelperProcess", "--", helperProcessSentinel}
|
|
|
|
cs = append(cs, s...)
|
|
|
|
|
|
|
|
cmd := exec.Command(os.Args[0], cs...)
|
|
|
|
destroy := func() {
|
|
|
|
if p := cmd.Process; p != nil {
|
|
|
|
p.Kill()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd, destroy
|
|
|
|
}
|
|
|
|
|
|
|
|
const fakeEnvoyTestData = "pahx9eiPoogheb4haeb2abeem1QuireWahtah1Udi5ae4fuD0c"
|
|
|
|
|
|
|
|
// This is not a real test. This is just a helper process kicked off by tests
|
|
|
|
// using the helperProcess helper function.
|
|
|
|
func TestHelperProcess(t *testing.T) {
|
|
|
|
args := os.Args
|
|
|
|
for len(args) > 0 {
|
|
|
|
if args[0] == "--" {
|
|
|
|
args = args[1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 || args[0] != helperProcessSentinel {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.Exit(0)
|
|
|
|
args = args[1:] // strip sentinel value
|
|
|
|
cmd, args := args[0], args[1:]
|
|
|
|
|
|
|
|
switch cmd {
|
|
|
|
case "exec-fake-envoy":
|
|
|
|
// this will just exec the "fake-envoy" flavor below
|
|
|
|
|
|
|
|
limitProcessLifetime(2 * time.Minute)
|
|
|
|
|
2021-05-31 22:10:58 +00:00
|
|
|
patchExecArgs(t)
|
2018-10-05 20:08:01 +00:00
|
|
|
err := execEnvoy(
|
|
|
|
os.Args[0],
|
|
|
|
[]string{
|
|
|
|
"-test.run=TestHelperProcess",
|
|
|
|
"--",
|
|
|
|
helperProcessSentinel,
|
|
|
|
"fake-envoy",
|
|
|
|
},
|
2018-10-09 09:57:26 +00:00
|
|
|
append([]string{"--fake-envoy-arg"}, args...),
|
2018-10-05 20:08:01 +00:00
|
|
|
[]byte(fakeEnvoyTestData),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "fake envoy process failed to exec: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "fake-envoy":
|
|
|
|
// This subcommand is instrumented to verify some settings
|
|
|
|
// survived an exec.
|
|
|
|
|
|
|
|
limitProcessLifetime(2 * time.Minute)
|
|
|
|
|
|
|
|
data := FakeEnvoyExecData{
|
|
|
|
Args: args,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump all of the args.
|
|
|
|
var captureNext bool
|
|
|
|
for _, arg := range args {
|
|
|
|
if arg == "--config-path" {
|
|
|
|
captureNext = true
|
|
|
|
} else if captureNext {
|
|
|
|
data.ConfigPath = arg
|
|
|
|
captureNext = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.ConfigPath == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "did not detect a --config-path argument passed through\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
d, err := os.ReadFile(data.ConfigPath)
|
2018-10-05 20:08:01 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "could not read provided --config-path file %q: %v\n", data.ConfigPath, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
data.ConfigData = string(d)
|
|
|
|
|
|
|
|
enc := json.NewEncoder(os.Stdout)
|
|
|
|
if err := enc.Encode(&data); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "could not dump results to stdout: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "Unknown command: %q\n", cmd)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// limitProcessLifetime installs a background goroutine that self-exits after
|
|
|
|
// the specified duration elapses to prevent leaking processes from tests that
|
|
|
|
// may spawn them.
|
|
|
|
func limitProcessLifetime(dur time.Duration) {
|
|
|
|
go time.AfterFunc(dur, func() {
|
|
|
|
os.Exit(99)
|
|
|
|
})
|
|
|
|
}
|
2021-05-31 22:10:58 +00:00
|
|
|
|
|
|
|
// patchExecArgs to use a version that will execute the commands using 'go run'.
|
|
|
|
// Also sets up a cleanup function to revert the patch when the test exits.
|
|
|
|
func patchExecArgs(t *testing.T) {
|
|
|
|
orig := execArgs
|
2021-06-01 15:35:32 +00:00
|
|
|
// go run will run the consul source from the root of the repo. The relative
|
|
|
|
// path is necessary because `go test` always sets the working directory to
|
|
|
|
// the directory of the package being tested.
|
2021-05-31 22:10:58 +00:00
|
|
|
execArgs = func(args ...string) (string, []string, error) {
|
|
|
|
args = append([]string{"run", "../../.."}, args...)
|
|
|
|
return "go", args, nil
|
|
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
|
|
execArgs = orig
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeBootstrapPipe_DoesNotBlockOnAFullPipe(t *testing.T) {
|
|
|
|
// A named pipe can buffer up to 64k, use a value larger than that
|
|
|
|
bootstrap := bytes.Repeat([]byte("a"), 66000)
|
|
|
|
|
|
|
|
patchExecArgs(t)
|
|
|
|
pipe, err := makeBootstrapPipe(bootstrap)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Read everything from the named pipe, to allow the sub-process to exit
|
|
|
|
f, err := os.Open(pipe)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
_, err = io.Copy(&buf, f)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, bootstrap, buf.Bytes())
|
|
|
|
}
|