Use longest suffix match to pick up checksum

This commit is contained in:
Andrey Smirnov
2017-05-22 23:28:13 +03:00
parent c026106352
commit f0360cf2d3
2 changed files with 31 additions and 4 deletions

View File

@@ -47,15 +47,21 @@ func DownloadTryCompression(downloader aptly.Downloader, url string, expectedChe
tryURL := url + method.extenstion
foundChecksum := false
for suffix, expected := range expectedChecksums {
bestSuffix := ""
for suffix := range expectedChecksums {
if strings.HasSuffix(tryURL, suffix) {
file, err = DownloadTempWithChecksum(downloader, tryURL, &expected, ignoreMismatch, maxTries)
foundChecksum = true
break
if len(suffix) > len(bestSuffix) {
bestSuffix = suffix
}
}
}
if !foundChecksum {
if foundChecksum {
expected := expectedChecksums[bestSuffix]
file, err = DownloadTempWithChecksum(downloader, tryURL, &expected, ignoreMismatch, maxTries)
} else {
if !ignoreMismatch {
continue
}

View File

@@ -89,6 +89,27 @@ func (s *CompressionSuite) TestDownloadTryCompression(c *C) {
c.Assert(d.Empty(), Equals, true)
}
func (s *CompressionSuite) TestDownloadTryCompressionLongestSuffix(c *C) {
var buf []byte
expectedChecksums := map[string]utils.ChecksumInfo{
"file.bz2": {Size: 1},
"subdir/file.bz2": {Size: int64(len(bzipData))},
"otherdir/file.bz2": {Size: 1},
}
// longest suffix should be picked up
buf = make([]byte, 4)
d := NewFakeDownloader()
d.ExpectResponse("http://example.com/subdir/file.bz2", bzipData)
r, file, err := DownloadTryCompression(d, "http://example.com/subdir/file", expectedChecksums, false, 1)
c.Assert(err, IsNil)
defer file.Close()
io.ReadFull(r, buf)
c.Assert(string(buf), Equals, rawData)
c.Assert(d.Empty(), Equals, true)
}
func (s *CompressionSuite) TestDownloadTryCompressionErrors(c *C) {
d := NewFakeDownloader()
_, _, err := DownloadTryCompression(d, "http://example.com/file", nil, true, 1)