packer-cn/builder/virtualbox/ovf/config_test.go
Adrien Delorme 0fa60c68fb
Drop the iso_checksum_type & iso_checksum_url fields (#8437)
* Drop the iso_checksum_type & iso_checksum_url fields

In favor of simply using iso_checksum that will know what to do.

* fix after master merge

* Update builder_test.go

* Update builder_test.go

* Update builder_test.go

* Update builder_test.go

* Update builder_test.go

* remove checksum lowercasing tests

* Update builder_test.go

* Update builder_test.go

* better docs

* Update builder_test.go

* even better docs

* Update config.go

* Update builder_test.go

* Update step_create_vmx_test.go

* make generate

* better docs

* fix imports

* up tests

* Update _ISOConfig-required.html.md

* Update builder_test.go

* don't use sha1.Sum("none") as a caching path

* Update builder_test.go

* better docs

* Update iso_config_test.go

remove ISOChecksumType/ISOChecksumURL references

* Update step_download_test.go

* add iso_checksum_url and iso_checksum_type fixers + tests

* add concrete examples of checksum values

* add examples of checksumming from local file

* update go-getter dep

* up deps

* use new go-getter version

* up ESX5Driver.VerifyChecksum: use go-getter's checksumming

* ISOConfig.Prepare: get checksum there in case we need it as a string in ESX5Driver.VerifyChecksum

* Update iso_config.go

* get go-getter from v2 branch

* Update driver_esx5.go

add more comments

* Update driver_esx5.go

* show better error message when the checksum is invalid

* Update builder_test.go

put in a valid checksum to fix tests, checksum is md5("packer")

* Update builder_test.go

test invalid and valid checksum

* more test updating

* fix default md5 string to be a valid md5

* TestChecksumFileNameMixedCaseBug: use 'file:' prefix for file checksumming

* Update iso_config_test.go

* Update iso_config_test.go

* Update builder_test.go

* Update builder_test.go

* Update builder_test.go

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update go.mod

* Update go.mod

* Update CHANGELOG.md
2020-05-28 11:02:09 +02:00

149 lines
3.3 KiB
Go

package ovf
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/hashicorp/packer/packer"
)
func testConfig(t *testing.T) map[string]interface{} {
return map[string]interface{}{
"ssh_username": "foo",
"shutdown_command": "foo",
"source_path": "config_test.go",
}
}
func getTempFile(t *testing.T) *os.File {
tf, err := ioutil.TempFile("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
tf.Close()
// don't forget to cleanup the file downstream:
// defer os.Remove(tf.Name())
return tf
}
func TestNewConfig_FloppyFiles(t *testing.T) {
cfg := testConfig(t)
floppies_path := "../../../common/test-fixtures/floppies"
cfg["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)}
var c Config
_, err := c.Prepare(cfg)
if err != nil {
t.Fatalf("should not have error: %s", err)
}
}
func TestNewConfig_InvalidFloppies(t *testing.T) {
cfg := testConfig(t)
cfg["floppy_files"] = []string{"nonexistent.bat", "nonexistent.ps1"}
var c Config
_, errs := c.Prepare(cfg)
if errs == nil {
t.Fatalf("Nonexistent floppies should trigger multierror")
}
if len(errs.(*packer.MultiError).Errors) != 2 {
t.Fatalf("Multierror should work and report 2 errors")
}
}
func TestNewConfig_sourcePath(t *testing.T) {
// Okay, because it gets caught during download
cfg := testConfig(t)
delete(cfg, "source_path")
var c Config
warns, err := c.Prepare(cfg)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err == nil {
t.Fatalf("should error with empty `source_path`")
}
// Good
tf := getTempFile(t)
defer os.Remove(tf.Name())
cfg = testConfig(t)
cfg["source_path"] = tf.Name()
warns, err = c.Prepare(cfg)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("bad: %s", err)
}
}
func TestNewConfig_shutdown_timeout(t *testing.T) {
cfg := testConfig(t)
tf := getTempFile(t)
defer os.Remove(tf.Name())
// Expect this to fail
cfg["source_path"] = tf.Name()
cfg["shutdown_timeout"] = "NaN"
var c Config
warns, err := c.Prepare(cfg)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err == nil {
t.Fatal("should error")
}
// Passes when given a valid time duration
cfg["shutdown_timeout"] = "10s"
warns, err = c.Prepare(cfg)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("bad: %s", err)
}
}
// TestChecksumFileNameMixedCaseBug reproduces Github issue #9049:
// https://github.com/hashicorp/packer/issues/9049
func TestChecksumFileNameMixedCaseBug(t *testing.T) {
tt := []struct {
Name string
ChecksumPath string
}{
{"Lowercase", "file:/tmp/random/file.md5"},
{"MiXeDcAsE", "file:/tmp/RaNdOm/FiLe.Md5"},
}
for _, tc := range tt {
cfg := testConfig(t)
cfg["source_path"] = "bug.ovf"
cfg["checksum"] = tc.ChecksumPath
cfg["type"] = "virtualbox-ovf"
cfg["guest_additions_mode"] = "disable"
cfg["headless"] = false
var c Config
warns, err := c.Prepare(cfg)
if err != nil {
t.Errorf("config failed to Prepare, %s", err.Error())
}
if len(warns) != 0 {
t.Errorf("Encountered warnings during config preparation: %s", warns)
}
if c.Checksum != tc.ChecksumPath {
t.Errorf("%s test failed, Checksum and ChecksumPath are expected to be equal, expected: %s, got: %s", tc.Name, tc.ChecksumPath, c.Checksum)
}
}
}