github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  				),
    29  			},
    30  			{
    31  				Config: testAccAWSCodeBuildProjectConfig_basicUpdated(name),
    32  				Check: resource.ComposeTestCheckFunc(
    33  					testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func TestAccAWSCodeBuildProject_artifactsTypeValidation(t *testing.T) {
    41  	cases := []struct {
    42  		Value    string
    43  		ErrCount int
    44  	}{
    45  		{Value: "CODEPIPELINE", ErrCount: 0},
    46  		{Value: "NO_ARTIFACTS", ErrCount: 0},
    47  		{Value: "S3", ErrCount: 0},
    48  		{Value: "XYZ", ErrCount: 1},
    49  	}
    50  
    51  	for _, tc := range cases {
    52  		_, errors := validateAwsCodeBuildArifactsType(tc.Value, "aws_codebuild_project")
    53  
    54  		if len(errors) != tc.ErrCount {
    55  			t.Fatalf("Expected the AWS CodeBuild project artifacts type to trigger a validation error")
    56  		}
    57  	}
    58  }
    59  
    60  func TestAccAWSCodeBuildProject_artifactsNamespaceTypeValidation(t *testing.T) {
    61  	cases := []struct {
    62  		Value    string
    63  		ErrCount int
    64  	}{
    65  		{Value: "NONE", ErrCount: 0},
    66  		{Value: "BUILD_ID", ErrCount: 0},
    67  		{Value: "XYZ", ErrCount: 1},
    68  	}
    69  
    70  	for _, tc := range cases {
    71  		_, errors := validateAwsCodeBuildArifactsNamespaceType(tc.Value, "aws_codebuild_project")
    72  
    73  		if len(errors) != tc.ErrCount {
    74  			t.Fatalf("Expected the AWS CodeBuild project artifacts namepsace_type to trigger a validation error")
    75  		}
    76  	}
    77  }
    78  
    79  func longTestData() string {
    80  	data := `
    81  	test-test-test-test-test-test-test-test-test-test-
    82  	test-test-test-test-test-test-test-test-test-test-
    83  	test-test-test-test-test-test-test-test-test-test-
    84  	test-test-test-test-test-test-test-test-test-test-
    85  	test-test-test-test-test-test-test-test-test-test-
    86  	test-test-test-test-test-test-test-test-test-test-
    87  	`
    88  
    89  	return strings.Map(func(r rune) rune {
    90  		if unicode.IsSpace(r) {
    91  			return -1
    92  		}
    93  		return r
    94  	}, data)
    95  }
    96  
    97  func TestAccAWSCodeBuildProject_nameValidation(t *testing.T) {
    98  	cases := []struct {
    99  		Value    string
   100  		ErrCount int
   101  	}{
   102  		{Value: "_test", ErrCount: 1},
   103  		{Value: "test", ErrCount: 0},
   104  		{Value: "1_test", ErrCount: 0},
   105  		{Value: "test**1", ErrCount: 1},
   106  		{Value: longTestData(), ErrCount: 1},
   107  	}
   108  
   109  	for _, tc := range cases {
   110  		_, errors := validateAwsCodeBuildProjectName(tc.Value, "aws_codebuild_project")
   111  
   112  		if len(errors) != tc.ErrCount {
   113  			t.Fatalf("Expected the AWS CodeBuild project name to trigger a validation error - %s", errors)
   114  		}
   115  	}
   116  }
   117  
   118  func TestAccAWSCodeBuildProject_descriptionValidation(t *testing.T) {
   119  	cases := []struct {
   120  		Value    string
   121  		ErrCount int
   122  	}{
   123  		{Value: "test", ErrCount: 0},
   124  		{Value: longTestData(), ErrCount: 1},
   125  	}
   126  
   127  	for _, tc := range cases {
   128  		_, errors := validateAwsCodeBuildProjectDescription(tc.Value, "aws_codebuild_project")
   129  
   130  		if len(errors) != tc.ErrCount {
   131  			t.Fatalf("Expected the AWS CodeBuild project description to trigger a validation error")
   132  		}
   133  	}
   134  }
   135  
   136  func TestAccAWSCodeBuildProject_environmentComputeTypeValidation(t *testing.T) {
   137  	cases := []struct {
   138  		Value    string
   139  		ErrCount int
   140  	}{
   141  		{Value: "BUILD_GENERAL1_SMALL", ErrCount: 0},
   142  		{Value: "BUILD_GENERAL1_MEDIUM", ErrCount: 0},
   143  		{Value: "BUILD_GENERAL1_LARGE", ErrCount: 0},
   144  		{Value: "BUILD_GENERAL1_VERYLARGE", ErrCount: 1},
   145  	}
   146  
   147  	for _, tc := range cases {
   148  		_, errors := validateAwsCodeBuildEnvironmentComputeType(tc.Value, "aws_codebuild_project")
   149  
   150  		if len(errors) != tc.ErrCount {
   151  			t.Fatalf("Expected the AWS CodeBuild project environment compute_type to trigger a validation error")
   152  		}
   153  	}
   154  }
   155  
   156  func TestAccAWSCodeBuildProject_environmentTypeValidation(t *testing.T) {
   157  	cases := []struct {
   158  		Value    string
   159  		ErrCount int
   160  	}{
   161  		{Value: "LINUX_CONTAINER", ErrCount: 0},
   162  		{Value: "WINDOWS_CONTAINER", ErrCount: 1},
   163  	}
   164  
   165  	for _, tc := range cases {
   166  		_, errors := validateAwsCodeBuildEnvironmentType(tc.Value, "aws_codebuild_project")
   167  
   168  		if len(errors) != tc.ErrCount {
   169  			t.Fatalf("Expected the AWS CodeBuild project environment type to trigger a validation error")
   170  		}
   171  	}
   172  }
   173  
   174  func TestAccAWSCodeBuildProject_sourceTypeValidation(t *testing.T) {
   175  	cases := []struct {
   176  		Value    string
   177  		ErrCount int
   178  	}{
   179  		{Value: "CODECOMMIT", ErrCount: 0},
   180  		{Value: "CODEPIPELINE", ErrCount: 0},
   181  		{Value: "GITHUB", ErrCount: 0},
   182  		{Value: "S3", ErrCount: 0},
   183  		{Value: "GITLAB", ErrCount: 1},
   184  	}
   185  
   186  	for _, tc := range cases {
   187  		_, errors := validateAwsCodeBuildSourceType(tc.Value, "aws_codebuild_project")
   188  
   189  		if len(errors) != tc.ErrCount {
   190  			t.Fatalf("Expected the AWS CodeBuild project source type to trigger a validation error")
   191  		}
   192  	}
   193  }
   194  
   195  func TestAccAWSCodeBuildProject_sourceAuthTypeValidation(t *testing.T) {
   196  	cases := []struct {
   197  		Value    string
   198  		ErrCount int
   199  	}{
   200  		{Value: "OAUTH", ErrCount: 0},
   201  		{Value: "PASSWORD", ErrCount: 1},
   202  	}
   203  
   204  	for _, tc := range cases {
   205  		_, errors := validateAwsCodeBuildSourceAuthType(tc.Value, "aws_codebuild_project")
   206  
   207  		if len(errors) != tc.ErrCount {
   208  			t.Fatalf("Expected the AWS CodeBuild project source auth to trigger a validation error")
   209  		}
   210  	}
   211  }
   212  
   213  func TestAccAWSCodeBuildProject_timeoutValidation(t *testing.T) {
   214  	cases := []struct {
   215  		Value    int
   216  		ErrCount int
   217  	}{
   218  		{Value: 10, ErrCount: 0},
   219  		{Value: 200, ErrCount: 0},
   220  		{Value: 1, ErrCount: 1},
   221  		{Value: 500, ErrCount: 1},
   222  	}
   223  
   224  	for _, tc := range cases {
   225  		_, errors := validateAwsCodeBuildTimeout(tc.Value, "aws_codebuild_project")
   226  
   227  		if len(errors) != tc.ErrCount {
   228  			t.Fatalf("Expected the AWS CodeBuild project timeout to trigger a validation error")
   229  		}
   230  	}
   231  }
   232  
   233  func testAccCheckAWSCodeBuildProjectExists(n string) resource.TestCheckFunc {
   234  	return func(s *terraform.State) error {
   235  		rs, ok := s.RootModule().Resources[n]
   236  		if !ok {
   237  			return fmt.Errorf("Not found: %s", n)
   238  		}
   239  
   240  		if rs.Primary.ID == "" {
   241  			return fmt.Errorf("No CodeBuild Project ID is set")
   242  		}
   243  
   244  		conn := testAccProvider.Meta().(*AWSClient).codebuildconn
   245  
   246  		out, err := conn.BatchGetProjects(&codebuild.BatchGetProjectsInput{
   247  			Names: []*string{
   248  				aws.String(rs.Primary.ID),
   249  			},
   250  		})
   251  
   252  		if err != nil {
   253  			return err
   254  		}
   255  
   256  		if len(out.Projects) < 1 {
   257  			return fmt.Errorf("No project found")
   258  		}
   259  
   260  		return nil
   261  	}
   262  }
   263  
   264  func testAccCheckAWSCodeBuildProjectDestroy(s *terraform.State) error {
   265  	conn := testAccProvider.Meta().(*AWSClient).codebuildconn
   266  
   267  	for _, rs := range s.RootModule().Resources {
   268  		if rs.Type != "aws_codebuild_project" {
   269  			continue
   270  		}
   271  
   272  		out, err := conn.BatchGetProjects(&codebuild.BatchGetProjectsInput{
   273  			Names: []*string{
   274  				aws.String(rs.Primary.ID),
   275  			},
   276  		})
   277  
   278  		if err != nil {
   279  			return err
   280  		}
   281  
   282  		if out != nil && len(out.Projects) > 0 {
   283  			return fmt.Errorf("Expected AWS CodeBuild Project to be gone, but was still found")
   284  		}
   285  
   286  		return nil
   287  	}
   288  
   289  	return fmt.Errorf("Default error in CodeBuild Test")
   290  }
   291  
   292  func testAccAWSCodeBuildProjectConfig_basic(rName string) string {
   293  	return fmt.Sprintf(`
   294  resource "aws_iam_role" "codebuild_role" {
   295    name = "codebuild-role-%s"
   296    assume_role_policy = <<EOF
   297  {
   298    "Version": "2012-10-17",
   299    "Statement": [
   300      {
   301        "Effect": "Allow",
   302        "Principal": {
   303          "Service": "codebuild.amazonaws.com"
   304        },
   305        "Action": "sts:AssumeRole"
   306      }
   307    ]
   308  }
   309  EOF
   310  }
   311  
   312  resource "aws_iam_policy" "codebuild_policy" {
   313      name        = "codebuild-policy-%s"
   314      path        = "/service-role/"
   315      description = "Policy used in trust relationship with CodeBuild"
   316      policy      = <<POLICY
   317  {
   318    "Version": "2012-10-17",
   319    "Statement": [
   320      {
   321        "Effect": "Allow",
   322        "Resource": [
   323          "*"
   324        ],
   325        "Action": [
   326          "logs:CreateLogGroup",
   327          "logs:CreateLogStream",
   328          "logs:PutLogEvents"
   329        ]
   330      }
   331    ]
   332  }
   333  POLICY
   334  }
   335  
   336  resource "aws_iam_policy_attachment" "codebuild_policy_attachment" {
   337    name       = "codebuild-policy-attachment-%s"
   338    policy_arn = "${aws_iam_policy.codebuild_policy.arn}"
   339    roles      = ["${aws_iam_role.codebuild_role.id}"]
   340  }
   341  
   342  resource "aws_codebuild_project" "foo" {
   343    name         = "test-project-%s"
   344    description  = "test_codebuild_project"
   345    timeout      = "5"
   346  	service_role = "${aws_iam_role.codebuild_role.arn}"
   347  
   348  	artifacts {
   349  		type = "NO_ARTIFACTS"
   350  	}
   351  
   352    environment {
   353      compute_type = "BUILD_GENERAL1_SMALL"
   354      image        = "2"
   355      type         = "LINUX_CONTAINER"
   356  
   357  		environment_variable = {
   358  			"name"  = "SOME_KEY"
   359  			"value" = "SOME_VALUE"
   360  		}
   361    }
   362  
   363    source {
   364      auth {
   365        type = "OAUTH"
   366      }
   367  
   368      type     = "GITHUB"
   369      location = "https://github.com/mitchellh/packer.git"
   370    }
   371  
   372    tags {
   373      "Environment" = "Test"
   374    }
   375  }
   376  `, rName, rName, rName, rName)
   377  }
   378  
   379  func testAccAWSCodeBuildProjectConfig_basicUpdated(rName string) string {
   380  	return fmt.Sprintf(`
   381  resource "aws_iam_role" "codebuild_role" {
   382    name = "codebuild-role-%s"
   383    assume_role_policy = <<EOF
   384  {
   385    "Version": "2012-10-17",
   386    "Statement": [
   387      {
   388        "Effect": "Allow",
   389        "Principal": {
   390          "Service": "codebuild.amazonaws.com"
   391        },
   392        "Action": "sts:AssumeRole"
   393      }
   394    ]
   395  }
   396  EOF
   397  }
   398  
   399  resource "aws_iam_policy" "codebuild_policy" {
   400      name        = "codebuild-policy-%s"
   401      path        = "/service-role/"
   402      description = "Policy used in trust relationship with CodeBuild"
   403      policy      = <<POLICY
   404  {
   405    "Version": "2012-10-17",
   406    "Statement": [
   407      {
   408        "Effect": "Allow",
   409        "Resource": [
   410          "*"
   411        ],
   412        "Action": [
   413          "logs:CreateLogGroup",
   414          "logs:CreateLogStream",
   415          "logs:PutLogEvents"
   416        ]
   417      }
   418    ]
   419  }
   420  POLICY
   421  }
   422  
   423  resource "aws_iam_policy_attachment" "codebuild_policy_attachment" {
   424    name       = "codebuild-policy-attachment-%s"
   425    policy_arn = "${aws_iam_policy.codebuild_policy.arn}"
   426    roles      = ["${aws_iam_role.codebuild_role.id}"]
   427  }
   428  
   429  resource "aws_codebuild_project" "foo" {
   430    name         = "test-project-%s"
   431    description  = "test_codebuild_project"
   432    timeout      = "5"
   433  	service_role = "${aws_iam_role.codebuild_role.arn}"
   434  
   435  	artifacts {
   436  		type = "NO_ARTIFACTS"
   437  	}
   438  
   439    environment {
   440      compute_type = "BUILD_GENERAL1_SMALL"
   441      image        = "2"
   442      type         = "LINUX_CONTAINER"
   443  
   444  		environment_variable = {
   445  			"name"  = "SOME_OTHERKEY"
   446  			"value" = "SOME_OTHERVALUE"
   447  		}
   448    }
   449  
   450    source {
   451      auth {
   452        type = "OAUTH"
   453      }
   454  
   455      type     = "GITHUB"
   456      location = "https://github.com/mitchellh/packer.git"
   457    }
   458  
   459    tags {
   460      "Environment" = "Test"
   461    }
   462  }
   463  `, rName, rName, rName, rName)
   464  }