github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/validators_test.go (about)

     1  package alicloud
     2  
     3  import "testing"
     4  
     5  func TestValidateInstancePort(t *testing.T) {
     6  	validPorts := []int{1, 22, 80, 100, 8088, 65535}
     7  	for _, v := range validPorts {
     8  		_, errors := validateInstancePort(v, "instance_port")
     9  		if len(errors) != 0 {
    10  			t.Fatalf("%q should be a valid instance port number between 1 and 65535: %q", v, errors)
    11  		}
    12  	}
    13  
    14  	invalidPorts := []int{-10, -1, 0}
    15  	for _, v := range invalidPorts {
    16  		_, errors := validateInstancePort(v, "instance_port")
    17  		if len(errors) == 0 {
    18  			t.Fatalf("%q should be an invalid instance port number", v)
    19  		}
    20  	}
    21  }
    22  
    23  func TestValidateInstanceProtocol(t *testing.T) {
    24  	validProtocols := []string{"http", "tcp", "https", "udp"}
    25  	for _, v := range validProtocols {
    26  		_, errors := validateInstanceProtocol(v, "instance_protocol")
    27  		if len(errors) != 0 {
    28  			t.Fatalf("%q should be a valid instance protocol: %q", v, errors)
    29  		}
    30  	}
    31  
    32  	invalidProtocols := []string{"HTTP", "abc", "ecmp", "dubbo"}
    33  	for _, v := range invalidProtocols {
    34  		_, errors := validateInstanceProtocol(v, "instance_protocol")
    35  		if len(errors) == 0 {
    36  			t.Fatalf("%q should be an invalid instance protocol", v)
    37  		}
    38  	}
    39  }
    40  
    41  func TestValidateInstanceDiskCategory(t *testing.T) {
    42  	validDiskCategory := []string{"cloud", "cloud_efficiency", "cloud_ssd"}
    43  	for _, v := range validDiskCategory {
    44  		_, errors := validateDiskCategory(v, "instance_disk_category")
    45  		if len(errors) != 0 {
    46  			t.Fatalf("%q should be a valid instance disk category: %q", v, errors)
    47  		}
    48  	}
    49  
    50  	invalidDiskCategory := []string{"all", "ephemeral", "ephemeral_ssd", "ALL", "efficiency"}
    51  	for _, v := range invalidDiskCategory {
    52  		_, errors := validateDiskCategory(v, "instance_disk_category")
    53  		if len(errors) == 0 {
    54  			t.Fatalf("%q should be an invalid instance disk category", v)
    55  		}
    56  	}
    57  }
    58  
    59  func TestValidateInstanceName(t *testing.T) {
    60  	validInstanceName := []string{"hi", "hi http://", "some word + any word &", "http", "中文"}
    61  	for _, v := range validInstanceName {
    62  		_, errors := validateInstanceName(v, "instance_name")
    63  		if len(errors) != 0 {
    64  			t.Fatalf("%q should be a valid instance name: %q", v, errors)
    65  		}
    66  	}
    67  
    68  	invalidInstanceName := []string{"y", "http://", "https://", "+"}
    69  	for _, v := range invalidInstanceName {
    70  		_, errors := validateInstanceName(v, "instance_name")
    71  		if len(errors) == 0 {
    72  			t.Fatalf("%q should be an invalid instance name", v)
    73  		}
    74  	}
    75  }
    76  
    77  func TestValidateInstanceDescription(t *testing.T) {
    78  	validInstanceDescription := []string{"hi", "hi http://", "some word + any word &", "http://", "中文"}
    79  	for _, v := range validInstanceDescription {
    80  		_, errors := validateInstanceDescription(v, "instance_description")
    81  		if len(errors) != 0 {
    82  			t.Fatalf("%q should be a valid instance description: %q", v, errors)
    83  		}
    84  	}
    85  
    86  	invalidvalidInstanceDescription := []string{"y", ""}
    87  	for _, v := range invalidvalidInstanceDescription {
    88  		_, errors := validateInstanceName(v, "instance_description")
    89  		if len(errors) == 0 {
    90  			t.Fatalf("%q should be an invalid instance description", v)
    91  		}
    92  	}
    93  }
    94  
    95  func TestValidateSecurityGroupName(t *testing.T) {
    96  	validSecurityGroupName := []string{"hi", "hi http://", "some word + any word &", "http", "中文", "12345"}
    97  	for _, v := range validSecurityGroupName {
    98  		_, errors := validateSecurityGroupName(v, "security_group_name")
    99  		if len(errors) != 0 {
   100  			t.Fatalf("%q should be a valid security group name: %q", v, errors)
   101  		}
   102  	}
   103  
   104  	invalidSecurityGroupName := []string{"y", "http://", "https://", "+"}
   105  	for _, v := range invalidSecurityGroupName {
   106  		_, errors := validateSecurityGroupName(v, "security_group_name")
   107  		if len(errors) == 0 {
   108  			t.Fatalf("%q should be an invalid security group name", v)
   109  		}
   110  	}
   111  }
   112  
   113  func TestValidateSecurityGroupDescription(t *testing.T) {
   114  	validSecurityGroupDescription := []string{"hi", "hi http://", "some word + any word &", "http://", "中文"}
   115  	for _, v := range validSecurityGroupDescription {
   116  		_, errors := validateSecurityGroupDescription(v, "security_group_description")
   117  		if len(errors) != 0 {
   118  			t.Fatalf("%q should be a valid security group description: %q", v, errors)
   119  		}
   120  	}
   121  
   122  	invalidSecurityGroupDescription := []string{"y", ""}
   123  	for _, v := range invalidSecurityGroupDescription {
   124  		_, errors := validateSecurityGroupDescription(v, "security_group_description")
   125  		if len(errors) == 0 {
   126  			t.Fatalf("%q should be an invalid security group description", v)
   127  		}
   128  	}
   129  }
   130  
   131  func TestValidateSecurityRuleType(t *testing.T) {
   132  	validSecurityRuleType := []string{"ingress", "egress"}
   133  	for _, v := range validSecurityRuleType {
   134  		_, errors := validateSecurityRuleType(v, "security_rule_type")
   135  		if len(errors) != 0 {
   136  			t.Fatalf("%q should be a valid security rule type: %q", v, errors)
   137  		}
   138  	}
   139  
   140  	invalidSecurityRuleType := []string{"y", "gress", "in", "out"}
   141  	for _, v := range invalidSecurityRuleType {
   142  		_, errors := validateSecurityRuleType(v, "security_rule_type")
   143  		if len(errors) == 0 {
   144  			t.Fatalf("%q should be an invalid security rule type", v)
   145  		}
   146  	}
   147  }
   148  
   149  func TestValidateSecurityRuleIpProtocol(t *testing.T) {
   150  	validIpProtocol := []string{"tcp", "udp", "icmp", "gre", "all"}
   151  	for _, v := range validIpProtocol {
   152  		_, errors := validateSecurityRuleIpProtocol(v, "security_rule_ip_protocol")
   153  		if len(errors) != 0 {
   154  			t.Fatalf("%q should be a valid ip protocol: %q", v, errors)
   155  		}
   156  	}
   157  
   158  	invalidIpProtocol := []string{"y", "ecmp", "http", "https"}
   159  	for _, v := range invalidIpProtocol {
   160  		_, errors := validateSecurityRuleIpProtocol(v, "security_rule_ip_protocol")
   161  		if len(errors) == 0 {
   162  			t.Fatalf("%q should be an invalid ip protocol", v)
   163  		}
   164  	}
   165  }
   166  
   167  func TestValidateSecurityRuleNicType(t *testing.T) {
   168  	validRuleNicType := []string{"intranet", "internet"}
   169  	for _, v := range validRuleNicType {
   170  		_, errors := validateSecurityRuleNicType(v, "security_rule_nic_type")
   171  		if len(errors) != 0 {
   172  			t.Fatalf("%q should be a valid nic type: %q", v, errors)
   173  		}
   174  	}
   175  
   176  	invalidRuleNicType := []string{"inter", "ecmp", "http", "https"}
   177  	for _, v := range invalidRuleNicType {
   178  		_, errors := validateSecurityRuleNicType(v, "security_rule_nic_type")
   179  		if len(errors) == 0 {
   180  			t.Fatalf("%q should be an invalid nic type", v)
   181  		}
   182  	}
   183  }
   184  
   185  func TestValidateSecurityRulePolicy(t *testing.T) {
   186  	validRulePolicy := []string{"accept", "drop"}
   187  	for _, v := range validRulePolicy {
   188  		_, errors := validateSecurityRulePolicy(v, "security_rule_policy")
   189  		if len(errors) != 0 {
   190  			t.Fatalf("%q should be a valid security rule policy: %q", v, errors)
   191  		}
   192  	}
   193  
   194  	invalidRulePolicy := []string{"inter", "ecmp", "http", "https"}
   195  	for _, v := range invalidRulePolicy {
   196  		_, errors := validateSecurityRulePolicy(v, "security_rule_policy")
   197  		if len(errors) == 0 {
   198  			t.Fatalf("%q should be an invalid security rule policy", v)
   199  		}
   200  	}
   201  }
   202  
   203  func TestValidateSecurityRulePriority(t *testing.T) {
   204  	validPriority := []int{1, 50, 100}
   205  	for _, v := range validPriority {
   206  		_, errors := validateSecurityPriority(v, "security_rule_priority")
   207  		if len(errors) != 0 {
   208  			t.Fatalf("%q should be a valid security rule priority: %q", v, errors)
   209  		}
   210  	}
   211  
   212  	invalidPriority := []int{-1, 0, 101}
   213  	for _, v := range invalidPriority {
   214  		_, errors := validateSecurityPriority(v, "security_rule_priority")
   215  		if len(errors) == 0 {
   216  			t.Fatalf("%q should be an invalid security rule priority", v)
   217  		}
   218  	}
   219  }
   220  
   221  func TestValidateCIDRNetworkAddress(t *testing.T) {
   222  	validCIDRNetworkAddress := []string{"192.168.10.0/24", "0.0.0.0/0", "10.121.10.0/24"}
   223  	for _, v := range validCIDRNetworkAddress {
   224  		_, errors := validateCIDRNetworkAddress(v, "cidr_network_address")
   225  		if len(errors) != 0 {
   226  			t.Fatalf("%q should be a valid cidr network address: %q", v, errors)
   227  		}
   228  	}
   229  
   230  	invalidCIDRNetworkAddress := []string{"1.2.3.4", "0x38732/21"}
   231  	for _, v := range invalidCIDRNetworkAddress {
   232  		_, errors := validateCIDRNetworkAddress(v, "cidr_network_address")
   233  		if len(errors) == 0 {
   234  			t.Fatalf("%q should be an invalid cidr network address", v)
   235  		}
   236  	}
   237  }
   238  
   239  func TestValidateRouteEntryNextHopType(t *testing.T) {
   240  	validNexthopType := []string{"Instance", "Tunnel"}
   241  	for _, v := range validNexthopType {
   242  		_, errors := validateRouteEntryNextHopType(v, "route_entry_nexthop_type")
   243  		if len(errors) != 0 {
   244  			t.Fatalf("%q should be a valid route entry nexthop type: %q", v, errors)
   245  		}
   246  	}
   247  
   248  	invalidNexthopType := []string{"ri", "vpc"}
   249  	for _, v := range invalidNexthopType {
   250  		_, errors := validateRouteEntryNextHopType(v, "route_entry_nexthop_type")
   251  		if len(errors) == 0 {
   252  			t.Fatalf("%q should be an invalid route entry nexthop type", v)
   253  		}
   254  	}
   255  }
   256  
   257  func TestValidateSwitchCIDRNetworkAddress(t *testing.T) {
   258  	validSwitchCIDRNetworkAddress := []string{"192.168.10.0/24", "0.0.0.0/16", "127.0.0.0/29", "10.121.10.0/24"}
   259  	for _, v := range validSwitchCIDRNetworkAddress {
   260  		_, errors := validateSwitchCIDRNetworkAddress(v, "switch_cidr_network_address")
   261  		if len(errors) != 0 {
   262  			t.Fatalf("%q should be a valid switch cidr network address: %q", v, errors)
   263  		}
   264  	}
   265  
   266  	invalidSwitchCIDRNetworkAddress := []string{"1.2.3.4", "0x38732/21", "10.121.10.0/15", "10.121.10.0/30", "256.121.10.0/22"}
   267  	for _, v := range invalidSwitchCIDRNetworkAddress {
   268  		_, errors := validateSwitchCIDRNetworkAddress(v, "switch_cidr_network_address")
   269  		if len(errors) == 0 {
   270  			t.Fatalf("%q should be an invalid switch cidr network address", v)
   271  		}
   272  	}
   273  }
   274  
   275  func TestValidateIoOptimized(t *testing.T) {
   276  	validIoOptimized := []string{"", "none", "optimized"}
   277  	for _, v := range validIoOptimized {
   278  		_, errors := validateIoOptimized(v, "ioOptimized")
   279  		if len(errors) != 0 {
   280  			t.Fatalf("%q should be a valid IoOptimized value: %q", v, errors)
   281  		}
   282  	}
   283  
   284  	invalidIoOptimized := []string{"true", "ioOptimized"}
   285  	for _, v := range invalidIoOptimized {
   286  		_, errors := validateIoOptimized(v, "ioOptimized")
   287  		if len(errors) == 0 {
   288  			t.Fatalf("%q should be an invalid IoOptimized value", v)
   289  		}
   290  	}
   291  }
   292  
   293  func TestValidateInstanceNetworkType(t *testing.T) {
   294  	validInstanceNetworkType := []string{"", "classic", "vpc"}
   295  	for _, v := range validInstanceNetworkType {
   296  		_, errors := validateInstanceNetworkType(v, "instance_network_type")
   297  		if len(errors) != 0 {
   298  			t.Fatalf("%q should be a valid instance network type value: %q", v, errors)
   299  		}
   300  	}
   301  
   302  	invalidInstanceNetworkType := []string{"Classic", "vswitch", "123"}
   303  	for _, v := range invalidInstanceNetworkType {
   304  		_, errors := validateInstanceNetworkType(v, "instance_network_type")
   305  		if len(errors) == 0 {
   306  			t.Fatalf("%q should be an invalid instance network type value", v)
   307  		}
   308  	}
   309  }
   310  
   311  func TestValidateInstanceChargeType(t *testing.T) {
   312  	validInstanceChargeType := []string{"", "PrePaid", "PostPaid"}
   313  	for _, v := range validInstanceChargeType {
   314  		_, errors := validateInstanceChargeType(v, "instance_charge_type")
   315  		if len(errors) != 0 {
   316  			t.Fatalf("%q should be a valid instance charge type value: %q", v, errors)
   317  		}
   318  	}
   319  
   320  	invalidInstanceChargeType := []string{"prepay", "yearly", "123"}
   321  	for _, v := range invalidInstanceChargeType {
   322  		_, errors := validateInstanceChargeType(v, "instance_charge_type")
   323  		if len(errors) == 0 {
   324  			t.Fatalf("%q should be an invalid instance charge type value", v)
   325  		}
   326  	}
   327  }
   328  
   329  func TestValidateInternetChargeType(t *testing.T) {
   330  	validInternetChargeType := []string{"", "PayByBandwidth", "PayByTraffic"}
   331  	for _, v := range validInternetChargeType {
   332  		_, errors := validateInternetChargeType(v, "internet_charge_type")
   333  		if len(errors) != 0 {
   334  			t.Fatalf("%q should be a valid internet charge type value: %q", v, errors)
   335  		}
   336  	}
   337  
   338  	invalidInternetChargeType := []string{"paybybandwidth", "paybytraffic", "123"}
   339  	for _, v := range invalidInternetChargeType {
   340  		_, errors := validateInternetChargeType(v, "internet_charge_type")
   341  		if len(errors) == 0 {
   342  			t.Fatalf("%q should be an invalid internet charge type value", v)
   343  		}
   344  	}
   345  }
   346  
   347  func TestValidateInternetMaxBandWidthOut(t *testing.T) {
   348  	validInternetMaxBandWidthOut := []int{1, 22, 100}
   349  	for _, v := range validInternetMaxBandWidthOut {
   350  		_, errors := validateInternetMaxBandWidthOut(v, "internet_max_bandwidth_out")
   351  		if len(errors) != 0 {
   352  			t.Fatalf("%q should be a valid internet max bandwidth out value: %q", v, errors)
   353  		}
   354  	}
   355  
   356  	invalidInternetMaxBandWidthOut := []int{-2, 101, 123}
   357  	for _, v := range invalidInternetMaxBandWidthOut {
   358  		_, errors := validateInternetMaxBandWidthOut(v, "internet_max_bandwidth_out")
   359  		if len(errors) == 0 {
   360  			t.Fatalf("%q should be an invalid internet max bandwidth out value", v)
   361  		}
   362  	}
   363  }
   364  
   365  func TestValidateSlbName(t *testing.T) {
   366  	validSlbName := []string{"h", "http://", "123", "hello, aliyun! "}
   367  	for _, v := range validSlbName {
   368  		_, errors := validateSlbName(v, "slb_name")
   369  		if len(errors) != 0 {
   370  			t.Fatalf("%q should be a valid slb name: %q", v, errors)
   371  		}
   372  	}
   373  
   374  	// todo: add invalid case
   375  }
   376  
   377  func TestValidateSlbInternetChargeType(t *testing.T) {
   378  	validSlbInternetChargeType := []string{"paybybandwidth", "paybytraffic"}
   379  	for _, v := range validSlbInternetChargeType {
   380  		_, errors := validateSlbInternetChargeType(v, "slb_internet_charge_type")
   381  		if len(errors) != 0 {
   382  			t.Fatalf("%q should be a valid slb internet charge type value: %q", v, errors)
   383  		}
   384  	}
   385  
   386  	invalidSlbInternetChargeType := []string{"PayByBandwidth", "PayByTraffic"}
   387  	for _, v := range invalidSlbInternetChargeType {
   388  		_, errors := validateSlbInternetChargeType(v, "slb_internet_charge_type")
   389  		if len(errors) == 0 {
   390  			t.Fatalf("%q should be an invalid slb internet charge type value", v)
   391  		}
   392  	}
   393  }
   394  
   395  func TestValidateSlbBandwidth(t *testing.T) {
   396  	validSlbBandwidth := []int{1, 22, 1000}
   397  	for _, v := range validSlbBandwidth {
   398  		_, errors := validateSlbBandwidth(v, "slb_bandwidth")
   399  		if len(errors) != 0 {
   400  			t.Fatalf("%q should be a valid slb bandwidth value: %q", v, errors)
   401  		}
   402  	}
   403  
   404  	invalidSlbBandwidth := []int{-2, 0, 1001}
   405  	for _, v := range invalidSlbBandwidth {
   406  		_, errors := validateSlbBandwidth(v, "slb_bandwidth")
   407  		if len(errors) == 0 {
   408  			t.Fatalf("%q should be an invalid slb bandwidth value", v)
   409  		}
   410  	}
   411  }
   412  
   413  func TestValidateSlbListenerBandwidth(t *testing.T) {
   414  	validSlbListenerBandwidth := []int{-1, 1, 22, 1000}
   415  	for _, v := range validSlbListenerBandwidth {
   416  		_, errors := validateSlbListenerBandwidth(v, "slb_bandwidth")
   417  		if len(errors) != 0 {
   418  			t.Fatalf("%q should be a valid slb listener bandwidth value: %q", v, errors)
   419  		}
   420  	}
   421  
   422  	invalidSlbListenerBandwidth := []int{-2, 0, -10, 1001}
   423  	for _, v := range invalidSlbListenerBandwidth {
   424  		_, errors := validateSlbListenerBandwidth(v, "slb_bandwidth")
   425  		if len(errors) == 0 {
   426  			t.Fatalf("%q should be an invalid slb listener bandwidth value", v)
   427  		}
   428  	}
   429  }
   430  
   431  func TestValidateAllowedStringValue(t *testing.T) {
   432  	exceptValues := []string{"aliyun", "alicloud", "alibaba"}
   433  	validValues := []string{"aliyun"}
   434  	for _, v := range validValues {
   435  		_, errors := validateAllowedStringValue(exceptValues)(v, "allowvalue")
   436  		if len(errors) != 0 {
   437  			t.Fatalf("%q should be a valid value in %#v: %q", v, exceptValues, errors)
   438  		}
   439  	}
   440  
   441  	invalidValues := []string{"ali", "alidata", "terraform"}
   442  	for _, v := range invalidValues {
   443  		_, errors := validateAllowedStringValue(exceptValues)(v, "allowvalue")
   444  		if len(errors) == 0 {
   445  			t.Fatalf("%q should be an invalid value", v)
   446  		}
   447  	}
   448  }
   449  
   450  func TestValidateAllowedStringSplitValue(t *testing.T) {
   451  	exceptValues := []string{"aliyun", "alicloud", "alibaba"}
   452  	validValues := "aliyun,alicloud"
   453  	_, errors := validateAllowedSplitStringValue(exceptValues, ",")(validValues, "allowvalue")
   454  	if len(errors) != 0 {
   455  		t.Fatalf("%q should be a valid value in %#v: %q", validValues, exceptValues, errors)
   456  	}
   457  
   458  	invalidValues := "ali,alidata"
   459  	_, invalidErr := validateAllowedSplitStringValue(exceptValues, ",")(invalidValues, "allowvalue")
   460  	if len(invalidErr) == 0 {
   461  		t.Fatalf("%q should be an invalid value", invalidValues)
   462  	}
   463  }
   464  
   465  func TestValidateAllowedIntValue(t *testing.T) {
   466  	exceptValues := []int{1, 3, 5, 6}
   467  	validValues := []int{1, 3, 5, 6}
   468  	for _, v := range validValues {
   469  		_, errors := validateAllowedIntValue(exceptValues)(v, "allowvalue")
   470  		if len(errors) != 0 {
   471  			t.Fatalf("%q should be a valid value in %#v: %q", v, exceptValues, errors)
   472  		}
   473  	}
   474  
   475  	invalidValues := []int{0, 7, 10}
   476  	for _, v := range invalidValues {
   477  		_, errors := validateAllowedIntValue(exceptValues)(v, "allowvalue")
   478  		if len(errors) == 0 {
   479  			t.Fatalf("%q should be an invalid value", v)
   480  		}
   481  	}
   482  }
   483  
   484  func TestValidateIntegerInRange(t *testing.T) {
   485  	validIntegers := []int{-259, 0, 1, 5, 999}
   486  	min := -259
   487  	max := 999
   488  	for _, v := range validIntegers {
   489  		_, errors := validateIntegerInRange(min, max)(v, "name")
   490  		if len(errors) != 0 {
   491  			t.Fatalf("%q should be an integer in range (%d, %d): %q", v, min, max, errors)
   492  		}
   493  	}
   494  
   495  	invalidIntegers := []int{-260, -99999, 1000, 25678}
   496  	for _, v := range invalidIntegers {
   497  		_, errors := validateIntegerInRange(min, max)(v, "name")
   498  		if len(errors) == 0 {
   499  			t.Fatalf("%q should be an integer outside range (%d, %d)", v, min, max)
   500  		}
   501  	}
   502  }