github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/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      type     = "GITHUB"
   397      location = "https://github.com/hashicorp/packer.git"
   398    }
   399  
   400    tags {
   401      "Environment" = "Test"
   402    }
   403  }
   404  `, rName, rName, rName, rName)
   405  }
   406  
   407  func testAccAWSCodeBuildProjectConfig_basicUpdated(rName string) string {
   408  	return fmt.Sprintf(`
   409  resource "aws_iam_role" "codebuild_role" {
   410    name = "codebuild-role-%s"
   411    assume_role_policy = <<EOF
   412  {
   413    "Version": "2012-10-17",
   414    "Statement": [
   415      {
   416        "Effect": "Allow",
   417        "Principal": {
   418          "Service": "codebuild.amazonaws.com"
   419        },
   420        "Action": "sts:AssumeRole"
   421      }
   422    ]
   423  }
   424  EOF
   425  }
   426  
   427  resource "aws_iam_policy" "codebuild_policy" {
   428      name        = "codebuild-policy-%s"
   429      path        = "/service-role/"
   430      description = "Policy used in trust relationship with CodeBuild"
   431      policy      = <<POLICY
   432  {
   433    "Version": "2012-10-17",
   434    "Statement": [
   435      {
   436        "Effect": "Allow",
   437        "Resource": [
   438          "*"
   439        ],
   440        "Action": [
   441          "logs:CreateLogGroup",
   442          "logs:CreateLogStream",
   443          "logs:PutLogEvents"
   444        ]
   445      }
   446    ]
   447  }
   448  POLICY
   449  }
   450  
   451  resource "aws_iam_policy_attachment" "codebuild_policy_attachment" {
   452    name       = "codebuild-policy-attachment-%s"
   453    policy_arn = "${aws_iam_policy.codebuild_policy.arn}"
   454    roles      = ["${aws_iam_role.codebuild_role.id}"]
   455  }
   456  
   457  resource "aws_codebuild_project" "foo" {
   458    name         = "test-project-%s"
   459    description  = "test_codebuild_project"
   460    build_timeout      = "50"
   461  	service_role = "${aws_iam_role.codebuild_role.arn}"
   462  
   463  	artifacts {
   464  		type = "NO_ARTIFACTS"
   465  	}
   466  
   467    environment {
   468      compute_type = "BUILD_GENERAL1_SMALL"
   469      image        = "2"
   470      type         = "LINUX_CONTAINER"
   471  
   472  		environment_variable = {
   473  			"name"  = "SOME_OTHERKEY"
   474  			"value" = "SOME_OTHERVALUE"
   475  		}
   476    }
   477  
   478    source {
   479      type     = "GITHUB"
   480      location = "https://github.com/hashicorp/packer.git"
   481    }
   482  
   483    tags {
   484      "Environment" = "Test"
   485    }
   486  }
   487  `, rName, rName, rName, rName)
   488  }
   489  
   490  func testAccAWSCodeBuildProjectConfig_default_timeout(rName string) string {
   491  	return fmt.Sprintf(`
   492  resource "aws_iam_role" "codebuild_role" {
   493    name = "codebuild-role-%s"
   494    assume_role_policy = <<EOF
   495  {
   496    "Version": "2012-10-17",
   497    "Statement": [
   498      {
   499        "Effect": "Allow",
   500        "Principal": {
   501          "Service": "codebuild.amazonaws.com"
   502        },
   503        "Action": "sts:AssumeRole"
   504      }
   505    ]
   506  }
   507  EOF
   508  }
   509  
   510  resource "aws_iam_policy" "codebuild_policy" {
   511      name        = "codebuild-policy-%s"
   512      path        = "/service-role/"
   513      description = "Policy used in trust relationship with CodeBuild"
   514      policy      = <<POLICY
   515  {
   516    "Version": "2012-10-17",
   517    "Statement": [
   518      {
   519        "Effect": "Allow",
   520        "Resource": [
   521          "*"
   522        ],
   523        "Action": [
   524          "logs:CreateLogGroup",
   525          "logs:CreateLogStream",
   526          "logs:PutLogEvents"
   527        ]
   528      }
   529    ]
   530  }
   531  POLICY
   532  }
   533  
   534  resource "aws_iam_policy_attachment" "codebuild_policy_attachment" {
   535    name       = "codebuild-policy-attachment-%s"
   536    policy_arn = "${aws_iam_policy.codebuild_policy.arn}"
   537    roles      = ["${aws_iam_role.codebuild_role.id}"]
   538  }
   539  
   540  resource "aws_codebuild_project" "foo" {
   541    name         = "test-project-%s"
   542    description  = "test_codebuild_project"
   543  
   544  	service_role = "${aws_iam_role.codebuild_role.arn}"
   545  
   546  	artifacts {
   547  		type = "NO_ARTIFACTS"
   548  	}
   549  
   550    environment {
   551      compute_type = "BUILD_GENERAL1_SMALL"
   552      image        = "2"
   553      type         = "LINUX_CONTAINER"
   554  
   555  		environment_variable = {
   556  			"name"  = "SOME_OTHERKEY"
   557  			"value" = "SOME_OTHERVALUE"
   558  		}
   559    }
   560  
   561    source {
   562      type     = "GITHUB"
   563      location = "https://github.com/hashicorp/packer.git"
   564    }
   565  
   566    tags {
   567      "Environment" = "Test"
   568    }
   569  }
   570  `, rName, rName, rName, rName)
   571  }