Add iso file name when targetPath is absolute

This commit is contained in:
Moss 2020-02-04 18:06:47 +01:00
parent 08b0bd1d2c
commit e020da49f3
2 changed files with 81 additions and 10 deletions

View File

@ -141,8 +141,6 @@ func (s *StepDownload) download(ctx context.Context, ui packer.Ui, source string
u.RawQuery = q.Encode() u.RawQuery = q.Encode()
} }
targetPath := s.TargetPath
if targetPath == "" {
// store file under sha1(hash) if set // store file under sha1(hash) if set
// hash can sometimes be a checksum url // hash can sometimes be a checksum url
// otherwise, use sha1(source_url) // otherwise, use sha1(source_url)
@ -152,7 +150,11 @@ func (s *StepDownload) download(ctx context.Context, ui packer.Ui, source string
} else { } else {
shaSum = sha1.Sum([]byte(u.String())) shaSum = sha1.Sum([]byte(u.String()))
} }
targetPath = hex.EncodeToString(shaSum[:]) shaSumString := hex.EncodeToString(shaSum[:])
targetPath := s.TargetPath
if targetPath == "" {
targetPath = shaSumString
if s.Extension != "" { if s.Extension != "" {
targetPath += "." + s.Extension targetPath += "." + s.Extension
} }
@ -160,6 +162,18 @@ func (s *StepDownload) download(ctx context.Context, ui packer.Ui, source string
if err != nil { if err != nil {
return "", fmt.Errorf("CachePath: %s", err) return "", fmt.Errorf("CachePath: %s", err)
} }
} else if filepath.Ext(targetPath) == "" {
// When an absolute path is provided
// this adds the file to the targetPath
if !strings.HasSuffix(targetPath, "/") {
targetPath += "/"
}
targetPath += shaSumString
if s.Extension != "" {
targetPath += "." + s.Extension
} else {
targetPath += ".iso"
}
} }
lockFile := targetPath + ".lock" lockFile := targetPath + ".lock"

View File

@ -1,9 +1,11 @@
package common package common
import ( import (
"bytes"
"context" "context"
"crypto/sha1" "crypto/sha1"
"encoding/hex" "encoding/hex"
"github.com/hashicorp/packer/packer"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -244,6 +246,61 @@ func TestStepDownload_Run(t *testing.T) {
} }
} }
func TestStepDownload_download(t *testing.T) {
step := &StepDownload{
Checksum: "f572d396fae9206628714fb2ce00f72e94f2258f",
ChecksumType: "sha1",
Description: "ISO",
ResultKey: "iso_path",
Url: nil,
}
ui := &packer.BasicUi{
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),
}
dir := createTempDir(t)
defer os.RemoveAll(dir)
defer os.Setenv("PACKER_CACHE_DIR", os.Getenv("PACKER_CACHE_DIR"))
os.Setenv("PACKER_CACHE_DIR", dir)
// Abs path with extension provided
step.TargetPath = "./packer"
step.Extension = "ova"
path, err := step.download(context.TODO(), ui, "./test-fixtures/root/basic.txt")
if err != nil {
t.Fatalf("Bad: non expected error %s", err.Error())
}
if filepath.Ext(path) != "." + step.Extension {
t.Fatalf("bad: path should contain extension %s but it was %s", step.Extension, filepath.Ext(path))
}
os.RemoveAll(step.TargetPath)
// Abs path with no extension provided
step.TargetPath = "./packer"
step.Extension = ""
path, err = step.download(context.TODO(), ui, "./test-fixtures/root/basic.txt")
if err != nil {
t.Fatalf("Bad: non expected error %s", err.Error())
}
if filepath.Ext(path) != ".iso" {
t.Fatalf("bad: path should contain extension %s but it was .iso", step.Extension)
}
os.RemoveAll(step.TargetPath)
// Path with file
step.TargetPath = "./packer/file.iso"
path, err = step.download(context.TODO(), ui, "./test-fixtures/root/basic.txt")
if err != nil {
t.Fatalf("Bad: non expected error %s", err.Error())
}
if path != "./packer/file.iso" {
t.Fatalf("bad: path should be ./packer/file.iso but it was %s", path)
}
os.RemoveAll(step.TargetPath)
}
func createTempDir(t *testing.T) string { func createTempDir(t *testing.T) string {
dir, err := tmp.Dir("pkr") dir, err := tmp.Dir("pkr")
if err != nil { if err != nil {