|
7 | 7 | "github.com/hashicorp/terraform/helper/acctest"
|
8 | 8 | "github.com/hashicorp/terraform/helper/resource"
|
9 | 9 | "github.com/hashicorp/terraform/terraform"
|
| 10 | + cloudbuild "google.golang.org/api/cloudbuild/v1" |
10 | 11 | )
|
11 | 12 |
|
12 | 13 | func TestAccCloudBuildTrigger_basic(t *testing.T) {
|
@@ -37,24 +38,80 @@ func TestAccCloudBuildTrigger_basic(t *testing.T) {
|
37 | 38 | })
|
38 | 39 | }
|
39 | 40 |
|
| 41 | +func TestAccCloudBuildTrigger_filename(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + |
| 44 | + projectID := "terraform-" + acctest.RandString(10) |
| 45 | + projectOrg := getTestOrgFromEnv(t) |
| 46 | + projectBillingAccount := getTestBillingAccountFromEnv(t) |
| 47 | + |
| 48 | + resource.Test(t, resource.TestCase{ |
| 49 | + PreCheck: func() { testAccPreCheck(t) }, |
| 50 | + Providers: testAccProviders, |
| 51 | + CheckDestroy: testAccCheckGoogleCloudBuildTriggerVersionsDestroyed, |
| 52 | + Steps: []resource.TestStep{ |
| 53 | + resource.TestStep{ |
| 54 | + Config: testGoogleCloudBuildTrigger_filename(projectID, projectOrg, projectBillingAccount), |
| 55 | + Check: resource.ComposeTestCheckFunc( |
| 56 | + testAccCheckGoogleCloudFilenameConfig("google_cloudbuild_trigger.filename_build_trigger"), |
| 57 | + ), |
| 58 | + }, |
| 59 | + resource.TestStep{ |
| 60 | + Config: testGoogleCloudBuildTrigger_removed(projectID, projectOrg, projectBillingAccount), |
| 61 | + Check: resource.ComposeTestCheckFunc( |
| 62 | + testAccCheckGoogleCloudBuildTriggerWasRemovedFromState("google_cloudbuild_trigger.filename_build_trigger"), |
| 63 | + ), |
| 64 | + }, |
| 65 | + }, |
| 66 | + }) |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +func testAccGetBuildTrigger(s *terraform.State, resourceName string) (*cloudbuild.BuildTrigger, error) { |
| 71 | + rs, ok := s.RootModule().Resources[resourceName] |
| 72 | + if !ok { |
| 73 | + return nil, fmt.Errorf("Resource not found: %s", resourceName) |
| 74 | + } |
| 75 | + |
| 76 | + if rs.Primary.ID == "" { |
| 77 | + return nil, fmt.Errorf("No ID is set") |
| 78 | + } |
| 79 | + |
| 80 | + config := testAccProvider.Meta().(*Config) |
| 81 | + project := rs.Primary.Attributes["project"] |
| 82 | + |
| 83 | + trigger, err := config.clientBuild.Projects.Triggers.Get(project, rs.Primary.ID).Do() |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("Trigger does not exist") |
| 86 | + } |
| 87 | + |
| 88 | + return trigger, nil |
| 89 | +} |
| 90 | + |
40 | 91 | func testAccCheckGoogleCloudBuildTriggerExists(resourceName string) resource.TestCheckFunc {
|
41 | 92 | return func(s *terraform.State) error {
|
| 93 | + _, err := testAccGetBuildTrigger(s, resourceName) |
42 | 94 |
|
43 |
| - rs, ok := s.RootModule().Resources[resourceName] |
44 |
| - if !ok { |
45 |
| - return fmt.Errorf("Resource not found: %s", resourceName) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("Trigger does not exist") |
46 | 97 | }
|
47 | 98 |
|
48 |
| - if rs.Primary.ID == "" { |
49 |
| - return fmt.Errorf("No ID is set") |
50 |
| - } |
51 |
| - config := testAccProvider.Meta().(*Config) |
52 |
| - project := rs.Primary.Attributes["project"] |
| 99 | + return nil |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func testAccCheckGoogleCloudFilenameConfig(resourceName string) resource.TestCheckFunc { |
| 104 | + return func(s *terraform.State) error { |
| 105 | + trigger, err := testAccGetBuildTrigger(s, resourceName) |
53 | 106 |
|
54 |
| - _, err := config.clientBuild.Projects.Triggers.Get(project, rs.Primary.ID).Do() |
55 | 107 | if err != nil {
|
56 | 108 | return fmt.Errorf("Trigger does not exist")
|
57 | 109 | }
|
| 110 | + |
| 111 | + if trigger.Filename != "cloudbuild.yaml" { |
| 112 | + return fmt.Errorf("Config filename mismatch: %s", trigger.Filename) |
| 113 | + } |
| 114 | + |
58 | 115 | return nil
|
59 | 116 | }
|
60 | 117 | }
|
@@ -147,6 +204,40 @@ resource "google_cloudbuild_trigger" "build_trigger" {
|
147 | 204 | `, projectID, projectID, projectOrg, projectBillingAccount)
|
148 | 205 | }
|
149 | 206 |
|
| 207 | +func testGoogleCloudBuildTrigger_filename(projectID, projectOrg, projectBillingAccount string) string { |
| 208 | + return fmt.Sprintf(` |
| 209 | +resource "google_project" "acceptance" { |
| 210 | + name = "%s" |
| 211 | + project_id = "%s" |
| 212 | + org_id = "%s" |
| 213 | + billing_account = "%s" |
| 214 | +} |
| 215 | +
|
| 216 | +resource "google_project_services" "acceptance" { |
| 217 | + project = "${google_project.acceptance.project_id}" |
| 218 | +
|
| 219 | + services = [ |
| 220 | + "cloudbuild.googleapis.com", |
| 221 | + "containerregistry.googleapis.com", |
| 222 | + "logging.googleapis.com", |
| 223 | + "pubsub.googleapis.com", |
| 224 | + "storage-api.googleapis.com", |
| 225 | + ] |
| 226 | +} |
| 227 | +
|
| 228 | +resource "google_cloudbuild_trigger" "filename_build_trigger" { |
| 229 | + project = "${google_project_services.acceptance.project}" |
| 230 | + description = "acceptance test build trigger" |
| 231 | + trigger_template { |
| 232 | + branch_name = "master" |
| 233 | + project = "${google_project_services.acceptance.project}" |
| 234 | + repo_name = "some-repo" |
| 235 | + } |
| 236 | + filename = "cloudbuild.yaml" |
| 237 | +} |
| 238 | + `, projectID, projectID, projectOrg, projectBillingAccount) |
| 239 | +} |
| 240 | + |
150 | 241 | func testGoogleCloudBuildTrigger_removed(projectID, projectOrg, projectBillingAccount string) string {
|
151 | 242 | return fmt.Sprintf(`
|
152 | 243 | resource "google_project" "acceptance" {
|
|
0 commit comments