2020-11-11 18:04:28 -05:00
|
|
|
package commonsteps
|
2013-08-15 14:05:36 -04:00
|
|
|
|
|
|
|
import (
|
2020-02-04 12:06:47 -05:00
|
|
|
"bytes"
|
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"
|
2020-06-04 09:00:18 -04:00
|
|
|
"runtime"
|
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"
|
2020-02-06 11:07:58 -05:00
|
|
|
urlhelper "github.com/hashicorp/go-getter/v2/helper/url"
|
2020-02-06 12:17:57 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-16 18:13:08 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/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 {
|
2020-05-28 05:02:09 -04:00
|
|
|
Checksum string
|
|
|
|
Description string
|
|
|
|
ResultKey string
|
|
|
|
TargetPath string
|
|
|
|
Url []string
|
|
|
|
Extension string
|
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
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
want multistep.StepAction
|
|
|
|
wantFiles []string
|
|
|
|
}{
|
2020-01-07 13:45:24 -05:00
|
|
|
{"Empty URL field passes",
|
|
|
|
fields{Url: []string{}},
|
|
|
|
multistep.ActionContinue,
|
|
|
|
nil,
|
|
|
|
},
|
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")) + ".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")) + ".lock",
|
|
|
|
},
|
|
|
|
},
|
2020-05-28 05:02:09 -04:00
|
|
|
{"none checksum works",
|
|
|
|
fields{Url: []string{abs(t, "./test-fixtures/root/another.txt")}, Checksum: "none"},
|
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
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
|
|
|
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",
|
2020-05-28 05:02:09 -04:00
|
|
|
fields{Extension: "txt", Url: []string{abs(t, "./test-fixtures/root/another.txt")}, Checksum: "sha1:" + cs["/root/basic.txt"]},
|
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
|
|
|
multistep.ActionHalt,
|
|
|
|
[]string{
|
2020-05-28 05:02:09 -04:00
|
|
|
toSha1("sha1:"+cs["/root/basic.txt"]) + ".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
|
|
|
},
|
|
|
|
},
|
|
|
|
{"bad checksum removes file - checksum from url - Checksum Type",
|
2020-05-28 05:02:09 -04:00
|
|
|
fields{Extension: "txt", Url: []string{abs(t, "./test-fixtures/root/basic.txt")}, Checksum: "file:" + srvr.URL + "/root/another.txt.sha1sum"},
|
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
|
|
|
multistep.ActionHalt,
|
|
|
|
[]string{
|
2020-05-28 05:02:09 -04:00
|
|
|
toSha1("file:"+srvr.URL+"/root/another.txt.sha1sum") + ".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
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull http dl - checksum from http file - parameter",
|
2020-05-28 05:02:09 -04:00
|
|
|
fields{Extension: "txt", Url: []string{srvr.URL + "/root/another.txt"}, Checksum: "file:" + srvr.URL + "/root/another.txt.sha1sum"},
|
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
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
2020-05-28 05:02:09 -04:00
|
|
|
toSha1("file:"+srvr.URL+"/root/another.txt.sha1sum") + ".txt",
|
|
|
|
toSha1("file:"+srvr.URL+"/root/another.txt.sha1sum") + ".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
|
|
|
},
|
|
|
|
},
|
|
|
|
{"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",
|
2020-05-28 05:02:09 -04:00
|
|
|
fields{Extension: "txt", Url: []string{srvr.URL + "/root/another.txt?"}, Checksum: "sha1:" + cs["/root/another.txt"]},
|
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
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
2020-05-28 05:02:09 -04:00
|
|
|
toSha1("sha1:"+cs["/root/another.txt"]) + ".txt",
|
|
|
|
toSha1("sha1:"+cs["/root/another.txt"]) + ".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
|
|
|
},
|
|
|
|
},
|
|
|
|
{"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.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.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull relative symlink - checksum from parameter - checksum type",
|
2020-05-28 05:02:09 -04:00
|
|
|
fields{Extension: "txt", Url: []string{"./test-fixtures/root/another.txt?"}, Checksum: "sha1:" + cs["/root/another.txt"]},
|
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
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
2020-05-28 05:02:09 -04:00
|
|
|
toSha1("sha1:"+cs["/root/another.txt"]) + ".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
|
|
|
},
|
|
|
|
},
|
|
|
|
{"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.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.lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"successfull absolute symlink - checksum from parameter - checksum type",
|
2020-05-28 05:02:09 -04:00
|
|
|
fields{Extension: "txt", Url: []string{abs(t, "./test-fixtures/root/another.txt") + "?"}, Checksum: "sha1:" + cs["/root/another.txt"]},
|
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
|
|
|
multistep.ActionContinue,
|
|
|
|
[]string{
|
2020-05-28 05:02:09 -04:00
|
|
|
toSha1("sha1:"+cs["/root/another.txt"]) + ".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
|
|
|
},
|
|
|
|
},
|
|
|
|
{"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"]) + ".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{
|
2020-05-28 05:02:09 -04:00
|
|
|
TargetPath: tt.fields.TargetPath,
|
|
|
|
Checksum: tt.fields.Checksum,
|
|
|
|
ResultKey: tt.fields.ResultKey,
|
|
|
|
Url: tt.fields.Url,
|
|
|
|
Extension: tt.fields.Extension,
|
|
|
|
Description: tt.name,
|
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
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 12:06:47 -05:00
|
|
|
func TestStepDownload_download(t *testing.T) {
|
|
|
|
step := &StepDownload{
|
2020-05-28 05:02:09 -04:00
|
|
|
Checksum: "sha1:f572d396fae9206628714fb2ce00f72e94f2258f",
|
|
|
|
Description: "ISO",
|
|
|
|
ResultKey: "iso_path",
|
|
|
|
Url: nil,
|
2020-02-04 12:06:47 -05:00
|
|
|
}
|
|
|
|
ui := &packer.BasicUi{
|
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
2020-09-21 19:23:00 -04:00
|
|
|
PB: &packer.NoopProgressTracker{},
|
2020-02-04 12:06:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2020-02-06 12:24:13 -05:00
|
|
|
_, err := step.download(context.TODO(), ui, "./test-fixtures/root/basic.txt")
|
2020-02-04 12:06:47 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Bad: non expected error %s", err.Error())
|
|
|
|
}
|
|
|
|
os.RemoveAll(step.TargetPath)
|
|
|
|
|
|
|
|
// Abs path with no extension provided
|
|
|
|
step.TargetPath = "./packer"
|
|
|
|
step.Extension = ""
|
2020-02-06 12:24:13 -05:00
|
|
|
_, err = step.download(context.TODO(), ui, "./test-fixtures/root/basic.txt")
|
2020-02-04 12:06:47 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Bad: non expected error %s", err.Error())
|
|
|
|
}
|
|
|
|
os.RemoveAll(step.TargetPath)
|
|
|
|
|
|
|
|
// Path with file
|
|
|
|
step.TargetPath = "./packer/file.iso"
|
2020-02-06 12:17:57 -05:00
|
|
|
_, err = step.download(context.TODO(), ui, "./test-fixtures/root/basic.txt")
|
2020-02-04 12:06:47 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Bad: non expected error %s", err.Error())
|
|
|
|
}
|
|
|
|
os.RemoveAll(step.TargetPath)
|
|
|
|
}
|
|
|
|
|
2020-06-04 09:00:18 -04:00
|
|
|
func TestStepDownload_WindowsParseSourceURL(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.Skip("skip windows specific tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
source := "\\\\hostname\\dir\\filename.txt"
|
|
|
|
url, err := parseSourceURL(source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: parsing source url failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
if url.Scheme != "smb" {
|
|
|
|
t.Fatalf("bad: url should contain smb scheme but contains: %s", url.Scheme)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepDownload_ParseSourceSmbURL(t *testing.T) {
|
|
|
|
source := "smb://hostname/dir/filename.txt"
|
|
|
|
url, err := parseSourceURL(source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: parsing source url failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
if url.Scheme != "smb" {
|
|
|
|
t.Fatalf("bad: url should contain smb scheme but contains: %s", url.Scheme)
|
|
|
|
}
|
|
|
|
if url.String() != "smb://hostname/dir/filename.txt" {
|
|
|
|
t.Fatalf("bad: url should contain smb scheme but contains: %s", url.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
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
|
|
|
}
|