github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/validators_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestValidateEcrRepositoryName(t *testing.T) {
     9  	validNames := []string{
    10  		"nginx-web-app",
    11  		"project-a/nginx-web-app",
    12  		"domain.ltd/nginx-web-app",
    13  		"3chosome-thing.com/01different-pattern",
    14  		"0123456789/999999999",
    15  		"double/forward/slash",
    16  		"000000000000000",
    17  	}
    18  	for _, v := range validNames {
    19  		_, errors := validateEcrRepositoryName(v, "name")
    20  		if len(errors) != 0 {
    21  			t.Fatalf("%q should be a valid ECR repository name: %q", v, errors)
    22  		}
    23  	}
    24  
    25  	invalidNames := []string{
    26  		// length > 256
    27  		"3cho_some-thing.com/01different.-_pattern01different.-_pattern01diff" +
    28  			"erent.-_pattern01different.-_pattern01different.-_pattern01different" +
    29  			".-_pattern01different.-_pattern01different.-_pattern01different.-_pa" +
    30  			"ttern01different.-_pattern01different.-_pattern234567",
    31  		// length < 2
    32  		"i",
    33  		"special@character",
    34  		"different+special=character",
    35  		"double//slash",
    36  		"double..dot",
    37  		"/slash-at-the-beginning",
    38  		"slash-at-the-end/",
    39  	}
    40  	for _, v := range invalidNames {
    41  		_, errors := validateEcrRepositoryName(v, "name")
    42  		if len(errors) == 0 {
    43  			t.Fatalf("%q should be an invalid ECR repository name", v)
    44  		}
    45  	}
    46  }
    47  
    48  func TestValidateCloudWatchEventRuleName(t *testing.T) {
    49  	validNames := []string{
    50  		"HelloWorl_d",
    51  		"hello-world",
    52  		"hello.World0125",
    53  	}
    54  	for _, v := range validNames {
    55  		_, errors := validateCloudWatchEventRuleName(v, "name")
    56  		if len(errors) != 0 {
    57  			t.Fatalf("%q should be a valid CW event rule name: %q", v, errors)
    58  		}
    59  	}
    60  
    61  	invalidNames := []string{
    62  		"special@character",
    63  		"slash/in-the-middle",
    64  		// Length > 64
    65  		"TooLooooooooooooooooooooooooooooooooooooooooooooooooooooooongName",
    66  	}
    67  	for _, v := range invalidNames {
    68  		_, errors := validateCloudWatchEventRuleName(v, "name")
    69  		if len(errors) == 0 {
    70  			t.Fatalf("%q should be an invalid CW event rule name", v)
    71  		}
    72  	}
    73  }
    74  
    75  func TestValidateLambdaFunctionName(t *testing.T) {
    76  	validNames := []string{
    77  		"arn:aws:lambda:us-west-2:123456789012:function:ThumbNail",
    78  		"FunctionName",
    79  		"function-name",
    80  	}
    81  	for _, v := range validNames {
    82  		_, errors := validateLambdaFunctionName(v, "name")
    83  		if len(errors) != 0 {
    84  			t.Fatalf("%q should be a valid Lambda function name: %q", v, errors)
    85  		}
    86  	}
    87  
    88  	invalidNames := []string{
    89  		"/FunctionNameWithSlash",
    90  		"function.name.with.dots",
    91  		// lenght > 140
    92  		"arn:aws:lambda:us-west-2:123456789012:function:TooLoooooo" +
    93  			"ooooooooooooooooooooooooooooooooooooooooooooooooooooooo" +
    94  			"ooooooooooooooooongFunctionName",
    95  	}
    96  	for _, v := range invalidNames {
    97  		_, errors := validateLambdaFunctionName(v, "name")
    98  		if len(errors) == 0 {
    99  			t.Fatalf("%q should be an invalid Lambda function name", v)
   100  		}
   101  	}
   102  }
   103  
   104  func TestValidateLambdaQualifier(t *testing.T) {
   105  	validNames := []string{
   106  		"123",
   107  		"prod",
   108  		"PROD",
   109  		"MyTestEnv",
   110  		"$LATEST",
   111  	}
   112  	for _, v := range validNames {
   113  		_, errors := validateLambdaQualifier(v, "name")
   114  		if len(errors) != 0 {
   115  			t.Fatalf("%q should be a valid Lambda function qualifier: %q", v, errors)
   116  		}
   117  	}
   118  
   119  	invalidNames := []string{
   120  		// No ARNs allowed
   121  		"arn:aws:lambda:us-west-2:123456789012:function:prod",
   122  		// lenght > 128
   123  		"TooLooooooooooooooooooooooooooooooooooooooooooooooooooo" +
   124  			"ooooooooooooooooooooooooooooooooooooooooooooooooooo" +
   125  			"oooooooooooongQualifier",
   126  	}
   127  	for _, v := range invalidNames {
   128  		_, errors := validateLambdaQualifier(v, "name")
   129  		if len(errors) == 0 {
   130  			t.Fatalf("%q should be an invalid Lambda function qualifier", v)
   131  		}
   132  	}
   133  }
   134  
   135  func TestValidateLambdaPermissionAction(t *testing.T) {
   136  	validNames := []string{
   137  		"lambda:*",
   138  		"lambda:InvokeFunction",
   139  		"*",
   140  	}
   141  	for _, v := range validNames {
   142  		_, errors := validateLambdaPermissionAction(v, "action")
   143  		if len(errors) != 0 {
   144  			t.Fatalf("%q should be a valid Lambda permission action: %q", v, errors)
   145  		}
   146  	}
   147  
   148  	invalidNames := []string{
   149  		"yada",
   150  		"lambda:123",
   151  		"*:*",
   152  		"lambda:Invoke*",
   153  	}
   154  	for _, v := range invalidNames {
   155  		_, errors := validateLambdaPermissionAction(v, "action")
   156  		if len(errors) == 0 {
   157  			t.Fatalf("%q should be an invalid Lambda permission action", v)
   158  		}
   159  	}
   160  }
   161  
   162  func TestValidateAwsAccountId(t *testing.T) {
   163  	validNames := []string{
   164  		"123456789012",
   165  		"999999999999",
   166  	}
   167  	for _, v := range validNames {
   168  		_, errors := validateAwsAccountId(v, "account_id")
   169  		if len(errors) != 0 {
   170  			t.Fatalf("%q should be a valid AWS Account ID: %q", v, errors)
   171  		}
   172  	}
   173  
   174  	invalidNames := []string{
   175  		"12345678901",   // too short
   176  		"1234567890123", // too long
   177  		"invalid",
   178  		"x123456789012",
   179  	}
   180  	for _, v := range invalidNames {
   181  		_, errors := validateAwsAccountId(v, "account_id")
   182  		if len(errors) == 0 {
   183  			t.Fatalf("%q should be an invalid AWS Account ID", v)
   184  		}
   185  	}
   186  }
   187  
   188  func TestValidateArn(t *testing.T) {
   189  	validNames := []string{
   190  		"arn:aws:elasticbeanstalk:us-east-1:123456789012:environment/My App/MyEnvironment", // Beanstalk
   191  		"arn:aws:iam::123456789012:user/David",                                             // IAM User
   192  		"arn:aws:rds:eu-west-1:123456789012:db:mysql-db",                                   // RDS
   193  		"arn:aws:s3:::my_corporate_bucket/exampleobject.png",                               // S3 object
   194  		"arn:aws:events:us-east-1:319201112229:rule/rule_name",                             // CloudWatch Rule
   195  		"arn:aws:lambda:eu-west-1:319201112229:function:myCustomFunction",                  // Lambda function
   196  		"arn:aws:lambda:eu-west-1:319201112229:function:myCustomFunction:Qualifier",        // Lambda func qualifier
   197  	}
   198  	for _, v := range validNames {
   199  		_, errors := validateArn(v, "arn")
   200  		if len(errors) != 0 {
   201  			t.Fatalf("%q should be a valid ARN: %q", v, errors)
   202  		}
   203  	}
   204  
   205  	invalidNames := []string{
   206  		"arn",
   207  		"123456789012",
   208  		"arn:aws",
   209  		"arn:aws:logs",
   210  		"arn:aws:logs:region:*:*",
   211  	}
   212  	for _, v := range invalidNames {
   213  		_, errors := validateArn(v, "arn")
   214  		if len(errors) == 0 {
   215  			t.Fatalf("%q should be an invalid ARN", v)
   216  		}
   217  	}
   218  }
   219  
   220  func TestValidatePolicyStatementId(t *testing.T) {
   221  	validNames := []string{
   222  		"YadaHereAndThere",
   223  		"Valid-5tatement_Id",
   224  		"1234",
   225  	}
   226  	for _, v := range validNames {
   227  		_, errors := validatePolicyStatementId(v, "statement_id")
   228  		if len(errors) != 0 {
   229  			t.Fatalf("%q should be a valid Statement ID: %q", v, errors)
   230  		}
   231  	}
   232  
   233  	invalidNames := []string{
   234  		"Invalid/StatementId/with/slashes",
   235  		"InvalidStatementId.with.dots",
   236  		// length > 100
   237  		"TooooLoooooooooooooooooooooooooooooooooooooooooooo" +
   238  			"ooooooooooooooooooooooooooooooooooooooooStatementId",
   239  	}
   240  	for _, v := range invalidNames {
   241  		_, errors := validatePolicyStatementId(v, "statement_id")
   242  		if len(errors) == 0 {
   243  			t.Fatalf("%q should be an invalid Statement ID", v)
   244  		}
   245  	}
   246  }
   247  
   248  func TestValidateCIDRNetworkAddress(t *testing.T) {
   249  	cases := []struct {
   250  		CIDR              string
   251  		ExpectedErrSubstr string
   252  	}{
   253  		{"notacidr", `must contain a valid CIDR`},
   254  		{"10.0.1.0/16", `must contain a valid network CIDR`},
   255  		{"10.0.1.0/24", ``},
   256  	}
   257  
   258  	for i, tc := range cases {
   259  		_, errs := validateCIDRNetworkAddress(tc.CIDR, "foo")
   260  		if tc.ExpectedErrSubstr == "" {
   261  			if len(errs) != 0 {
   262  				t.Fatalf("%d/%d: Expected no error, got errs: %#v",
   263  					i+1, len(cases), errs)
   264  			}
   265  		} else {
   266  			if len(errs) != 1 {
   267  				t.Fatalf("%d/%d: Expected 1 err containing %q, got %d errs",
   268  					i+1, len(cases), tc.ExpectedErrSubstr, len(errs))
   269  			}
   270  			if !strings.Contains(errs[0].Error(), tc.ExpectedErrSubstr) {
   271  				t.Fatalf("%d/%d: Expected err: %q, to include %q",
   272  					i+1, len(cases), errs[0], tc.ExpectedErrSubstr)
   273  			}
   274  		}
   275  	}
   276  }
   277  
   278  func TestValidateHTTPMethod(t *testing.T) {
   279  	validCases := []string{"GET", "PUT", "POST", "DELETE", "OPTIONS", "HEAD", "PATCH"}
   280  	for i, method := range validCases {
   281  		_, errs := validateHTTPMethod(method, "foo")
   282  		if len(errs) != 0 {
   283  			t.Fatalf("%d/%d: Expected no error, got errs: %#v",
   284  				i+1, len(validCases), errs)
   285  		}
   286  	}
   287  }
   288  
   289  func TestValidateLogMetricFilterName(t *testing.T) {
   290  	validNames := []string{
   291  		"YadaHereAndThere",
   292  		"Valid-5Metric_Name",
   293  		"This . is also %% valid@!)+(",
   294  		"1234",
   295  		strings.Repeat("W", 512),
   296  	}
   297  	for _, v := range validNames {
   298  		_, errors := validateLogMetricFilterName(v, "name")
   299  		if len(errors) != 0 {
   300  			t.Fatalf("%q should be a valid Log Metric Filter Name: %q", v, errors)
   301  		}
   302  	}
   303  
   304  	invalidNames := []string{
   305  		"Here is a name with: colon",
   306  		"and here is another * invalid name",
   307  		"*",
   308  		// length > 512
   309  		strings.Repeat("W", 513),
   310  	}
   311  	for _, v := range invalidNames {
   312  		_, errors := validateLogMetricFilterName(v, "name")
   313  		if len(errors) == 0 {
   314  			t.Fatalf("%q should be an invalid Log Metric Filter Name", v)
   315  		}
   316  	}
   317  }
   318  
   319  func TestValidateLogMetricTransformationName(t *testing.T) {
   320  	validNames := []string{
   321  		"YadaHereAndThere",
   322  		"Valid-5Metric_Name",
   323  		"This . is also %% valid@!)+(",
   324  		"1234",
   325  		"",
   326  		strings.Repeat("W", 255),
   327  	}
   328  	for _, v := range validNames {
   329  		_, errors := validateLogMetricFilterTransformationName(v, "name")
   330  		if len(errors) != 0 {
   331  			t.Fatalf("%q should be a valid Log Metric Filter Transformation Name: %q", v, errors)
   332  		}
   333  	}
   334  
   335  	invalidNames := []string{
   336  		"Here is a name with: colon",
   337  		"and here is another * invalid name",
   338  		"also $ invalid",
   339  		"*",
   340  		// length > 255
   341  		strings.Repeat("W", 256),
   342  	}
   343  	for _, v := range invalidNames {
   344  		_, errors := validateLogMetricFilterTransformationName(v, "name")
   345  		if len(errors) == 0 {
   346  			t.Fatalf("%q should be an invalid Log Metric Filter Transformation Name", v)
   347  		}
   348  	}
   349  }
   350  
   351  func TestValidateLogGroupName(t *testing.T) {
   352  	validNames := []string{
   353  		"ValidLogGroupName",
   354  		"ValidLogGroup.Name",
   355  		"valid/Log-group",
   356  		"1234",
   357  		"YadaValid#0123",
   358  		"Also_valid-name",
   359  		strings.Repeat("W", 512),
   360  	}
   361  	for _, v := range validNames {
   362  		_, errors := validateLogGroupName(v, "name")
   363  		if len(errors) != 0 {
   364  			t.Fatalf("%q should be a valid Log Metric Filter Transformation Name: %q", v, errors)
   365  		}
   366  	}
   367  
   368  	invalidNames := []string{
   369  		"Here is a name with: colon",
   370  		"and here is another * invalid name",
   371  		"also $ invalid",
   372  		"This . is also %% invalid@!)+(",
   373  		"*",
   374  		"",
   375  		// length > 512
   376  		strings.Repeat("W", 513),
   377  	}
   378  	for _, v := range invalidNames {
   379  		_, errors := validateLogGroupName(v, "name")
   380  		if len(errors) == 0 {
   381  			t.Fatalf("%q should be an invalid Log Metric Filter Transformation Name", v)
   382  		}
   383  	}
   384  }
   385  
   386  func TestValidateS3BucketLifecycleTimestamp(t *testing.T) {
   387  	validDates := []string{
   388  		"2016-01-01",
   389  		"2006-01-02",
   390  	}
   391  
   392  	for _, v := range validDates {
   393  		_, errors := validateS3BucketLifecycleTimestamp(v, "date")
   394  		if len(errors) != 0 {
   395  			t.Fatalf("%q should be valid date: %q", v, errors)
   396  		}
   397  	}
   398  
   399  	invalidDates := []string{
   400  		"Jan 01 2016",
   401  		"20160101",
   402  	}
   403  
   404  	for _, v := range invalidDates {
   405  		_, errors := validateS3BucketLifecycleTimestamp(v, "date")
   406  		if len(errors) == 0 {
   407  			t.Fatalf("%q should be invalid date", v)
   408  		}
   409  	}
   410  }
   411  
   412  func TestValidateS3BucketLifecycleStorageClass(t *testing.T) {
   413  	validStorageClass := []string{
   414  		"STANDARD_IA",
   415  		"GLACIER",
   416  	}
   417  
   418  	for _, v := range validStorageClass {
   419  		_, errors := validateS3BucketLifecycleStorageClass(v, "storage_class")
   420  		if len(errors) != 0 {
   421  			t.Fatalf("%q should be valid storage class: %q", v, errors)
   422  		}
   423  	}
   424  
   425  	invalidStorageClass := []string{
   426  		"STANDARD",
   427  		"1234",
   428  	}
   429  	for _, v := range invalidStorageClass {
   430  		_, errors := validateS3BucketLifecycleStorageClass(v, "storage_class")
   431  		if len(errors) == 0 {
   432  			t.Fatalf("%q should be invalid storage class", v)
   433  		}
   434  	}
   435  }
   436  
   437  func TestValidateS3BucketLifecycleRuleId(t *testing.T) {
   438  	validId := []string{
   439  		"YadaHereAndThere",
   440  		"Valid-5Rule_ID",
   441  		"This . is also %% valid@!)+*(:ID",
   442  		"1234",
   443  		strings.Repeat("W", 255),
   444  	}
   445  	for _, v := range validId {
   446  		_, errors := validateS3BucketLifecycleRuleId(v, "id")
   447  		if len(errors) != 0 {
   448  			t.Fatalf("%q should be a valid lifecycle rule id: %q", v, errors)
   449  		}
   450  	}
   451  
   452  	invalidId := []string{
   453  		// length > 255
   454  		strings.Repeat("W", 256),
   455  	}
   456  	for _, v := range invalidId {
   457  		_, errors := validateS3BucketLifecycleRuleId(v, "id")
   458  		if len(errors) == 0 {
   459  			t.Fatalf("%q should be an invalid lifecycle rule id", v)
   460  		}
   461  	}
   462  }
   463  
   464  func TestValidateIntegerInRange(t *testing.T) {
   465  	validIntegers := []int{-259, 0, 1, 5, 999}
   466  	min := -259
   467  	max := 999
   468  	for _, v := range validIntegers {
   469  		_, errors := validateIntegerInRange(min, max)(v, "name")
   470  		if len(errors) != 0 {
   471  			t.Fatalf("%q should be an integer in range (%d, %d): %q", v, min, max, errors)
   472  		}
   473  	}
   474  
   475  	invalidIntegers := []int{-260, -99999, 1000, 25678}
   476  	for _, v := range invalidIntegers {
   477  		_, errors := validateIntegerInRange(min, max)(v, "name")
   478  		if len(errors) == 0 {
   479  			t.Fatalf("%q should be an integer outside range (%d, %d)", v, min, max)
   480  		}
   481  	}
   482  }
   483  
   484  func TestResourceAWSElastiCacheClusterIdValidation(t *testing.T) {
   485  	cases := []struct {
   486  		Value    string
   487  		ErrCount int
   488  	}{
   489  		{
   490  			Value:    "tEsting",
   491  			ErrCount: 1,
   492  		},
   493  		{
   494  			Value:    "t.sting",
   495  			ErrCount: 1,
   496  		},
   497  		{
   498  			Value:    "t--sting",
   499  			ErrCount: 1,
   500  		},
   501  		{
   502  			Value:    "1testing",
   503  			ErrCount: 1,
   504  		},
   505  		{
   506  			Value:    "testing-",
   507  			ErrCount: 1,
   508  		},
   509  		{
   510  			Value:    randomString(65),
   511  			ErrCount: 1,
   512  		},
   513  	}
   514  
   515  	for _, tc := range cases {
   516  		_, errors := validateElastiCacheClusterId(tc.Value, "aws_elasticache_cluster_cluster_id")
   517  
   518  		if len(errors) != tc.ErrCount {
   519  			t.Fatalf("Expected the ElastiCache Cluster cluster_id to trigger a validation error")
   520  		}
   521  	}
   522  }
   523  
   524  func TestValidateDbEventSubscriptionName(t *testing.T) {
   525  	validNames := []string{
   526  		"valid-name",
   527  		"valid02-name",
   528  		"Valid-Name1",
   529  	}
   530  	for _, v := range validNames {
   531  		_, errors := validateDbEventSubscriptionName(v, "name")
   532  		if len(errors) != 0 {
   533  			t.Fatalf("%q should be a valid RDS Event Subscription Name: %q", v, errors)
   534  		}
   535  	}
   536  
   537  	invalidNames := []string{
   538  		"Here is a name with: colon",
   539  		"and here is another * invalid name",
   540  		"also $ invalid",
   541  		"This . is also %% invalid@!)+(",
   542  		"*",
   543  		"",
   544  		" ",
   545  		"_",
   546  		// length > 255
   547  		strings.Repeat("W", 256),
   548  	}
   549  	for _, v := range invalidNames {
   550  		_, errors := validateDbEventSubscriptionName(v, "name")
   551  		if len(errors) == 0 {
   552  			t.Fatalf("%q should be an invalid RDS Event Subscription Name", v)
   553  		}
   554  	}
   555  }