Added tests for expandPolicyAttributes/flattenPolicyAttributes

This commit is contained in:
Mosley, Franklin 2016-03-15 02:20:50 -05:00 committed by Kraig Amador
parent eb0cd14f41
commit 6d7933b41a
3 changed files with 111 additions and 3 deletions

View File

@ -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)

View File

@ -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{}

View File

@ -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)
}
}
}