Commit 9d3f4e85 authored by Kamil Trzciński's avatar Kamil Trzciński

Fix test failures

parent 845d1965
Pipeline #26179055 passed with stages
in 29 minutes and 19 seconds
......@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"
"net/url"
"os"
"path"
......@@ -162,9 +163,12 @@ func (b *Build) executeStage(ctx context.Context, buildStage BuildStage, executo
return nil
}
log.Println("executeStage", buildStage, script)
cmd := ExecutorCommand{
Context: ctx,
Script: script,
Stage: buildStage,
}
switch buildStage {
......@@ -185,9 +189,9 @@ func (b *Build) executeStage(ctx context.Context, buildStage BuildStage, executo
func (b *Build) executeUploadArtifacts(ctx context.Context, state error, executor Executor) (err error) {
if state == nil {
return b.executeStage(ctx, BuildStageUploadOnSuccessArtifacts, executor)
} else {
return b.executeStage(ctx, BuildStageUploadOnFailureArtifacts, executor)
}
return b.executeStage(ctx, BuildStageUploadOnFailureArtifacts, executor)
}
func (b *Build) executeScript(ctx context.Context, executor Executor) error {
......@@ -220,14 +224,15 @@ func (b *Build) executeScript(ctx context.Context, executor Executor) error {
err = b.executeStage(ctx, BuildStageArchiveCache, executor)
}
jobState := err
err = b.executeUploadArtifacts(ctx, jobState, executor)
uploadError := b.executeUploadArtifacts(ctx, err, executor)
// Use job's error if set
if jobState != nil {
err = jobState
// Use job's error as most important
if err != nil {
return err
}
return err
// Otherwise, use uploadError
return uploadError
}
func (b *Build) attemptExecuteStage(ctx context.Context, buildStage BuildStage, executor Executor, attempts int) (err error) {
......
......@@ -161,6 +161,12 @@ func TestPrepareFailureOnBuildError(t *testing.T) {
assert.IsType(t, err, &BuildError{})
}
func matchBuildStage(buildStage BuildStage) interface{} {
return mock.MatchedBy(func(cmd ExecutorCommand) bool {
return cmd.Stage == buildStage
})
}
func TestRunFailure(t *testing.T) {
e := MockExecutor{}
defer e.AssertExpectations(t)
......@@ -214,8 +220,9 @@ func TestGetSourcesRunFailure(t *testing.T) {
// Fail a build script
e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
e.On("Run", mock.Anything).Return(nil).Once()
e.On("Run", mock.Anything).Return(errors.New("build fail")).Times(3)
e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageGetSources)).Return(errors.New("build fail")).Times(3)
e.On("Run", matchBuildStage(BuildStageUploadOnFailureArtifacts)).Return(nil).Once()
e.On("Finish", errors.New("build fail")).Return().Once()
RegisterExecutor("build-get-sources-run-failure", &p)
......@@ -253,8 +260,11 @@ func TestArtifactDownloadRunFailure(t *testing.T) {
// Fail a build script
e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
e.On("Run", mock.Anything).Return(nil).Times(3)
e.On("Run", mock.Anything).Return(errors.New("build fail")).Times(3)
e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageDownloadArtifacts)).Return(errors.New("build fail")).Times(3)
e.On("Run", matchBuildStage(BuildStageUploadOnFailureArtifacts)).Return(nil).Once()
e.On("Finish", errors.New("build fail")).Return().Once()
RegisterExecutor("build-artifacts-run-failure", &p)
......@@ -292,9 +302,14 @@ func TestArtifactUploadRunFailure(t *testing.T) {
// Successful build script
e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"}).Times(8)
e.On("Run", mock.Anything).Return(nil).Times(7)
// Fail last stage (upload stage)
e.On("Run", mock.Anything).Return(errors.New("upload fail")).Once()
e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageDownloadArtifacts)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageUserScript)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageAfterScript)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageArchiveCache)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageUploadOnSuccessArtifacts)).Return(errors.New("upload fail")).Once()
e.On("Finish", errors.New("upload fail")).Return().Once()
RegisterExecutor("build-upload-artifacts-run-failure", &p)
......@@ -338,8 +353,10 @@ func TestRestoreCacheRunFailure(t *testing.T) {
// Fail a build script
e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
e.On("Run", mock.Anything).Return(nil).Times(2)
e.On("Run", mock.Anything).Return(errors.New("build fail")).Times(3)
e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(errors.New("build fail")).Times(3)
e.On("Run", matchBuildStage(BuildStageUploadOnFailureArtifacts)).Return(nil).Once()
e.On("Finish", errors.New("build fail")).Return().Once()
RegisterExecutor("build-cache-run-failure", &p)
......
......@@ -10,6 +10,7 @@ type ExecutorData interface{}
type ExecutorCommand struct {
Script string
Stage BuildStage
Predefined bool
Context context.Context
}
......
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