Commit 1ff9d8c6 authored by Shinya Maeda's avatar Shinya Maeda

Adopting the suggestions

parent e371c31e
......@@ -268,7 +268,10 @@ func (b *Build) handleError(err error) error {
case context.DeadlineExceeded:
b.CurrentState = BuildRunRuntimeTimedout
return &BuildError{Inner: fmt.Errorf("execution took longer than %v seconds", b.GetBuildTimeout()), FailureReason: JobExecutionTimeout}
return &BuildError{
Inner: fmt.Errorf("execution took longer than %v seconds", b.GetBuildTimeout()),
FailureReason: JobExecutionTimeout
}
default:
b.CurrentState = BuildRunRuntimeFinished
......
......@@ -5,6 +5,7 @@ import (
"fmt"
"os"
"testing"
"time"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
......@@ -167,10 +168,10 @@ func TestPrepareFailureOnBuildError(t *testing.T) {
RegisterExecutor("build-run-prepare-failure-on-build-error", &p)
failedBuild, err := GetFailedBuild()
successfulBuild, err := GetSuccessfulBuild()
assert.NoError(t, err)
build := &Build{
JobResponse: failedBuild,
JobResponse: successfulBuild,
Runner: &RunnerConfig{
RunnerSettings: RunnerSettings{
Executor: "build-run-prepare-failure-on-build-error",
......@@ -179,12 +180,9 @@ func TestPrepareFailureOnBuildError(t *testing.T) {
}
err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
assert.IsType(t, err, &BuildError{})
if buildError, ok := err.(*BuildError); ok {
assert.Equal(t, buildError.FailureReason, ScriptFailure)
}
}
func TestPrepareFailureOnExecutionTimeout(t *testing.T) {
func TestJobFailure(t *testing.T) {
e := MockExecutor{}
defer e.AssertExpectations(t)
......@@ -200,10 +198,16 @@ func TestPrepareFailureOnExecutionTimeout(t *testing.T) {
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
Return(&BuildError{FailureReason: JobExecutionTimeout}).Times(1)
Return(nil).Times(1)
e.On("Cleanup").Return().Times(1)
RegisterExecutor("build-run-prepare-failure-on-execution-timeout", &p)
// Succeed a build script
thrownErr := &BuildError{Inner: errors.New("test error")}
e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
e.On("Run", mock.Anything).Return(thrownErr)
e.On("Finish", thrownErr).Return().Once()
RegisterExecutor("build-run-prepare-failure-on-build-error", &p)
failedBuild, err := GetFailedBuild()
assert.NoError(t, err)
......@@ -211,16 +215,76 @@ func TestPrepareFailureOnExecutionTimeout(t *testing.T) {
JobResponse: failedBuild,
Runner: &RunnerConfig{
RunnerSettings: RunnerSettings{
Executor: "build-run-prepare-failure-on-execution-timeout",
Executor: "build-run-prepare-failure-on-build-error",
},
},
}
err = build.Run(&Config{}, &Trace{Writer: os.Stdout})
assert.IsType(t, err, &BuildError{})
if buildError, ok := err.(*BuildError); ok {
assert.Equal(t, buildError.FailureReason, JobExecutionTimeout)
trace := new(MockJobTrace)
defer trace.AssertExpectations(t)
trace.On("Write", mock.Anything).Return(0, nil)
trace.On("IsStdout").Return(true)
trace.On("SetCancelFunc", mock.Anything).Once()
trace.On("Fail", thrownErr, ScriptFailure).Once()
err = build.Run(&Config{}, trace)
require.IsType(t, &BuildError{}, err)
}
func TestJobFailureOnExecutionTimeout(t *testing.T) {
e := MockExecutor{}
defer e.AssertExpectations(t)
p := MockExecutorProvider{}
defer p.AssertExpectations(t)
// Create executor
p.On("CanCreate").Return(true).Once()
p.On("GetDefaultShell").Return("bash").Once()
p.On("GetFeatures", mock.Anything).Return(nil).Twice()
p.On("Create").Return(&e).Times(1)
// Prepare plan
e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).
Return(nil).Times(1)
e.On("Cleanup").Return().Times(1)
// Succeed a build script
e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
e.On("Run", matchBuildStage(BuildStageUserScript)).Run(func(arguments mock.Arguments) {
time.Sleep(2 * time.Second)
}).Return(nil)
e.On("Run", mock.Anything).Return(nil)
e.On("Finish", mock.Anything).Return().Once()
RegisterExecutor("build-run-prepare-failure-on-build-error", &p)
successfulBuild, err := GetSuccessfulBuild()
assert.NoError(t, err)
successfulBuild.RunnerInfo.Timeout = 1
build := &Build{
JobResponse: successfulBuild,
Runner: &RunnerConfig{
RunnerSettings: RunnerSettings{
Executor: "build-run-prepare-failure-on-build-error",
},
},
}
trace := new(MockJobTrace)
defer trace.AssertExpectations(t)
trace.On("Write", mock.Anything).Return(0, nil)
trace.On("IsStdout").Return(true)
trace.On("SetCancelFunc", mock.Anything).Once()
trace.On("Fail", mock.Anything, JobExecutionTimeout).Run(func(arguments mock.Arguments) {
assert.Error(t, arguments.Get(0).(error))
}).Once()
err = build.Run(&Config{}, trace)
require.IsType(t, &BuildError{}, err)
}
func matchBuildStage(buildStage BuildStage) interface{} {
......
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