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

     1  package aws
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/flatmap"
     8  	"github.com/mitchellh/goamz/ec2"
     9  	"github.com/mitchellh/goamz/elb"
    10  )
    11  
    12  // Returns test configuration
    13  func testConf() map[string]string {
    14  	return map[string]string{
    15  		"listener.#":                   "1",
    16  		"listener.0.lb_port":           "80",
    17  		"listener.0.lb_protocol":       "http",
    18  		"listener.0.instance_port":     "8000",
    19  		"listener.0.instance_protocol": "http",
    20  		"availability_zones.#":         "2",
    21  		"availability_zones.0":         "us-east-1a",
    22  		"availability_zones.1":         "us-east-1b",
    23  		"ingress.#":                    "1",
    24  		"ingress.0.protocol":           "icmp",
    25  		"ingress.0.from_port":          "1",
    26  		"ingress.0.to_port":            "-1",
    27  		"ingress.0.cidr_blocks.#":      "1",
    28  		"ingress.0.cidr_blocks.0":      "0.0.0.0/0",
    29  		"ingress.0.security_groups.#":  "1",
    30  		"ingress.0.security_groups.0":  "sg-11111",
    31  	}
    32  }
    33  
    34  func Test_expandIPPerms(t *testing.T) {
    35  	expanded := flatmap.Expand(testConf(), "ingress").([]interface{})
    36  	perms, err := expandIPPerms(expanded)
    37  
    38  	if err != nil {
    39  		t.Fatalf("bad: %#v", err)
    40  	}
    41  	expected := ec2.IPPerm{
    42  		Protocol:  "icmp",
    43  		FromPort:  1,
    44  		ToPort:    -1,
    45  		SourceIPs: []string{"0.0.0.0/0"},
    46  		SourceGroups: []ec2.UserSecurityGroup{
    47  			ec2.UserSecurityGroup{
    48  				Id: "sg-11111",
    49  			},
    50  		},
    51  	}
    52  
    53  	if !reflect.DeepEqual(perms[0], expected) {
    54  		t.Fatalf(
    55  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
    56  			perms[0],
    57  			expected)
    58  	}
    59  
    60  }
    61  
    62  func Test_flattenIPPerms(t *testing.T) {
    63  	cases := []struct {
    64  		Input  []ec2.IPPerm
    65  		Output []map[string]interface{}
    66  	}{
    67  		{
    68  			Input: []ec2.IPPerm{
    69  				ec2.IPPerm{
    70  					Protocol:  "icmp",
    71  					FromPort:  1,
    72  					ToPort:    -1,
    73  					SourceIPs: []string{"0.0.0.0/0"},
    74  					SourceGroups: []ec2.UserSecurityGroup{
    75  						ec2.UserSecurityGroup{
    76  							Id: "sg-11111",
    77  						},
    78  					},
    79  				},
    80  			},
    81  
    82  			Output: []map[string]interface{}{
    83  				map[string]interface{}{
    84  					"protocol":        "icmp",
    85  					"from_port":       1,
    86  					"to_port":         -1,
    87  					"cidr_blocks":     []string{"0.0.0.0/0"},
    88  					"security_groups": []string{"sg-11111"},
    89  				},
    90  			},
    91  		},
    92  
    93  		{
    94  			Input: []ec2.IPPerm{
    95  				ec2.IPPerm{
    96  					Protocol:     "icmp",
    97  					FromPort:     1,
    98  					ToPort:       -1,
    99  					SourceIPs:    []string{"0.0.0.0/0"},
   100  					SourceGroups: nil,
   101  				},
   102  			},
   103  
   104  			Output: []map[string]interface{}{
   105  				map[string]interface{}{
   106  					"protocol":    "icmp",
   107  					"from_port":   1,
   108  					"to_port":     -1,
   109  					"cidr_blocks": []string{"0.0.0.0/0"},
   110  				},
   111  			},
   112  		},
   113  	}
   114  
   115  	for _, tc := range cases {
   116  		output := flattenIPPerms(tc.Input)
   117  		if !reflect.DeepEqual(output, tc.Output) {
   118  			t.Fatalf("Input:\n\n%#v\n\nOutput:\n\n%#v", tc.Input, output)
   119  		}
   120  	}
   121  }
   122  
   123  func Test_expandListeners(t *testing.T) {
   124  	expanded := flatmap.Expand(testConf(), "listener").([]interface{})
   125  	listeners, err := expandListeners(expanded)
   126  	if err != nil {
   127  		t.Fatalf("bad: %#v", err)
   128  	}
   129  
   130  	expected := elb.Listener{
   131  		InstancePort:     8000,
   132  		LoadBalancerPort: 80,
   133  		InstanceProtocol: "http",
   134  		Protocol:         "http",
   135  	}
   136  
   137  	if !reflect.DeepEqual(listeners[0], expected) {
   138  		t.Fatalf(
   139  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   140  			listeners[0],
   141  			expected)
   142  	}
   143  
   144  }
   145  
   146  func Test_expandStringList(t *testing.T) {
   147  	expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{})
   148  	stringList := expandStringList(expanded)
   149  	expected := []string{
   150  		"us-east-1a",
   151  		"us-east-1b",
   152  	}
   153  
   154  	if !reflect.DeepEqual(stringList, expected) {
   155  		t.Fatalf(
   156  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   157  			stringList,
   158  			expected)
   159  	}
   160  
   161  }
   162  
   163  func Test_expandStringListWildcard(t *testing.T) {
   164  	stringList := expandStringList([]interface{}{"us-east-1a,us-east-1b"})
   165  	expected := []string{
   166  		"us-east-1a",
   167  		"us-east-1b",
   168  	}
   169  
   170  	if !reflect.DeepEqual(stringList, expected) {
   171  		t.Fatalf(
   172  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   173  			stringList,
   174  			expected)
   175  	}
   176  
   177  }