This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
forked from gitlabhq/terraform-provider-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_gitlab_project_push_rules_test.go
232 lines (214 loc) · 7.23 KB
/
resource_gitlab_project_push_rules_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package gitlab
import (
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
gitlab "github.com/xanzy/go-gitlab"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)
func TestAccGitlabProjectPushRules_basic(t *testing.T) {
var pushRules gitlab.ProjectPushRules
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGitlabProjectPushRulesDestroy,
Steps: []resource.TestStep{
// Create project and push rules with basic options
{
SkipFunc: isRunningInCE,
Config: testAccGitlabProjectPushRulesConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectPushRulesExists("gitlab_project_push_rules.foo", &pushRules),
testAccCheckGitlabProjectPushRulesAttributes(&pushRules, &testAccGitlabProjectPushRulesExpectedAttributes{
CommitMessageRegex: "^(foo|bar).*",
BranchNameRegex: "^(foo|bar).*",
AuthorEmailRegex: "^(foo|bar).*",
FileNameRegex: "^(foo|bar).*",
DenyDeleteTag: true,
MemberCheck: true,
PreventSecrets: true,
MaxFileSize: 10,
}),
),
},
// Update the project push rules
{
SkipFunc: isRunningInCE,
Config: testAccGitlabProjectPushRulesUpdate(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectPushRulesExists("gitlab_project_push_rules.foo", &pushRules),
testAccCheckGitlabProjectPushRulesAttributes(&pushRules, &testAccGitlabProjectPushRulesExpectedAttributes{
CommitMessageRegex: "^(fu|baz).*",
BranchNameRegex: "^(fu|baz).*",
AuthorEmailRegex: "^(fu|baz).*",
FileNameRegex: "^(fu|baz).*",
DenyDeleteTag: false,
MemberCheck: false,
PreventSecrets: false,
MaxFileSize: 42,
}),
),
},
// Update the project push rules to original config
{
SkipFunc: isRunningInCE,
Config: testAccGitlabProjectPushRulesConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectPushRulesExists("gitlab_project_push_rules.foo", &pushRules),
testAccCheckGitlabProjectPushRulesAttributes(&pushRules, &testAccGitlabProjectPushRulesExpectedAttributes{
CommitMessageRegex: "^(foo|bar).*",
BranchNameRegex: "^(foo|bar).*",
AuthorEmailRegex: "^(foo|bar).*",
FileNameRegex: "^(foo|bar).*",
DenyDeleteTag: true,
MemberCheck: true,
PreventSecrets: true,
MaxFileSize: 10,
}),
),
},
},
})
}
func TestAccGitlabProjectPushRules_import(t *testing.T) {
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGitlabProjectPushRulesDestroy,
Steps: []resource.TestStep{
{
SkipFunc: isRunningInCE,
Config: testAccGitlabProjectPushRulesConfig(rInt),
},
{
SkipFunc: isRunningInCE,
ResourceName: "gitlab_project_push_rules.foo",
ImportStateId: fmt.Sprintf("root/foo-%d", rInt),
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccCheckGitlabProjectPushRulesExists(n string, pushRules *gitlab.ProjectPushRules) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not Found: %s", n)
}
repoName := rs.Primary.Attributes["project"]
if repoName == "" {
return fmt.Errorf("No project ID is set")
}
conn := testAccProvider.Meta().(*gitlab.Client)
gotPushRules, _, err := conn.Projects.GetProjectPushRules(repoName)
if err != nil {
return err
}
*pushRules = *gotPushRules
return nil
}
}
type testAccGitlabProjectPushRulesExpectedAttributes struct {
CommitMessageRegex string
BranchNameRegex string
AuthorEmailRegex string
FileNameRegex string
DenyDeleteTag bool
MemberCheck bool
PreventSecrets bool
MaxFileSize int
}
func testAccCheckGitlabProjectPushRulesAttributes(pushRules *gitlab.ProjectPushRules, want *testAccGitlabProjectPushRulesExpectedAttributes) resource.TestCheckFunc {
return func(s *terraform.State) error {
if pushRules.CommitMessageRegex != want.CommitMessageRegex {
return fmt.Errorf("got commit_message_regex %s; want %s", pushRules.CommitMessageRegex, want.CommitMessageRegex)
}
if pushRules.BranchNameRegex != want.BranchNameRegex {
return fmt.Errorf("got branch_name_regex %s; want %s", pushRules.BranchNameRegex, want.BranchNameRegex)
}
if pushRules.AuthorEmailRegex != want.AuthorEmailRegex {
return fmt.Errorf("got author_email_regex %s; want %s", pushRules.AuthorEmailRegex, want.AuthorEmailRegex)
}
if pushRules.FileNameRegex != want.FileNameRegex {
return fmt.Errorf("got file_name_regex %s; want %s", pushRules.FileNameRegex, want.FileNameRegex)
}
if pushRules.DenyDeleteTag != want.DenyDeleteTag {
return fmt.Errorf("got deny_delete_tag %t; want %t", pushRules.DenyDeleteTag, want.DenyDeleteTag)
}
if pushRules.MemberCheck != want.MemberCheck {
return fmt.Errorf("got member_check %t; want %t", pushRules.MemberCheck, want.MemberCheck)
}
if pushRules.PreventSecrets != want.PreventSecrets {
return fmt.Errorf("got prevent_secrets %t; want %t", pushRules.PreventSecrets, want.PreventSecrets)
}
if pushRules.MaxFileSize != want.MaxFileSize {
return fmt.Errorf("got max_file_size %d; want %d", pushRules.MaxFileSize, want.MaxFileSize)
}
return nil
}
}
func testAccCheckGitlabProjectPushRulesDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*gitlab.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "gitlab_project" {
continue
}
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil)
if err == nil {
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
if gotRepo.MarkedForDeletionAt == nil {
return fmt.Errorf("Repository still exists")
}
}
}
if resp.StatusCode != 404 {
return err
}
return nil
}
return nil
}
func testAccGitlabProjectPushRulesConfig(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_project" "foo" {
name = "foo-%d"
description = "Terraform acceptance test - Push Rule"
visibility_level = "public"
}
resource "gitlab_project_push_rules" "foo" {
project = "${gitlab_project.foo.id}"
commit_message_regex = "^(foo|bar).*"
branch_name_regex = "^(foo|bar).*"
author_email_regex = "^(foo|bar).*"
file_name_regex = "^(foo|bar).*"
deny_delete_tag = true
member_check = true
prevent_secrets = true
max_file_size = 10
}
`, rInt)
}
func testAccGitlabProjectPushRulesUpdate(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_project" "foo" {
name = "foo-%d"
description = "Terraform acceptance test - Push Rule"
visibility_level = "public"
}
resource "gitlab_project_push_rules" "foo" {
project = "${gitlab_project.foo.id}"
commit_message_regex = "^(fu|baz).*"
branch_name_regex = "^(fu|baz).*"
author_email_regex = "^(fu|baz).*"
file_name_regex = "^(fu|baz).*"
deny_delete_tag = false
member_check = false
prevent_secrets = false
max_file_size = 42
}
`, rInt)
}