github.com/jfrog/jfrog-client-go@v1.40.2/tests/artifactorydownload_test.go (about) 1 package tests 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/jfrog/jfrog-client-go/utils/tests" 9 10 "github.com/stretchr/testify/assert" 11 12 "github.com/jfrog/jfrog-client-go/artifactory/services" 13 "github.com/jfrog/jfrog-client-go/artifactory/services/utils" 14 clientutils "github.com/jfrog/jfrog-client-go/utils" 15 "github.com/jfrog/jfrog-client-go/utils/io/fileutils" 16 ) 17 18 func TestArtifactoryDownload(t *testing.T) { 19 initArtifactoryTest(t) 20 uploadDummyFile(t) 21 t.Run("flat", flatDownload) 22 t.Run("recursive", recursiveDownload) 23 t.Run("placeholder", placeholderDownload) 24 t.Run("includeDirs", includeDirsDownload) 25 t.Run("exclusions", exclusionsDownload) 26 t.Run("explodeArchive", explodeArchiveDownload) 27 t.Run("summary", summaryDownload) 28 t.Run("duplicate", duplicateDownload) 29 artifactoryCleanup(t) 30 } 31 32 func flatDownload(t *testing.T) { 33 var err error 34 workingDir, err := os.MkdirTemp("", "downloadTests") 35 if err != nil { 36 t.Error(err) 37 } 38 defer tests.RemoveAllAndAssert(t, workingDir) 39 downloadPattern := getRtTargetRepo() + "*" 40 downloadTarget := workingDir + string(filepath.Separator) 41 // Download all from TargetRepo with flat = true 42 _, err = testsDownloadService.DownloadFiles(services.DownloadParams{CommonParams: &utils.CommonParams{Pattern: downloadPattern, Recursive: true, Target: downloadTarget}, Flat: true}) 43 if err != nil { 44 t.Error(err) 45 } 46 if !fileutils.IsPathExists(filepath.Join(workingDir, "a.in"), false) { 47 t.Error("Missing file a.in") 48 } 49 if !fileutils.IsPathExists(filepath.Join(workingDir, "b.in"), false) { 50 t.Error("Missing file b.in") 51 } 52 if !fileutils.IsPathExists(filepath.Join(workingDir, "c.tar.gz"), false) { 53 t.Error("Missing file c.tar.gz") 54 } 55 56 workingDir2, err := os.MkdirTemp("", "downloadTests") 57 downloadTarget = workingDir2 + string(filepath.Separator) 58 if err != nil { 59 t.Error(err) 60 } 61 defer tests.RemoveAllAndAssert(t, workingDir2) 62 // Download all from TargetRepo with flat = false 63 _, err = testsDownloadService.DownloadFiles(services.DownloadParams{CommonParams: &utils.CommonParams{Pattern: downloadPattern, Recursive: true, Target: downloadTarget}, Flat: false}) 64 if err != nil { 65 t.Error(err) 66 } 67 if !fileutils.IsPathExists(filepath.Join(workingDir2, "test", "a.in"), false) { 68 t.Error("Missing file a.in") 69 } 70 if !fileutils.IsPathExists(filepath.Join(workingDir2, "b.in"), false) { 71 t.Error("Missing file b.in") 72 } 73 if !fileutils.IsPathExists(filepath.Join(workingDir2, "c.tar.gz"), false) { 74 t.Error("Missing file c.tar.gz") 75 } 76 } 77 78 func recursiveDownload(t *testing.T) { 79 uploadDummyFile(t) 80 var err error 81 workingDir, err := os.MkdirTemp("", "downloadTests") 82 if err != nil { 83 t.Error(err) 84 } 85 defer tests.RemoveAllAndAssert(t, workingDir) 86 downloadPattern := getRtTargetRepo() + "*" 87 downloadTarget := workingDir + string(filepath.Separator) 88 _, err = testsDownloadService.DownloadFiles(services.DownloadParams{CommonParams: &utils.CommonParams{Pattern: downloadPattern, Recursive: true, Target: downloadTarget}, Flat: true}) 89 if err != nil { 90 t.Error(err) 91 } 92 if !fileutils.IsPathExists(filepath.Join(workingDir, "a.in"), false) { 93 t.Error("Missing file a.in") 94 } 95 96 if !fileutils.IsPathExists(filepath.Join(workingDir, "b.in"), false) { 97 t.Error("Missing file b.in") 98 } 99 if !fileutils.IsPathExists(filepath.Join(workingDir, "c.tar.gz"), false) { 100 t.Error("Missing file c.tar.gz") 101 } 102 103 workingDir2, err := os.MkdirTemp("", "downloadTests") 104 if err != nil { 105 t.Error(err) 106 } 107 defer tests.RemoveAllAndAssert(t, workingDir2) 108 downloadTarget = workingDir2 + string(filepath.Separator) 109 _, err = testsDownloadService.DownloadFiles(services.DownloadParams{CommonParams: &utils.CommonParams{Pattern: downloadPattern, Recursive: false, Target: downloadTarget}, Flat: true}) 110 if err != nil { 111 t.Error(err) 112 } 113 if fileutils.IsPathExists(filepath.Join(workingDir2, "a.in"), false) { 114 t.Error("Should not download a.in") 115 } 116 117 if !fileutils.IsPathExists(filepath.Join(workingDir2, "b.in"), false) { 118 t.Error("Missing file b.in") 119 } 120 if !fileutils.IsPathExists(filepath.Join(workingDir2, "c.tar.gz"), false) { 121 t.Error("Missing file c.tar.gz") 122 } 123 } 124 125 func placeholderDownload(t *testing.T) { 126 uploadDummyFile(t) 127 var err error 128 workingDir, err := os.MkdirTemp("", "downloadTests") 129 if err != nil { 130 t.Error(err) 131 } 132 defer tests.RemoveAllAndAssert(t, workingDir) 133 downloadPattern := getRtTargetRepo() + "(*).in" 134 downloadTarget := workingDir + string(filepath.Separator) + "{1}" + string(filepath.Separator) 135 _, err = testsDownloadService.DownloadFiles(services.DownloadParams{CommonParams: &utils.CommonParams{Pattern: downloadPattern, Recursive: true, Target: downloadTarget}, Flat: true}) 136 if err != nil { 137 t.Error(err) 138 } 139 if !fileutils.IsPathExists(filepath.Join(workingDir, "test", "a", "a.in"), false) { 140 t.Error("Missing file a.in") 141 } 142 143 if !fileutils.IsPathExists(filepath.Join(workingDir, "b", "b.in"), false) { 144 t.Error("Missing file b.in") 145 } 146 } 147 148 func includeDirsDownload(t *testing.T) { 149 var err error 150 workingDir, err := os.MkdirTemp("", "downloadTests") 151 if err != nil { 152 t.Error(err) 153 } 154 defer tests.RemoveAllAndAssert(t, workingDir) 155 downloadPattern := getRtTargetRepo() + "*" 156 downloadTarget := workingDir + string(filepath.Separator) 157 _, err = testsDownloadService.DownloadFiles(services.DownloadParams{CommonParams: &utils.CommonParams{Pattern: downloadPattern, IncludeDirs: true, Recursive: false, Target: downloadTarget}, Flat: false}) 158 if err != nil { 159 t.Error(err) 160 } 161 if !fileutils.IsPathExists(filepath.Join(workingDir, "test"), false) { 162 t.Error("Missing test folder") 163 } 164 if !fileutils.IsPathExists(filepath.Join(workingDir, "b.in"), false) { 165 t.Error("Missing file b.in") 166 } 167 if !fileutils.IsPathExists(filepath.Join(workingDir, "c.tar.gz"), false) { 168 t.Error("Missing file c.tsr.gz") 169 } 170 } 171 172 func exclusionsDownload(t *testing.T) { 173 workingDir, err := os.MkdirTemp("", "downloadTests") 174 if err != nil { 175 t.Error(err) 176 } 177 defer tests.RemoveAllAndAssert(t, workingDir) 178 downloadPattern := getRtTargetRepo() + "*" 179 downloadTarget := workingDir + string(filepath.Separator) 180 exclusions := []string{"*b.in", "*.tar.gz"} 181 _, err = testsDownloadService.DownloadFiles(services.DownloadParams{CommonParams: &utils.CommonParams{Pattern: downloadPattern, Recursive: true, Target: downloadTarget, Exclusions: exclusions}, Flat: true}) 182 if err != nil { 183 t.Error(err) 184 } 185 if !fileutils.IsPathExists(filepath.Join(workingDir, "a.in"), false) { 186 t.Error("Missing file a.in") 187 } 188 189 if fileutils.IsPathExists(filepath.Join(workingDir, "b.in"), false) { 190 t.Error("File b.in should have been excluded") 191 } 192 if fileutils.IsPathExists(filepath.Join(workingDir, "c.tar.gz"), false) { 193 t.Error("File c.tar.gz should have been excluded") 194 } 195 } 196 197 func explodeArchiveDownload(t *testing.T) { 198 workingDir, err := os.MkdirTemp("", "downloadTests") 199 if err != nil { 200 t.Error(err) 201 } 202 defer tests.RemoveAllAndAssert(t, workingDir) 203 downloadPattern := getRtTargetRepo() + "*.tar.gz" 204 downloadTarget := workingDir + string(filepath.Separator) 205 downloadParams := services.DownloadParams{CommonParams: &utils.CommonParams{Pattern: downloadPattern, Recursive: true, Target: downloadTarget}, Flat: true, Explode: false} 206 // First we'll download c.tar.gz without extracting it (explode = false by default). 207 _, err = testsDownloadService.DownloadFiles(downloadParams) 208 if err != nil { 209 t.Error(err) 210 } 211 if fileutils.IsPathExists(filepath.Join(workingDir, "a.in"), false) { 212 t.Error("File a.in should not have been downloaded") 213 } 214 if fileutils.IsPathExists(filepath.Join(workingDir, "b.in"), false) { 215 t.Error("File b.in should not have been downloaded") 216 } 217 if !fileutils.IsPathExists(filepath.Join(workingDir, "c.tar.gz"), false) { 218 t.Error("Missing file c.tar.gz") 219 } 220 221 // Scenario 1: Download c.tar.gz with explode = true, when it already exists in the target dir. 222 // Artifactory should perform "checksum download" and not actually downloading it, but still need to extract it. 223 downloadParams.Explode = true 224 explodeDownloadAndVerify(t, &downloadParams, workingDir) 225 226 // Remove the download target dir. 227 tests.RemoveAllAndAssert(t, workingDir) 228 // Scenario 2: Download c.tar.gz with explode = true, when it does not exist in the target dir. 229 // Artifactory should download the file and extract it. 230 explodeDownloadAndVerify(t, &downloadParams, workingDir) 231 } 232 233 func explodeDownloadAndVerify(t *testing.T, downloadParams *services.DownloadParams, workingDir string) { 234 _, err := testsDownloadService.DownloadFiles(*downloadParams) 235 if err != nil { 236 t.Error(err) 237 } 238 if fileutils.IsPathExists(filepath.Join(workingDir, "c.tar.gz"), false) { 239 t.Error("File c.tar.gz should have been extracted") 240 } 241 if !fileutils.IsPathExists(filepath.Join(workingDir, "a.in"), false) { 242 t.Error("Missing file a.in") 243 } 244 } 245 246 func summaryDownload(t *testing.T) { 247 workingDir, err := os.MkdirTemp("", "downloadTests") 248 if err != nil { 249 t.Error(err) 250 } 251 defer tests.RemoveAllAndAssert(t, workingDir) 252 testsDownloadService.SetSaveSummary(true) 253 defer testsDownloadService.SetSaveSummary(false) 254 downloadPattern := getRtTargetRepo() + "*.tar.gz" 255 downloadTarget := workingDir + string(filepath.Separator) 256 summary, err := testsDownloadService.DownloadFiles(services.DownloadParams{CommonParams: &utils.CommonParams{Pattern: downloadPattern, Recursive: true, Target: downloadTarget}, Flat: true}) 257 if err != nil { 258 t.Error(err) 259 } 260 defer func() { 261 assert.NoError(t, summary.Close()) 262 }() 263 if summary.TotalSucceeded != 1 { 264 t.Error("Expected to download 1 files.") 265 } 266 if summary.TotalFailed != 0 { 267 t.Error("Failed to download", summary.TotalFailed, "files.") 268 } 269 var transfers []clientutils.FileTransferDetails 270 for item := new(clientutils.FileTransferDetails); summary.TransferDetailsReader.NextRecord(item) == nil; item = new(clientutils.FileTransferDetails) { 271 transfers = append(transfers, *item) 272 } 273 assert.Len(t, transfers, 1) 274 assert.Equal(t, testsUploadService.ArtDetails.GetUrl()+getRtTargetRepo()+"c.tar.gz", transfers[0].RtUrl+transfers[0].SourcePath) 275 assert.Equal(t, filepath.Join(workingDir, "c.tar.gz"), transfers[0].TargetPath) 276 var artifacts []utils.ArtifactDetails 277 for item := new(utils.ArtifactDetails); summary.ArtifactsDetailsReader.NextRecord(item) == nil; item = new(utils.ArtifactDetails) { 278 artifacts = append(artifacts, *item) 279 } 280 assert.Len(t, artifacts, 1) 281 assert.Equal(t, getRtTargetRepo()+"c.tar.gz", artifacts[0].ArtifactoryPath) 282 } 283 284 // Test downloading of two different files to the same path in the local machine. Only the first of them will be downloaded. 285 func duplicateDownload(t *testing.T) { 286 workingDir, err := os.MkdirTemp("", "downloadTests") 287 if err != nil { 288 t.Error(err) 289 } 290 defer tests.RemoveAllAndAssert(t, workingDir) 291 downloadPattern := getRtTargetRepo() + "*.in" 292 downloadTarget := workingDir + string(filepath.Separator) 293 summary, err := testsDownloadService.DownloadFiles(services.DownloadParams{CommonParams: &utils.CommonParams{Pattern: downloadPattern, Recursive: true, Target: downloadTarget}, Flat: true}) 294 if err != nil { 295 t.Error(err) 296 } 297 if summary.TotalSucceeded != 2 { 298 t.Error("Expected to download 2 files.") 299 } 300 if summary.TotalFailed != 0 { 301 t.Error("Failed to download", summary.TotalFailed, "files.") 302 } 303 downloadTarget2 := workingDir + string(filepath.Separator) + "file" 304 summary2, err := testsDownloadService.DownloadFiles(services.DownloadParams{CommonParams: &utils.CommonParams{Pattern: downloadPattern, Recursive: true, Target: downloadTarget2}, Flat: true}) 305 if err != nil { 306 t.Error(err) 307 } 308 // Two files match the pattern, but both are planned to be downloaded to the same path, so only one of them is downloaded 309 if summary2.TotalSucceeded != 1 { 310 t.Error("Expected to download 1 files.") 311 } 312 if summary2.TotalFailed != 0 { 313 t.Error("Failed to download", summary2.TotalFailed, "files.") 314 } 315 }