terraform/builtin/providers/aws/structure_test.go

334 lines
7.2 KiB
Go
Raw Normal View History

package aws
import (
"reflect"
"testing"
"github.com/hashicorp/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/elb"
"github.com/hashicorp/aws-sdk-go/gen/rds"
"github.com/hashicorp/terraform/flatmap"
2014-10-21 19:57:55 +02:00
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/ec2"
)
// Returns test configuration
func testConf() map[string]string {
return map[string]string{
"listener.#": "1",
"listener.0.lb_port": "80",
"listener.0.lb_protocol": "http",
"listener.0.instance_port": "8000",
"listener.0.instance_protocol": "http",
"availability_zones.#": "2",
"availability_zones.0": "us-east-1a",
"availability_zones.1": "us-east-1b",
"ingress.#": "1",
"ingress.0.protocol": "icmp",
"ingress.0.from_port": "1",
"ingress.0.to_port": "-1",
"ingress.0.cidr_blocks.#": "1",
"ingress.0.cidr_blocks.0": "0.0.0.0/0",
"ingress.0.security_groups.#": "2",
"ingress.0.security_groups.0": "sg-11111",
"ingress.0.security_groups.1": "foo/sg-22222",
}
}
func TestExpandIPPerms(t *testing.T) {
2014-10-21 19:57:55 +02:00
hash := func(v interface{}) int {
return hashcode.String(v.(string))
}
expanded := []interface{}{
map[string]interface{}{
"protocol": "icmp",
"from_port": 1,
"to_port": -1,
"cidr_blocks": []interface{}{"0.0.0.0/0"},
2014-10-21 19:57:55 +02:00
"security_groups": schema.NewSet(hash, []interface{}{
"sg-11111",
"foo/sg-22222",
2014-10-21 19:57:55 +02:00
}),
},
map[string]interface{}{
"protocol": "icmp",
"from_port": 1,
"to_port": -1,
"self": true,
},
2014-07-25 00:50:18 +02:00
}
perms := expandIPPerms("foo", expanded)
expected := []ec2.IPPerm{
ec2.IPPerm{
Protocol: "icmp",
FromPort: 1,
ToPort: -1,
SourceIPs: []string{"0.0.0.0/0"},
SourceGroups: []ec2.UserSecurityGroup{
ec2.UserSecurityGroup{
OwnerId: "foo",
Id: "sg-22222",
},
2014-10-21 19:57:55 +02:00
ec2.UserSecurityGroup{
Id: "sg-11111",
},
},
},
ec2.IPPerm{
Protocol: "icmp",
FromPort: 1,
ToPort: -1,
SourceGroups: []ec2.UserSecurityGroup{
ec2.UserSecurityGroup{
Id: "foo",
},
},
},
}
if !reflect.DeepEqual(perms, expected) {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
perms[0],
expected)
}
}
func TestFlattenIPPerms(t *testing.T) {
cases := []struct {
Input []ec2.IPPerm
Output []map[string]interface{}
}{
{
Input: []ec2.IPPerm{
ec2.IPPerm{
Protocol: "icmp",
FromPort: 1,
ToPort: -1,
SourceIPs: []string{"0.0.0.0/0"},
SourceGroups: []ec2.UserSecurityGroup{
ec2.UserSecurityGroup{
Id: "sg-11111",
},
},
},
},
Output: []map[string]interface{}{
map[string]interface{}{
"protocol": "icmp",
"from_port": 1,
"to_port": -1,
"cidr_blocks": []string{"0.0.0.0/0"},
"security_groups": []string{"sg-11111"},
},
},
},
{
Input: []ec2.IPPerm{
ec2.IPPerm{
Protocol: "icmp",
FromPort: 1,
ToPort: -1,
SourceIPs: []string{"0.0.0.0/0"},
SourceGroups: nil,
},
},
Output: []map[string]interface{}{
map[string]interface{}{
"protocol": "icmp",
"from_port": 1,
"to_port": -1,
"cidr_blocks": []string{"0.0.0.0/0"},
},
},
},
{
Input: []ec2.IPPerm{
ec2.IPPerm{
Protocol: "icmp",
FromPort: 1,
ToPort: -1,
SourceIPs: nil,
},
},
Output: []map[string]interface{}{
map[string]interface{}{
"protocol": "icmp",
"from_port": 1,
"to_port": -1,
},
},
},
}
for _, tc := range cases {
output := flattenIPPerms(tc.Input)
if !reflect.DeepEqual(output, tc.Output) {
t.Fatalf("Input:\n\n%#v\n\nOutput:\n\n%#v", tc.Input, output)
}
}
}
func TestExpandListeners(t *testing.T) {
2014-10-10 19:26:48 +02:00
expanded := []interface{}{
map[string]interface{}{
2014-10-10 23:50:35 +02:00
"instance_port": 8000,
"lb_port": 80,
2014-10-10 19:26:48 +02:00
"instance_protocol": "http",
2014-10-10 23:50:35 +02:00
"lb_protocol": "http",
2014-10-10 19:26:48 +02:00
},
}
2014-07-25 00:50:18 +02:00
listeners, err := expandListeners(expanded)
if err != nil {
t.Fatalf("bad: %#v", err)
}
expected := elb.Listener{
InstancePort: aws.Integer(8000),
LoadBalancerPort: aws.Integer(80),
InstanceProtocol: aws.String("http"),
Protocol: aws.String("http"),
}
if !reflect.DeepEqual(listeners[0], expected) {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
listeners[0],
expected)
}
}
func TestFlattenHealthCheck(t *testing.T) {
cases := []struct {
Input elb.HealthCheck
Output []map[string]interface{}
}{
{
Input: elb.HealthCheck{
UnhealthyThreshold: aws.Integer(10),
HealthyThreshold: aws.Integer(10),
Target: aws.String("HTTP:80/"),
Timeout: aws.Integer(30),
Interval: aws.Integer(30),
},
Output: []map[string]interface{}{
map[string]interface{}{
"unhealthy_threshold": 10,
"healthy_threshold": 10,
"target": "HTTP:80/",
"timeout": 30,
"interval": 30,
},
},
},
}
for _, tc := range cases {
output := flattenHealthCheck(&tc.Input)
if !reflect.DeepEqual(output, tc.Output) {
t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
}
}
}
func TestExpandStringList(t *testing.T) {
expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{})
stringList := expandStringList(expanded)
expected := []string{
"us-east-1a",
"us-east-1b",
}
if !reflect.DeepEqual(stringList, expected) {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
stringList,
expected)
}
}
func TestExpandParameters(t *testing.T) {
expanded := []interface{}{
map[string]interface{}{
"name": "character_set_client",
"value": "utf8",
"apply_method": "immediate",
},
}
parameters, err := expandParameters(expanded)
if err != nil {
t.Fatalf("bad: %#v", err)
}
expected := rds.Parameter{
ParameterName: aws.String("character_set_client"),
ParameterValue: aws.String("utf8"),
ApplyMethod: aws.String("immediate"),
}
if !reflect.DeepEqual(parameters[0], expected) {
t.Fatalf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
parameters[0],
expected)
}
}
func TestFlattenParameters(t *testing.T) {
cases := []struct {
Input []rds.Parameter
Output []map[string]interface{}
}{
{
Input: []rds.Parameter{
rds.Parameter{
ParameterName: aws.String("character_set_client"),
ParameterValue: aws.String("utf8"),
},
},
Output: []map[string]interface{}{
map[string]interface{}{
2015-02-20 19:22:26 +01:00
"name": "character_set_client",
"value": "utf8",
},
},
},
}
for _, tc := range cases {
output := flattenParameters(tc.Input)
if !reflect.DeepEqual(output, tc.Output) {
t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
}
}
}
func TestExpandInstanceString(t *testing.T) {
expected := []elb.Instance{
elb.Instance{aws.String("test-one")},
elb.Instance{aws.String("test-two")},
}
ids := []interface{}{
"test-one",
"test-two",
}
expanded := expandInstanceString(ids)
if !reflect.DeepEqual(expanded, expected) {
t.Fatalf("Expand Instance String output did not match.\nGot:\n%#v\n\nexpected:\n%#v", expanded, expected)
}
}