github.com/jgadling/terraform@v0.3.8-0.20150227214559-abd68c2c87bc/builtin/providers/aws/structure_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/aws-sdk-go/aws"
     8  	"github.com/hashicorp/aws-sdk-go/gen/rds"
     9  	"github.com/hashicorp/terraform/flatmap"
    10  	"github.com/hashicorp/terraform/helper/hashcode"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  	"github.com/mitchellh/goamz/ec2"
    13  	"github.com/mitchellh/goamz/elb"
    14  )
    15  
    16  // Returns test configuration
    17  func testConf() map[string]string {
    18  	return map[string]string{
    19  		"listener.#":                   "1",
    20  		"listener.0.lb_port":           "80",
    21  		"listener.0.lb_protocol":       "http",
    22  		"listener.0.instance_port":     "8000",
    23  		"listener.0.instance_protocol": "http",
    24  		"availability_zones.#":         "2",
    25  		"availability_zones.0":         "us-east-1a",
    26  		"availability_zones.1":         "us-east-1b",
    27  		"ingress.#":                    "1",
    28  		"ingress.0.protocol":           "icmp",
    29  		"ingress.0.from_port":          "1",
    30  		"ingress.0.to_port":            "-1",
    31  		"ingress.0.cidr_blocks.#":      "1",
    32  		"ingress.0.cidr_blocks.0":      "0.0.0.0/0",
    33  		"ingress.0.security_groups.#":  "2",
    34  		"ingress.0.security_groups.0":  "sg-11111",
    35  		"ingress.0.security_groups.1":  "foo/sg-22222",
    36  	}
    37  }
    38  
    39  func Test_expandIPPerms(t *testing.T) {
    40  	hash := func(v interface{}) int {
    41  		return hashcode.String(v.(string))
    42  	}
    43  
    44  	expanded := []interface{}{
    45  		map[string]interface{}{
    46  			"protocol":    "icmp",
    47  			"from_port":   1,
    48  			"to_port":     -1,
    49  			"cidr_blocks": []interface{}{"0.0.0.0/0"},
    50  			"security_groups": schema.NewSet(hash, []interface{}{
    51  				"sg-11111",
    52  				"foo/sg-22222",
    53  			}),
    54  		},
    55  		map[string]interface{}{
    56  			"protocol":  "icmp",
    57  			"from_port": 1,
    58  			"to_port":   -1,
    59  			"self":      true,
    60  		},
    61  	}
    62  	perms := expandIPPerms("foo", expanded)
    63  
    64  	expected := []ec2.IPPerm{
    65  		ec2.IPPerm{
    66  			Protocol:  "icmp",
    67  			FromPort:  1,
    68  			ToPort:    -1,
    69  			SourceIPs: []string{"0.0.0.0/0"},
    70  			SourceGroups: []ec2.UserSecurityGroup{
    71  				ec2.UserSecurityGroup{
    72  					OwnerId: "foo",
    73  					Id:      "sg-22222",
    74  				},
    75  				ec2.UserSecurityGroup{
    76  					Id: "sg-11111",
    77  				},
    78  			},
    79  		},
    80  		ec2.IPPerm{
    81  			Protocol: "icmp",
    82  			FromPort: 1,
    83  			ToPort:   -1,
    84  			SourceGroups: []ec2.UserSecurityGroup{
    85  				ec2.UserSecurityGroup{
    86  					Id: "foo",
    87  				},
    88  			},
    89  		},
    90  	}
    91  
    92  	if !reflect.DeepEqual(perms, expected) {
    93  		t.Fatalf(
    94  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
    95  			perms[0],
    96  			expected)
    97  	}
    98  
    99  }
   100  
   101  func Test_flattenIPPerms(t *testing.T) {
   102  	cases := []struct {
   103  		Input  []ec2.IPPerm
   104  		Output []map[string]interface{}
   105  	}{
   106  		{
   107  			Input: []ec2.IPPerm{
   108  				ec2.IPPerm{
   109  					Protocol:  "icmp",
   110  					FromPort:  1,
   111  					ToPort:    -1,
   112  					SourceIPs: []string{"0.0.0.0/0"},
   113  					SourceGroups: []ec2.UserSecurityGroup{
   114  						ec2.UserSecurityGroup{
   115  							Id: "sg-11111",
   116  						},
   117  					},
   118  				},
   119  			},
   120  
   121  			Output: []map[string]interface{}{
   122  				map[string]interface{}{
   123  					"protocol":        "icmp",
   124  					"from_port":       1,
   125  					"to_port":         -1,
   126  					"cidr_blocks":     []string{"0.0.0.0/0"},
   127  					"security_groups": []string{"sg-11111"},
   128  				},
   129  			},
   130  		},
   131  
   132  		{
   133  			Input: []ec2.IPPerm{
   134  				ec2.IPPerm{
   135  					Protocol:     "icmp",
   136  					FromPort:     1,
   137  					ToPort:       -1,
   138  					SourceIPs:    []string{"0.0.0.0/0"},
   139  					SourceGroups: nil,
   140  				},
   141  			},
   142  
   143  			Output: []map[string]interface{}{
   144  				map[string]interface{}{
   145  					"protocol":    "icmp",
   146  					"from_port":   1,
   147  					"to_port":     -1,
   148  					"cidr_blocks": []string{"0.0.0.0/0"},
   149  				},
   150  			},
   151  		},
   152  		{
   153  			Input: []ec2.IPPerm{
   154  				ec2.IPPerm{
   155  					Protocol:  "icmp",
   156  					FromPort:  1,
   157  					ToPort:    -1,
   158  					SourceIPs: nil,
   159  				},
   160  			},
   161  
   162  			Output: []map[string]interface{}{
   163  				map[string]interface{}{
   164  					"protocol":  "icmp",
   165  					"from_port": 1,
   166  					"to_port":   -1,
   167  				},
   168  			},
   169  		},
   170  	}
   171  
   172  	for _, tc := range cases {
   173  		output := flattenIPPerms(tc.Input)
   174  		if !reflect.DeepEqual(output, tc.Output) {
   175  			t.Fatalf("Input:\n\n%#v\n\nOutput:\n\n%#v", tc.Input, output)
   176  		}
   177  	}
   178  }
   179  
   180  func Test_expandListeners(t *testing.T) {
   181  	expanded := []interface{}{
   182  		map[string]interface{}{
   183  			"instance_port":     8000,
   184  			"lb_port":           80,
   185  			"instance_protocol": "http",
   186  			"lb_protocol":       "http",
   187  		},
   188  	}
   189  	listeners, err := expandListeners(expanded)
   190  	if err != nil {
   191  		t.Fatalf("bad: %#v", err)
   192  	}
   193  
   194  	expected := elb.Listener{
   195  		InstancePort:     8000,
   196  		LoadBalancerPort: 80,
   197  		InstanceProtocol: "http",
   198  		Protocol:         "http",
   199  	}
   200  
   201  	if !reflect.DeepEqual(listeners[0], expected) {
   202  		t.Fatalf(
   203  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   204  			listeners[0],
   205  			expected)
   206  	}
   207  
   208  }
   209  
   210  func Test_flattenHealthCheck(t *testing.T) {
   211  	cases := []struct {
   212  		Input  elb.HealthCheck
   213  		Output []map[string]interface{}
   214  	}{
   215  		{
   216  			Input: elb.HealthCheck{
   217  				UnhealthyThreshold: 10,
   218  				HealthyThreshold:   10,
   219  				Target:             "HTTP:80/",
   220  				Timeout:            30,
   221  				Interval:           30,
   222  			},
   223  			Output: []map[string]interface{}{
   224  				map[string]interface{}{
   225  					"unhealthy_threshold": 10,
   226  					"healthy_threshold":   10,
   227  					"target":              "HTTP:80/",
   228  					"timeout":             30,
   229  					"interval":            30,
   230  				},
   231  			},
   232  		},
   233  	}
   234  
   235  	for _, tc := range cases {
   236  		output := flattenHealthCheck(tc.Input)
   237  		if !reflect.DeepEqual(output, tc.Output) {
   238  			t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
   239  		}
   240  	}
   241  }
   242  
   243  func Test_expandStringList(t *testing.T) {
   244  	expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{})
   245  	stringList := expandStringList(expanded)
   246  	expected := []string{
   247  		"us-east-1a",
   248  		"us-east-1b",
   249  	}
   250  
   251  	if !reflect.DeepEqual(stringList, expected) {
   252  		t.Fatalf(
   253  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   254  			stringList,
   255  			expected)
   256  	}
   257  
   258  }
   259  
   260  func Test_expandParameters(t *testing.T) {
   261  	expanded := []interface{}{
   262  		map[string]interface{}{
   263  			"name":         "character_set_client",
   264  			"value":        "utf8",
   265  			"apply_method": "immediate",
   266  		},
   267  	}
   268  	parameters, err := expandParameters(expanded)
   269  	if err != nil {
   270  		t.Fatalf("bad: %#v", err)
   271  	}
   272  
   273  	expected := rds.Parameter{
   274  		ParameterName:  aws.String("character_set_client"),
   275  		ParameterValue: aws.String("utf8"),
   276  		ApplyMethod:    aws.String("immediate"),
   277  	}
   278  
   279  	if !reflect.DeepEqual(parameters[0], expected) {
   280  		t.Fatalf(
   281  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   282  			parameters[0],
   283  			expected)
   284  	}
   285  }
   286  
   287  func Test_flattenParameters(t *testing.T) {
   288  	cases := []struct {
   289  		Input  []rds.Parameter
   290  		Output []map[string]interface{}
   291  	}{
   292  		{
   293  			Input: []rds.Parameter{
   294  				rds.Parameter{
   295  					ParameterName:  aws.String("character_set_client"),
   296  					ParameterValue: aws.String("utf8"),
   297  				},
   298  			},
   299  			Output: []map[string]interface{}{
   300  				map[string]interface{}{
   301  					"name":  "character_set_client",
   302  					"value": "utf8",
   303  				},
   304  			},
   305  		},
   306  	}
   307  
   308  	for _, tc := range cases {
   309  		output := flattenParameters(tc.Input)
   310  		if !reflect.DeepEqual(output, tc.Output) {
   311  			t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
   312  		}
   313  	}
   314  }