Skip to content

Commit

Permalink
refactor, add additional test
Browse files Browse the repository at this point in the history
  • Loading branch information
catsby committed Dec 11, 2015
1 parent fe9c23f commit 84dae07
Showing 1 changed file with 84 additions and 30 deletions.
114 changes: 84 additions & 30 deletions builtin/providers/aws/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package aws
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -83,7 +82,90 @@ func TestAWSConfig_shouldBeStatic(t *testing.T) {
Secret: "test",
Token: "test",
},
//{},
}

for _, c := range simple {
cfg := Config{
AccessKey: c.Key,
SecretKey: c.Secret,
Token: c.Token,
}

creds := getCreds(cfg.AccessKey, cfg.SecretKey, cfg.Token)
if creds == nil {
t.Fatalf("Expected a static creds provider to be returned")
}
v, err := creds.Get()
if err != nil {
t.Fatalf("Error gettings creds: %s", err)
}
if v.AccessKeyID != c.Key {
t.Fatalf("AccessKeyID mismatch, expected: (%s), got (%s)", c.Key, v.AccessKeyID)
}
if v.SecretAccessKey != c.Secret {
t.Fatalf("SecretAccessKey mismatch, expected: (%s), got (%s)", c.Secret, v.SecretAccessKey)
}
if v.SessionToken != c.Token {
t.Fatalf("SessionToken mismatch, expected: (%s), got (%s)", c.Token, v.SessionToken)
}
}
}

// TestAWSConfig_shouldIAM is designed to test the scenario of running Terraform
// from an EC2 instance, without environment variables or manually supplied
// credentials.
func TestAWSConfig_shouldIAM(t *testing.T) {
// clear AWS_* environment variables
unsetEnv(t)
defer resetEnv(t)

// capture the test server's close method, to call after the test returns
ts := awsEnv(t)
defer ts()

// An empty config, no key supplied
cfg := Config{}

creds := getCreds(cfg.AccessKey, cfg.SecretKey, cfg.Token)
if creds == nil {
t.Fatalf("Expected a static creds provider to be returned")
}

v, err := creds.Get()
if err != nil {
t.Fatalf("Error gettings creds: %s", err)
}
if v.AccessKeyID != "somekey" {
t.Fatalf("AccessKeyID mismatch, expected: (somekey), got (%s)", v.AccessKeyID)
}
if v.SecretAccessKey != "somesecret" {
t.Fatalf("SecretAccessKey mismatch, expected: (somesecret), got (%s)", v.SecretAccessKey)
}
if v.SessionToken != "sometoken" {
t.Fatalf("SessionToken mismatch, expected: (sometoken), got (%s)", v.SessionToken)
}
}

// TestAWSConfig_shouldIAM is designed to test the scenario of running Terraform
// from an EC2 instance, without environment variables or manually supplied
// credentials.
func TestAWSConfig_shouldIgnoreIAM(t *testing.T) {
unsetEnv(t)
defer resetEnv(t)
// capture the test server's close method, to call after the test returns
ts := awsEnv(t)
defer ts()
simple := []struct {
Key, Secret, Token string
}{
{
Key: "test",
Secret: "secret",
}, {
Key: "test",
Secret: "test",
Token: "test",
},
}

for _, c := range simple {
Expand Down Expand Up @@ -178,40 +260,12 @@ func setEnv(s string, t *testing.T) {
}
}

func TestAWSConfig_shouldIAM(t *testing.T) {
unsetEnv(t)
a := awsEnv(t)
defer a()
defer resetEnv(t)
cfg := Config{}

creds := getCreds(cfg.AccessKey, cfg.SecretKey, cfg.Token)
if creds == nil {
t.Fatalf("Expected a static creds provider to be returned")
}
v, err := creds.Get()
if err != nil {
t.Fatalf("Error gettings creds: %s", err)
}
if v.AccessKeyID != "somekey" {
t.Fatalf("AccessKeyID mismatch, expected: (somekey), got (%s)", v.AccessKeyID)
}
if v.SecretAccessKey != "somesecret" {
t.Fatalf("SecretAccessKey mismatch, expected: (somesecret), got (%s)", v.SecretAccessKey)
}
if v.SessionToken != "sometoken" {
t.Fatalf("SessionToken mismatch, expected: (sometoken), got (%s)", v.SessionToken)
}
}

func awsEnv(t *testing.T) func() {
// os.Setenv("AWS_ENV_URL", "http://127.0.0.1/latest/meta-data/")
routes := routes{}
if err := json.Unmarshal([]byte(aws_routes), &routes); err != nil {
t.Fatalf("Failed to unmarshal JSON in AWS ENV test: %s", err)
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("\n\t*** uri: %s\n", r.RequestURI)
for _, e := range routes.Endpoints {
if r.RequestURI == e.Uri {
w.Header().Set("Content-Type", e.ContentType)
Expand Down

0 comments on commit 84dae07

Please sign in to comment.