2013-12-24 00:58:41 -05:00
|
|
|
package iso
|
2013-06-06 17:38:14 -04:00
|
|
|
|
|
|
|
import (
|
2013-06-10 01:08:53 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-07-08 23:56:23 -04:00
|
|
|
"reflect"
|
2013-06-06 17:38:14 -04:00
|
|
|
"testing"
|
2015-06-13 18:52:44 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-06 17:38:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2013-07-14 02:58:56 -04:00
|
|
|
"iso_checksum": "foo",
|
|
|
|
"iso_checksum_type": "md5",
|
|
|
|
"iso_url": "http://www.packer.io",
|
2013-11-03 00:17:21 -04:00
|
|
|
"shutdown_command": "foo",
|
2013-07-14 02:58:56 -04:00
|
|
|
"ssh_username": "foo",
|
2013-07-01 14:11:31 -04:00
|
|
|
|
|
|
|
packer.BuildNameConfigKey: "foo",
|
2013-06-06 17:38:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-06-06 17:38:14 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.DiskName != "disk" {
|
|
|
|
t.Errorf("bad disk name: %s", b.config.DiskName)
|
|
|
|
}
|
|
|
|
|
2013-07-01 14:11:31 -04:00
|
|
|
if b.config.OutputDir != "output-foo" {
|
2013-06-06 17:38:14 -04:00
|
|
|
t.Errorf("bad output dir: %s", b.config.OutputDir)
|
|
|
|
}
|
|
|
|
|
2014-09-25 00:20:35 -04:00
|
|
|
if b.config.Version != "9" {
|
|
|
|
t.Errorf("bad Version: %s", b.config.Version)
|
|
|
|
}
|
|
|
|
|
2013-07-01 14:11:31 -04:00
|
|
|
if b.config.VMName != "packer-foo" {
|
2013-06-06 17:38:14 -04:00
|
|
|
t.Errorf("bad vm name: %s", b.config.VMName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 18:07:19 -04:00
|
|
|
func TestBuilderPrepare_DiskSize(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "disk_size")
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-06-23 18:07:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.DiskSize != 40000 {
|
|
|
|
t.Fatalf("bad size: %d", b.config.DiskSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["disk_size"] = 60000
|
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-06-23 18:07:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.DiskSize != 60000 {
|
2014-12-15 13:27:17 -05:00
|
|
|
t.Fatalf("bad size: %d", b.config.DiskSize)
|
2013-06-23 18:07:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-08 23:56:23 -04:00
|
|
|
func TestBuilderPrepare_FloppyFiles(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "floppy_files")
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-07-08 23:56:23 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b.config.FloppyFiles) != 0 {
|
|
|
|
t.Fatalf("bad: %#v", b.config.FloppyFiles)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["floppy_files"] = []string{"foo", "bar"}
|
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-07-08 23:56:23 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{"foo", "bar"}
|
|
|
|
if !reflect.DeepEqual(b.config.FloppyFiles, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.FloppyFiles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 19:50:14 -04:00
|
|
|
func TestBuilderPrepare_Format(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Bad
|
|
|
|
config["format"] = "foobar"
|
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
goodFormats := []string{"ova", "ovf", "vmx"}
|
|
|
|
|
|
|
|
for _, format := range goodFormats {
|
|
|
|
// Good
|
|
|
|
config["format"] = format
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-13 20:28:56 -04:00
|
|
|
func TestBuilderPrepare_InvalidKey(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Add a random key
|
|
|
|
config["i_should_not_be_valid"] = true
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-07-13 20:28:56 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 22:15:24 -04:00
|
|
|
func TestBuilderPrepare_OutputDir(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test with existing dir
|
|
|
|
dir, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
config["output_directory"] = dir
|
2013-08-15 15:02:01 -04:00
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2015-06-13 17:00:14 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
2013-06-27 22:15:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
config["output_directory"] = "i-hope-i-dont-exist"
|
2013-08-15 15:02:01 -04:00
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-06-27 22:15:24 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 13:40:38 -04:00
|
|
|
func TestBuilderPrepare_ToolsUploadPath(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test a default
|
|
|
|
delete(config, "tools_upload_path")
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-07-01 13:40:38 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.ToolsUploadPath == "" {
|
|
|
|
t.Fatalf("bad value: %s", b.config.ToolsUploadPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a bad value
|
|
|
|
config["tools_upload_path"] = "{{{nope}"
|
2013-08-15 15:02:01 -04:00
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-07-01 13:40:38 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
config["tools_upload_path"] = "hey"
|
2013-08-15 15:02:01 -04:00
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-07-01 13:40:38 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 20:23:22 -04:00
|
|
|
func TestBuilderPrepare_VMXTemplatePath(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
config["vmx_template_path"] = "/i/dont/exist/forreal"
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-08-27 20:23:22 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
if _, err := tf.Write([]byte("HELLO!")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["vmx_template_path"] = tf.Name()
|
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-08-27 20:23:22 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad template
|
|
|
|
tf2, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf2.Name())
|
|
|
|
defer tf2.Close()
|
|
|
|
|
|
|
|
if _, err := tf2.Write([]byte("{{foo}")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["vmx_template_path"] = tf2.Name()
|
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-08-27 20:23:22 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-07 18:11:13 -04:00
|
|
|
func TestBuilderPrepare_VNCPort(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Bad
|
|
|
|
config["vnc_port_min"] = 1000
|
|
|
|
config["vnc_port_max"] = 500
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err := b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-06-07 18:11:13 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad
|
|
|
|
config["vnc_port_min"] = -500
|
2013-08-15 15:02:01 -04:00
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-06-07 18:11:13 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Good
|
|
|
|
config["vnc_port_min"] = 500
|
|
|
|
config["vnc_port_max"] = 1000
|
2013-08-15 15:02:01 -04:00
|
|
|
b = Builder{}
|
2013-11-03 00:03:59 -04:00
|
|
|
warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2013-06-07 18:11:13 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 07:02:55 -04:00
|
|
|
|
|
|
|
func TestBuilderPrepare_CommConfig(t *testing.T) {
|
|
|
|
// Test Winrm
|
|
|
|
{
|
|
|
|
config := testConfig()
|
|
|
|
config["communicator"] = "winrm"
|
|
|
|
config["winrm_username"] = "username"
|
|
|
|
config["winrm_password"] = "password"
|
|
|
|
config["winrm_host"] = "1.2.3.4"
|
|
|
|
|
|
|
|
var 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.CommConfig.WinRMUser != "username" {
|
|
|
|
t.Errorf("bad winrm_username: %s", b.config.CommConfig.WinRMUser)
|
|
|
|
}
|
|
|
|
if b.config.CommConfig.WinRMPassword != "password" {
|
|
|
|
t.Errorf("bad winrm_password: %s", b.config.CommConfig.WinRMPassword)
|
|
|
|
}
|
|
|
|
if host := b.config.CommConfig.Host(); host != "1.2.3.4" {
|
|
|
|
t.Errorf("bad host: %s", host)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test SSH
|
|
|
|
{
|
|
|
|
config := testConfig()
|
|
|
|
config["communicator"] = "ssh"
|
|
|
|
config["ssh_username"] = "username"
|
|
|
|
config["ssh_password"] = "password"
|
|
|
|
config["ssh_host"] = "1.2.3.4"
|
|
|
|
|
|
|
|
var 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.CommConfig.SSHUsername != "username" {
|
|
|
|
t.Errorf("bad ssh_username: %s", b.config.CommConfig.SSHUsername)
|
|
|
|
}
|
|
|
|
if b.config.CommConfig.SSHPassword != "password" {
|
|
|
|
t.Errorf("bad ssh_password: %s", b.config.CommConfig.SSHPassword)
|
|
|
|
}
|
|
|
|
if host := b.config.CommConfig.Host(); host != "1.2.3.4" {
|
|
|
|
t.Errorf("bad host: %s", host)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|