From b4d9a8f4a77a0c243e0ae12d1739f10c8abb965d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 1 Jul 2013 15:22:35 -0700 Subject: [PATCH] builder/amazonebs: tests for env vars --- CHANGELOG.md | 2 ++ builder/amazonebs/builder_test.go | 60 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be3f91c9f..028a00409 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ IMPROVEMENTS: * 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) diff --git a/builder/amazonebs/builder_test.go b/builder/amazonebs/builder_test.go index b54f8f13a..d0f0d3f0c 100644 --- a/builder/amazonebs/builder_test.go +++ b/builder/amazonebs/builder_test.go @@ -2,9 +2,19 @@ package amazonebs import ( "github.com/mitchellh/packer/packer" + "os" "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{} { return map[string]interface{}{ "access_key": "foo", @@ -59,6 +69,31 @@ func TestBuilderPrepare_AccessKey(t *testing.T) { if err == nil { 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) { @@ -167,6 +202,31 @@ func TestBuilderPrepare_SecretKey(t *testing.T) { if err == nil { 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) {