2013-08-15 14:05:36 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
"context"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2013-08-15 14:05:36 -04:00
|
|
|
"testing"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
urlhelper "github.com/hashicorp/go-getter/helper/url"
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
"github.com/hashicorp/packer/packer/tmp"
|
2013-08-15 14:05:36 -04:00
|
|
|
)
|
|
|
|
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
var _ multistep.Step = new(StepDownload)
|
|
|
|
|
|
|
|
func toSha1(in string) string {
|
|
|
|
b := sha1.Sum([]byte(in))
|
|
|
|
return hex.EncodeToString(b[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func abs(t *testing.T, path string) string {
|
|
|
|
path, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
u, err := urlhelper.Parse(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepDownload_Run(t *testing.T) {
|
|
|
|
srvr := httptest.NewServer(http.FileServer(http.Dir("test-fixtures")))
|
|
|
|
defer srvr.Close()
|
|
|
|
|
|
|
|
cs := map[string]string{
|
|
|
|
"/root/basic.txt": "f572d396fae9206628714fb2ce00f72e94f2258f",
|
|
|
|
"/root/another.txt": "7c6e5dd1bacb3b48fdffba2ed096097eb172497d",
|
|
|
|
}
|
|
|
|
|
|
|
|
type fields struct {
|
|
|
|
Checksum string
|
|
|
|
ChecksumType string
|
|
|
|
Description string
|
|
|
|
ResultKey string
|
|
|
|
TargetPath string
|
|
|
|
Url []string
|
|
|
|
Extension string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
want multistep.StepAction
|
|
|
|
wantFiles []string
|
|
|
|
}{
|
2019-04-16 11:53:32 -04:00
|
|
|
{"not passing a checksum passes",
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
fields{Url: []string{abs(t, "./test-fixtures/root/another.txt")}},
|
2019-04-16 11:53:32 -04:00
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(abs(t, "./test-fixtures/root/another.txt")),
|
|
|
|
toSha1(abs(t, "./test-fixtures/root/another.txt")) + ".lock",
|
|
|
|
},
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
},
|
2019-07-22 12:17:48 -04:00
|
|
|
{"double slashes on a local filesystem passes",
|
|
|
|
fields{Url: []string{abs(t, "./test-fixtures/root//another.txt")}},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(abs(t, "./test-fixtures/root//another.txt")),
|
|
|
|
toSha1(abs(t, "./test-fixtures/root//another.txt")) + ".lock",
|
|
|
|
},
|
|
|
|
},
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
{"none checksum works, without a checksum",
|
|
|
|
fields{Url: []string{abs(t, "./test-fixtures/root/another.txt")}, ChecksumType: "none"},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(abs(t, "./test-fixtures/root/another.txt")),
|
|
|
|
toSha1(abs(t, "./test-fixtures/root/another.txt")) + ".lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"bad checksum removes file - checksum from string - no Checksum Type",
|
|
|
|
fields{Extension: "txt", Url: []string{abs(t, "./test-fixtures/root/another.txt")}, Checksum: cs["/root/basic.txt"]},
|
|
|
|
multistep.ActionHalt,
|
|
|
|
[]string{
|
|
|
|
toSha1(cs["/root/basic.txt"]) + ".txt.lock", // a lock file is created & deleted on mac for each download
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"bad checksum removes file - checksum from string - Checksum Type",
|
|
|
|
fields{Extension: "txt", Url: []string{abs(t, "./test-fixtures/root/another.txt")}, ChecksumType: "sha1", Checksum: cs["/root/basic.txt"]},
|
|
|
|
multistep.ActionHalt,
|
|
|
|
[]string{
|
|
|
|
toSha1(cs["/root/basic.txt"]) + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"bad checksum removes file - checksum from url - Checksum Type",
|
|
|
|
fields{Extension: "txt", Url: []string{abs(t, "./test-fixtures/root/basic.txt")}, Checksum: srvr.URL + "/root/another.txt.sha1sum", ChecksumType: "file"},
|
|
|
|
multistep.ActionHalt,
|
|
|
|
[]string{
|
|
|
|
toSha1(srvr.URL+"/root/another.txt.sha1sum") + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull http dl - checksum from http file - parameter",
|
|
|
|
fields{Extension: "txt", Url: []string{srvr.URL + "/root/another.txt"}, Checksum: srvr.URL + "/root/another.txt.sha1sum", ChecksumType: "file"},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(srvr.URL+"/root/another.txt.sha1sum") + ".txt",
|
|
|
|
toSha1(srvr.URL+"/root/another.txt.sha1sum") + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull http dl - checksum from http file - url",
|
|
|
|
fields{Extension: "txt", Url: []string{srvr.URL + "/root/another.txt?checksum=file:" + srvr.URL + "/root/another.txt.sha1sum"}},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1("file:"+srvr.URL+"/root/another.txt.sha1sum") + ".txt",
|
|
|
|
toSha1("file:"+srvr.URL+"/root/another.txt.sha1sum") + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull http dl - checksum from url",
|
|
|
|
fields{Extension: "txt", Url: []string{srvr.URL + "/root/another.txt?checksum=" + cs["/root/another.txt"]}},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt",
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull http dl - checksum from parameter - no checksum type",
|
|
|
|
fields{Extension: "txt", Url: []string{srvr.URL + "/root/another.txt?"}, Checksum: cs["/root/another.txt"]},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt",
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull http dl - checksum from parameter - checksum type",
|
|
|
|
fields{Extension: "txt", Url: []string{srvr.URL + "/root/another.txt?"}, ChecksumType: "sha1", Checksum: cs["/root/another.txt"]},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt",
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull relative symlink - checksum from url",
|
|
|
|
fields{Extension: "txt", Url: []string{"./test-fixtures/root/another.txt?checksum=" + cs["/root/another.txt"]}},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt",
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull relative symlink - checksum from parameter - no checksum type",
|
|
|
|
fields{Extension: "txt", Url: []string{"./test-fixtures/root/another.txt?"}, Checksum: cs["/root/another.txt"]},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt",
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull relative symlink - checksum from parameter - checksum type",
|
|
|
|
fields{Extension: "txt", Url: []string{"./test-fixtures/root/another.txt?"}, ChecksumType: "sha1", Checksum: cs["/root/another.txt"]},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt",
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull absolute symlink - checksum from url",
|
|
|
|
fields{Extension: "txt", Url: []string{abs(t, "./test-fixtures/root/another.txt") + "?checksum=" + cs["/root/another.txt"]}},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt",
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull absolute symlink - checksum from parameter - no checksum type",
|
|
|
|
fields{Extension: "txt", Url: []string{abs(t, "./test-fixtures/root/another.txt") + "?"}, Checksum: cs["/root/another.txt"]},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt",
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull absolute symlink - checksum from parameter - checksum type",
|
|
|
|
fields{Extension: "txt", Url: []string{abs(t, "./test-fixtures/root/another.txt") + "?"}, ChecksumType: "sha1", Checksum: cs["/root/another.txt"]},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt",
|
|
|
|
toSha1(cs["/root/another.txt"]) + ".txt.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"wrong first 2 urls - absolute urls - checksum from parameter - no checksum type",
|
|
|
|
fields{
|
|
|
|
Url: []string{
|
|
|
|
abs(t, "./test-fixtures/root/another.txt"),
|
|
|
|
abs(t, "./test-fixtures/root/not_found"),
|
|
|
|
abs(t, "./test-fixtures/root/basic.txt"),
|
|
|
|
},
|
|
|
|
Checksum: cs["/root/basic.txt"],
|
|
|
|
},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
toSha1(cs["/root/basic.txt"]),
|
|
|
|
toSha1(cs["/root/basic.txt"]) + ".lock",
|
|
|
|
},
|
|
|
|
},
|
2013-08-15 14:05:36 -04:00
|
|
|
}
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
dir := createTempDir(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
s := &StepDownload{
|
|
|
|
TargetPath: tt.fields.TargetPath,
|
|
|
|
Checksum: tt.fields.Checksum,
|
|
|
|
ChecksumType: tt.fields.ChecksumType,
|
|
|
|
ResultKey: tt.fields.ResultKey,
|
|
|
|
Url: tt.fields.Url,
|
|
|
|
Extension: tt.fields.Extension,
|
|
|
|
Description: tt.name,
|
|
|
|
}
|
|
|
|
defer os.Setenv("PACKER_CACHE_DIR", os.Getenv("PACKER_CACHE_DIR"))
|
|
|
|
os.Setenv("PACKER_CACHE_DIR", dir)
|
|
|
|
|
|
|
|
if got := s.Run(context.Background(), testState(t)); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Fatalf("StepDownload.Run() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
files := listFiles(t, dir)
|
|
|
|
if diff := cmp.Diff(tt.wantFiles, files); diff != "" {
|
|
|
|
t.Fatalf("file list differs in %s: %s", dir, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTempDir(t *testing.T) string {
|
|
|
|
dir, err := tmp.Dir("pkr")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func listFiles(t *testing.T, dir string) []string {
|
|
|
|
fs, err := ioutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var files []string
|
|
|
|
for _, file := range fs {
|
|
|
|
if file.Name() == "." {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
files = append(files, file.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
return files
|
2013-08-15 14:05:36 -04:00
|
|
|
}
|