Skip to content

Commit

Permalink
Upload archive tests (#306)
Browse files Browse the repository at this point in the history
* Added tests for summary of upload and download.
* Changed ArtifactoryPath in ArtifactDetailsReader of the upload operation to be targetPath (instead of targetUrl), so it'll be the same as the summary of the download operation.
  • Loading branch information
asafgabai authored Mar 9, 2021
1 parent 9a7336f commit 3bc2bc7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 19 deletions.
6 changes: 3 additions & 3 deletions artifactory/services/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ func (us *UploadService) createArtifactHandlerFunc(uploadResult *utils.Result, u
if uploaded {
uploadResult.SuccessCount[threadId]++
if us.saveSummary {
us.resultsManager.addFinalResult(artifact.Artifact.LocalPath, targetUrl, &uploadFileDetails.Checksum)
us.resultsManager.addFinalResult(artifact.Artifact.LocalPath, artifact.Artifact.TargetPath, targetUrl, &uploadFileDetails.Checksum)
}
}
return
Expand Down Expand Up @@ -868,14 +868,14 @@ func newResultManager() (*resultsManager, error) {
}

// Write a result of a successful upload
func (rm *resultsManager) addFinalResult(localPath, targetUrl string, checksums *fileutils.ChecksumDetails) {
func (rm *resultsManager) addFinalResult(localPath, targetPath, targetUrl string, checksums *fileutils.ChecksumDetails) {
fileTransferDetails := utils.FileTransferDetails{
SourcePath: localPath,
TargetPath: targetUrl,
}
rm.singleFinalTransfersWriter.Write(fileTransferDetails)
artifactDetails := utils.ArtifactDetails{
ArtifactoryPath: targetUrl,
ArtifactoryPath: targetPath,
Checksums: utils.Checksums{
Sha256: checksums.Sha256,
Sha1: checksums.Sha1,
Expand Down
39 changes: 39 additions & 0 deletions tests/artifactorydownload_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -20,6 +21,7 @@ func TestArtifactoryDownload(t *testing.T) {
t.Run("excludePatterns", excludePatternsDownload)
t.Run("exclusions", exclusionsDownload)
t.Run("explodeArchive", explodeArchiveDownload)
t.Run("summary", summaryDownload)
artifactoryCleanup(t)
}

Expand Down Expand Up @@ -265,3 +267,40 @@ func explodeDownloadAndVerify(t *testing.T, downloadParams *services.DownloadPar
t.Error("Missing file a.in")
}
}

func summaryDownload(t *testing.T) {
workingDir, err := ioutil.TempDir("", "downloadTests")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(workingDir)
testsDownloadService.SetSaveSummary(true)
defer testsDownloadService.SetSaveSummary(false)
downloadPattern := RtTargetRepo + "*.tar.gz"
downloadTarget := workingDir + string(filepath.Separator)
summary, err := testsDownloadService.DownloadFiles(services.DownloadParams{ArtifactoryCommonParams: &utils.ArtifactoryCommonParams{Pattern: downloadPattern, Recursive: true, Target: downloadTarget}, Flat: true})
if err != nil {
t.Error(err)
}
defer summary.Close()
if summary.TotalSucceeded != 1 {
t.Error("Expected to download 1 files.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to download", summary.TotalFailed, "files.")
}
var transfers []utils.FileTransferDetails
for item := new(utils.FileTransferDetails); summary.TransferDetailsReader.NextRecord(item) == nil; item = new(utils.FileTransferDetails) {
transfers = append(transfers, *item)
}
assert.Len(t, transfers, 1)
assert.Equal(t, testsUploadService.ArtDetails.GetUrl()+RtTargetRepo+"c.tar.gz", transfers[0].SourcePath)
assert.Equal(t, filepath.Join(workingDir, "c.tar.gz"), transfers[0].TargetPath)
var artifacts []utils.ArtifactDetails
for item := new(utils.ArtifactDetails); summary.ArtifactsDetailsReader.NextRecord(item) == nil; item = new(utils.ArtifactDetails) {
artifacts = append(artifacts, *item)
}
assert.Len(t, artifacts, 1)
assert.Equal(t, RtTargetRepo+"c.tar.gz", artifacts[0].ArtifactoryPath)
artifactoryCleanup(t)
}
70 changes: 54 additions & 16 deletions tests/artifactoryupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestArtifactoryUpload(t *testing.T) {
t.Run("includeDirs", includeDirsUpload)
t.Run("explode", explodeUpload)
t.Run("props", propsUpload)
t.Run("summary", summaryUpload)
}

func flatUpload(t *testing.T) {
Expand All @@ -30,15 +31,15 @@ func flatUpload(t *testing.T) {
up.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{Pattern: pattern, Recursive: true, Target: RtTargetRepo}
up.Flat = true
summary, err := testsUploadService.UploadFiles(up)
if err != nil {
t.Error(err)
}
if summary.TotalSucceeded != 1 {
t.Error("Expected to upload 1 file.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to upload", summary.TotalFailed, "files.")
}
if err != nil {
t.Error(err)
}
searchParams := services.NewSearchParams()
searchParams.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{}
searchParams.Pattern = RtTargetRepo
Expand Down Expand Up @@ -70,15 +71,15 @@ func recursiveUpload(t *testing.T) {
up.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{Pattern: pattern, Recursive: true, Target: RtTargetRepo}
up.Flat = true
summary, err := testsUploadService.UploadFiles(up)
if err != nil {
t.Error(err)
}
if summary.TotalSucceeded != 1 {
t.Error("Expected to upload 1 file.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to upload", summary.TotalFailed, "files.")
}
if err != nil {
t.Error(err)
}
searchParams := services.NewSearchParams()
searchParams.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{}
searchParams.Pattern = RtTargetRepo
Expand Down Expand Up @@ -113,15 +114,15 @@ func placeholderUpload(t *testing.T) {
up.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{Pattern: pattern, Recursive: true, Target: RtTargetRepo + "{1}"}
up.Flat = true
summary, err := testsUploadService.UploadFiles(up)
if err != nil {
t.Error(err)
}
if summary.TotalSucceeded != 1 {
t.Error("Expected to upload 1 file.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to upload", summary.TotalFailed, "files.")
}
if err != nil {
t.Error(err)
}
searchParams := services.NewSearchParams()
searchParams.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{}
searchParams.Pattern = RtTargetRepo
Expand Down Expand Up @@ -156,15 +157,15 @@ func includeDirsUpload(t *testing.T) {
up.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{Pattern: pattern, IncludeDirs: true, Recursive: false, Target: RtTargetRepo}
up.Flat = true
summary, err := testsUploadService.UploadFiles(up)
if err != nil {
t.Error(err)
}
if summary.TotalSucceeded != 0 {
t.Error("Expected to upload 1 file.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to upload", summary.TotalFailed, "files.")
}
if err != nil {
t.Error(err)
}
searchParams := services.NewSearchParams()
searchParams.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{}
searchParams.Pattern = RtTargetRepo
Expand Down Expand Up @@ -212,15 +213,15 @@ func explodeUpload(t *testing.T) {
up.Flat = true
up.ExplodeArchive = true
summary, err := testsUploadService.UploadFiles(up)
if err != nil {
t.Error(err)
}
if summary.TotalSucceeded != 1 {
t.Error("Expected to upload 1 file.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to upload", summary.TotalFailed, "files.")
}
if err != nil {
t.Error(err)
}
searchParams := services.NewSearchParams()
searchParams.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{}
searchParams.Pattern = RtTargetRepo
Expand Down Expand Up @@ -257,9 +258,9 @@ func propsUpload(t *testing.T) {
up.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{Pattern: pattern, Target: RtTargetRepo, TargetProps: "key1=val1"}
up.Flat = true
summary, err := testsUploadService.UploadFiles(up)
assert.NoError(t, err)
assert.Equal(t, 1, summary.TotalSucceeded)
assert.Equal(t, 0, summary.TotalFailed)
assert.NoError(t, err)

// Search a.in with property key1=val1
searchParams := services.NewSearchParams()
Expand All @@ -286,6 +287,43 @@ func propsUpload(t *testing.T) {
artifactoryCleanup(t)
}

func summaryUpload(t *testing.T) {
workingDir, _ := createWorkingDir(t)
defer os.RemoveAll(workingDir)

pattern := FixWinPath(filepath.Join(workingDir, "*"))
up := services.NewUploadParams()
up.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{Pattern: pattern, Recursive: true, Target: RtTargetRepo}
up.Flat = true
testsUploadService.SetSaveSummary(true)
defer testsUploadService.SetSaveSummary(false)
summary, err := testsUploadService.UploadFiles(up)
if err != nil {
t.Error(err)
}
defer summary.Close()
if summary.TotalSucceeded != 1 {
t.Error("Expected to upload 1 file.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to upload", summary.TotalFailed, "files.")
}
var transfers []utils.FileTransferDetails
for item := new(utils.FileTransferDetails); summary.TransferDetailsReader.NextRecord(item) == nil; item = new(utils.FileTransferDetails) {
transfers = append(transfers, *item)
}
assert.Len(t, transfers, 1)
assert.Equal(t, filepath.Join(workingDir, "out", "a.in"), transfers[0].SourcePath)
assert.Equal(t, testsUploadService.ArtDetails.GetUrl()+RtTargetRepo+"a.in", transfers[0].TargetPath)
var artifacts []utils.ArtifactDetails
for item := new(utils.ArtifactDetails); summary.ArtifactsDetailsReader.NextRecord(item) == nil; item = new(utils.ArtifactDetails) {
artifacts = append(artifacts, *item)
}
assert.Len(t, artifacts, 1)
assert.Equal(t, RtTargetRepo+"a.in", artifacts[0].ArtifactoryPath)
artifactoryCleanup(t)
}

func createWorkingDir(t *testing.T) (string, string) {
workingDir, relativePath, err := tests.CreateFileWithContent("a.in", "/out/")
if err != nil {
Expand Down

0 comments on commit 3bc2bc7

Please sign in to comment.