github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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 }