forked from hashicorp/terraform-provider-google-beta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource_sourcerepo_repository_test.go
186 lines (167 loc) · 6.32 KB
/
resource_sourcerepo_repository_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package sourcerepo_test
import (
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
)
func TestAccSourceRepoRepository_basic(t *testing.T) {
t.Parallel()
repositoryName := fmt.Sprintf("source-repo-repository-test-%s", acctest.RandString(t, 10))
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckSourceRepoRepositoryDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccSourceRepoRepository_basic(repositoryName),
},
{
ResourceName: "google_sourcerepo_repository.acceptance",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccSourceRepoRepository_update(t *testing.T) {
t.Parallel()
repositoryName := fmt.Sprintf("source-repo-repository-test-%s", acctest.RandString(t, 10))
accountId := fmt.Sprintf("account-id-%s", acctest.RandString(t, 10))
topicName := fmt.Sprintf("topic-name-%s", acctest.RandString(t, 10))
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckSourceRepoRepositoryDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccSourceRepoRepository_basic(repositoryName),
},
{
ResourceName: "google_sourcerepo_repository.acceptance",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccSourceRepoRepository_extended(accountId, topicName, repositoryName),
},
{
ResourceName: "google_sourcerepo_repository.acceptance",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccSourceRepoRepository_basic(repositoryName string) string {
return fmt.Sprintf(`
resource "google_sourcerepo_repository" "acceptance" {
name = "%s"
}
`, repositoryName)
}
func testAccSourceRepoRepository_extended(accountId string, topicName string, repositoryName string) string {
return fmt.Sprintf(`
resource "google_service_account" "test-account" {
account_id = "%s"
display_name = "Test Service Account"
}
resource "google_pubsub_topic" "topic" {
name = "%s"
}
resource "google_sourcerepo_repository" "acceptance" {
name = "%s"
pubsub_configs {
topic = google_pubsub_topic.topic.id
message_format = "JSON"
service_account_email = google_service_account.test-account.email
}
}
`, accountId, topicName, repositoryName)
}
// Test setting create_ignore_already_exists on an existing resource
func TestAccSourceRepoRepository_existingResourceCreateIgnoreAlreadyExists(t *testing.T) {
t.Parallel()
project := envvar.GetTestProjectFromEnv()
repositoryName := fmt.Sprintf("source-repo-repository-test-%s", acctest.RandString(t, 10))
id := fmt.Sprintf("projects/%s/repos/%s", project, repositoryName)
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckSourceRepoRepositoryDestroyProducer(t),
Steps: []resource.TestStep{
// The first step creates a new resource with create_ignore_already_exists=false
{
Config: testAccSourceRepoRepositoryCreateIgnoreAlreadyExists(repositoryName, false),
Check: resource.TestCheckResourceAttr("google_sourcerepo_repository.acceptance", "id", id),
},
{
ResourceName: "google_sourcerepo_repository.acceptance",
ImportStateId: id,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"create_ignore_already_exists"}, // Import leaves this field out when false
},
// The second step updates the resource to have create_ignore_already_exists=true
{
Config: testAccSourceRepoRepositoryCreateIgnoreAlreadyExists(repositoryName, true),
Check: resource.TestCheckResourceAttr("google_sourcerepo_repository.acceptance", "id", id),
},
},
})
}
// Test the option to ignore ALREADY_EXISTS error from creating a Source Repository.
func TestAccSourceRepoRepository_createIgnoreAlreadyExists(t *testing.T) {
t.Parallel()
project := envvar.GetTestProjectFromEnv()
repositoryName := fmt.Sprintf("source-repo-repository-test-%s", acctest.RandString(t, 10))
id := fmt.Sprintf("projects/%s/repos/%s", project, repositoryName)
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckSourceRepoRepositoryDestroyProducer(t),
Steps: []resource.TestStep{
// The first step creates a basic Source Repository
{
Config: testAccSourceRepoRepository_basic(repositoryName),
Check: resource.TestCheckResourceAttr("google_sourcerepo_repository.acceptance", "id", id),
},
{
ResourceName: "google_sourcerepo_repository.acceptance",
ImportStateId: id,
ImportState: true,
ImportStateVerify: true,
},
// The second step creates a new resource that duplicates with the existing Source Repository.
{
Config: testAccSourceRepoRepositoryDuplicateIgnoreAlreadyExists(repositoryName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_sourcerepo_repository.acceptance", "id", id),
resource.TestCheckResourceAttr("google_sourcerepo_repository.duplicate", "id", id),
),
},
},
})
}
func testAccSourceRepoRepositoryCreateIgnoreAlreadyExists(repositoryName string, ignore_already_exists bool) string {
return fmt.Sprintf(`
resource "google_sourcerepo_repository" "acceptance" {
name = "%s"
create_ignore_already_exists = %t
}
`, repositoryName, ignore_already_exists)
}
func testAccSourceRepoRepositoryDuplicateIgnoreAlreadyExists(repositoryName string) string {
return fmt.Sprintf(`
resource "google_sourcerepo_repository" "acceptance" {
name = "%s"
}
resource "google_sourcerepo_repository" "duplicate" {
name = "%s"
create_ignore_already_exists = true
}
`, repositoryName, repositoryName)
}