|
6 | 6 |
|
7 | 7 | "github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
8 | 8 | "github.com/hashicorp/terraform-provider-google/google/acctest"
|
| 9 | + "github.com/hashicorp/terraform-provider-google/google/envvar" |
9 | 10 | )
|
10 | 11 |
|
11 | 12 | func TestAccSourceRepoRepository_basic(t *testing.T) {
|
@@ -89,3 +90,95 @@ func testAccSourceRepoRepository_extended(accountId string, topicName string, re
|
89 | 90 | }
|
90 | 91 | `, accountId, topicName, repositoryName)
|
91 | 92 | }
|
| 93 | + |
| 94 | +// Test setting create_ignore_already_exists on an existing resource |
| 95 | +func TestAccSourceRepoRepository_existingResourceCreateIgnoreAlreadyExists(t *testing.T) { |
| 96 | + t.Parallel() |
| 97 | + |
| 98 | + project := envvar.GetTestProjectFromEnv() |
| 99 | + repositoryName := fmt.Sprintf("source-repo-repository-test-%s", acctest.RandString(t, 10)) |
| 100 | + id := fmt.Sprintf("projects/%s/repos/%s", project, repositoryName) |
| 101 | + |
| 102 | + acctest.VcrTest(t, resource.TestCase{ |
| 103 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 104 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 105 | + CheckDestroy: testAccCheckSourceRepoRepositoryDestroyProducer(t), |
| 106 | + Steps: []resource.TestStep{ |
| 107 | + // The first step creates a new resource with create_ignore_already_exists=false |
| 108 | + { |
| 109 | + Config: testAccSourceRepoRepositoryCreateIgnoreAlreadyExists(repositoryName, false), |
| 110 | + Check: resource.TestCheckResourceAttr("google_sourcerepo_repository.acceptance", "id", id), |
| 111 | + }, |
| 112 | + { |
| 113 | + ResourceName: "google_sourcerepo_repository.acceptance", |
| 114 | + ImportStateId: id, |
| 115 | + ImportState: true, |
| 116 | + ImportStateVerify: true, |
| 117 | + ImportStateVerifyIgnore: []string{"create_ignore_already_exists"}, // Import leaves this field out when false |
| 118 | + }, |
| 119 | + // The second step updates the resource to have create_ignore_already_exists=true |
| 120 | + { |
| 121 | + Config: testAccSourceRepoRepositoryCreateIgnoreAlreadyExists(repositoryName, true), |
| 122 | + Check: resource.TestCheckResourceAttr("google_sourcerepo_repository.acceptance", "id", id), |
| 123 | + }, |
| 124 | + }, |
| 125 | + }) |
| 126 | +} |
| 127 | + |
| 128 | +// Test the option to ignore ALREADY_EXISTS error from creating a Source Repository. |
| 129 | +func TestAccSourceRepoRepository_createIgnoreAlreadyExists(t *testing.T) { |
| 130 | + t.Parallel() |
| 131 | + |
| 132 | + project := envvar.GetTestProjectFromEnv() |
| 133 | + repositoryName := fmt.Sprintf("source-repo-repository-test-%s", acctest.RandString(t, 10)) |
| 134 | + id := fmt.Sprintf("projects/%s/repos/%s", project, repositoryName) |
| 135 | + |
| 136 | + acctest.VcrTest(t, resource.TestCase{ |
| 137 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 138 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 139 | + CheckDestroy: testAccCheckSourceRepoRepositoryDestroyProducer(t), |
| 140 | + Steps: []resource.TestStep{ |
| 141 | + // The first step creates a basic Source Repository |
| 142 | + { |
| 143 | + Config: testAccSourceRepoRepository_basic(repositoryName), |
| 144 | + Check: resource.TestCheckResourceAttr("google_sourcerepo_repository.acceptance", "id", id), |
| 145 | + }, |
| 146 | + { |
| 147 | + ResourceName: "google_sourcerepo_repository.acceptance", |
| 148 | + ImportStateId: id, |
| 149 | + ImportState: true, |
| 150 | + ImportStateVerify: true, |
| 151 | + }, |
| 152 | + // The second step creates a new resource that duplicates with the existing Source Repository. |
| 153 | + { |
| 154 | + Config: testAccSourceRepoRepositoryDuplicateIgnoreAlreadyExists(repositoryName), |
| 155 | + Check: resource.ComposeTestCheckFunc( |
| 156 | + resource.TestCheckResourceAttr("google_sourcerepo_repository.acceptance", "id", id), |
| 157 | + resource.TestCheckResourceAttr("google_sourcerepo_repository.duplicate", "id", id), |
| 158 | + ), |
| 159 | + }, |
| 160 | + }, |
| 161 | + }) |
| 162 | +} |
| 163 | + |
| 164 | +func testAccSourceRepoRepositoryCreateIgnoreAlreadyExists(repositoryName string, ignore_already_exists bool) string { |
| 165 | + return fmt.Sprintf(` |
| 166 | +resource "google_sourcerepo_repository" "acceptance" { |
| 167 | + name = "%s" |
| 168 | + create_ignore_already_exists = %t |
| 169 | +} |
| 170 | +`, repositoryName, ignore_already_exists) |
| 171 | +} |
| 172 | + |
| 173 | +func testAccSourceRepoRepositoryDuplicateIgnoreAlreadyExists(repositoryName string) string { |
| 174 | + return fmt.Sprintf(` |
| 175 | +resource "google_sourcerepo_repository" "acceptance" { |
| 176 | + name = "%s" |
| 177 | +} |
| 178 | +
|
| 179 | +resource "google_sourcerepo_repository" "duplicate" { |
| 180 | + name = "%s" |
| 181 | + create_ignore_already_exists = true |
| 182 | +} |
| 183 | +`, repositoryName, repositoryName) |
| 184 | +} |
0 commit comments