github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/builtin/providers/aws/resource_aws_elb_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/mitchellh/goamz/elb"
    11  )
    12  
    13  func TestAccAWSELB_basic(t *testing.T) {
    14  	var conf elb.LoadBalancer
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSELBDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSELBConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
    25  					testAccCheckAWSELBAttributes(&conf),
    26  					resource.TestCheckResourceAttr(
    27  						"aws_elb.bar", "name", "foobar-terraform-test"),
    28  					resource.TestCheckResourceAttr(
    29  						"aws_elb.bar", "availability_zones.0", "us-west-2a"),
    30  					resource.TestCheckResourceAttr(
    31  						"aws_elb.bar", "availability_zones.1", "us-west-2b"),
    32  					resource.TestCheckResourceAttr(
    33  						"aws_elb.bar", "availability_zones.2", "us-west-2c"),
    34  					resource.TestCheckResourceAttr(
    35  						"aws_elb.bar", "listener.0.instance_port", "8000"),
    36  					resource.TestCheckResourceAttr(
    37  						"aws_elb.bar", "listener.0.instance_protocol", "http"),
    38  					resource.TestCheckResourceAttr(
    39  						"aws_elb.bar", "listener.0.lb_port", "80"),
    40  					resource.TestCheckResourceAttr(
    41  						"aws_elb.bar", "listener.0.lb_protocol", "http"),
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func TestAccAWSELB_InstanceAttaching(t *testing.T) {
    49  	var conf elb.LoadBalancer
    50  
    51  	testCheckInstanceAttached := func(count int) resource.TestCheckFunc {
    52  		return func(*terraform.State) error {
    53  			if len(conf.Instances) != count {
    54  				return fmt.Errorf("instance count does not match")
    55  			}
    56  			return nil
    57  		}
    58  	}
    59  
    60  	resource.Test(t, resource.TestCase{
    61  		PreCheck:     func() { testAccPreCheck(t) },
    62  		Providers:    testAccProviders,
    63  		CheckDestroy: testAccCheckAWSELBDestroy,
    64  		Steps: []resource.TestStep{
    65  			resource.TestStep{
    66  				Config: testAccAWSELBConfig,
    67  				Check: resource.ComposeTestCheckFunc(
    68  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
    69  					testAccCheckAWSELBAttributes(&conf),
    70  				),
    71  			},
    72  
    73  			resource.TestStep{
    74  				Config: testAccAWSELBConfigNewInstance,
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
    77  					testCheckInstanceAttached(1),
    78  				),
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func testAccCheckAWSELBDestroy(s *terraform.State) error {
    85  	conn := testAccProvider.elbconn
    86  
    87  	for _, rs := range s.Resources {
    88  		if rs.Type != "aws_elb" {
    89  			continue
    90  		}
    91  
    92  		describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{
    93  			Names: []string{rs.ID},
    94  		})
    95  
    96  		if err == nil {
    97  			if len(describe.LoadBalancers) != 0 &&
    98  				describe.LoadBalancers[0].LoadBalancerName == rs.ID {
    99  				return fmt.Errorf("ELB still exists")
   100  			}
   101  		}
   102  
   103  		// Verify the error
   104  		providerErr, ok := err.(*elb.Error)
   105  		if !ok {
   106  			return err
   107  		}
   108  
   109  		if providerErr.Code != "InvalidLoadBalancerName.NotFound" {
   110  			return fmt.Errorf("Unexpected error: %s", err)
   111  		}
   112  	}
   113  
   114  	return nil
   115  }
   116  
   117  func testAccCheckAWSELBAttributes(conf *elb.LoadBalancer) resource.TestCheckFunc {
   118  	return func(s *terraform.State) error {
   119  		if conf.AvailabilityZones[0].AvailabilityZone != "us-west-2a" {
   120  			return fmt.Errorf("bad availability_zones")
   121  		}
   122  
   123  		if conf.LoadBalancerName != "foobar-terraform-test" {
   124  			return fmt.Errorf("bad name")
   125  		}
   126  
   127  		l := elb.Listener{
   128  			InstancePort:     8000,
   129  			InstanceProtocol: "HTTP",
   130  			LoadBalancerPort: 80,
   131  			Protocol:         "HTTP",
   132  		}
   133  
   134  		if !reflect.DeepEqual(conf.Listeners[0], l) {
   135  			return fmt.Errorf(
   136  				"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   137  				conf.Listeners[0],
   138  				l)
   139  		}
   140  
   141  		if conf.DNSName == "" {
   142  			return fmt.Errorf("empty dns_name")
   143  		}
   144  
   145  		return nil
   146  	}
   147  }
   148  
   149  func testAccCheckAWSELBExists(n string, res *elb.LoadBalancer) resource.TestCheckFunc {
   150  	return func(s *terraform.State) error {
   151  		rs, ok := s.Resources[n]
   152  		if !ok {
   153  			return fmt.Errorf("Not found: %s", n)
   154  		}
   155  
   156  		if rs.ID == "" {
   157  			return fmt.Errorf("No ELB ID is set")
   158  		}
   159  
   160  		conn := testAccProvider.elbconn
   161  
   162  		describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{
   163  			Names: []string{rs.ID},
   164  		})
   165  
   166  		if err != nil {
   167  			return err
   168  		}
   169  
   170  		if len(describe.LoadBalancers) != 1 ||
   171  			describe.LoadBalancers[0].LoadBalancerName != rs.ID {
   172  			return fmt.Errorf("ELB not found")
   173  		}
   174  
   175  		*res = describe.LoadBalancers[0]
   176  
   177  		return nil
   178  	}
   179  }
   180  
   181  const testAccAWSELBConfig = `
   182  resource "aws_elb" "bar" {
   183    name = "foobar-terraform-test"
   184    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   185  
   186    listener {
   187      instance_port = 8000
   188      instance_protocol = "http"
   189      lb_port = 80
   190      lb_protocol = "http"
   191    }
   192  
   193    instances = []
   194  }
   195  `
   196  
   197  const testAccAWSELBConfigNewInstance = `
   198  resource "aws_elb" "bar" {
   199    name = "foobar-terraform-test"
   200    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   201  
   202    listener {
   203      instance_port = 8000
   204      instance_protocol = "http"
   205      lb_port = 80
   206      lb_protocol = "http"
   207    }
   208  
   209    instances = ["${aws_instance.foo.id}"]
   210  }
   211  
   212  resource "aws_instance" "foo" {
   213  	# us-west-2
   214  	ami = "ami-043a5034"
   215  	instance_type = "t1.micro"
   216  }
   217  `