1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
//go:build !integration
// +build !integration
package helpers
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
)
var downloaderCredentials = common.JobCredentials{
ID: 1000,
Token: "test",
URL: "test",
}
func TestArtifactsDownloaderRequirements(t *testing.T) {
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
cmd := ArtifactsDownloaderCommand{}
assert.Panics(t, func() {
cmd.Execute(nil)
})
}
func TestArtifactsDownloader(t *testing.T) {
testCases := map[string]struct {
downloadState common.DownloadState
directDownload bool
stagingDir string
expectedSuccess bool
expectedDownloadCalled int
expectedDirectDownloadCalled int
}{
"download not found": {
downloadState: common.DownloadNotFound,
expectedSuccess: false,
expectedDownloadCalled: 1,
},
"download forbidden": {
downloadState: common.DownloadForbidden,
expectedSuccess: false,
expectedDownloadCalled: 1,
},
"retries are called": {
downloadState: common.DownloadFailed,
expectedSuccess: false,
expectedDownloadCalled: 3,
},
"first try is always direct download": {
downloadState: common.DownloadFailed,
directDownload: true,
expectedSuccess: false,
expectedDirectDownloadCalled: 1,
expectedDownloadCalled: 3,
},
"downloads artifact without direct download if requested": {
downloadState: common.DownloadSucceeded,
directDownload: false,
expectedSuccess: true,
expectedDirectDownloadCalled: 0,
expectedDownloadCalled: 1,
},
"downloads artifact with direct download if requested": {
downloadState: common.DownloadSucceeded,
directDownload: true,
expectedSuccess: true,
expectedDirectDownloadCalled: 1,
expectedDownloadCalled: 1,
},
"setting invalid staging directory": {
downloadState: common.DownloadSucceeded,
stagingDir: "/dev/null",
},
}
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
// ensure clean state
os.Remove(artifactsTestArchivedFile)
for testName, testCase := range testCases {
OnEachZipArchiver(t, func(t *testing.T) {
t.Run(testName, func(t *testing.T) {
network := &testNetwork{
downloadState: testCase.downloadState,
}
cmd := ArtifactsDownloaderCommand{
JobCredentials: downloaderCredentials,
DirectDownload: testCase.directDownload,
network: network,
retryHelper: retryHelper{
Retry: 2,
},
StagingDir: testCase.stagingDir,
}
// file is cleaned after running test
defer os.Remove(artifactsTestArchivedFile)
if testCase.expectedSuccess {
require.NotPanics(t, func() {
cmd.Execute(nil)
})
assert.FileExists(t, artifactsTestArchivedFile)
} else {
require.Panics(t, func() {
cmd.Execute(nil)
})
}
assert.Equal(t, testCase.expectedDirectDownloadCalled, network.directDownloadCalled)
assert.Equal(t, testCase.expectedDownloadCalled, network.downloadCalled)
})
})
}
}
|