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