github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_alb_listener_rule_test.go (about) 1 package aws 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/elbv2" 10 "github.com/hashicorp/errwrap" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSALBListenerRule_basic(t *testing.T) { 17 var conf elbv2.Rule 18 albName := fmt.Sprintf("testrule-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) 19 targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 IDRefreshName: "aws_alb_listener_rule.static", 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckAWSALBListenerRuleDestroy, 26 Steps: []resource.TestStep{ 27 { 28 Config: testAccAWSALBListenerRuleConfig_basic(albName, targetGroupName), 29 Check: resource.ComposeAggregateTestCheckFunc( 30 testAccCheckAWSALBListenerRuleExists("aws_alb_listener_rule.static", &conf), 31 resource.TestCheckResourceAttrSet("aws_alb_listener_rule.static", "arn"), 32 resource.TestCheckResourceAttr("aws_alb_listener_rule.static", "priority", "100"), 33 resource.TestCheckResourceAttr("aws_alb_listener_rule.static", "action.#", "1"), 34 resource.TestCheckResourceAttr("aws_alb_listener_rule.static", "action.0.type", "forward"), 35 resource.TestCheckResourceAttrSet("aws_alb_listener_rule.static", "action.0.target_group_arn"), 36 resource.TestCheckResourceAttr("aws_alb_listener_rule.static", "condition.#", "1"), 37 resource.TestCheckResourceAttr("aws_alb_listener_rule.static", "condition.0.field", "path-pattern"), 38 resource.TestCheckResourceAttr("aws_alb_listener_rule.static", "condition.0.values.#", "1"), 39 resource.TestCheckResourceAttrSet("aws_alb_listener_rule.static", "condition.0.values.0"), 40 ), 41 }, 42 }, 43 }) 44 } 45 46 func testAccCheckAWSALBListenerRuleExists(n string, res *elbv2.Rule) resource.TestCheckFunc { 47 return func(s *terraform.State) error { 48 rs, ok := s.RootModule().Resources[n] 49 if !ok { 50 return fmt.Errorf("Not found: %s", n) 51 } 52 53 if rs.Primary.ID == "" { 54 return errors.New("No Listener Rule ID is set") 55 } 56 57 conn := testAccProvider.Meta().(*AWSClient).elbv2conn 58 59 describe, err := conn.DescribeRules(&elbv2.DescribeRulesInput{ 60 RuleArns: []*string{aws.String(rs.Primary.ID)}, 61 }) 62 63 if err != nil { 64 return err 65 } 66 67 if len(describe.Rules) != 1 || 68 *describe.Rules[0].RuleArn != rs.Primary.ID { 69 return errors.New("Listener Rule not found") 70 } 71 72 *res = *describe.Rules[0] 73 return nil 74 } 75 } 76 77 func testAccCheckAWSALBListenerRuleDestroy(s *terraform.State) error { 78 conn := testAccProvider.Meta().(*AWSClient).elbv2conn 79 80 for _, rs := range s.RootModule().Resources { 81 if rs.Type != "aws_alb_listener_rule" { 82 continue 83 } 84 85 describe, err := conn.DescribeRules(&elbv2.DescribeRulesInput{ 86 RuleArns: []*string{aws.String(rs.Primary.ID)}, 87 }) 88 89 if err == nil { 90 if len(describe.Rules) != 0 && 91 *describe.Rules[0].RuleArn == rs.Primary.ID { 92 return fmt.Errorf("Listener Rule %q still exists", rs.Primary.ID) 93 } 94 } 95 96 // Verify the error 97 if isRuleNotFound(err) { 98 return nil 99 } else { 100 return errwrap.Wrapf("Unexpected error checking ALB Listener Rule destroyed: {{err}}", err) 101 } 102 } 103 104 return nil 105 } 106 107 func testAccAWSALBListenerRuleConfig_basic(albName, targetGroupName string) string { 108 return fmt.Sprintf(`resource "aws_alb_listener_rule" "static" { 109 listener_arn = "${aws_alb_listener.front_end.arn}" 110 priority = 100 111 112 action { 113 type = "forward" 114 target_group_arn = "${aws_alb_target_group.test.arn}" 115 } 116 117 condition { 118 field = "path-pattern" 119 values = ["/static/*"] 120 } 121 } 122 123 resource "aws_alb_listener" "front_end" { 124 load_balancer_arn = "${aws_alb.alb_test.id}" 125 protocol = "HTTP" 126 port = "80" 127 128 default_action { 129 target_group_arn = "${aws_alb_target_group.test.id}" 130 type = "forward" 131 } 132 } 133 134 resource "aws_alb" "alb_test" { 135 name = "%s" 136 internal = false 137 security_groups = ["${aws_security_group.alb_test.id}"] 138 subnets = ["${aws_subnet.alb_test.*.id}"] 139 140 idle_timeout = 30 141 enable_deletion_protection = false 142 143 tags { 144 TestName = "TestAccAWSALB_basic" 145 } 146 } 147 148 resource "aws_alb_target_group" "test" { 149 name = "%s" 150 port = 8080 151 protocol = "HTTP" 152 vpc_id = "${aws_vpc.alb_test.id}" 153 154 health_check { 155 path = "/health" 156 interval = 60 157 port = 8081 158 protocol = "HTTP" 159 timeout = 3 160 healthy_threshold = 3 161 unhealthy_threshold = 3 162 matcher = "200-299" 163 } 164 } 165 166 variable "subnets" { 167 default = ["10.0.1.0/24", "10.0.2.0/24"] 168 type = "list" 169 } 170 171 data "aws_availability_zones" "available" {} 172 173 resource "aws_vpc" "alb_test" { 174 cidr_block = "10.0.0.0/16" 175 176 tags { 177 TestName = "TestAccAWSALB_basic" 178 } 179 } 180 181 resource "aws_subnet" "alb_test" { 182 count = 2 183 vpc_id = "${aws_vpc.alb_test.id}" 184 cidr_block = "${element(var.subnets, count.index)}" 185 map_public_ip_on_launch = true 186 availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}" 187 188 tags { 189 TestName = "TestAccAWSALB_basic" 190 } 191 } 192 193 resource "aws_security_group" "alb_test" { 194 name = "allow_all_alb_test" 195 description = "Used for ALB Testing" 196 vpc_id = "${aws_vpc.alb_test.id}" 197 198 ingress { 199 from_port = 0 200 to_port = 0 201 protocol = "-1" 202 cidr_blocks = ["0.0.0.0/0"] 203 } 204 205 egress { 206 from_port = 0 207 to_port = 0 208 protocol = "-1" 209 cidr_blocks = ["0.0.0.0/0"] 210 } 211 212 tags { 213 TestName = "TestAccAWSALB_basic" 214 } 215 }`, albName, targetGroupName) 216 }