2018-10-16 12:41:27 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2015-10-20 19:27:47 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2020-05-11 05:14:50 -04:00
|
|
|
"io"
|
2016-10-14 03:56:05 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-05-11 05:14:50 -04:00
|
|
|
"os"
|
2020-03-18 06:32:51 -04:00
|
|
|
"path/filepath"
|
2015-10-20 19:27:47 -04:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testISOConfig() ISOConfig {
|
|
|
|
return ISOConfig{
|
|
|
|
ISOChecksum: "foo",
|
2016-02-08 09:51:43 -05:00
|
|
|
ISOChecksumURL: "",
|
2015-10-20 19:27:47 -04:00
|
|
|
ISOChecksumType: "md5",
|
2016-02-08 09:51:43 -05:00
|
|
|
RawSingleISOUrl: "http://www.packer.io/the-OS.iso",
|
2015-10-20 19:27:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 09:51:43 -05:00
|
|
|
var cs_bsd_style = `
|
|
|
|
MD5 (other.iso) = bAr
|
|
|
|
MD5 (the-OS.iso) = baZ
|
|
|
|
`
|
|
|
|
|
2017-11-05 18:32:39 -05:00
|
|
|
var cs_bsd_style_subdir = `
|
|
|
|
MD5 (other.iso) = bAr
|
|
|
|
MD5 (./subdir/the-OS.iso) = baZ
|
|
|
|
`
|
|
|
|
|
2016-02-08 09:51:43 -05:00
|
|
|
var cs_gnu_style = `
|
|
|
|
bAr0 *the-OS.iso
|
|
|
|
baZ0 other.iso
|
|
|
|
`
|
|
|
|
|
2017-11-05 18:32:39 -05:00
|
|
|
var cs_gnu_style_subdir = `
|
|
|
|
bAr0 *./subdir/the-OS.iso
|
|
|
|
baZ0 other.iso
|
|
|
|
`
|
|
|
|
|
2016-03-31 03:05:00 -04:00
|
|
|
var cs_bsd_style_no_newline = `
|
|
|
|
MD5 (other.iso) = bAr
|
|
|
|
MD5 (the-OS.iso) = baZ`
|
|
|
|
|
|
|
|
var cs_gnu_style_no_newline = `
|
|
|
|
bAr0 *the-OS.iso
|
|
|
|
baZ0 other.iso`
|
|
|
|
|
2015-10-20 19:27:47 -04:00
|
|
|
func TestISOConfigPrepare_ISOChecksum(t *testing.T) {
|
|
|
|
i := testISOConfig()
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
warns, err := i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksum = "FOo"
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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 TestISOConfigPrepare_ISOChecksumURLBad(t *testing.T) {
|
2016-02-08 09:51:43 -05:00
|
|
|
i := testISOConfig()
|
|
|
|
i.ISOChecksumURL = "file:///not_read"
|
2019-06-28 18:29:39 -04:00
|
|
|
i.ISOChecksum = "shouldoverride"
|
2016-02-08 09:51:43 -05:00
|
|
|
|
|
|
|
// Test ISOChecksum overrides url
|
|
|
|
warns, err := i.Prepare(nil)
|
2019-06-28 18:29:39 -04:00
|
|
|
if len(warns) != 1 {
|
|
|
|
t.Fatalf("Bad: should have warned because both checksum and " +
|
|
|
|
"checksumURL are set.")
|
|
|
|
}
|
|
|
|
if len(err) > 0 {
|
|
|
|
t.Fatalf("Bad; should have warned but not errored.")
|
2016-02-08 09:51:43 -05:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:21:55 -05:00
|
|
|
// Test that we won't try to read an iso into memory because of a user
|
|
|
|
// error
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksumURL = "file:///not_read.iso"
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should have error because iso is bad filetype: %s", err)
|
|
|
|
}
|
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
|
|
|
|
2016-02-08 09:51:43 -05:00
|
|
|
}
|
|
|
|
|
2015-10-20 19:27:47 -04:00
|
|
|
func TestISOConfigPrepare_ISOChecksumType(t *testing.T) {
|
|
|
|
i := testISOConfig()
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
i.ISOChecksumType = ""
|
|
|
|
warns, err := i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
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
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
2015-10-20 19:27:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksumType = "mD5"
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ISOChecksumType != "md5" {
|
|
|
|
t.Fatalf("should've lowercased: %s", i.ISOChecksumType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test none
|
|
|
|
i = testISOConfig()
|
|
|
|
i.ISOChecksumType = "none"
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) == 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.ISOChecksumType != "none" {
|
|
|
|
t.Fatalf("should've lowercased: %s", i.ISOChecksumType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestISOConfigPrepare_ISOUrl(t *testing.T) {
|
|
|
|
i := testISOConfig()
|
|
|
|
|
|
|
|
// Test both empty
|
|
|
|
i.RawSingleISOUrl = ""
|
|
|
|
i.ISOUrls = []string{}
|
|
|
|
warns, err := i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
2016-10-14 03:56:05 -04:00
|
|
|
// Test iso_url not set but checksum url is
|
2020-03-18 06:32:51 -04:00
|
|
|
ts := httpTestModule("root")
|
2016-10-14 03:56:05 -04:00
|
|
|
defer ts.Close()
|
|
|
|
i = testISOConfig()
|
|
|
|
i.RawSingleISOUrl = ""
|
|
|
|
i.ISOChecksum = ""
|
|
|
|
i.ISOChecksumURL = ts.URL + "/basic.txt"
|
2019-09-20 08:06:25 -04:00
|
|
|
// ISOConfig.Prepare() returns a slice of errors
|
|
|
|
var errs []error
|
|
|
|
warns, errs = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("expected no warnings, got:%v", warns)
|
|
|
|
}
|
|
|
|
if len(errs) < 1 || err[0] == nil {
|
|
|
|
t.Fatalf("expected a populated error slice, got: %v", errs)
|
|
|
|
}
|
2016-10-14 03:56:05 -04:00
|
|
|
|
2015-10-20 19:27:47 -04:00
|
|
|
// Test iso_url set
|
|
|
|
i = testISOConfig()
|
2016-02-08 09:51:43 -05:00
|
|
|
i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso"
|
2015-10-20 19:27:47 -04:00
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-02-08 09:51:43 -05:00
|
|
|
expected := []string{"http://www.packer.io/the-OS.iso"}
|
2015-10-20 19:27:47 -04:00
|
|
|
if !reflect.DeepEqual(i.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", i.ISOUrls)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test both set
|
|
|
|
i = testISOConfig()
|
2016-02-08 09:51:43 -05:00
|
|
|
i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso"
|
|
|
|
i.ISOUrls = []string{"http://www.packer.io/the-OS.iso"}
|
2015-10-20 19:27:47 -04:00
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test just iso_urls set
|
|
|
|
i = testISOConfig()
|
|
|
|
i.RawSingleISOUrl = ""
|
|
|
|
i.ISOUrls = []string{
|
2016-02-08 09:51:43 -05:00
|
|
|
"http://www.packer.io/the-OS.iso",
|
|
|
|
"http://www.hashicorp.com/the-OS.iso",
|
2015-10-20 19:27:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = []string{
|
2016-02-08 09:51:43 -05:00
|
|
|
"http://www.packer.io/the-OS.iso",
|
|
|
|
"http://www.hashicorp.com/the-OS.iso",
|
2015-10-20 19:27:47 -04:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(i.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", i.ISOUrls)
|
|
|
|
}
|
|
|
|
}
|
2016-12-17 05:49:54 -05:00
|
|
|
|
|
|
|
func TestISOConfigPrepare_TargetExtension(t *testing.T) {
|
|
|
|
i := testISOConfig()
|
|
|
|
|
|
|
|
// Test the default value
|
|
|
|
warns, err := i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.TargetExtension != "iso" {
|
|
|
|
t.Fatalf("should've found \"iso\" got: %s", i.TargetExtension)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the lowercased value
|
|
|
|
i = testISOConfig()
|
|
|
|
i.TargetExtension = "DMG"
|
|
|
|
warns, err = i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.TargetExtension != "dmg" {
|
|
|
|
t.Fatalf("should've lowercased: %s", i.TargetExtension)
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 06:32:51 -04:00
|
|
|
|
|
|
|
func TestISOConfigPrepare_ISOChecksumURLMyTest(t *testing.T) {
|
|
|
|
httpChecksums := httpTestModule("root")
|
|
|
|
defer httpChecksums.Close()
|
|
|
|
i := ISOConfig{
|
|
|
|
ISOChecksumURL: httpChecksums.URL + "/subfolder.sum",
|
|
|
|
ISOChecksumType: "sha256",
|
|
|
|
ISOUrls: []string{"http://hashicorp.com/ubuntu/dists/bionic-updates/main/installer-amd64/current/images/netboot/mini.iso"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test ISOChecksum overrides url
|
|
|
|
warns, err := i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("Bad: should not have warnings")
|
|
|
|
}
|
|
|
|
if len(err) > 0 {
|
|
|
|
t.Fatalf("Bad; should not have errored.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 05:14:50 -04:00
|
|
|
func TestISOConfigPrepare_ISOChecksumLocalFile(t *testing.T) {
|
|
|
|
// Creates checksum file in local dir
|
|
|
|
p := filepath.Join(fixtureDir, "root/subfolder.sum")
|
|
|
|
source, err := os.Open(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
destination, err := os.Create("local.sum")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
defer os.Remove("local.sum")
|
|
|
|
defer destination.Close()
|
|
|
|
if _, err := io.Copy(destination, source); err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
i := ISOConfig{
|
|
|
|
ISOChecksumURL: "./local.sum",
|
|
|
|
ISOChecksumType: "sha256",
|
|
|
|
ISOUrls: []string{"http://hashicorp.com/ubuntu/dists/bionic-updates/main/installer-amd64/current/images/netboot/mini.iso"},
|
|
|
|
}
|
|
|
|
|
|
|
|
warns, errs := i.Prepare(nil)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("Bad: should not have warnings")
|
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("Bad; should not have errored. %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 06:32:51 -04:00
|
|
|
const fixtureDir = "./test-fixtures"
|
|
|
|
|
|
|
|
func httpTestModule(n string) *httptest.Server {
|
|
|
|
p := filepath.Join(fixtureDir, n)
|
|
|
|
p, err := filepath.Abs(p)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return httptest.NewServer(http.FileServer(http.Dir(p)))
|
|
|
|
}
|