@@ -8,243 +8,145 @@ import (
88 "github.com/hashicorp/terraform-plugin-testing/helper/resource"
99)
1010
11- func TestAccGithubEnterprisePrivateRepositoryForkingSetting (t * testing.T ) {
12- t .Run ("enables private repository forking with policy" , func (t * testing.T ) {
13- config := fmt .Sprintf (`
11+ const testAccEnterpriseForkingSettingResource = "github_enterprise_private_repository_forking_setting.test"
12+
13+ func testAccEnterpriseForkingSettingConfig (settingValue , policyValue string ) string {
14+ if policyValue != "" {
15+ return fmt .Sprintf (`
1416 resource "github_enterprise_private_repository_forking_setting" "test" {
1517 enterprise_slug = "%s"
16- setting_value = "ENABLED "
17- policy_value = "SAME_ORGANIZATION "
18+ setting_value = "%s "
19+ policy_value = "%s "
1820 }
19- ` , testAccConf .enterpriseSlug )
21+ ` , testAccConf .enterpriseSlug , settingValue , policyValue )
22+ }
23+ return fmt .Sprintf (`
24+ resource "github_enterprise_private_repository_forking_setting" "test" {
25+ enterprise_slug = "%s"
26+ setting_value = "%s"
27+ }
28+ ` , testAccConf .enterpriseSlug , settingValue )
29+ }
2030
21- check := resource .ComposeTestCheckFunc (
22- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "enterprise_slug" , testAccConf .enterpriseSlug ),
23- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "setting_value" , "ENABLED" ),
24- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "policy_value" , "SAME_ORGANIZATION" ),
25- )
31+ func testAccEnterpriseForkingSettingCheck (settingValue , policyValue string ) resource.TestCheckFunc {
32+ return resource .ComposeTestCheckFunc (
33+ resource .TestCheckResourceAttr (testAccEnterpriseForkingSettingResource , "enterprise_slug" , testAccConf .enterpriseSlug ),
34+ resource .TestCheckResourceAttr (testAccEnterpriseForkingSettingResource , "setting_value" , settingValue ),
35+ resource .TestCheckResourceAttr (testAccEnterpriseForkingSettingResource , "policy_value" , policyValue ),
36+ )
37+ }
2638
39+ func TestAccGithubEnterprisePrivateRepositoryForkingSetting (t * testing.T ) {
40+ t .Run ("enables private repository forking with policy" , func (t * testing.T ) {
2741 resource .Test (t , resource.TestCase {
2842 PreCheck : func () { skipUnlessEnterprise (t ) },
2943 ProviderFactories : providerFactories ,
3044 Steps : []resource.TestStep {
3145 {
32- Config : config ,
33- Check : check ,
46+ Config : testAccEnterpriseForkingSettingConfig ( "ENABLED" , "SAME_ORGANIZATION" ) ,
47+ Check : testAccEnterpriseForkingSettingCheck ( "ENABLED" , "SAME_ORGANIZATION" ) ,
3448 },
3549 },
3650 })
3751 })
3852
3953 t .Run ("updates policy value" , func (t * testing.T ) {
40- configs := map [string ]string {
41- "before" : fmt .Sprintf (`
42- resource "github_enterprise_private_repository_forking_setting" "test" {
43- enterprise_slug = "%s"
44- setting_value = "ENABLED"
45- policy_value = "SAME_ORGANIZATION"
46- }
47- ` , testAccConf .enterpriseSlug ),
48-
49- "after" : fmt .Sprintf (`
50- resource "github_enterprise_private_repository_forking_setting" "test" {
51- enterprise_slug = "%s"
52- setting_value = "ENABLED"
53- policy_value = "ENTERPRISE_ORGANIZATIONS_USER_ACCOUNTS"
54- }
55- ` , testAccConf .enterpriseSlug ),
56- }
57-
58- checks := map [string ]resource.TestCheckFunc {
59- "before" : resource .ComposeTestCheckFunc (
60- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "setting_value" , "ENABLED" ),
61- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "policy_value" , "SAME_ORGANIZATION" ),
62- ),
63- "after" : resource .ComposeTestCheckFunc (
64- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "setting_value" , "ENABLED" ),
65- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "policy_value" , "ENTERPRISE_ORGANIZATIONS_USER_ACCOUNTS" ),
66- ),
67- }
68-
6954 resource .Test (t , resource.TestCase {
7055 PreCheck : func () { skipUnlessEnterprise (t ) },
7156 ProviderFactories : providerFactories ,
7257 Steps : []resource.TestStep {
7358 {
74- Config : configs [ "before" ] ,
75- Check : checks [ "before" ] ,
59+ Config : testAccEnterpriseForkingSettingConfig ( "ENABLED" , "SAME_ORGANIZATION" ) ,
60+ Check : testAccEnterpriseForkingSettingCheck ( "ENABLED" , "SAME_ORGANIZATION" ) ,
7661 },
7762 {
78- Config : configs [ "after" ] ,
79- Check : checks [ "after" ] ,
63+ Config : testAccEnterpriseForkingSettingConfig ( "ENABLED" , "ENTERPRISE_ORGANIZATIONS_USER_ACCOUNTS" ) ,
64+ Check : testAccEnterpriseForkingSettingCheck ( "ENABLED" , "ENTERPRISE_ORGANIZATIONS_USER_ACCOUNTS" ) ,
8065 },
8166 },
8267 })
8368 })
8469
8570 t .Run ("disables private repository forking" , func (t * testing.T ) {
86- config := fmt .Sprintf (`
87- resource "github_enterprise_private_repository_forking_setting" "test" {
88- enterprise_slug = "%s"
89- setting_value = "DISABLED"
90- }
91- ` , testAccConf .enterpriseSlug )
92-
93- check := resource .ComposeTestCheckFunc (
94- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "enterprise_slug" , testAccConf .enterpriseSlug ),
95- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "setting_value" , "DISABLED" ),
96- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "policy_value" , "" ),
97- )
98-
9971 resource .Test (t , resource.TestCase {
10072 PreCheck : func () { skipUnlessEnterprise (t ) },
10173 ProviderFactories : providerFactories ,
10274 Steps : []resource.TestStep {
10375 {
104- Config : config ,
105- Check : check ,
76+ Config : testAccEnterpriseForkingSettingConfig ( "DISABLED" , "" ) ,
77+ Check : testAccEnterpriseForkingSettingCheck ( "DISABLED" , "" ) ,
10678 },
10779 },
10880 })
10981 })
11082
11183 t .Run ("sets no policy" , func (t * testing.T ) {
112- config := fmt .Sprintf (`
113- resource "github_enterprise_private_repository_forking_setting" "test" {
114- enterprise_slug = "%s"
115- setting_value = "NO_POLICY"
116- }
117- ` , testAccConf .enterpriseSlug )
118-
119- check := resource .ComposeTestCheckFunc (
120- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "enterprise_slug" , testAccConf .enterpriseSlug ),
121- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "setting_value" , "NO_POLICY" ),
122- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "policy_value" , "" ),
123- )
124-
12584 resource .Test (t , resource.TestCase {
12685 PreCheck : func () { skipUnlessEnterprise (t ) },
12786 ProviderFactories : providerFactories ,
12887 Steps : []resource.TestStep {
12988 {
130- Config : config ,
131- Check : check ,
89+ Config : testAccEnterpriseForkingSettingConfig ( "NO_POLICY" , "" ) ,
90+ Check : testAccEnterpriseForkingSettingCheck ( "NO_POLICY" , "" ) ,
13291 },
13392 },
13493 })
13594 })
13695
13796 t .Run ("transitions from enabled to disabled" , func (t * testing.T ) {
138- configs := map [string ]string {
139- "enabled" : fmt .Sprintf (`
140- resource "github_enterprise_private_repository_forking_setting" "test" {
141- enterprise_slug = "%s"
142- setting_value = "ENABLED"
143- policy_value = "SAME_ORGANIZATION"
144- }
145- ` , testAccConf .enterpriseSlug ),
146-
147- "disabled" : fmt .Sprintf (`
148- resource "github_enterprise_private_repository_forking_setting" "test" {
149- enterprise_slug = "%s"
150- setting_value = "DISABLED"
151- }
152- ` , testAccConf .enterpriseSlug ),
153- }
154-
155- checks := map [string ]resource.TestCheckFunc {
156- "enabled" : resource .ComposeTestCheckFunc (
157- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "setting_value" , "ENABLED" ),
158- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "policy_value" , "SAME_ORGANIZATION" ),
159- ),
160- "disabled" : resource .ComposeTestCheckFunc (
161- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "setting_value" , "DISABLED" ),
162- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "policy_value" , "" ),
163- ),
164- }
165-
16697 resource .Test (t , resource.TestCase {
16798 PreCheck : func () { skipUnlessEnterprise (t ) },
16899 ProviderFactories : providerFactories ,
169100 Steps : []resource.TestStep {
170101 {
171- Config : configs [ "enabled" ] ,
172- Check : checks [ "enabled" ] ,
102+ Config : testAccEnterpriseForkingSettingConfig ( "ENABLED" , "SAME_ORGANIZATION" ) ,
103+ Check : testAccEnterpriseForkingSettingCheck ( "ENABLED" , "SAME_ORGANIZATION" ) ,
173104 },
174105 {
175- Config : configs [ "disabled" ] ,
176- Check : checks [ "disabled" ] ,
106+ Config : testAccEnterpriseForkingSettingConfig ( "DISABLED" , "" ) ,
107+ Check : testAccEnterpriseForkingSettingCheck ( "DISABLED" , "" ) ,
177108 },
178109 },
179110 })
180111 })
181112
182113 t .Run ("rejects policy_value when disabled" , func (t * testing.T ) {
183- config := fmt .Sprintf (`
184- resource "github_enterprise_private_repository_forking_setting" "test" {
185- enterprise_slug = "%s"
186- setting_value = "DISABLED"
187- policy_value = "SAME_ORGANIZATION"
188- }
189- ` , testAccConf .enterpriseSlug )
190-
191114 resource .Test (t , resource.TestCase {
192115 PreCheck : func () { skipUnlessEnterprise (t ) },
193116 ProviderFactories : providerFactories ,
194117 Steps : []resource.TestStep {
195118 {
196- Config : config ,
119+ Config : testAccEnterpriseForkingSettingConfig ( "DISABLED" , "SAME_ORGANIZATION" ) ,
197120 ExpectError : regexp .MustCompile (`policy_value must not be set when setting_value is DISABLED` ),
198121 },
199122 },
200123 })
201124 })
202125
203126 t .Run ("requires policy_value when enabled" , func (t * testing.T ) {
204- config := fmt .Sprintf (`
205- resource "github_enterprise_private_repository_forking_setting" "test" {
206- enterprise_slug = "%s"
207- setting_value = "ENABLED"
208- }
209- ` , testAccConf .enterpriseSlug )
210-
211127 resource .Test (t , resource.TestCase {
212128 PreCheck : func () { skipUnlessEnterprise (t ) },
213129 ProviderFactories : providerFactories ,
214130 Steps : []resource.TestStep {
215131 {
216- Config : config ,
132+ Config : testAccEnterpriseForkingSettingConfig ( "ENABLED" , "" ) ,
217133 ExpectError : regexp .MustCompile (`policy_value is required when setting_value is ENABLED` ),
218134 },
219135 },
220136 })
221137 })
222138
223139 t .Run ("imports without error" , func (t * testing.T ) {
224- config := fmt .Sprintf (`
225- resource "github_enterprise_private_repository_forking_setting" "test" {
226- enterprise_slug = "%s"
227- setting_value = "ENABLED"
228- policy_value = "ENTERPRISE_ORGANIZATIONS_USER_ACCOUNTS"
229- }
230- ` , testAccConf .enterpriseSlug )
231-
232- check := resource .ComposeTestCheckFunc (
233- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "enterprise_slug" , testAccConf .enterpriseSlug ),
234- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "setting_value" , "ENABLED" ),
235- resource .TestCheckResourceAttr ("github_enterprise_private_repository_forking_setting.test" , "policy_value" , "ENTERPRISE_ORGANIZATIONS_USER_ACCOUNTS" ),
236- )
237-
238140 resource .Test (t , resource.TestCase {
239141 PreCheck : func () { skipUnlessEnterprise (t ) },
240142 ProviderFactories : providerFactories ,
241143 Steps : []resource.TestStep {
242144 {
243- Config : config ,
244- Check : check ,
145+ Config : testAccEnterpriseForkingSettingConfig ( "ENABLED" , "ENTERPRISE_ORGANIZATIONS_USER_ACCOUNTS" ) ,
146+ Check : testAccEnterpriseForkingSettingCheck ( "ENABLED" , "ENTERPRISE_ORGANIZATIONS_USER_ACCOUNTS" ) ,
245147 },
246148 {
247- ResourceName : "github_enterprise_private_repository_forking_setting.test" ,
149+ ResourceName : testAccEnterpriseForkingSettingResource ,
248150 ImportState : true ,
249151 ImportStateVerify : true ,
250152 },
0 commit comments