mirror of https://github.com/hashicorp/packer
The ISO builders (parallels, qemu, virtualbox, and vmware) had too much common code which needed to be maintained separately. This change moves that code to a common ISO configuration.pull/2849/head
parent
a563944b58
commit
cdcffecc2d
@ -0,0 +1,73 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/packer/template/interpolate"
|
||||
)
|
||||
|
||||
// ISOConfig contains configuration for downloading ISO images.
|
||||
type ISOConfig struct {
|
||||
ISOChecksum string `mapstructure:"iso_checksum"`
|
||||
ISOChecksumType string `mapstructure:"iso_checksum_type"`
|
||||
ISOUrls []string `mapstructure:"iso_urls"`
|
||||
TargetPath string `mapstructure:"iso_target_path"`
|
||||
RawSingleISOUrl string `mapstructure:"iso_url"`
|
||||
}
|
||||
|
||||
func (c *ISOConfig) Prepare(ctx *interpolate.Context) ([]string, []error) {
|
||||
// Validation
|
||||
var errs []error
|
||||
var err error
|
||||
var warnings []string
|
||||
|
||||
if c.ISOChecksumType == "" {
|
||||
errs = append(
|
||||
errs, errors.New("The iso_checksum_type must be specified."))
|
||||
} else {
|
||||
c.ISOChecksumType = strings.ToLower(c.ISOChecksumType)
|
||||
if c.ISOChecksumType != "none" {
|
||||
if c.ISOChecksum == "" {
|
||||
errs = append(
|
||||
errs, errors.New("Due to large file sizes, an iso_checksum is required"))
|
||||
} else {
|
||||
c.ISOChecksum = strings.ToLower(c.ISOChecksum)
|
||||
}
|
||||
|
||||
if h := HashForType(c.ISOChecksumType); h == nil {
|
||||
errs = append(
|
||||
errs,
|
||||
fmt.Errorf("Unsupported checksum type: %s", c.ISOChecksumType))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if c.RawSingleISOUrl == "" && len(c.ISOUrls) == 0 {
|
||||
errs = append(
|
||||
errs, errors.New("One of iso_url or iso_urls must be specified."))
|
||||
} else if c.RawSingleISOUrl != "" && len(c.ISOUrls) > 0 {
|
||||
errs = append(
|
||||
errs, errors.New("Only one of iso_url or iso_urls may be specified."))
|
||||
} else if c.RawSingleISOUrl != "" {
|
||||
c.ISOUrls = []string{c.RawSingleISOUrl}
|
||||
}
|
||||
|
||||
for i, url := range c.ISOUrls {
|
||||
c.ISOUrls[i], err = DownloadableURL(url)
|
||||
if err != nil {
|
||||
errs = append(
|
||||
errs, fmt.Errorf("Failed to parse iso_url %d: %s", i+1, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Warnings
|
||||
if c.ISOChecksumType == "none" {
|
||||
warnings = append(warnings,
|
||||
"A checksum type of 'none' was specified. Since ISO files are so big,\n"+
|
||||
"a checksum is highly recommended.")
|
||||
}
|
||||
|
||||
return warnings, errs
|
||||
}
|
||||
@ -0,0 +1,165 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testISOConfig() ISOConfig {
|
||||
return ISOConfig{
|
||||
ISOChecksum: "foo",
|
||||
ISOChecksumType: "md5",
|
||||
RawSingleISOUrl: "http://www.packer.io",
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if i.ISOChecksum != "foo" {
|
||||
t.Fatalf("should've lowercased: %s", i.ISOChecksum)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
|
||||
// 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 unknown
|
||||
i = testISOConfig()
|
||||
i.ISOChecksumType = "fake"
|
||||
warns, err = i.Prepare(nil)
|
||||
if len(warns) > 0 {
|
||||
t.Fatalf("bad: %#v", warns)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
// Test iso_url set
|
||||
i = testISOConfig()
|
||||
i.RawSingleISOUrl = "http://www.packer.io"
|
||||
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{"http://www.packer.io"}
|
||||
if !reflect.DeepEqual(i.ISOUrls, expected) {
|
||||
t.Fatalf("bad: %#v", i.ISOUrls)
|
||||
}
|
||||
|
||||
// Test both set
|
||||
i = testISOConfig()
|
||||
i.RawSingleISOUrl = "http://www.packer.io"
|
||||
i.ISOUrls = []string{"http://www.packer.io"}
|
||||
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{
|
||||
"http://www.packer.io",
|
||||
"http://www.hashicorp.com",
|
||||
}
|
||||
|
||||
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{
|
||||
"http://www.packer.io",
|
||||
"http://www.hashicorp.com",
|
||||
}
|
||||
if !reflect.DeepEqual(i.ISOUrls, expected) {
|
||||
t.Fatalf("bad: %#v", i.ISOUrls)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue