2017-03-12 07:31:31 -04:00
|
|
|
package vmcx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-03-12 07:31:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"iso_checksum": "foo",
|
|
|
|
"iso_checksum_type": "md5",
|
|
|
|
"iso_url": "http://www.packer.io",
|
|
|
|
"shutdown_command": "yes",
|
|
|
|
"ssh_username": "foo",
|
|
|
|
"ram_size": 64,
|
|
|
|
"guest_additions_mode": "none",
|
2017-05-21 12:29:26 -04:00
|
|
|
"clone_from_vmxc_path": "generated",
|
2017-03-12 07:31:31 -04:00
|
|
|
packer.BuildNameConfigKey: "foo",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Builder{}
|
|
|
|
if _, ok := raw.(packer.Builder); !ok {
|
|
|
|
t.Error("Builder must implement builder.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_Defaults(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
//Create vmxc folder
|
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
config["clone_from_vmxc_path"] = td
|
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.VMName != "packer-foo" {
|
|
|
|
t.Errorf("bad vm name: %s", b.config.VMName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
func TestBuilderPrepare_InvalidKey(t *testing.T) {
|
2017-03-12 07:31:31 -04:00
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
//Create vmxc folder
|
|
|
|
td, err := ioutil.TempDir("", "packer")
|
2017-03-12 07:31:31 -04:00
|
|
|
if err != nil {
|
2017-05-21 12:29:26 -04:00
|
|
|
t.Fatalf("err: %s", err)
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
2017-05-21 12:29:26 -04:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
config["clone_from_vmxc_path"] = td
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
// Add a random key
|
|
|
|
config["i_should_not_be_valid"] = true
|
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ISOChecksum(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
//Create vmxc folder
|
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
config["clone_from_vmxc_path"] = td
|
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
// Test bad
|
|
|
|
config["iso_checksum"] = ""
|
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
config["iso_checksum"] = "FOo"
|
|
|
|
b = Builder{}
|
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.ISOChecksum != "foo" {
|
|
|
|
t.Fatalf("should've lowercased: %s", b.config.ISOChecksum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ISOChecksumType(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
//Create vmxc folder
|
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
config["clone_from_vmxc_path"] = td
|
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
// Test bad
|
|
|
|
config["iso_checksum_type"] = ""
|
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
config["iso_checksum_type"] = "mD5"
|
|
|
|
b = Builder{}
|
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.ISOChecksumType != "md5" {
|
|
|
|
t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test unknown
|
|
|
|
config["iso_checksum_type"] = "fake"
|
|
|
|
b = Builder{}
|
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test none
|
|
|
|
config["iso_checksum_type"] = "none"
|
|
|
|
b = Builder{}
|
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) == 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.ISOChecksumType != "none" {
|
|
|
|
t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ISOUrl(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
2017-05-21 12:29:26 -04:00
|
|
|
|
|
|
|
//Create vmxc folder
|
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
config["clone_from_vmxc_path"] = td
|
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
delete(config, "iso_url")
|
|
|
|
delete(config, "iso_urls")
|
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
// Test both empty (should be allowed, as we cloning a vm so we probably don't need an ISO file)
|
2017-03-12 07:31:31 -04:00
|
|
|
config["iso_url"] = ""
|
|
|
|
b = Builder{}
|
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2017-05-21 12:29:26 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("should not have an error")
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test iso_url set
|
|
|
|
config["iso_url"] = "http://www.packer.io"
|
|
|
|
b = Builder{}
|
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{"http://www.packer.io"}
|
|
|
|
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.ISOUrls)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test both set
|
|
|
|
config["iso_url"] = "http://www.packer.io"
|
|
|
|
config["iso_urls"] = []string{"http://www.packer.io"}
|
|
|
|
b = Builder{}
|
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test just iso_urls set
|
|
|
|
delete(config, "iso_url")
|
|
|
|
config["iso_urls"] = []string{
|
|
|
|
"http://www.packer.io",
|
|
|
|
"http://www.hashicorp.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
b = Builder{}
|
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = []string{
|
|
|
|
"http://www.packer.io",
|
|
|
|
"http://www.hashicorp.com",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.ISOUrls)
|
|
|
|
}
|
|
|
|
}
|