-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathresource_branchpolicy_status_check_test.go
213 lines (184 loc) · 6.98 KB
/
resource_branchpolicy_status_check_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
//go:build (all || resource_branchpolicy_status_check_acceptance_test || policy) && (!exclude_resource_branchpolicy_status_check_acceptance_test || !exclude_policy)
// +build all resource_branchpolicy_status_check_acceptance_test policy
// +build !exclude_resource_branchpolicy_status_check_acceptance_test !exclude_policy
package acceptancetests
import (
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/acceptancetests/testutils"
)
func TestAccBranchPolicyStatusCheck_basic(t *testing.T) {
statusCheckTfNode := "azuredevops_branch_policy_status_check.p"
projectName := testutils.GenerateResourceName()
repoName := testutils.GenerateResourceName()
resource.Test(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.GetProviders(),
Steps: []resource.TestStep{
{
Config: hclBranchPolicyStatusCheckResourceBasic(projectName, repoName, "update"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(statusCheckTfNode, "settings.0.name", "update")),
}, {
ResourceName: statusCheckTfNode,
ImportStateIdFunc: testutils.ComputeProjectQualifiedResourceImportID(statusCheckTfNode),
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccBranchPolicyStatusCheck_complete(t *testing.T) {
statusCheckTfNode := "azuredevops_branch_policy_status_check.p"
projectName := testutils.GenerateResourceName()
repoName := testutils.GenerateResourceName()
resource.Test(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.GetProviders(),
Steps: []resource.TestStep{
{
Config: hclBranchPolicyStatusCheckResourceComplete(projectName, repoName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(statusCheckTfNode, "settings.0.author_id"),
resource.TestCheckResourceAttr(statusCheckTfNode, "settings.0.name", "Release"),
resource.TestCheckResourceAttr(statusCheckTfNode, "settings.0.invalidate_on_update", "true"),
resource.TestCheckResourceAttr(statusCheckTfNode, "settings.0.applicability", "conditional"),
resource.TestCheckResourceAttr(statusCheckTfNode, "settings.0.display_name", "PreCheck"),
),
}, {
ResourceName: statusCheckTfNode,
ImportStateIdFunc: testutils.ComputeProjectQualifiedResourceImportID(statusCheckTfNode),
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccBranchPolicyStatusCheckUpdate(t *testing.T) {
statusCheckTfNode := "azuredevops_branch_policy_status_check.p"
projectName := testutils.GenerateResourceName()
repoName := testutils.GenerateResourceName()
resource.Test(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.GetProviders(),
Steps: []resource.TestStep{
{
Config: hclBranchPolicyStatusCheckResourceBasic(projectName, repoName, "update"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(statusCheckTfNode, "settings.0.name", "update"),
),
}, {
Config: hclBranchPolicyStatusCheckResourceUpdate(projectName, repoName, "releaseCheck", true, "conditional", "updateName"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(statusCheckTfNode, "settings.0.author_id"),
resource.TestCheckResourceAttr(statusCheckTfNode, "settings.0.name", "releaseCheck"),
resource.TestCheckResourceAttr(statusCheckTfNode, "settings.0.invalidate_on_update", "true"),
resource.TestCheckResourceAttr(statusCheckTfNode, "settings.0.applicability", "conditional"),
resource.TestCheckResourceAttr(statusCheckTfNode, "settings.0.display_name", "updateName"),
),
}, {
ResourceName: statusCheckTfNode,
ImportStateIdFunc: testutils.ComputeProjectQualifiedResourceImportID(statusCheckTfNode),
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func hclBranchPolicyStatusCheckResourceTemplate(projectName string, repoName string) string {
return fmt.Sprintf(`
resource "azuredevops_project" "p" {
name = "%s"
description = "Test Project Description"
visibility = "private"
version_control = "Git"
work_item_template = "Agile"
}
resource "azuredevops_git_repository" "r" {
project_id = azuredevops_project.p.id
name = "%s"
initialization {
init_type = "Clean"
}
}
`, projectName, repoName)
}
func hclBranchPolicyStatusCheckResourceBasic(projectName string, repoName string, statusName string) string {
projectAndRepo := hclBranchPolicyStatusCheckResourceTemplate(projectName, repoName)
statusCheck := fmt.Sprintf(`
resource "azuredevops_branch_policy_status_check" "p" {
project_id = azuredevops_project.p.id
enabled = true
blocking = true
settings {
name = "%s"
scope {
repository_id = azuredevops_git_repository.r.id
repository_ref = azuredevops_git_repository.r.default_branch
match_type = "Exact"
}
}
}
`, statusName)
return fmt.Sprintf(`%s %s`, projectAndRepo, statusCheck)
}
func hclBranchPolicyStatusCheckResourceComplete(projectName string, repoName string) string {
return fmt.Sprintf(
`%s %s`,
hclBranchPolicyStatusCheckResourceTemplate(projectName, repoName), `
resource "azuredevops_user_entitlement" "user" {
principal_name = "[email protected]"
account_license_type = "basic"
}
resource "azuredevops_branch_policy_status_check" "p" {
project_id = azuredevops_project.p.id
enabled = true
blocking = true
settings {
name = "Release"
author_id = azuredevops_user_entitlement.user.id
invalidate_on_update = true
applicability = "conditional"
display_name = "PreCheck"
filename_patterns = ["*.go", "**.ts"]
scope {
repository_id = azuredevops_git_repository.r.id
repository_ref = azuredevops_git_repository.r.default_branch
match_type = "Exact"
}
}
}
`)
}
func hclBranchPolicyStatusCheckResourceUpdate(projectName string, repoName string,
statusName string, invalid bool, applicability string, displayName string) string {
statusCheck := fmt.Sprintf(`
data "azuredevops_group" "group" {
project_id = azuredevops_project.p.id
name = "Project Administrators"
}
resource "azuredevops_branch_policy_status_check" "p" {
project_id = azuredevops_project.p.id
enabled = true
blocking = true
settings {
name = "%s"
author_id = data.azuredevops_group.group.origin_id
invalidate_on_update = %t
applicability = "%s"
display_name = "%s"
filename_patterns = ["*.go", "**.ts"]
scope {
repository_id = azuredevops_git_repository.r.id
repository_ref = azuredevops_git_repository.r.default_branch
match_type = "Exact"
}
}
}
`, statusName, invalid, applicability, displayName)
return fmt.Sprintf(
`%s %s`,
hclBranchPolicyStatusCheckResourceTemplate(projectName, repoName),
statusCheck)
}