forked from gitlabhq/terraform-provider-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_gitlab_project_level_mr_approvals_test.go
196 lines (176 loc) · 7 KB
/
resource_gitlab_project_level_mr_approvals_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
package gitlab
import (
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
gitlab "github.com/xanzy/go-gitlab"
)
func TestAccGitlabProjectLevelMRApprovals_basic(t *testing.T) {
var projectApprovals gitlab.ProjectApprovals
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGitlabProjectLevelMRApprovalsDestroy,
Steps: []resource.TestStep{
{
SkipFunc: isRunningInCE,
Config: testAccGitlabProjectLevelMRApprovalsConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectLevelMRApprovalsExists("gitlab_project_level_mr_approvals.foo", &projectApprovals),
testAccCheckGitlabProjectLevelMRApprovalsAttributes(&projectApprovals, &testAccGitlabProjectLevelMRApprovalsExpectedAttributes{
resetApprovalsOnPush: true,
disableOverridingApproversPerMergeRequest: true,
mergeRequestsAuthorApproval: true,
mergeRequestsDisableCommittersApproval: true,
}),
),
},
{
SkipFunc: isRunningInCE,
Config: testAccGitlabProjectLevelMRApprovalsUpdateConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectLevelMRApprovalsExists("gitlab_project_level_mr_approvals.foo", &projectApprovals),
testAccCheckGitlabProjectLevelMRApprovalsAttributes(&projectApprovals, &testAccGitlabProjectLevelMRApprovalsExpectedAttributes{
resetApprovalsOnPush: false,
disableOverridingApproversPerMergeRequest: false,
mergeRequestsAuthorApproval: false,
mergeRequestsDisableCommittersApproval: false,
}),
),
},
{
SkipFunc: isRunningInCE,
Config: testAccGitlabProjectLevelMRApprovalsConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectLevelMRApprovalsExists("gitlab_project_level_mr_approvals.foo", &projectApprovals),
testAccCheckGitlabProjectLevelMRApprovalsAttributes(&projectApprovals, &testAccGitlabProjectLevelMRApprovalsExpectedAttributes{
resetApprovalsOnPush: true,
disableOverridingApproversPerMergeRequest: true,
mergeRequestsAuthorApproval: true,
mergeRequestsDisableCommittersApproval: true,
}),
),
},
},
})
}
// lintignore: AT002 // TODO: Resolve this tfproviderlint issue
func TestAccGitlabProjectLevelMRApprovals_import(t *testing.T) {
resourceName := "gitlab_project_level_mr_approvals.foo"
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGitlabProjectLevelMRApprovalsDestroy,
Steps: []resource.TestStep{
{
SkipFunc: isRunningInCE,
Config: testAccGitlabProjectLevelMRApprovalsConfig(rInt),
},
{
SkipFunc: isRunningInCE,
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
type testAccGitlabProjectLevelMRApprovalsExpectedAttributes struct {
resetApprovalsOnPush bool
disableOverridingApproversPerMergeRequest bool
mergeRequestsAuthorApproval bool
mergeRequestsDisableCommittersApproval bool
}
func testAccCheckGitlabProjectLevelMRApprovalsAttributes(projectApprovals *gitlab.ProjectApprovals, want *testAccGitlabProjectLevelMRApprovalsExpectedAttributes) resource.TestCheckFunc {
return func(s *terraform.State) error {
if projectApprovals.ResetApprovalsOnPush != want.resetApprovalsOnPush {
return fmt.Errorf("got reset_approvals_on_push %t; want %t", projectApprovals.ResetApprovalsOnPush, want.resetApprovalsOnPush)
}
if projectApprovals.DisableOverridingApproversPerMergeRequest != want.disableOverridingApproversPerMergeRequest {
return fmt.Errorf("got disable_overriding_approvers_per_merge_request %t; want %t", projectApprovals.DisableOverridingApproversPerMergeRequest, want.disableOverridingApproversPerMergeRequest)
}
if projectApprovals.MergeRequestsAuthorApproval != want.mergeRequestsAuthorApproval {
return fmt.Errorf("got merge_requests_author_approval %t; want %t", projectApprovals.MergeRequestsAuthorApproval, want.mergeRequestsAuthorApproval)
}
if projectApprovals.MergeRequestsDisableCommittersApproval != want.mergeRequestsDisableCommittersApproval {
return fmt.Errorf("got merge_requests_disable_committers_approval %t; want %t", projectApprovals.MergeRequestsDisableCommittersApproval, want.mergeRequestsDisableCommittersApproval)
}
return nil
}
}
func testAccCheckGitlabProjectLevelMRApprovalsDestroy(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 testAccCheckGitlabProjectLevelMRApprovalsExists(n string, projectApprovals *gitlab.ProjectApprovals) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not Found: %s", n)
}
projectId := rs.Primary.ID
if projectId == "" {
return fmt.Errorf("No project ID is set")
}
conn := testAccProvider.Meta().(*gitlab.Client)
gotApprovalConfig, _, err := conn.Projects.GetApprovalConfiguration(projectId)
if err != nil {
return err
}
*projectApprovals = *gotApprovalConfig
return nil
}
}
func testAccGitlabProjectLevelMRApprovalsConfig(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_project" "foo" {
name = "foo-%d"
description = "Terraform acceptance tests"
visibility_level = "public"
}
resource "gitlab_project_level_mr_approvals" "foo" {
project_id = gitlab_project.foo.id
reset_approvals_on_push = true
disable_overriding_approvers_per_merge_request = true
merge_requests_author_approval = true
merge_requests_disable_committers_approval = true
}
`, rInt)
}
func testAccGitlabProjectLevelMRApprovalsUpdateConfig(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_project" "foo" {
name = "foo-%d"
description = "Terraform acceptance tests"
visibility_level = "public"
}
resource "gitlab_project_level_mr_approvals" "foo" {
project_id = gitlab_project.foo.id
reset_approvals_on_push = false
disable_overriding_approvers_per_merge_request = false
merge_requests_author_approval = false
merge_requests_disable_committers_approval = false
}
`, rInt)
}