2014-07-03 00:55:28 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-03-10 22:33:15 +01:00
|
|
|
"log"
|
2014-07-03 00:55:28 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-02-26 16:33:33 +01:00
|
|
|
"github.com/hashicorp/aws-sdk-go/aws"
|
2015-03-10 22:33:15 +01:00
|
|
|
awsEC2 "github.com/hashicorp/aws-sdk-go/gen/ec2"
|
2015-03-02 16:44:06 +01:00
|
|
|
"github.com/hashicorp/aws-sdk-go/gen/elb"
|
2015-02-26 16:33:33 +01:00
|
|
|
"github.com/hashicorp/aws-sdk-go/gen/rds"
|
2014-07-03 00:55:28 +02:00
|
|
|
"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"
|
2014-07-03 00:55:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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",
|
2014-07-03 01:57:57 +02:00
|
|
|
"availability_zones.#": "2",
|
|
|
|
"availability_zones.0": "us-east-1a",
|
|
|
|
"availability_zones.1": "us-east-1b",
|
2014-07-17 02:13:16 +02:00
|
|
|
"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",
|
2014-08-08 20:42:32 +02:00
|
|
|
"ingress.0.security_groups.#": "2",
|
2014-07-17 02:13:16 +02:00
|
|
|
"ingress.0.security_groups.0": "sg-11111",
|
2014-08-08 20:42:32 +02:00
|
|
|
"ingress.0.security_groups.1": "foo/sg-22222",
|
2014-07-03 00:55:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:47:06 +01:00
|
|
|
func TestExpandIPPerms(t *testing.T) {
|
2014-10-21 19:57:55 +02:00
|
|
|
hash := func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:54:43 +02:00
|
|
|
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{}{
|
2014-08-20 19:54:43 +02:00
|
|
|
"sg-11111",
|
|
|
|
"foo/sg-22222",
|
2014-10-21 19:57:55 +02:00
|
|
|
}),
|
2014-08-20 19:54:43 +02:00
|
|
|
},
|
2014-09-30 23:19:16 +02:00
|
|
|
map[string]interface{}{
|
|
|
|
"protocol": "icmp",
|
|
|
|
"from_port": 1,
|
|
|
|
"to_port": -1,
|
|
|
|
"self": true,
|
|
|
|
},
|
2014-07-25 00:50:18 +02:00
|
|
|
}
|
2014-09-30 23:19:16 +02:00
|
|
|
perms := expandIPPerms("foo", expanded)
|
2014-08-20 19:54:43 +02:00
|
|
|
|
2015-03-10 22:33:15 +01:00
|
|
|
log.Printf("wtf is perms:\n%#v", perms)
|
|
|
|
|
|
|
|
expected := []awsEC2.IPPermission{
|
|
|
|
awsEC2.IPPermission{
|
|
|
|
IPProtocol: aws.String("icmp"),
|
|
|
|
FromPort: aws.Integer(1),
|
|
|
|
ToPort: aws.Integer(-1),
|
|
|
|
IPRanges: []awsEC2.IPRange{awsEC2.IPRange{aws.String("0.0.0.0/0")}},
|
|
|
|
UserIDGroupPairs: []awsEC2.UserIDGroupPair{
|
|
|
|
awsEC2.UserIDGroupPair{
|
|
|
|
UserID: aws.String("foo"),
|
|
|
|
GroupID: aws.String("sg-22222"),
|
2014-09-30 23:19:16 +02:00
|
|
|
},
|
2015-03-10 22:33:15 +01:00
|
|
|
awsEC2.UserIDGroupPair{
|
|
|
|
GroupID: aws.String("sg-22222"),
|
2014-10-21 19:57:55 +02:00
|
|
|
},
|
2014-07-08 22:33:59 +02:00
|
|
|
},
|
2014-09-30 23:19:16 +02:00
|
|
|
},
|
2015-03-10 22:33:15 +01:00
|
|
|
awsEC2.IPPermission{
|
|
|
|
IPProtocol: aws.String("icmp"),
|
|
|
|
FromPort: aws.Integer(1),
|
|
|
|
ToPort: aws.Integer(-1),
|
|
|
|
UserIDGroupPairs: []awsEC2.UserIDGroupPair{
|
|
|
|
awsEC2.UserIDGroupPair{
|
|
|
|
UserID: aws.String("foo"),
|
2014-09-30 23:19:16 +02:00
|
|
|
},
|
2014-08-08 20:42:32 +02:00
|
|
|
},
|
2014-07-29 16:02:49 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-10 22:33:15 +01:00
|
|
|
exp := expected[0]
|
|
|
|
perm := perms[0]
|
|
|
|
|
|
|
|
if *exp.FromPort != *perm.FromPort {
|
2014-07-29 16:02:49 +02:00
|
|
|
t.Fatalf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
2015-03-10 22:33:15 +01:00
|
|
|
*perm.FromPort,
|
|
|
|
*exp.FromPort)
|
2014-07-29 16:02:49 +02:00
|
|
|
}
|
|
|
|
|
2015-03-10 22:33:15 +01:00
|
|
|
if *exp.IPRanges[0].CIDRIP != *perm.IPRanges[0].CIDRIP {
|
|
|
|
t.Fatalf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
|
|
|
*perm.IPRanges[0].CIDRIP,
|
|
|
|
*exp.IPRanges[0].CIDRIP)
|
2014-07-17 02:13:16 +02:00
|
|
|
}
|
|
|
|
|
2015-03-10 22:33:15 +01:00
|
|
|
if *exp.UserIDGroupPairs[0].UserID != *perm.UserIDGroupPairs[0].UserID {
|
|
|
|
t.Fatalf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
|
|
|
*perm.UserIDGroupPairs[0].UserID,
|
|
|
|
*exp.UserIDGroupPairs[0].UserID)
|
2014-07-17 02:13:16 +02:00
|
|
|
}
|
2015-03-10 22:33:15 +01:00
|
|
|
|
2014-07-17 02:13:16 +02:00
|
|
|
}
|
|
|
|
|
2015-03-02 18:47:06 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-07-03 00:55:28 +02:00
|
|
|
expected := elb.Listener{
|
2015-03-02 16:44:06 +01:00
|
|
|
InstancePort: aws.Integer(8000),
|
|
|
|
LoadBalancerPort: aws.Integer(80),
|
|
|
|
InstanceProtocol: aws.String("http"),
|
|
|
|
Protocol: aws.String("http"),
|
2014-07-03 00:55:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(listeners[0], expected) {
|
|
|
|
t.Fatalf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
|
|
|
listeners[0],
|
|
|
|
expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-07-03 01:57:57 +02:00
|
|
|
|
2015-03-02 18:47:06 +01:00
|
|
|
func TestFlattenHealthCheck(t *testing.T) {
|
2014-07-30 16:15:22 +02:00
|
|
|
cases := []struct {
|
|
|
|
Input elb.HealthCheck
|
|
|
|
Output []map[string]interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: elb.HealthCheck{
|
2015-03-02 16:44:06 +01:00
|
|
|
UnhealthyThreshold: aws.Integer(10),
|
|
|
|
HealthyThreshold: aws.Integer(10),
|
|
|
|
Target: aws.String("HTTP:80/"),
|
|
|
|
Timeout: aws.Integer(30),
|
|
|
|
Interval: aws.Integer(30),
|
2014-07-30 16:15:22 +02:00
|
|
|
},
|
|
|
|
Output: []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"unhealthy_threshold": 10,
|
|
|
|
"healthy_threshold": 10,
|
|
|
|
"target": "HTTP:80/",
|
|
|
|
"timeout": 30,
|
|
|
|
"interval": 30,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2015-03-02 16:44:06 +01:00
|
|
|
output := flattenHealthCheck(&tc.Input)
|
2014-07-30 16:15:22 +02:00
|
|
|
if !reflect.DeepEqual(output, tc.Output) {
|
|
|
|
t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:47:06 +01:00
|
|
|
func TestExpandStringList(t *testing.T) {
|
2014-07-03 01:57:57 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-10-22 23:22:30 +02:00
|
|
|
|
2015-03-02 18:47:06 +01:00
|
|
|
func TestExpandParameters(t *testing.T) {
|
2014-10-22 23:22:30 +02:00
|
|
|
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{
|
2015-02-26 16:33:33 +01:00
|
|
|
ParameterName: aws.String("character_set_client"),
|
|
|
|
ParameterValue: aws.String("utf8"),
|
|
|
|
ApplyMethod: aws.String("immediate"),
|
2014-10-22 23:22:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(parameters[0], expected) {
|
|
|
|
t.Fatalf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
|
|
|
parameters[0],
|
|
|
|
expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:47:06 +01:00
|
|
|
func TestFlattenParameters(t *testing.T) {
|
2014-10-22 23:22:30 +02:00
|
|
|
cases := []struct {
|
|
|
|
Input []rds.Parameter
|
|
|
|
Output []map[string]interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: []rds.Parameter{
|
|
|
|
rds.Parameter{
|
2015-02-26 16:33:33 +01:00
|
|
|
ParameterName: aws.String("character_set_client"),
|
|
|
|
ParameterValue: aws.String("utf8"),
|
2014-10-22 23:22:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Output: []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
2015-02-20 19:22:26 +01:00
|
|
|
"name": "character_set_client",
|
|
|
|
"value": "utf8",
|
2014-10-22 23:22:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 17:00:45 +01:00
|
|
|
|
2015-03-02 18:47:06 +01:00
|
|
|
func TestExpandInstanceString(t *testing.T) {
|
2015-03-02 17:00:45 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|