builder/amazonebs: tests for env vars

This commit is contained in:
Mitchell Hashimoto 2013-07-01 15:22:35 -07:00
parent 94e2999b89
commit b4d9a8f4a7
2 changed files with 62 additions and 0 deletions

View File

@ -3,6 +3,8 @@
IMPROVEMENTS: IMPROVEMENTS:
* core: Template syntax errors now show line and character number. [GH-56] * core: Template syntax errors now show line and character number. [GH-56]
* amazon-ebs: Access key and secret access key default to
environmental variables. [GH-40]
## 0.1.3 (July 1, 2013) ## 0.1.3 (July 1, 2013)

View File

@ -2,9 +2,19 @@ package amazonebs
import ( import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"os"
"testing" "testing"
) )
func init() {
// Clear out the AWS access key env vars so they don't
// affect our tests.
os.Setenv("AWS_ACCESS_KEY_ID", "")
os.Setenv("AWS_ACCESS_KEY", "")
os.Setenv("AWS_SECRET_ACCESS_KEY", "")
os.Setenv("AWS_SECRET_KEY", "")
}
func testConfig() map[string]interface{} { func testConfig() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"access_key": "foo", "access_key": "foo",
@ -59,6 +69,31 @@ func TestBuilderPrepare_AccessKey(t *testing.T) {
if err == nil { if err == nil {
t.Fatal("should have error") t.Fatal("should have error")
} }
// Test env
delete(config, "access_key")
os.Setenv("AWS_ACCESS_KEY_ID", "foo")
defer os.Setenv("AWS_ACCESS_KEY_ID", "")
err = b.Prepare(config)
if err != nil {
t.Fatalf("should not have error: %s", err)
}
if b.config.AccessKey != "foo" {
t.Errorf("access key invalid: %s", b.config.AccessKey)
}
delete(config, "access_key")
os.Setenv("AWS_ACCESS_KEY", "foo")
defer os.Setenv("AWS_ACCESS_KEY", "")
err = b.Prepare(config)
if err != nil {
t.Fatalf("should not have error: %s", err)
}
if b.config.AccessKey != "foo" {
t.Errorf("access key invalid: %s", b.config.AccessKey)
}
} }
func TestBuilderPrepare_AMIName(t *testing.T) { func TestBuilderPrepare_AMIName(t *testing.T) {
@ -167,6 +202,31 @@ func TestBuilderPrepare_SecretKey(t *testing.T) {
if err == nil { if err == nil {
t.Fatal("should have error") t.Fatal("should have error")
} }
// Test env
delete(config, "secret_key")
os.Setenv("AWS_SECRET_ACCESS_KEY", "foo")
defer os.Setenv("AWS_SECRET_ACCESS_KEY", "")
err = b.Prepare(config)
if err != nil {
t.Fatalf("should not have error: %s", err)
}
if b.config.SecretKey != "foo" {
t.Errorf("access key invalid: %s", b.config.SecretKey)
}
delete(config, "secret_key")
os.Setenv("AWS_SECRET_KEY", "foo")
defer os.Setenv("AWS_SECRET_KEY", "")
err = b.Prepare(config)
if err != nil {
t.Fatalf("should not have error: %s", err)
}
if b.config.SecretKey != "foo" {
t.Errorf("access key invalid: %s", b.config.SecretKey)
}
} }
func TestBuilderPrepare_SourceAmi(t *testing.T) { func TestBuilderPrepare_SourceAmi(t *testing.T) {