Commit 4223e5bc authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/amazonebs: Additional validation

parent 6a23cb72
...@@ -60,15 +60,24 @@ func (b *Builder) Prepare(raw interface{}) (err error) { ...@@ -60,15 +60,24 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
errs = append(errs, errors.New("A secret_key must be specified")) errs = append(errs, errors.New("A secret_key must be specified"))
} }
if b.config.SourceAmi == "" {
errs = append(errs, errors.New("A source_ami must be specified"))
}
if b.config.InstanceType == "" {
errs = append(errs, errors.New("An instance_type must be specified"))
}
if b.config.SSHUsername == "" {
errs = append(errs, errors.New("An ssh_username must be specified"))
}
if len(errs) > 0 { if len(errs) > 0 {
return &packer.MultiError{errs} return &packer.MultiError{errs}
} }
// TODO: config validation and asking for fields: // TODO: config validation and asking for fields:
// * region (exists and valid) // * region (exists and valid)
// * source ami
// * instance type
// * SSH username
log.Printf("Config: %+v", b.config) log.Printf("Config: %+v", b.config)
return return
......
...@@ -9,6 +9,9 @@ func testConfig() map[string]interface{} { ...@@ -9,6 +9,9 @@ func testConfig() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"access_key": "foo", "access_key": "foo",
"secret_key": "bar", "secret_key": "bar",
"source_ami": "foo",
"instance_type": "foo",
"ssh_username": "root",
} }
} }
...@@ -56,6 +59,30 @@ func TestBuilderPrepare_AccessKey(t *testing.T) { ...@@ -56,6 +59,30 @@ func TestBuilderPrepare_AccessKey(t *testing.T) {
} }
} }
func TestBuilderPrepare_InstanceType(t *testing.T) {
var b Builder
config := testConfig()
// Test good
config["instance_type"] = "foo"
err := b.Prepare(config)
if err != nil {
t.Fatalf("should not have error: %s", err)
}
if b.config.InstanceType != "foo" {
t.Errorf("invalid: %s", b.config.InstanceType)
}
// Test bad
delete(config, "instance_type")
b = Builder{}
err = b.Prepare(config)
if err == nil {
t.Fatal("should have error")
}
}
func TestBuilderPrepare_SecretKey(t *testing.T) { func TestBuilderPrepare_SecretKey(t *testing.T) {
var b Builder var b Builder
config := testConfig() config := testConfig()
...@@ -80,6 +107,30 @@ func TestBuilderPrepare_SecretKey(t *testing.T) { ...@@ -80,6 +107,30 @@ func TestBuilderPrepare_SecretKey(t *testing.T) {
} }
} }
func TestBuilderPrepare_SourceAmi(t *testing.T) {
var b Builder
config := testConfig()
// Test good
config["source_ami"] = "foo"
err := b.Prepare(config)
if err != nil {
t.Fatalf("should not have error: %s", err)
}
if b.config.SourceAmi != "foo" {
t.Errorf("invalid: %s", b.config.SourceAmi)
}
// Test bad
delete(config, "source_ami")
b = Builder{}
err = b.Prepare(config)
if err == nil {
t.Fatal("should have error")
}
}
func TestBuilderPrepare_SSHPort(t *testing.T) { func TestBuilderPrepare_SSHPort(t *testing.T) {
var b Builder var b Builder
config := testConfig() config := testConfig()
...@@ -106,3 +157,27 @@ func TestBuilderPrepare_SSHPort(t *testing.T) { ...@@ -106,3 +157,27 @@ func TestBuilderPrepare_SSHPort(t *testing.T) {
t.Errorf("invalid: %d", b.config.SSHPort) t.Errorf("invalid: %d", b.config.SSHPort)
} }
} }
func TestBuilderPrepare_SSHUsername(t *testing.T) {
var b Builder
config := testConfig()
// Test good
config["ssh_username"] = "foo"
err := b.Prepare(config)
if err != nil {
t.Fatalf("should not have error: %s", err)
}
if b.config.SSHUsername != "foo" {
t.Errorf("invalid: %s", b.config.SSHUsername)
}
// Test bad
delete(config, "ssh_username")
b = Builder{}
err = b.Prepare(config)
if err == nil {
t.Fatal("should have error")
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment