Commit 056a1510 authored by Steve Azzopardi's avatar Steve Azzopardi

Merge branch 'allow-to-disable-debug-tracing' into 'master'

Allow to disable debug tracing

See merge request !1286
parents 302c5edb 5ab2285d
......@@ -757,6 +757,14 @@ func (b *Build) GetGitCleanFlags() []string {
func (b *Build) IsDebugTraceEnabled() bool {
trace, err := strconv.ParseBool(b.GetAllVariables().Get("CI_DEBUG_TRACE"))
if err != nil {
trace = false
}
if b.Runner.DebugTraceDisabled {
if trace == true {
b.logger.Warningln("CI_DEBUG_TRACE usage is disabled on this Runner")
}
return false
}
......
......@@ -13,6 +13,7 @@ import (
"time"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
......@@ -729,26 +730,65 @@ func TestRunSuccessOnSecondAttempt(t *testing.T) {
}
func TestDebugTrace(t *testing.T) {
build := &Build{}
assert.False(t, build.IsDebugTraceEnabled(), "IsDebugTraceEnabled should be false if CI_DEBUG_TRACE is not set")
testCases := map[string]struct {
debugTraceVariableValue string
expectedValue bool
debugTraceFeatureDisabled bool
expectedLogOutput string
}{
"variable not set": {
expectedValue: false,
},
"variable set to false": {
debugTraceVariableValue: "false",
expectedValue: false,
},
"variable set to true": {
debugTraceVariableValue: "true",
expectedValue: true,
},
"variable set to a non-bool value": {
debugTraceVariableValue: "xyz",
expectedValue: false,
},
"variable set to true and feature disabled from configuration": {
debugTraceVariableValue: "true",
expectedValue: false,
debugTraceFeatureDisabled: true,
expectedLogOutput: "CI_DEBUG_TRACE usage is disabled on this Runner",
},
}
successfulBuild, err := GetSuccessfulBuild()
assert.NoError(t, err)
for testName, testCase := range testCases {
t.Run(testName, func(t *testing.T) {
logger, hooks := test.NewNullLogger()
successfulBuild.Variables = append(successfulBuild.Variables, JobVariable{Key: "CI_DEBUG_TRACE", Value: "false", Public: true, Internal: true})
build = &Build{
JobResponse: successfulBuild,
}
assert.False(t, build.IsDebugTraceEnabled(), "IsDebugTraceEnabled should be false if CI_DEBUG_TRACE is set to false")
build := &Build{
logger: NewBuildLogger(nil, logrus.NewEntry(logger)),
JobResponse: JobResponse{
Variables: JobVariables{},
},
Runner: &RunnerConfig{
RunnerSettings: RunnerSettings{
DebugTraceDisabled: testCase.debugTraceFeatureDisabled,
},
},
}
successfulBuild, err = GetSuccessfulBuild()
assert.NoError(t, err)
if testCase.debugTraceVariableValue != "" {
build.Variables = append(build.Variables, JobVariable{Key: "CI_DEBUG_TRACE", Value: testCase.debugTraceVariableValue, Public: true})
}
successfulBuild.Variables = append(successfulBuild.Variables, JobVariable{Key: "CI_DEBUG_TRACE", Value: "true", Public: true, Internal: true})
build = &Build{
JobResponse: successfulBuild,
isTraceEnabled := build.IsDebugTraceEnabled()
assert.Equal(t, testCase.expectedValue, isTraceEnabled)
if testCase.expectedLogOutput != "" {
output, err := hooks.LastEntry().String()
require.NoError(t, err)
assert.Contains(t, output, testCase.expectedLogOutput)
}
})
}
assert.True(t, build.IsDebugTraceEnabled(), "IsDebugTraceEnabled should be true if CI_DEBUG_TRACE is set to true")
}
func TestDefaultEnvVariables(t *testing.T) {
......
......@@ -276,6 +276,8 @@ type RunnerSettings struct {
PreBuildScript string `toml:"pre_build_script,omitempty" json:"pre_build_script" long:"pre-build-script" env:"RUNNER_PRE_BUILD_SCRIPT" description:"Runner-specific command script executed after code is pulled, just before build executes"`
PostBuildScript string `toml:"post_build_script,omitempty" json:"post_build_script" long:"post-build-script" env:"RUNNER_POST_BUILD_SCRIPT" description:"Runner-specific command script executed after code is pulled and just after build executes"`
DebugTraceDisabled bool `toml:"debug_trace_disabled,omitempty" json:"debug_trace_disabled" long:"debug-trace-disabled" env:"RUNNER_DEBUG_TRACE_DISABLED" description:"When set to true Runner will disable the possibility of using the CI_DEBUG_TRACE feature"`
Shell string `toml:"shell,omitempty" json:"shell" long:"shell" env:"RUNNER_SHELL" description:"Select bash, cmd or powershell"`
CustomBuildDir *CustomBuildDir `toml:"custom_build_dir,omitempty" json:"custom_build_dir" group:"custom build dir configuration" namespace:"custom_build_dir"`
......
......@@ -121,6 +121,7 @@ This defines one runner entry.
| `pre_build_script` | Commands to be executed on the Runner after cloning the Git repository, but before executing the build. To insert multiple commands, use a (triple-quoted) multi-line string or "\n" character. |
| `post_build_script` | Commands to be executed on the Runner just after executing the build, but before executing `after_script`. To insert multiple commands, use a (triple-quoted) multi-line string or "\n" character. |
| `clone_url` | Overwrite the URL for the GitLab instance. Used if the Runner can't connect to GitLab on the URL GitLab exposes itself. |
| `debug_trace_disabled` | Disables the `CI_DEBUG_TRACE` feature. When set to true, then debug trace will remain disabled even if `CI_DEBUG_TRACE` will be set to `true` by the user. |
Example:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment