github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_codebuild_project_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 "unicode" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/service/codebuild" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSCodeBuildProject_basic(t *testing.T) { 17 name := acctest.RandString(10) 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy, 23 Steps: []resource.TestStep{ 24 { 25 Config: testAccAWSCodeBuildProjectConfig_basic(name), 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"), 28 resource.TestCheckResourceAttr( 29 "aws_codebuild_project.foo", "build_timeout", "5"), 30 ), 31 }, 32 { 33 Config: testAccAWSCodeBuildProjectConfig_basicUpdated(name), 34 Check: resource.ComposeTestCheckFunc( 35 testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"), 36 resource.TestCheckResourceAttr( 37 "aws_codebuild_project.foo", "build_timeout", "50"), 38 ), 39 }, 40 }, 41 }) 42 } 43 44 func TestAccAWSCodeBuildProject_default_build_timeout(t *testing.T) { 45 name := acctest.RandString(10) 46 47 resource.Test(t, resource.TestCase{ 48 PreCheck: func() { testAccPreCheck(t) }, 49 Providers: testAccProviders, 50 CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy, 51 Steps: []resource.TestStep{ 52 { 53 Config: testAccAWSCodeBuildProjectConfig_default_timeout(name), 54 Check: resource.ComposeTestCheckFunc( 55 testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"), 56 resource.TestCheckResourceAttr( 57 "aws_codebuild_project.foo", "build_timeout", "60"), 58 ), 59 }, 60 { 61 Config: testAccAWSCodeBuildProjectConfig_basicUpdated(name), 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"), 64 resource.TestCheckResourceAttr( 65 "aws_codebuild_project.foo", "build_timeout", "50"), 66 ), 67 }, 68 }, 69 }) 70 } 71 72 func TestAWSCodeBuildProject_artifactsTypeValidation(t *testing.T) { 73 cases := []struct { 74 Value string 75 ErrCount int 76 }{ 77 {Value: "CODEPIPELINE", ErrCount: 0}, 78 {Value: "NO_ARTIFACTS", ErrCount: 0}, 79 {Value: "S3", ErrCount: 0}, 80 {Value: "XYZ", ErrCount: 1}, 81 } 82 83 for _, tc := range cases { 84 _, errors := validateAwsCodeBuildArifactsType(tc.Value, "aws_codebuild_project") 85 86 if len(errors) != tc.ErrCount { 87 t.Fatalf("Expected the AWS CodeBuild project artifacts type to trigger a validation error") 88 } 89 } 90 } 91 92 func TestAWSCodeBuildProject_artifactsNamespaceTypeValidation(t *testing.T) { 93 cases := []struct { 94 Value string 95 ErrCount int 96 }{ 97 {Value: "NONE", ErrCount: 0}, 98 {Value: "BUILD_ID", ErrCount: 0}, 99 {Value: "XYZ", ErrCount: 1}, 100 } 101 102 for _, tc := range cases { 103 _, errors := validateAwsCodeBuildArifactsNamespaceType(tc.Value, "aws_codebuild_project") 104 105 if len(errors) != tc.ErrCount { 106 t.Fatalf("Expected the AWS CodeBuild project artifacts namepsace_type to trigger a validation error") 107 } 108 } 109 } 110 111 func longTestData() string { 112 data := ` 113 test-test-test-test-test-test-test-test-test-test- 114 test-test-test-test-test-test-test-test-test-test- 115 test-test-test-test-test-test-test-test-test-test- 116 test-test-test-test-test-test-test-test-test-test- 117 test-test-test-test-test-test-test-test-test-test- 118 test-test-test-test-test-test-test-test-test-test- 119 ` 120 121 return strings.Map(func(r rune) rune { 122 if unicode.IsSpace(r) { 123 return -1 124 } 125 return r 126 }, data) 127 } 128 129 func TestAWSCodeBuildProject_nameValidation(t *testing.T) { 130 cases := []struct { 131 Value string 132 ErrCount int 133 }{ 134 {Value: "_test", ErrCount: 1}, 135 {Value: "test", ErrCount: 0}, 136 {Value: "1_test", ErrCount: 0}, 137 {Value: "test**1", ErrCount: 1}, 138 {Value: longTestData(), ErrCount: 1}, 139 } 140 141 for _, tc := range cases { 142 _, errors := validateAwsCodeBuildProjectName(tc.Value, "aws_codebuild_project") 143 144 if len(errors) != tc.ErrCount { 145 t.Fatalf("Expected the AWS CodeBuild project name to trigger a validation error - %s", errors) 146 } 147 } 148 } 149 150 func TestAWSCodeBuildProject_descriptionValidation(t *testing.T) { 151 cases := []struct { 152 Value string 153 ErrCount int 154 }{ 155 {Value: "test", ErrCount: 0}, 156 {Value: longTestData(), ErrCount: 1}, 157 } 158 159 for _, tc := range cases { 160 _, errors := validateAwsCodeBuildProjectDescription(tc.Value, "aws_codebuild_project") 161 162 if len(errors) != tc.ErrCount { 163 t.Fatalf("Expected the AWS CodeBuild project description to trigger a validation error") 164 } 165 } 166 } 167 168 func TestAWSCodeBuildProject_environmentComputeTypeValidation(t *testing.T) { 169 cases := []struct { 170 Value string 171 ErrCount int 172 }{ 173 {Value: "BUILD_GENERAL1_SMALL", ErrCount: 0}, 174 {Value: "BUILD_GENERAL1_MEDIUM", ErrCount: 0}, 175 {Value: "BUILD_GENERAL1_LARGE", ErrCount: 0}, 176 {Value: "BUILD_GENERAL1_VERYLARGE", ErrCount: 1}, 177 } 178 179 for _, tc := range cases { 180 _, errors := validateAwsCodeBuildEnvironmentComputeType(tc.Value, "aws_codebuild_project") 181 182 if len(errors) != tc.ErrCount { 183 t.Fatalf("Expected the AWS CodeBuild project environment compute_type to trigger a validation error") 184 } 185 } 186 } 187 188 func TestAWSCodeBuildProject_environmentTypeValidation(t *testing.T) { 189 cases := []struct { 190 Value string 191 ErrCount int 192 }{ 193 {Value: "LINUX_CONTAINER", ErrCount: 0}, 194 {Value: "WINDOWS_CONTAINER", ErrCount: 1}, 195 } 196 197 for _, tc := range cases { 198 _, errors := validateAwsCodeBuildEnvironmentType(tc.Value, "aws_codebuild_project") 199 200 if len(errors) != tc.ErrCount { 201 t.Fatalf("Expected the AWS CodeBuild project environment type to trigger a validation error") 202 } 203 } 204 } 205 206 func TestAWSCodeBuildProject_sourceTypeValidation(t *testing.T) { 207 cases := []struct { 208 Value string 209 ErrCount int 210 }{ 211 {Value: "CODECOMMIT", ErrCount: 0}, 212 {Value: "CODEPIPELINE", ErrCount: 0}, 213 {Value: "GITHUB", ErrCount: 0}, 214 {Value: "S3", ErrCount: 0}, 215 {Value: "GITLAB", ErrCount: 1}, 216 } 217 218 for _, tc := range cases { 219 _, errors := validateAwsCodeBuildSourceType(tc.Value, "aws_codebuild_project") 220 221 if len(errors) != tc.ErrCount { 222 t.Fatalf("Expected the AWS CodeBuild project source type to trigger a validation error") 223 } 224 } 225 } 226 227 func TestAWSCodeBuildProject_sourceAuthTypeValidation(t *testing.T) { 228 cases := []struct { 229 Value string 230 ErrCount int 231 }{ 232 {Value: "OAUTH", ErrCount: 0}, 233 {Value: "PASSWORD", ErrCount: 1}, 234 } 235 236 for _, tc := range cases { 237 _, errors := validateAwsCodeBuildSourceAuthType(tc.Value, "aws_codebuild_project") 238 239 if len(errors) != tc.ErrCount { 240 t.Fatalf("Expected the AWS CodeBuild project source auth to trigger a validation error") 241 } 242 } 243 } 244 245 func TestAWSCodeBuildProject_timeoutValidation(t *testing.T) { 246 cases := []struct { 247 Value int 248 ErrCount int 249 }{ 250 {Value: 10, ErrCount: 0}, 251 {Value: 200, ErrCount: 0}, 252 {Value: 1, ErrCount: 1}, 253 {Value: 500, ErrCount: 1}, 254 } 255 256 for _, tc := range cases { 257 _, errors := validateAwsCodeBuildTimeout(tc.Value, "aws_codebuild_project") 258 259 if len(errors) != tc.ErrCount { 260 t.Fatalf("Expected the AWS CodeBuild project timeout to trigger a validation error") 261 } 262 } 263 } 264 265 func testAccCheckAWSCodeBuildProjectExists(n string) resource.TestCheckFunc { 266 return func(s *terraform.State) error { 267 rs, ok := s.RootModule().Resources[n] 268 if !ok { 269 return fmt.Errorf("Not found: %s", n) 270 } 271 272 if rs.Primary.ID == "" { 273 return fmt.Errorf("No CodeBuild Project ID is set") 274 } 275 276 conn := testAccProvider.Meta().(*AWSClient).codebuildconn 277 278 out, err := conn.BatchGetProjects(&codebuild.BatchGetProjectsInput{ 279 Names: []*string{ 280 aws.String(rs.Primary.ID), 281 }, 282 }) 283 284 if err != nil { 285 return err 286 } 287 288 if len(out.Projects) < 1 { 289 return fmt.Errorf("No project found") 290 } 291 292 return nil 293 } 294 } 295 296 func testAccCheckAWSCodeBuildProjectDestroy(s *terraform.State) error { 297 conn := testAccProvider.Meta().(*AWSClient).codebuildconn 298 299 for _, rs := range s.RootModule().Resources { 300 if rs.Type != "aws_codebuild_project" { 301 continue 302 } 303 304 out, err := conn.BatchGetProjects(&codebuild.BatchGetProjectsInput{ 305 Names: []*string{ 306 aws.String(rs.Primary.ID), 307 }, 308 }) 309 310 if err != nil { 311 return err 312 } 313 314 if out != nil && len(out.Projects) > 0 { 315 return fmt.Errorf("Expected AWS CodeBuild Project to be gone, but was still found") 316 } 317 318 return nil 319 } 320 321 return fmt.Errorf("Default error in CodeBuild Test") 322 } 323 324 func testAccAWSCodeBuildProjectConfig_basic(rName string) string { 325 return fmt.Sprintf(` 326 resource "aws_iam_role" "codebuild_role" { 327 name = "codebuild-role-%s" 328 assume_role_policy = <<EOF 329 { 330 "Version": "2012-10-17", 331 "Statement": [ 332 { 333 "Effect": "Allow", 334 "Principal": { 335 "Service": "codebuild.amazonaws.com" 336 }, 337 "Action": "sts:AssumeRole" 338 } 339 ] 340 } 341 EOF 342 } 343 344 resource "aws_iam_policy" "codebuild_policy" { 345 name = "codebuild-policy-%s" 346 path = "/service-role/" 347 description = "Policy used in trust relationship with CodeBuild" 348 policy = <<POLICY 349 { 350 "Version": "2012-10-17", 351 "Statement": [ 352 { 353 "Effect": "Allow", 354 "Resource": [ 355 "*" 356 ], 357 "Action": [ 358 "logs:CreateLogGroup", 359 "logs:CreateLogStream", 360 "logs:PutLogEvents" 361 ] 362 } 363 ] 364 } 365 POLICY 366 } 367 368 resource "aws_iam_policy_attachment" "codebuild_policy_attachment" { 369 name = "codebuild-policy-attachment-%s" 370 policy_arn = "${aws_iam_policy.codebuild_policy.arn}" 371 roles = ["${aws_iam_role.codebuild_role.id}"] 372 } 373 374 resource "aws_codebuild_project" "foo" { 375 name = "test-project-%s" 376 description = "test_codebuild_project" 377 build_timeout = "5" 378 service_role = "${aws_iam_role.codebuild_role.arn}" 379 380 artifacts { 381 type = "NO_ARTIFACTS" 382 } 383 384 environment { 385 compute_type = "BUILD_GENERAL1_SMALL" 386 image = "2" 387 type = "LINUX_CONTAINER" 388 389 environment_variable = { 390 "name" = "SOME_KEY" 391 "value" = "SOME_VALUE" 392 } 393 } 394 395 source { 396 auth { 397 type = "OAUTH" 398 } 399 400 type = "GITHUB" 401 location = "https://github.com/mitchellh/packer.git" 402 } 403 404 tags { 405 "Environment" = "Test" 406 } 407 } 408 `, rName, rName, rName, rName) 409 } 410 411 func testAccAWSCodeBuildProjectConfig_basicUpdated(rName string) string { 412 return fmt.Sprintf(` 413 resource "aws_iam_role" "codebuild_role" { 414 name = "codebuild-role-%s" 415 assume_role_policy = <<EOF 416 { 417 "Version": "2012-10-17", 418 "Statement": [ 419 { 420 "Effect": "Allow", 421 "Principal": { 422 "Service": "codebuild.amazonaws.com" 423 }, 424 "Action": "sts:AssumeRole" 425 } 426 ] 427 } 428 EOF 429 } 430 431 resource "aws_iam_policy" "codebuild_policy" { 432 name = "codebuild-policy-%s" 433 path = "/service-role/" 434 description = "Policy used in trust relationship with CodeBuild" 435 policy = <<POLICY 436 { 437 "Version": "2012-10-17", 438 "Statement": [ 439 { 440 "Effect": "Allow", 441 "Resource": [ 442 "*" 443 ], 444 "Action": [ 445 "logs:CreateLogGroup", 446 "logs:CreateLogStream", 447 "logs:PutLogEvents" 448 ] 449 } 450 ] 451 } 452 POLICY 453 } 454 455 resource "aws_iam_policy_attachment" "codebuild_policy_attachment" { 456 name = "codebuild-policy-attachment-%s" 457 policy_arn = "${aws_iam_policy.codebuild_policy.arn}" 458 roles = ["${aws_iam_role.codebuild_role.id}"] 459 } 460 461 resource "aws_codebuild_project" "foo" { 462 name = "test-project-%s" 463 description = "test_codebuild_project" 464 build_timeout = "50" 465 service_role = "${aws_iam_role.codebuild_role.arn}" 466 467 artifacts { 468 type = "NO_ARTIFACTS" 469 } 470 471 environment { 472 compute_type = "BUILD_GENERAL1_SMALL" 473 image = "2" 474 type = "LINUX_CONTAINER" 475 476 environment_variable = { 477 "name" = "SOME_OTHERKEY" 478 "value" = "SOME_OTHERVALUE" 479 } 480 } 481 482 source { 483 auth { 484 type = "OAUTH" 485 } 486 487 type = "GITHUB" 488 location = "https://github.com/mitchellh/packer.git" 489 } 490 491 tags { 492 "Environment" = "Test" 493 } 494 } 495 `, rName, rName, rName, rName) 496 } 497 498 func testAccAWSCodeBuildProjectConfig_default_timeout(rName string) string { 499 return fmt.Sprintf(` 500 resource "aws_iam_role" "codebuild_role" { 501 name = "codebuild-role-%s" 502 assume_role_policy = <<EOF 503 { 504 "Version": "2012-10-17", 505 "Statement": [ 506 { 507 "Effect": "Allow", 508 "Principal": { 509 "Service": "codebuild.amazonaws.com" 510 }, 511 "Action": "sts:AssumeRole" 512 } 513 ] 514 } 515 EOF 516 } 517 518 resource "aws_iam_policy" "codebuild_policy" { 519 name = "codebuild-policy-%s" 520 path = "/service-role/" 521 description = "Policy used in trust relationship with CodeBuild" 522 policy = <<POLICY 523 { 524 "Version": "2012-10-17", 525 "Statement": [ 526 { 527 "Effect": "Allow", 528 "Resource": [ 529 "*" 530 ], 531 "Action": [ 532 "logs:CreateLogGroup", 533 "logs:CreateLogStream", 534 "logs:PutLogEvents" 535 ] 536 } 537 ] 538 } 539 POLICY 540 } 541 542 resource "aws_iam_policy_attachment" "codebuild_policy_attachment" { 543 name = "codebuild-policy-attachment-%s" 544 policy_arn = "${aws_iam_policy.codebuild_policy.arn}" 545 roles = ["${aws_iam_role.codebuild_role.id}"] 546 } 547 548 resource "aws_codebuild_project" "foo" { 549 name = "test-project-%s" 550 description = "test_codebuild_project" 551 552 service_role = "${aws_iam_role.codebuild_role.arn}" 553 554 artifacts { 555 type = "NO_ARTIFACTS" 556 } 557 558 environment { 559 compute_type = "BUILD_GENERAL1_SMALL" 560 image = "2" 561 type = "LINUX_CONTAINER" 562 563 environment_variable = { 564 "name" = "SOME_OTHERKEY" 565 "value" = "SOME_OTHERVALUE" 566 } 567 } 568 569 source { 570 auth { 571 type = "OAUTH" 572 } 573 574 type = "GITHUB" 575 location = "https://github.com/mitchellh/packer.git" 576 } 577 578 tags { 579 "Environment" = "Test" 580 } 581 } 582 `, rName, rName, rName, rName) 583 }