Commit 54132bb8 authored by Kamil Trzciński's avatar Kamil Trzciński

Add tests for raw file formats

parent 6263756f
Pipeline #33118354 passed with stages
in 28 minutes and 23 seconds
......@@ -100,6 +100,15 @@ func (m *testNetwork) consumeGzipUpload(config common.JobCredentials, reader io.
return m.uploadState
}
func (m *testNetwork) consumeRawUpload(config common.JobCredentials, reader io.Reader, options common.ArtifactsOptions) common.UploadState {
var buffer bytes.Buffer
io.Copy(&buffer, reader)
m.uploadedFiles = append(m.uploadedFiles, "raw")
m.uploadFormat = common.ArtifactFormatRaw
return m.uploadState
}
func (m *testNetwork) UploadRawArtifacts(config common.JobCredentials, reader io.Reader, options common.ArtifactsOptions) common.UploadState {
m.uploadCalled++
......@@ -114,6 +123,9 @@ func (m *testNetwork) UploadRawArtifacts(config common.JobCredentials, reader io
case common.ArtifactFormatGzip:
return m.consumeGzipUpload(config, reader, options)
case common.ArtifactFormatRaw:
return m.consumeRawUpload(config, reader, options)
default:
return common.UploadForbidden
}
......
......@@ -41,22 +41,13 @@ func (c *ArtifactsUploaderCommand) generateGzipStream(w *io.PipeWriter) {
w.CloseWithError(err)
}
func (c *ArtifactsUploaderCommand) generateRawStream(w *io.PipeWriter) {
if len(c.sortedFiles()) > 1 {
w.CloseWithError(errors.New("only single file supported"))
return
func (c *ArtifactsUploaderCommand) openRawStream() (io.ReadCloser, error) {
fileNames := c.sortedFiles()
if len(fileNames) > 1 {
return nil, errors.New("only one file can be send as raw")
}
fileName := c.sortedFiles()[0]
file, err := os.Open(fileName)
if err != nil {
w.CloseWithError(err)
return
}
defer file.Close()
_, err = io.Copy(w, file)
w.CloseWithError(err)
return os.Open(fileNames[0])
}
func (c *ArtifactsUploaderCommand) createReadStream() (string, io.ReadCloser, error) {
......@@ -81,9 +72,8 @@ func (c *ArtifactsUploaderCommand) createReadStream() (string, io.ReadCloser, er
return name + ".gz", pr, nil
case common.ArtifactFormatRaw:
pr, pw := io.Pipe()
go c.generateRawStream(pw)
return name, pr, nil
file, err := c.openRawStream()
return name, file, err
default:
return "", nil, fmt.Errorf("unsupported archive format: %s", c.Format)
......
......@@ -183,6 +183,61 @@ func TestArtifactsUploaderGzipSendsMultipleFiles(t *testing.T) {
assert.Contains(t, network.uploadedFiles, artifactsTestArchivedFile2)
}
func TestArtifactsUploaderRawSucceeded(t *testing.T) {
network := &testNetwork{
uploadState: common.UploadSucceeded,
}
cmd := ArtifactsUploaderCommand{
JobCredentials: UploaderCredentials,
Format: common.ArtifactFormatRaw,
Name: "my-release",
Type: "my-type",
network: network,
fileArchiver: fileArchiver{
Paths: []string{artifactsTestArchivedFile},
},
}
ioutil.WriteFile(artifactsTestArchivedFile, nil, 0600)
defer os.Remove(artifactsTestArchivedFile)
cmd.Execute(nil)
assert.Equal(t, 1, network.uploadCalled)
assert.Equal(t, common.ArtifactFormatRaw, network.uploadFormat)
assert.Equal(t, "my-release", network.uploadName)
assert.Equal(t, "my-type", network.uploadType)
assert.Contains(t, network.uploadedFiles, "raw")
}
func TestArtifactsUploaderRawDoesNotSendMultipleFiles(t *testing.T) {
network := &testNetwork{
uploadState: common.UploadSucceeded,
}
cmd := ArtifactsUploaderCommand{
JobCredentials: UploaderCredentials,
Format: common.ArtifactFormatRaw,
Name: "junit.xml",
Type: "junit",
network: network,
fileArchiver: fileArchiver{
Paths: []string{artifactsTestArchivedFile, artifactsTestArchivedFile2},
},
}
ioutil.WriteFile(artifactsTestArchivedFile, nil, 0600)
defer os.Remove(artifactsTestArchivedFile)
ioutil.WriteFile(artifactsTestArchivedFile2, nil, 0600)
defer os.Remove(artifactsTestArchivedFile)
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
assert.Panics(t, func() {
cmd.Execute(nil)
})
}
func TestArtifactsUploaderNoFilesDoNotGenerateError(t *testing.T) {
network := &testNetwork{
uploadState: common.UploadSucceeded,
......
......@@ -57,6 +57,7 @@ type FeaturesInfo struct {
Cache bool `json:"cache"`
Shared bool `json:"shared"`
UploadMultipleArtifacts bool `json:"upload_multiple_artifacts"`
UploadRawArtifacts bool `json:"upload_raw_artifacts"`
Session bool `json:"session"`
Terminal bool `json:"terminal"`
}
......
......@@ -20,6 +20,7 @@ type AbstractShell struct {
func (b *AbstractShell) GetFeatures(features *common.FeaturesInfo) {
features.Artifacts = true
features.UploadMultipleArtifacts = true
features.UploadRawArtifacts = true
features.Cache = true
}
......
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