github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/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.#": "2", 30 "ingress.0.security_groups.0": "sg-11111", 31 "ingress.0.security_groups.1": "foo/sg-22222", 32 } 33 } 34 35 func Test_expandIPPerms(t *testing.T) { 36 expanded := []interface{}{ 37 map[string]interface{}{ 38 "protocol": "icmp", 39 "from_port": 1, 40 "to_port": -1, 41 "cidr_blocks": []interface{}{"0.0.0.0/0"}, 42 "security_groups": []interface{}{ 43 "sg-11111", 44 "foo/sg-22222", 45 }, 46 }, 47 map[string]interface{}{ 48 "protocol": "icmp", 49 "from_port": 1, 50 "to_port": -1, 51 "self": true, 52 }, 53 } 54 perms := expandIPPerms("foo", expanded) 55 56 expected := []ec2.IPPerm{ 57 ec2.IPPerm{ 58 Protocol: "icmp", 59 FromPort: 1, 60 ToPort: -1, 61 SourceIPs: []string{"0.0.0.0/0"}, 62 SourceGroups: []ec2.UserSecurityGroup{ 63 ec2.UserSecurityGroup{ 64 Id: "sg-11111", 65 }, 66 ec2.UserSecurityGroup{ 67 OwnerId: "foo", 68 Id: "sg-22222", 69 }, 70 }, 71 }, 72 ec2.IPPerm{ 73 Protocol: "icmp", 74 FromPort: 1, 75 ToPort: -1, 76 SourceGroups: []ec2.UserSecurityGroup{ 77 ec2.UserSecurityGroup{ 78 Id: "foo", 79 }, 80 }, 81 }, 82 } 83 84 if !reflect.DeepEqual(perms, expected) { 85 t.Fatalf( 86 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 87 perms[0], 88 expected) 89 } 90 91 } 92 93 func Test_flattenIPPerms(t *testing.T) { 94 cases := []struct { 95 Input []ec2.IPPerm 96 Output []map[string]interface{} 97 }{ 98 { 99 Input: []ec2.IPPerm{ 100 ec2.IPPerm{ 101 Protocol: "icmp", 102 FromPort: 1, 103 ToPort: -1, 104 SourceIPs: []string{"0.0.0.0/0"}, 105 SourceGroups: []ec2.UserSecurityGroup{ 106 ec2.UserSecurityGroup{ 107 Id: "sg-11111", 108 }, 109 }, 110 }, 111 }, 112 113 Output: []map[string]interface{}{ 114 map[string]interface{}{ 115 "protocol": "icmp", 116 "from_port": 1, 117 "to_port": -1, 118 "cidr_blocks": []string{"0.0.0.0/0"}, 119 "security_groups": []string{"sg-11111"}, 120 }, 121 }, 122 }, 123 124 { 125 Input: []ec2.IPPerm{ 126 ec2.IPPerm{ 127 Protocol: "icmp", 128 FromPort: 1, 129 ToPort: -1, 130 SourceIPs: []string{"0.0.0.0/0"}, 131 SourceGroups: nil, 132 }, 133 }, 134 135 Output: []map[string]interface{}{ 136 map[string]interface{}{ 137 "protocol": "icmp", 138 "from_port": 1, 139 "to_port": -1, 140 "cidr_blocks": []string{"0.0.0.0/0"}, 141 }, 142 }, 143 }, 144 { 145 Input: []ec2.IPPerm{ 146 ec2.IPPerm{ 147 Protocol: "icmp", 148 FromPort: 1, 149 ToPort: -1, 150 SourceIPs: nil, 151 }, 152 }, 153 154 Output: []map[string]interface{}{ 155 map[string]interface{}{ 156 "protocol": "icmp", 157 "from_port": 1, 158 "to_port": -1, 159 }, 160 }, 161 }, 162 } 163 164 for _, tc := range cases { 165 output := flattenIPPerms(tc.Input) 166 if !reflect.DeepEqual(output, tc.Output) { 167 t.Fatalf("Input:\n\n%#v\n\nOutput:\n\n%#v", tc.Input, output) 168 } 169 } 170 } 171 172 func Test_expandListeners(t *testing.T) { 173 expanded := []interface{}{ 174 map[string]interface{}{ 175 "instance_port": 8000, 176 "lb_port": 80, 177 "instance_protocol": "http", 178 "lb_protocol": "http", 179 }, 180 } 181 listeners, err := expandListeners(expanded) 182 if err != nil { 183 t.Fatalf("bad: %#v", err) 184 } 185 186 expected := elb.Listener{ 187 InstancePort: 8000, 188 LoadBalancerPort: 80, 189 InstanceProtocol: "http", 190 Protocol: "http", 191 } 192 193 if !reflect.DeepEqual(listeners[0], expected) { 194 t.Fatalf( 195 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 196 listeners[0], 197 expected) 198 } 199 200 } 201 202 func Test_flattenHealthCheck(t *testing.T) { 203 cases := []struct { 204 Input elb.HealthCheck 205 Output []map[string]interface{} 206 }{ 207 { 208 Input: elb.HealthCheck{ 209 UnhealthyThreshold: 10, 210 HealthyThreshold: 10, 211 Target: "HTTP:80/", 212 Timeout: 30, 213 Interval: 30, 214 }, 215 Output: []map[string]interface{}{ 216 map[string]interface{}{ 217 "unhealthy_threshold": 10, 218 "healthy_threshold": 10, 219 "target": "HTTP:80/", 220 "timeout": 30, 221 "interval": 30, 222 }, 223 }, 224 }, 225 } 226 227 for _, tc := range cases { 228 output := flattenHealthCheck(tc.Input) 229 if !reflect.DeepEqual(output, tc.Output) { 230 t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) 231 } 232 } 233 } 234 235 func Test_expandStringList(t *testing.T) { 236 expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{}) 237 stringList := expandStringList(expanded) 238 expected := []string{ 239 "us-east-1a", 240 "us-east-1b", 241 } 242 243 if !reflect.DeepEqual(stringList, expected) { 244 t.Fatalf( 245 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 246 stringList, 247 expected) 248 } 249 250 }