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