github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/builtin/providers/aws/structure_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  	"github.com/aws/aws-sdk-go/service/elasticache"
    10  	"github.com/aws/aws-sdk-go/service/elb"
    11  	"github.com/aws/aws-sdk-go/service/rds"
    12  	"github.com/aws/aws-sdk-go/service/route53"
    13  	"github.com/hashicorp/terraform/flatmap"
    14  	"github.com/hashicorp/terraform/helper/schema"
    15  )
    16  
    17  // Returns test configuration
    18  func testConf() map[string]string {
    19  	return map[string]string{
    20  		"listener.#":                   "1",
    21  		"listener.0.lb_port":           "80",
    22  		"listener.0.lb_protocol":       "http",
    23  		"listener.0.instance_port":     "8000",
    24  		"listener.0.instance_protocol": "http",
    25  		"availability_zones.#":         "2",
    26  		"availability_zones.0":         "us-east-1a",
    27  		"availability_zones.1":         "us-east-1b",
    28  		"ingress.#":                    "1",
    29  		"ingress.0.protocol":           "icmp",
    30  		"ingress.0.from_port":          "1",
    31  		"ingress.0.to_port":            "-1",
    32  		"ingress.0.cidr_blocks.#":      "1",
    33  		"ingress.0.cidr_blocks.0":      "0.0.0.0/0",
    34  		"ingress.0.security_groups.#":  "2",
    35  		"ingress.0.security_groups.0":  "sg-11111",
    36  		"ingress.0.security_groups.1":  "foo/sg-22222",
    37  	}
    38  }
    39  
    40  func TestexpandIPPerms(t *testing.T) {
    41  	hash := schema.HashString
    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  	group := &ec2.SecurityGroup{
    62  		GroupId: aws.String("foo"),
    63  		VpcId:   aws.String("bar"),
    64  	}
    65  	perms, err := expandIPPerms(group, expanded)
    66  	if err != nil {
    67  		t.Fatalf("error expanding perms: %v", err)
    68  	}
    69  
    70  	expected := []ec2.IpPermission{
    71  		ec2.IpPermission{
    72  			IpProtocol: aws.String("icmp"),
    73  			FromPort:   aws.Int64(int64(1)),
    74  			ToPort:     aws.Int64(int64(-1)),
    75  			IpRanges:   []*ec2.IpRange{&ec2.IpRange{CidrIp: aws.String("0.0.0.0/0")}},
    76  			UserIdGroupPairs: []*ec2.UserIdGroupPair{
    77  				&ec2.UserIdGroupPair{
    78  					UserId:  aws.String("foo"),
    79  					GroupId: aws.String("sg-22222"),
    80  				},
    81  				&ec2.UserIdGroupPair{
    82  					GroupId: aws.String("sg-22222"),
    83  				},
    84  			},
    85  		},
    86  		ec2.IpPermission{
    87  			IpProtocol: aws.String("icmp"),
    88  			FromPort:   aws.Int64(int64(1)),
    89  			ToPort:     aws.Int64(int64(-1)),
    90  			UserIdGroupPairs: []*ec2.UserIdGroupPair{
    91  				&ec2.UserIdGroupPair{
    92  					UserId: aws.String("foo"),
    93  				},
    94  			},
    95  		},
    96  	}
    97  
    98  	exp := expected[0]
    99  	perm := perms[0]
   100  
   101  	if *exp.FromPort != *perm.FromPort {
   102  		t.Fatalf(
   103  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   104  			*perm.FromPort,
   105  			*exp.FromPort)
   106  	}
   107  
   108  	if *exp.IpRanges[0].CidrIp != *perm.IpRanges[0].CidrIp {
   109  		t.Fatalf(
   110  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   111  			*perm.IpRanges[0].CidrIp,
   112  			*exp.IpRanges[0].CidrIp)
   113  	}
   114  
   115  	if *exp.UserIdGroupPairs[0].UserId != *perm.UserIdGroupPairs[0].UserId {
   116  		t.Fatalf(
   117  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   118  			*perm.UserIdGroupPairs[0].UserId,
   119  			*exp.UserIdGroupPairs[0].UserId)
   120  	}
   121  
   122  }
   123  
   124  func TestExpandIPPerms_NegOneProtocol(t *testing.T) {
   125  	hash := schema.HashString
   126  
   127  	expanded := []interface{}{
   128  		map[string]interface{}{
   129  			"protocol":    "-1",
   130  			"from_port":   0,
   131  			"to_port":     0,
   132  			"cidr_blocks": []interface{}{"0.0.0.0/0"},
   133  			"security_groups": schema.NewSet(hash, []interface{}{
   134  				"sg-11111",
   135  				"foo/sg-22222",
   136  			}),
   137  		},
   138  	}
   139  	group := &ec2.SecurityGroup{
   140  		GroupId: aws.String("foo"),
   141  		VpcId:   aws.String("bar"),
   142  	}
   143  
   144  	perms, err := expandIPPerms(group, expanded)
   145  	if err != nil {
   146  		t.Fatalf("error expanding perms: %v", err)
   147  	}
   148  
   149  	expected := []ec2.IpPermission{
   150  		ec2.IpPermission{
   151  			IpProtocol: aws.String("-1"),
   152  			FromPort:   aws.Int64(int64(0)),
   153  			ToPort:     aws.Int64(int64(0)),
   154  			IpRanges:   []*ec2.IpRange{&ec2.IpRange{CidrIp: aws.String("0.0.0.0/0")}},
   155  			UserIdGroupPairs: []*ec2.UserIdGroupPair{
   156  				&ec2.UserIdGroupPair{
   157  					UserId:  aws.String("foo"),
   158  					GroupId: aws.String("sg-22222"),
   159  				},
   160  				&ec2.UserIdGroupPair{
   161  					GroupId: aws.String("sg-22222"),
   162  				},
   163  			},
   164  		},
   165  	}
   166  
   167  	exp := expected[0]
   168  	perm := perms[0]
   169  
   170  	if *exp.FromPort != *perm.FromPort {
   171  		t.Fatalf(
   172  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   173  			*perm.FromPort,
   174  			*exp.FromPort)
   175  	}
   176  
   177  	if *exp.IpRanges[0].CidrIp != *perm.IpRanges[0].CidrIp {
   178  		t.Fatalf(
   179  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   180  			*perm.IpRanges[0].CidrIp,
   181  			*exp.IpRanges[0].CidrIp)
   182  	}
   183  
   184  	if *exp.UserIdGroupPairs[0].UserId != *perm.UserIdGroupPairs[0].UserId {
   185  		t.Fatalf(
   186  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   187  			*perm.UserIdGroupPairs[0].UserId,
   188  			*exp.UserIdGroupPairs[0].UserId)
   189  	}
   190  
   191  	// Now test the error case. This *should* error when either from_port
   192  	// or to_port is not zero, but protocol is "-1".
   193  	errorCase := []interface{}{
   194  		map[string]interface{}{
   195  			"protocol":    "-1",
   196  			"from_port":   0,
   197  			"to_port":     65535,
   198  			"cidr_blocks": []interface{}{"0.0.0.0/0"},
   199  			"security_groups": schema.NewSet(hash, []interface{}{
   200  				"sg-11111",
   201  				"foo/sg-22222",
   202  			}),
   203  		},
   204  	}
   205  	securityGroups := &ec2.SecurityGroup{
   206  		GroupId: aws.String("foo"),
   207  		VpcId:   aws.String("bar"),
   208  	}
   209  
   210  	_, expandErr := expandIPPerms(securityGroups, errorCase)
   211  	if expandErr == nil {
   212  		t.Fatal("expandIPPerms should have errored!")
   213  	}
   214  }
   215  
   216  func TestExpandIPPerms_nonVPC(t *testing.T) {
   217  	hash := schema.HashString
   218  
   219  	expanded := []interface{}{
   220  		map[string]interface{}{
   221  			"protocol":    "icmp",
   222  			"from_port":   1,
   223  			"to_port":     -1,
   224  			"cidr_blocks": []interface{}{"0.0.0.0/0"},
   225  			"security_groups": schema.NewSet(hash, []interface{}{
   226  				"sg-11111",
   227  				"foo/sg-22222",
   228  			}),
   229  		},
   230  		map[string]interface{}{
   231  			"protocol":  "icmp",
   232  			"from_port": 1,
   233  			"to_port":   -1,
   234  			"self":      true,
   235  		},
   236  	}
   237  	group := &ec2.SecurityGroup{
   238  		GroupName: aws.String("foo"),
   239  	}
   240  	perms, err := expandIPPerms(group, expanded)
   241  	if err != nil {
   242  		t.Fatalf("error expanding perms: %v", err)
   243  	}
   244  
   245  	expected := []ec2.IpPermission{
   246  		ec2.IpPermission{
   247  			IpProtocol: aws.String("icmp"),
   248  			FromPort:   aws.Int64(int64(1)),
   249  			ToPort:     aws.Int64(int64(-1)),
   250  			IpRanges:   []*ec2.IpRange{&ec2.IpRange{CidrIp: aws.String("0.0.0.0/0")}},
   251  			UserIdGroupPairs: []*ec2.UserIdGroupPair{
   252  				&ec2.UserIdGroupPair{
   253  					GroupName: aws.String("sg-22222"),
   254  				},
   255  				&ec2.UserIdGroupPair{
   256  					GroupName: aws.String("sg-22222"),
   257  				},
   258  			},
   259  		},
   260  		ec2.IpPermission{
   261  			IpProtocol: aws.String("icmp"),
   262  			FromPort:   aws.Int64(int64(1)),
   263  			ToPort:     aws.Int64(int64(-1)),
   264  			UserIdGroupPairs: []*ec2.UserIdGroupPair{
   265  				&ec2.UserIdGroupPair{
   266  					GroupName: aws.String("foo"),
   267  				},
   268  			},
   269  		},
   270  	}
   271  
   272  	exp := expected[0]
   273  	perm := perms[0]
   274  
   275  	if *exp.FromPort != *perm.FromPort {
   276  		t.Fatalf(
   277  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   278  			*perm.FromPort,
   279  			*exp.FromPort)
   280  	}
   281  
   282  	if *exp.IpRanges[0].CidrIp != *perm.IpRanges[0].CidrIp {
   283  		t.Fatalf(
   284  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   285  			*perm.IpRanges[0].CidrIp,
   286  			*exp.IpRanges[0].CidrIp)
   287  	}
   288  }
   289  
   290  func TestexpandListeners(t *testing.T) {
   291  	expanded := []interface{}{
   292  		map[string]interface{}{
   293  			"instance_port":     8000,
   294  			"lb_port":           80,
   295  			"instance_protocol": "http",
   296  			"lb_protocol":       "http",
   297  		},
   298  	}
   299  	listeners, err := expandListeners(expanded)
   300  	if err != nil {
   301  		t.Fatalf("bad: %#v", err)
   302  	}
   303  
   304  	expected := &elb.Listener{
   305  		InstancePort:     aws.Int64(int64(8000)),
   306  		LoadBalancerPort: aws.Int64(int64(80)),
   307  		InstanceProtocol: aws.String("http"),
   308  		Protocol:         aws.String("http"),
   309  	}
   310  
   311  	if !reflect.DeepEqual(listeners[0], expected) {
   312  		t.Fatalf(
   313  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   314  			listeners[0],
   315  			expected)
   316  	}
   317  
   318  }
   319  
   320  func TestflattenHealthCheck(t *testing.T) {
   321  	cases := []struct {
   322  		Input  *elb.HealthCheck
   323  		Output []map[string]interface{}
   324  	}{
   325  		{
   326  			Input: &elb.HealthCheck{
   327  				UnhealthyThreshold: aws.Int64(int64(10)),
   328  				HealthyThreshold:   aws.Int64(int64(10)),
   329  				Target:             aws.String("HTTP:80/"),
   330  				Timeout:            aws.Int64(int64(30)),
   331  				Interval:           aws.Int64(int64(30)),
   332  			},
   333  			Output: []map[string]interface{}{
   334  				map[string]interface{}{
   335  					"unhealthy_threshold": int64(10),
   336  					"healthy_threshold":   int64(10),
   337  					"target":              "HTTP:80/",
   338  					"timeout":             int64(30),
   339  					"interval":            int64(30),
   340  				},
   341  			},
   342  		},
   343  	}
   344  
   345  	for _, tc := range cases {
   346  		output := flattenHealthCheck(tc.Input)
   347  		if !reflect.DeepEqual(output, tc.Output) {
   348  			t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
   349  		}
   350  	}
   351  }
   352  
   353  func TestExpandStringList(t *testing.T) {
   354  	expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{})
   355  	stringList := expandStringList(expanded)
   356  	expected := []*string{
   357  		aws.String("us-east-1a"),
   358  		aws.String("us-east-1b"),
   359  	}
   360  
   361  	if !reflect.DeepEqual(stringList, expected) {
   362  		t.Fatalf(
   363  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   364  			stringList,
   365  			expected)
   366  	}
   367  
   368  }
   369  
   370  func TestexpandParameters(t *testing.T) {
   371  	expanded := []interface{}{
   372  		map[string]interface{}{
   373  			"name":         "character_set_client",
   374  			"value":        "utf8",
   375  			"apply_method": "immediate",
   376  		},
   377  	}
   378  	parameters, err := expandParameters(expanded)
   379  	if err != nil {
   380  		t.Fatalf("bad: %#v", err)
   381  	}
   382  
   383  	expected := &rds.Parameter{
   384  		ParameterName:  aws.String("character_set_client"),
   385  		ParameterValue: aws.String("utf8"),
   386  		ApplyMethod:    aws.String("immediate"),
   387  	}
   388  
   389  	if !reflect.DeepEqual(parameters[0], expected) {
   390  		t.Fatalf(
   391  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   392  			parameters[0],
   393  			expected)
   394  	}
   395  }
   396  
   397  func TestexpandElasticacheParameters(t *testing.T) {
   398  	expanded := []interface{}{
   399  		map[string]interface{}{
   400  			"name":         "character_set_client",
   401  			"value":        "utf8",
   402  			"apply_method": "immediate",
   403  		},
   404  	}
   405  	parameters, err := expandElastiCacheParameters(expanded)
   406  	if err != nil {
   407  		t.Fatalf("bad: %#v", err)
   408  	}
   409  
   410  	expected := &elasticache.Parameter{
   411  		ParameterName:  aws.String("activerehashing"),
   412  		ParameterValue: aws.String("yes"),
   413  	}
   414  
   415  	if !reflect.DeepEqual(parameters[0], expected) {
   416  		t.Fatalf(
   417  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   418  			parameters[0],
   419  			expected)
   420  	}
   421  }
   422  
   423  func TestflattenParameters(t *testing.T) {
   424  	cases := []struct {
   425  		Input  []*rds.Parameter
   426  		Output []map[string]interface{}
   427  	}{
   428  		{
   429  			Input: []*rds.Parameter{
   430  				&rds.Parameter{
   431  					ParameterName:  aws.String("character_set_client"),
   432  					ParameterValue: aws.String("utf8"),
   433  				},
   434  			},
   435  			Output: []map[string]interface{}{
   436  				map[string]interface{}{
   437  					"name":  "character_set_client",
   438  					"value": "utf8",
   439  				},
   440  			},
   441  		},
   442  	}
   443  
   444  	for _, tc := range cases {
   445  		output := flattenParameters(tc.Input)
   446  		if !reflect.DeepEqual(output, tc.Output) {
   447  			t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
   448  		}
   449  	}
   450  }
   451  
   452  func TestflattenElasticacheParameters(t *testing.T) {
   453  	cases := []struct {
   454  		Input  []*elasticache.Parameter
   455  		Output []map[string]interface{}
   456  	}{
   457  		{
   458  			Input: []*elasticache.Parameter{
   459  				&elasticache.Parameter{
   460  					ParameterName:  aws.String("activerehashing"),
   461  					ParameterValue: aws.String("yes"),
   462  				},
   463  			},
   464  			Output: []map[string]interface{}{
   465  				map[string]interface{}{
   466  					"name":  "activerehashing",
   467  					"value": "yes",
   468  				},
   469  			},
   470  		},
   471  	}
   472  
   473  	for _, tc := range cases {
   474  		output := flattenElastiCacheParameters(tc.Input)
   475  		if !reflect.DeepEqual(output, tc.Output) {
   476  			t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
   477  		}
   478  	}
   479  }
   480  
   481  func TestexpandInstanceString(t *testing.T) {
   482  
   483  	expected := []*elb.Instance{
   484  		&elb.Instance{InstanceId: aws.String("test-one")},
   485  		&elb.Instance{InstanceId: aws.String("test-two")},
   486  	}
   487  
   488  	ids := []interface{}{
   489  		"test-one",
   490  		"test-two",
   491  	}
   492  
   493  	expanded := expandInstanceString(ids)
   494  
   495  	if !reflect.DeepEqual(expanded, expected) {
   496  		t.Fatalf("Expand Instance String output did not match.\nGot:\n%#v\n\nexpected:\n%#v", expanded, expected)
   497  	}
   498  }
   499  
   500  func TestflattenNetworkInterfacesPrivateIPAddresses(t *testing.T) {
   501  	expanded := []*ec2.NetworkInterfacePrivateIpAddress{
   502  		&ec2.NetworkInterfacePrivateIpAddress{PrivateIpAddress: aws.String("192.168.0.1")},
   503  		&ec2.NetworkInterfacePrivateIpAddress{PrivateIpAddress: aws.String("192.168.0.2")},
   504  	}
   505  
   506  	result := flattenNetworkInterfacesPrivateIPAddresses(expanded)
   507  
   508  	if result == nil {
   509  		t.Fatal("result was nil")
   510  	}
   511  
   512  	if len(result) != 2 {
   513  		t.Fatalf("expected result had %d elements, but got %d", 2, len(result))
   514  	}
   515  
   516  	if result[0] != "192.168.0.1" {
   517  		t.Fatalf("expected ip to be 192.168.0.1, but was %s", result[0])
   518  	}
   519  
   520  	if result[1] != "192.168.0.2" {
   521  		t.Fatalf("expected ip to be 192.168.0.2, but was %s", result[1])
   522  	}
   523  }
   524  
   525  func TestflattenGroupIdentifiers(t *testing.T) {
   526  	expanded := []*ec2.GroupIdentifier{
   527  		&ec2.GroupIdentifier{GroupId: aws.String("sg-001")},
   528  		&ec2.GroupIdentifier{GroupId: aws.String("sg-002")},
   529  	}
   530  
   531  	result := flattenGroupIdentifiers(expanded)
   532  
   533  	if len(result) != 2 {
   534  		t.Fatalf("expected result had %d elements, but got %d", 2, len(result))
   535  	}
   536  
   537  	if result[0] != "sg-001" {
   538  		t.Fatalf("expected id to be sg-001, but was %s", result[0])
   539  	}
   540  
   541  	if result[1] != "sg-002" {
   542  		t.Fatalf("expected id to be sg-002, but was %s", result[1])
   543  	}
   544  }
   545  
   546  func TestexpandPrivateIPAddresses(t *testing.T) {
   547  
   548  	ip1 := "192.168.0.1"
   549  	ip2 := "192.168.0.2"
   550  	flattened := []interface{}{
   551  		ip1,
   552  		ip2,
   553  	}
   554  
   555  	result := expandPrivateIPAddresses(flattened)
   556  
   557  	if len(result) != 2 {
   558  		t.Fatalf("expected result had %d elements, but got %d", 2, len(result))
   559  	}
   560  
   561  	if *result[0].PrivateIpAddress != "192.168.0.1" || !*result[0].Primary {
   562  		t.Fatalf("expected ip to be 192.168.0.1 and Primary, but got %v, %t", *result[0].PrivateIpAddress, *result[0].Primary)
   563  	}
   564  
   565  	if *result[1].PrivateIpAddress != "192.168.0.2" || *result[1].Primary {
   566  		t.Fatalf("expected ip to be 192.168.0.2 and not Primary, but got %v, %t", *result[1].PrivateIpAddress, *result[1].Primary)
   567  	}
   568  }
   569  
   570  func TestflattenAttachment(t *testing.T) {
   571  	expanded := &ec2.NetworkInterfaceAttachment{
   572  		InstanceId:   aws.String("i-00001"),
   573  		DeviceIndex:  aws.Int64(int64(1)),
   574  		AttachmentId: aws.String("at-002"),
   575  	}
   576  
   577  	result := flattenAttachment(expanded)
   578  
   579  	if result == nil {
   580  		t.Fatal("expected result to have value, but got nil")
   581  	}
   582  
   583  	if result["instance"] != "i-00001" {
   584  		t.Fatalf("expected instance to be i-00001, but got %s", result["instance"])
   585  	}
   586  
   587  	if result["device_index"] != int64(1) {
   588  		t.Fatalf("expected device_index to be 1, but got %d", result["device_index"])
   589  	}
   590  
   591  	if result["attachment_id"] != "at-002" {
   592  		t.Fatalf("expected attachment_id to be at-002, but got %s", result["attachment_id"])
   593  	}
   594  }
   595  
   596  func TestFlattenResourceRecords(t *testing.T) {
   597  	expanded := []*route53.ResourceRecord{
   598  		&route53.ResourceRecord{
   599  			Value: aws.String("127.0.0.1"),
   600  		},
   601  		&route53.ResourceRecord{
   602  			Value: aws.String("127.0.0.3"),
   603  		},
   604  	}
   605  
   606  	result := flattenResourceRecords(expanded)
   607  
   608  	if result == nil {
   609  		t.Fatal("expected result to have value, but got nil")
   610  	}
   611  
   612  	if len(result) != 2 {
   613  		t.Fatal("expected result to have value, but got nil")
   614  	}
   615  }