debug.sh: Add debugging to test runs. (#7370)

When tests are run, shell script debugging is enabled and written to a
file called "<script_name>_debug.txt" in the same directory as the
"<script_name>_output.txt" file.
This commit is contained in:
Justin Pettit 2020-05-20 20:27:44 -07:00 committed by GitHub
parent 82d6cc8103
commit 5f89f64d49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -30,6 +30,7 @@ import (
const (
testOutputDirEnvVar = "TEST_OUTPUT_DIR"
testDebugFile = "TEST_DEBUG_FILE"
kubeConfigEnvVar = "KUBECONFIG"
)
@ -43,6 +44,8 @@ var _ Step = Script{}
// Set to the working directory of the current test. By default, scripts are run from this
// directory. This variable is useful for cases where the execution `WorkDir` has been set,
// but the script needs to access files in the test working directory.
// - TEST_DEBUG_FILE:
// Set to the file where debugging output will be written.
// - KUBECONFIG:
// Set to the value from the test framework. This is necessary to make kubectl commands execute
// with the configuration specified on the command line.
@ -70,6 +73,7 @@ func (s Script) run(ctx Context) {
// Generate the body of the command.
commandLines := []string{"source ${REPO_ROOT}/tests/util/verify.sh"}
commandLines = append(commandLines, "source ${REPO_ROOT}/tests/util/debug.sh")
lines := strings.Split(content, "\n")
for index := 0; index < len(lines); index++ {
commandLines = append(commandLines, lines[index])
@ -96,7 +100,7 @@ func (s Script) run(ctx Context) {
// Create the command.
cmd := exec.Command(shell)
cmd.Dir = s.getWorkDir(ctx)
cmd.Env = s.getEnv(ctx)
cmd.Env = s.getEnv(ctx, fileName)
cmd.Stdin = strings.NewReader(command)
// Run the command and get the output.
@ -121,7 +125,7 @@ func (s Script) getWorkDir(ctx Context) string {
return ctx.WorkDir()
}
func (s Script) getEnv(ctx Context) []string {
func (s Script) getEnv(ctx Context, fileName string) []string {
// Start with the environment for the current process.
e := os.Environ()
@ -130,6 +134,7 @@ func (s Script) getEnv(ctx Context) []string {
// Set the output dir for the test.
testOutputDirEnvVar: ctx.WorkDir(),
}
customVars[testDebugFile] = fileName + "_debug.txt"
ctx.Environment().Case(environment.Kube, func() {
customVars[kubeConfigEnvVar] = ctx.KubeEnv().Settings().KubeConfig[0]
})

20
tests/util/debug.sh Normal file
View File

@ -0,0 +1,20 @@
#!/bin/bash
# Copyright Istio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
exec 5> "${TEST_DEBUG_FILE}"
BASH_XTRACEFD="5"
export PS4='+${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -x