Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Acceptance Test Framework #17

Merged
merged 9 commits into from
Jul 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ CGO_CFLAGS:=-I$(CURDIR)/vendor/libucl/include
CGO_LDFLAGS:=-L$(CURDIR)/vendor/libucl
LIBUCL_NAME=libucl.a
TEST?=./...
TESTARGS?=-timeout=5s

# Windows-only
ifeq ($(OS), Windows_NT)
Expand All @@ -23,7 +22,14 @@ dev: libucl
libucl: vendor/libucl/$(LIBUCL_NAME)

test: libucl
go test $(TEST) $(TESTARGS)
go test $(TEST) $(TESTARGS) -timeout=10s

testacc: libucl
@if [ "$(TEST)" = "./..." ]; then \
echo "ERROR: Set TEST to a specific package"; \
exit 1; \
fi
TF_ACC=1 go test $(TEST) -v $(TESTARGS)

testrace: libucl
go test -race $(TEST) $(TESTARGS)
Expand Down
5 changes: 5 additions & 0 deletions builtin/providers/aws/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"os"
"strings"
"unicode"

Expand Down Expand Up @@ -36,6 +37,10 @@ func (c *Config) AWSRegion() (aws.Region, error) {
return aws.Regions[c.Region], nil
}

if v := os.Getenv("AWS_REGION"); v != "" {
return aws.Regions[v], nil
}

md, err := aws.GetMetaData("placement/availability-zone")
if err != nil {
return aws.Region{}, err
Expand Down
86 changes: 86 additions & 0 deletions builtin/providers/aws/resource_aws_vpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/ec2"
)

func TestAccVpc(t *testing.T) {
testAccPreCheck(t)

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccVpcConfig,
Check: testAccCheckVpcExists("aws_vpc.foo"),
},
},
})
}

func testAccCheckVpcDestroy(s *terraform.State) error {
conn := testAccProvider.ec2conn

for _, rs := range s.Resources {
if rs.Type != "aws_vpc" {
continue
}

// Try to find the VPC
resp, err := conn.DescribeVpcs([]string{rs.ID}, ec2.NewFilter())
if err == nil {
if len(resp.VPCs) > 0 {
return fmt.Errorf("VPCs still exist.")
}

return nil
}

// Verify the error is what we want
ec2err, ok := err.(*ec2.Error)
if !ok {
return err
}
if ec2err.Code != "InvalidVpcID.NotFound" {
return err
}
}

return nil
}

func testAccCheckVpcExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.ID == "" {
return fmt.Errorf("No VPC ID is set")
}

conn := testAccProvider.ec2conn
resp, err := conn.DescribeVpcs([]string{rs.ID}, ec2.NewFilter())
if err != nil {
return err
}
if len(resp.VPCs) == 0 {
return fmt.Errorf("VPC not found")
}

return nil
}
}

const testAccVpcConfig = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
}
`
20 changes: 14 additions & 6 deletions builtin/providers/aws/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ type ResourceProvider struct {
}

func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
return nil, nil
v := &config.Validator{
Optional: []string{
"access_key",
"secret_key",
"region",
},
}

return v.Validate(c)
}

func (p *ResourceProvider) ValidateResource(
Expand All @@ -36,24 +44,24 @@ func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error {
// Get the auth and region. This can fail if keys/regions were not
// specified and we're attempting to use the environment.
var errs []error
log.Println("Building AWS auth structure")
log.Println("[INFO] Building AWS auth structure")
auth, err := p.Config.AWSAuth()
if err != nil {
errs = append(errs, err)
}

log.Println("Building AWS region structure")
log.Println("[INFO] Building AWS region structure")
region, err := p.Config.AWSRegion()
if err != nil {
errs = append(errs, err)
}

if len(errs) == 0 {
log.Println("Initializing EC2 connection")
log.Println("[INFO] Initializing EC2 connection")
p.ec2conn = ec2.New(auth, region)
log.Println("Initializing ELB connection")
log.Println("[INFO] Initializing ELB connection")
p.elbconn = elb.New(auth, region)
log.Println("Initializing AutoScaling connection")
log.Println("[INFO] Initializing AutoScaling connection")
p.autoscalingconn = autoscaling.New(auth, region)
}

Expand Down
25 changes: 25 additions & 0 deletions builtin/providers/aws/resource_provider_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package aws

import (
"os"
"log"
"reflect"
"testing"

"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform"
)

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *ResourceProvider

func init() {
testAccProvider = new(ResourceProvider)
testAccProviders = map[string]terraform.ResourceProvider{
"aws": testAccProvider,
}
}

func TestResourceProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = new(ResourceProvider)
}
Expand Down Expand Up @@ -41,3 +53,16 @@ func TestResourceProvider_Configure(t *testing.T) {
t.Fatalf("bad: %#v", rp.Config)
}
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("AWS_ACCESS_KEY"); v == "" {
t.Fatal("AWS_ACCESS_KEY must be set for acceptance tests")
}
if v := os.Getenv("AWS_SECRET_KEY"); v == "" {
t.Fatal("AWS_SECRET_KEY must be set for acceptance tests")
}
if v := os.Getenv("AWS_REGION"); v == "" {
log.Println("[INFO] Test: Using us-west-2 as test region")
os.Setenv("AWS_REGION", "us-west-2")
}
}
Loading