Commit 1f9ccc6b authored by Kamil Trzciński's avatar Kamil Trzciński

Improve test suite

parent 787331d5
Pipeline #26182443 failed with stages
in 19 minutes and 59 seconds
......@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"log"
"net/url"
"os"
"path"
......@@ -163,8 +162,6 @@ func (b *Build) executeStage(ctx context.Context, buildStage BuildStage, executo
return nil
}
log.Println("executeStage", buildStage, script)
cmd := ExecutorCommand{
Context: ctx,
Script: script,
......
......@@ -36,7 +36,14 @@ func TestBuildRun(t *testing.T) {
// Run script successfully
e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
e.On("Run", mock.Anything).Return(nil)
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(nil).Once()
RegisterExecutor("build-run-test", &p)
......@@ -167,7 +174,7 @@ func matchBuildStage(buildStage BuildStage) interface{} {
})
}
func TestRunFailure(t *testing.T) {
func TestRunFailureRunsAfterScriptAndArtifactsOnFailure(t *testing.T) {
e := MockExecutor{}
defer e.AssertExpectations(t)
......@@ -184,7 +191,13 @@ func TestRunFailure(t *testing.T) {
// Fail a build script
e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})
e.On("Run", mock.Anything).Return(errors.New("build fail"))
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(errors.New("build fail")).Once()
e.On("Run", matchBuildStage(BuildStageAfterScript)).Return(nil).Once()
e.On("Run", matchBuildStage(BuildStageUploadOnFailureArtifacts)).Return(nil).Once()
e.On("Finish", errors.New("build fail")).Return().Once()
RegisterExecutor("build-run-run-failure", &p)
......
......@@ -4,6 +4,9 @@ import (
"fmt"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers/tls"
"gitlab.com/gitlab-org/gitlab-runner/shells/mocks"
......@@ -40,3 +43,187 @@ func TestWriteGitSSLConfig(t *testing.T) {
mockWriter.AssertExpectations(t)
}
func TestWriteWritingArtifactsOnSuccess(t *testing.T) {
gitlabURL := "https://example.com:3443"
shell := AbstractShell{}
build := &common.Build{
JobResponse: common.JobResponse{
ID: 1000,
Token: "token",
Artifacts: common.Artifacts{
common.Artifact{
Paths: []string{"default"},
},
common.Artifact{
Paths: []string{"on-success"},
When: common.ArtifactWhenOnSuccess,
},
common.Artifact{
Paths: []string{"on-failure"},
When: common.ArtifactWhenOnFailure,
},
common.Artifact{
Paths: []string{"always"},
When: common.ArtifactWhenAlways,
},
common.Artifact{
Paths: []string{"zip-archive"},
When: common.ArtifactWhenAlways,
Format: common.ArtifactFormatZip,
Type: "archive",
},
common.Artifact{
Paths: []string{"gzip-junit"},
When: common.ArtifactWhenAlways,
Format: common.ArtifactFormatGzip,
Type: "junit",
},
},
},
Runner: &common.RunnerConfig{
RunnerCredentials: common.RunnerCredentials{
URL: gitlabURL,
},
},
}
info := common.ShellScriptInfo{
RunnerCommand: "gitlab-runner-helper",
Build: build,
}
mockWriter := new(mocks.ShellWriter)
defer mockWriter.AssertExpectations(t)
mockWriter.On("Variable", mock.Anything)
mockWriter.On("Cd", mock.Anything)
mockWriter.On("IfCmd", "gitlab-runner-helper", "--version")
mockWriter.On("Notice", mock.Anything)
mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader",
"--url", gitlabURL,
"--token", "token",
"--id", "1000",
"--path", "default").Once()
mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader",
"--url", gitlabURL,
"--token", "token",
"--id", "1000",
"--path", "on-success").Once()
mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader",
"--url", gitlabURL,
"--token", "token",
"--id", "1000",
"--path", "always").Once()
mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader",
"--url", gitlabURL,
"--token", "token",
"--id", "1000",
"--path", "zip-archive",
"--artifact-format", "zip",
"--artifact-type", "archive").Once()
mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader",
"--url", gitlabURL,
"--token", "token",
"--id", "1000",
"--path", "gzip-junit",
"--artifact-format", "gzip",
"--artifact-type", "junit").Once()
mockWriter.On("Else")
mockWriter.On("Warning", mock.Anything, mock.Anything, mock.Anything)
mockWriter.On("EndIf")
err := shell.writeScript(mockWriter, common.BuildStageUploadOnSuccessArtifacts, info)
require.NoError(t, err)
}
func TestWriteWritingArtifactsOnFailure(t *testing.T) {
gitlabURL := "https://example.com:3443"
shell := AbstractShell{}
build := &common.Build{
JobResponse: common.JobResponse{
ID: 1000,
Token: "token",
Artifacts: common.Artifacts{
common.Artifact{
Paths: []string{"default"},
},
common.Artifact{
Paths: []string{"on-success"},
When: common.ArtifactWhenOnSuccess,
},
common.Artifact{
Paths: []string{"on-failure"},
When: common.ArtifactWhenOnFailure,
},
common.Artifact{
Paths: []string{"always"},
When: common.ArtifactWhenAlways,
},
common.Artifact{
Paths: []string{"zip-archive"},
When: common.ArtifactWhenAlways,
Format: common.ArtifactFormatZip,
Type: "archive",
},
common.Artifact{
Paths: []string{"gzip-junit"},
When: common.ArtifactWhenAlways,
Format: common.ArtifactFormatGzip,
Type: "junit",
},
},
},
Runner: &common.RunnerConfig{
RunnerCredentials: common.RunnerCredentials{
URL: gitlabURL,
},
},
}
info := common.ShellScriptInfo{
RunnerCommand: "gitlab-runner-helper",
Build: build,
}
mockWriter := new(mocks.ShellWriter)
defer mockWriter.AssertExpectations(t)
mockWriter.On("Variable", mock.Anything)
mockWriter.On("Cd", mock.Anything)
mockWriter.On("IfCmd", "gitlab-runner-helper", "--version")
mockWriter.On("Notice", mock.Anything)
mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader",
"--url", gitlabURL,
"--token", "token",
"--id", "1000",
"--path", "default").Once()
mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader",
"--url", gitlabURL,
"--token", "token",
"--id", "1000",
"--path", "on-failure").Once()
mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader",
"--url", gitlabURL,
"--token", "token",
"--id", "1000",
"--path", "always").Once()
mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader",
"--url", gitlabURL,
"--token", "token",
"--id", "1000",
"--path", "zip-archive",
"--artifact-format", "zip",
"--artifact-type", "archive").Once()
mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader",
"--url", gitlabURL,
"--token", "token",
"--id", "1000",
"--path", "gzip-junit",
"--artifact-format", "gzip",
"--artifact-type", "junit").Once()
mockWriter.On("Else")
mockWriter.On("Warning", mock.Anything, mock.Anything, mock.Anything)
mockWriter.On("EndIf")
err := shell.writeScript(mockWriter, common.BuildStageUploadOnFailureArtifacts, info)
require.NoError(t, err)
}
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