forked from gitlabhq/terraform-provider-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_gitlab_project_level_mr_approvals_test.go
184 lines (170 loc) · 7.09 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
//go:build acceptance
// +build acceptance
package provider
import (
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
gitlab "github.com/xanzy/go-gitlab"
)
func TestAccGitlabProjectLevelMRApprovals_basic(t *testing.T) {
testAccCheckEE(t)
var projectApprovals gitlab.ProjectApprovals
testProject := testAccCreateProject(t)
resource.ParallelTest(t, resource.TestCase{
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckGitlabProjectLevelMRApprovalsDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "gitlab_project_level_mr_approvals" "foo" {
project_id = "%d"
reset_approvals_on_push = true
disable_overriding_approvers_per_merge_request = true
merge_requests_author_approval = true
merge_requests_disable_committers_approval = true
require_password_to_approve = true
}
`, testProject.ID),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectLevelMRApprovalsExists("gitlab_project_level_mr_approvals.foo", &projectApprovals),
testAccCheckGitlabProjectLevelMRApprovalsAttributes(&projectApprovals, &testAccGitlabProjectLevelMRApprovalsExpectedAttributes{
resetApprovalsOnPush: true,
disableOverridingApproversPerMergeRequest: true,
mergeRequestsAuthorApproval: true,
mergeRequestsDisableCommittersApproval: true,
requirePasswordToApprove: true,
}),
),
},
// Verify Import
{
ResourceName: "gitlab_project_level_mr_approvals.foo",
ImportState: true,
ImportStateVerify: true,
},
{
Config: fmt.Sprintf(`
resource "gitlab_project_level_mr_approvals" "foo" {
project_id = "%d"
reset_approvals_on_push = false
disable_overriding_approvers_per_merge_request = false
merge_requests_author_approval = false
merge_requests_disable_committers_approval = false
require_password_to_approve = false
}
`, testProject.ID),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectLevelMRApprovalsExists("gitlab_project_level_mr_approvals.foo", &projectApprovals),
testAccCheckGitlabProjectLevelMRApprovalsAttributes(&projectApprovals, &testAccGitlabProjectLevelMRApprovalsExpectedAttributes{
resetApprovalsOnPush: false,
disableOverridingApproversPerMergeRequest: false,
mergeRequestsAuthorApproval: false,
mergeRequestsDisableCommittersApproval: false,
requirePasswordToApprove: false,
}),
),
},
// Verify Import
{
ResourceName: "gitlab_project_level_mr_approvals.foo",
ImportState: true,
ImportStateVerify: true,
},
{
Config: fmt.Sprintf(`
resource "gitlab_project_level_mr_approvals" "foo" {
project_id = "%d"
reset_approvals_on_push = true
disable_overriding_approvers_per_merge_request = true
merge_requests_author_approval = true
merge_requests_disable_committers_approval = true
require_password_to_approve = true
}
`, testProject.ID),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectLevelMRApprovalsExists("gitlab_project_level_mr_approvals.foo", &projectApprovals),
testAccCheckGitlabProjectLevelMRApprovalsAttributes(&projectApprovals, &testAccGitlabProjectLevelMRApprovalsExpectedAttributes{
resetApprovalsOnPush: true,
disableOverridingApproversPerMergeRequest: true,
mergeRequestsAuthorApproval: true,
mergeRequestsDisableCommittersApproval: true,
requirePasswordToApprove: true,
}),
),
},
// Verify Import
{
ResourceName: "gitlab_project_level_mr_approvals.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
type testAccGitlabProjectLevelMRApprovalsExpectedAttributes struct {
resetApprovalsOnPush bool
disableOverridingApproversPerMergeRequest bool
mergeRequestsAuthorApproval bool
mergeRequestsDisableCommittersApproval bool
requirePasswordToApprove 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)
}
if projectApprovals.RequirePasswordToApprove != want.requirePasswordToApprove {
return fmt.Errorf("got require_password_to_approve %t; want %t", projectApprovals.RequirePasswordToApprove, want.requirePasswordToApprove)
}
return nil
}
}
func testAccCheckGitlabProjectLevelMRApprovalsDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "gitlab_project" {
continue
}
gotRepo, resp, err := testGitlabClient.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 != nil && 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")
}
gotApprovalConfig, _, err := testGitlabClient.Projects.GetApprovalConfiguration(projectId)
if err != nil {
return err
}
*projectApprovals = *gotApprovalConfig
return nil
}
}