Use the convention for default vmname

Use the convention for default hdd size
Tests added for builder
This commit is contained in:
Taliesin Sisson 2015-06-21 15:53:08 +01:00
parent 469415fdfb
commit 79a5f90379
2 changed files with 256 additions and 16 deletions

View File

@ -5,7 +5,6 @@
package iso package iso
import ( import (
"code.google.com/p/go-uuid/uuid"
"errors" "errors"
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
@ -18,13 +17,12 @@ import (
"github.com/mitchellh/packer/powershell/hyperv" "github.com/mitchellh/packer/powershell/hyperv"
"github.com/mitchellh/packer/template/interpolate" "github.com/mitchellh/packer/template/interpolate"
"log" "log"
"os"
"strings" "strings"
"time" "time"
) )
const ( const (
DefaultDiskSize = 127 * 1024 // 127GB DefaultDiskSize = 40000 // ~40GB
MinDiskSize = 10 * 1024 // 10GB MinDiskSize = 10 * 1024 // 10GB
MaxDiskSize = 65536 * 1024 // 64TB MaxDiskSize = 65536 * 1024 // 64TB
@ -71,6 +69,7 @@ type Config struct {
FloppyFiles []string `mapstructure:"floppy_files"` FloppyFiles []string `mapstructure:"floppy_files"`
// //
SecondaryDvdImages []string `mapstructure:"secondary_iso_images"` SecondaryDvdImages []string `mapstructure:"secondary_iso_images"`
// The checksum for the OS ISO file. Because ISO files are so large, // The checksum for the OS ISO file. Because ISO files are so large,
// this is required and Packer will verify it prior to booting a virtual // this is required and Packer will verify it prior to booting a virtual
// machine with the ISO attached. The type of the checksum is specified // machine with the ISO attached. The type of the checksum is specified
@ -91,6 +90,7 @@ type Config struct {
// same file (same checksum). By default this is empty and iso_url is // same file (same checksum). By default this is empty and iso_url is
// used. Only one of iso_url or iso_urls can be specified. // used. Only one of iso_url or iso_urls can be specified.
ISOUrls []string `mapstructure:"iso_urls"` ISOUrls []string `mapstructure:"iso_urls"`
// This is the name of the new virtual machine. // This is the name of the new virtual machine.
// By default this is "packer-BUILDNAME", where "BUILDNAME" is the name of the build. // By default this is "packer-BUILDNAME", where "BUILDNAME" is the name of the build.
VMName string `mapstructure:"vm_name"` VMName string `mapstructure:"vm_name"`
@ -140,7 +140,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
} }
if b.config.VMName == "" { if b.config.VMName == "" {
b.config.VMName = fmt.Sprintf("pvm_%s", uuid.New()) b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName)
} }
log.Println(fmt.Sprintf("%s: %v", "VMName", b.config.VMName)) log.Println(fmt.Sprintf("%s: %v", "VMName", b.config.VMName))
@ -149,7 +149,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
// no switch name, try to get one attached to a online network adapter // no switch name, try to get one attached to a online network adapter
onlineSwitchName, err := hyperv.GetExternalOnlineVirtualSwitch() onlineSwitchName, err := hyperv.GetExternalOnlineVirtualSwitch()
if onlineSwitchName == "" || err != nil { if onlineSwitchName == "" || err != nil {
b.config.SwitchName = fmt.Sprintf("pis_%s", uuid.New()) b.config.SwitchName = fmt.Sprintf("packer-%s", b.config.PackerBuildName)
} else { } else {
b.config.SwitchName = onlineSwitchName b.config.SwitchName = onlineSwitchName
} }
@ -209,20 +209,9 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
} }
} }
if b.config.RawSingleISOUrl == "" {
errs = packer.MultiErrorAppend(errs, errors.New("iso_url: The option can't be missed and a path must be specified."))
} else if _, err := os.Stat(b.config.RawSingleISOUrl); err != nil {
errs = packer.MultiErrorAppend(errs, errors.New("iso_url: Check the path is correct"))
}
log.Println(fmt.Sprintf("%s: %v", "RawSingleISOUrl", b.config.RawSingleISOUrl)) log.Println(fmt.Sprintf("%s: %v", "RawSingleISOUrl", b.config.RawSingleISOUrl))
// Warnings // Warnings
warning := b.checkHostAvailableMemory()
if warning != "" {
warnings = appendWarnings(warnings, warning)
}
if b.config.ISOChecksumType == "none" { if b.config.ISOChecksumType == "none" {
warnings = append(warnings, warnings = append(warnings,
"A checksum type of 'none' was specified. Since ISO files are so big,\n"+ "A checksum type of 'none' was specified. Since ISO files are so big,\n"+
@ -235,6 +224,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
"will forcibly halt the virtual machine, which may result in data loss.") "will forcibly halt the virtual machine, which may result in data loss.")
} }
warning := b.checkHostAvailableMemory()
if warning != "" {
warnings = appendWarnings(warnings, warning)
}
if errs != nil && len(errs.Errors) > 0 { if errs != nil && len(errs.Errors) > 0 {
return warnings, errs return warnings, errs
} }

View File

@ -0,0 +1,246 @@
package iso
import (
"github.com/mitchellh/packer/packer"
"reflect"
"testing"
)
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",
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()
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)
}
}
func TestBuilderPrepare_DiskSize(t *testing.T) {
var b Builder
config := testConfig()
delete(config, "disk_size")
warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
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{}
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.DiskSize != 60000 {
t.Fatalf("bad size: %d", b.config.DiskSize)
}
}
func TestBuilderPrepare_InvalidKey(t *testing.T) {
var b Builder
config := testConfig()
// 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()
// 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()
// 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()
delete(config, "iso_url")
delete(config, "iso_urls")
// Test both epty
config["iso_url"] = ""
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 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)
}
}