From 6d7933b41a2c8376106d7615e4ed59cd37bb3d7c Mon Sep 17 00:00:00 2001 From: "Mosley, Franklin" Date: Tue, 15 Mar 2016 02:20:50 -0500 Subject: [PATCH] Added tests for expandPolicyAttributes/flattenPolicyAttributes --- .../resource_aws_lb_ssl_negotiation_policy.go | 6 +- builtin/providers/aws/structure.go | 4 +- builtin/providers/aws/structure_test.go | 104 ++++++++++++++++++ 3 files changed, 111 insertions(+), 3 deletions(-) diff --git a/builtin/providers/aws/resource_aws_lb_ssl_negotiation_policy.go b/builtin/providers/aws/resource_aws_lb_ssl_negotiation_policy.go index dd8129de43..5d0ae78506 100644 --- a/builtin/providers/aws/resource_aws_lb_ssl_negotiation_policy.go +++ b/builtin/providers/aws/resource_aws_lb_ssl_negotiation_policy.go @@ -80,8 +80,12 @@ func resourceAwsLBSSLNegotiationPolicyCreate(d *schema.ResourceData, meta interf // Check for Policy Attributes if v, ok := d.GetOk("attribute"); ok { + var err error // Expand the "attribute" set to aws-sdk-go compat []*elb.PolicyAttribute - lbspOpts.PolicyAttributes = expandPolicyAttributes(v.(*schema.Set).List()) + lbspOpts.PolicyAttributes, err = expandPolicyAttributes(v.(*schema.Set).List()) + if err != nil { + return err + } } log.Printf("[DEBUG] Load Balancer Policy opts: %#v", lbspOpts) diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index db4e548a0d..0c9fb77ce2 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -1450,7 +1450,7 @@ func (s setMap) MapList() []map[string]interface{} { // Takes the result of flatmap.Expand for an array of policy attributes and // returns ELB API compatible objects -func expandPolicyAttributes(configured []interface{}) []*elb.PolicyAttribute { +func expandPolicyAttributes(configured []interface{}) ([]*elb.PolicyAttribute, error) { attributes := make([]*elb.PolicyAttribute, 0, len(configured)) // Loop over our configured attributes and create @@ -1467,7 +1467,7 @@ func expandPolicyAttributes(configured []interface{}) []*elb.PolicyAttribute { } - return attributes + return attributes, nil } // Flattens an array of PolicyAttributes into a []interface{} diff --git a/builtin/providers/aws/structure_test.go b/builtin/providers/aws/structure_test.go index d83e458a40..f80c4bb320 100644 --- a/builtin/providers/aws/structure_test.go +++ b/builtin/providers/aws/structure_test.go @@ -1012,3 +1012,107 @@ func TestFlattenApiGatewayStageKeys(t *testing.T) { } } } + +func TestExpandPolicyAttributes(t *testing.T) { + expanded := []interface{}{ + map[string]interface{}{ + "name": "Protocol-TLSv1", + "value": "false", + }, + map[string]interface{}{ + "name": "Protocol-TLSv1.1", + "value": "false", + }, + map[string]interface{}{ + "name": "Protocol-TLSv1.2", + "value": "true", + }, + } + attributes, err := expandPolicyAttributes(expanded) + if err != nil { + t.Fatalf("bad: %#v", err) + } + + if len(attributes) != 3 { + t.Fatalf("expected number of attributes to be 3, but got %s", len(attributes)) + } + + expected := &elb.PolicyAttribute{ + AttributeName: aws.String("Protocol-TLSv1.2"), + AttributeValue: aws.String("true"), + } + + if !reflect.DeepEqual(attributes[2], expected) { + t.Fatalf( + "Got:\n\n%#v\n\nExpected:\n\n%#v\n", + attributes[2], + expected) + } +} + +func TestExpandPolicyAttributes_invalid(t *testing.T) { + expanded := []interface{}{ + map[string]interface{}{ + "name": "Protocol-TLSv1.2", + "value": "true", + }, + } + attributes, err := expandPolicyAttributes(expanded) + if err != nil { + t.Fatalf("bad: %#v", err) + } + + expected := &elb.PolicyAttribute{ + AttributeName: aws.String("Protocol-TLSv1.2"), + AttributeValue: aws.String("false"), + } + + if reflect.DeepEqual(attributes[0], expected) { + t.Fatalf( + "Got:\n\n%#v\n\nExpected:\n\n%#v\n", + attributes[0], + expected) + } +} + +func TestExpandPolicyAttributes_empty(t *testing.T) { + var expanded []interface{} + + attributes, err := expandPolicyAttributes(expanded) + if err != nil { + t.Fatalf("bad: %#v", err) + } + + if len(attributes) != 0 { + t.Fatalf("expected number of attributes to be 0, but got %s", len(attributes)) + } +} + +func TestFlattenPolicyAttributes(t *testing.T) { + cases := []struct { + Input []*elb.PolicyAttributeDescription + Output []interface{} + }{ + { + Input: []*elb.PolicyAttributeDescription{ + &elb.PolicyAttributeDescription{ + AttributeName: aws.String("Protocol-TLSv1.2"), + AttributeValue: aws.String("true"), + }, + }, + Output: []interface{}{ + map[string]string{ + "name": "Protocol-TLSv1.2", + "value": "true", + }, + }, + }, + } + + for _, tc := range cases { + output := flattenPolicyAttributes(tc.Input) + if !reflect.DeepEqual(output, tc.Output) { + t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) + } + } +}