@@ -203,6 +203,74 @@ max_file_size = 123
203
203
Config : testAccGitlabProjectConfigPushRules (rInt , `author_email_regex = "foo_author"` ),
204
204
ExpectError : regexp .MustCompile (regexp .QuoteMeta ("Project push rules are not supported in your version of GitLab" )),
205
205
},
206
+ // Create a project using template name
207
+ {
208
+ Config : testAccGitlabProjectConfigTemplateName (rInt ),
209
+ Check : resource .ComposeTestCheckFunc (
210
+ testAccCheckGitlabProjectExists ("gitlab_project.template-name" , & received ),
211
+ testAccCheckGitlabProjectDefaultBranch (& received , & testAccGitlabProjectExpectedAttributes {
212
+ DefaultBranch : "master" ,
213
+ }),
214
+ func (state * terraform.State ) error {
215
+ client := testAccProvider .Meta ().(* gitlab.Client )
216
+
217
+ projectID := state .RootModule ().Resources ["gitlab_project.template-name" ].Primary .ID
218
+
219
+ _ , _ , err := client .RepositoryFiles .GetFile (projectID , ".ruby-version" , & gitlab.GetFileOptions {Ref : gitlab .String ("master" )}, nil )
220
+ if err != nil {
221
+ return fmt .Errorf ("failed to get '.ruby-version' file from template project: %w" , err )
222
+ }
223
+
224
+ return nil
225
+ },
226
+ ),
227
+ },
228
+ // Create a project using custom template name
229
+ {
230
+ Config : testAccGitlabProjectConfigTemplateNameCustom (rInt ),
231
+ SkipFunc : isRunningInCE ,
232
+ Check : resource .ComposeTestCheckFunc (
233
+ testAccCheckGitlabProjectExists ("gitlab_project.template-name-custom" , & received ),
234
+ testAccCheckGitlabProjectDefaultBranch (& received , & testAccGitlabProjectExpectedAttributes {
235
+ DefaultBranch : "master" ,
236
+ }),
237
+ func (state * terraform.State ) error {
238
+ client := testAccProvider .Meta ().(* gitlab.Client )
239
+
240
+ projectID := state .RootModule ().Resources ["gitlab_project.template-name-custom" ].Primary .ID
241
+
242
+ _ , _ , err := client .RepositoryFiles .GetFile (projectID , "Gemfile" , & gitlab.GetFileOptions {Ref : gitlab .String ("master" )}, nil )
243
+ if err != nil {
244
+ return fmt .Errorf ("failed to get 'Gemfile' file from template project: %w" , err )
245
+ }
246
+
247
+ return nil
248
+ },
249
+ ),
250
+ },
251
+ // Create a project using custom template project id
252
+ {
253
+ Config : testAccGitlabProjectConfigTemplateProjectID (rInt ),
254
+ SkipFunc : isRunningInCE ,
255
+ Check : resource .ComposeTestCheckFunc (
256
+ testAccCheckGitlabProjectExists ("gitlab_project.template-id" , & received ),
257
+ testAccCheckGitlabProjectDefaultBranch (& received , & testAccGitlabProjectExpectedAttributes {
258
+ DefaultBranch : "master" ,
259
+ }),
260
+ func (state * terraform.State ) error {
261
+ client := testAccProvider .Meta ().(* gitlab.Client )
262
+
263
+ projectID := state .RootModule ().Resources ["gitlab_project.template-id" ].Primary .ID
264
+
265
+ _ , _ , err := client .RepositoryFiles .GetFile (projectID , "Rakefile" , & gitlab.GetFileOptions {Ref : gitlab .String ("master" )}, nil )
266
+ if err != nil {
267
+ return fmt .Errorf ("failed to get 'Rakefile' file from template project: %w" , err )
268
+ }
269
+
270
+ return nil
271
+ },
272
+ ),
273
+ },
206
274
// Update to original project config
207
275
{
208
276
Config : testAccGitlabProjectConfig (rInt ),
@@ -224,7 +292,7 @@ func TestAccGitlabProject_initializeWithReadme(t *testing.T) {
224
292
Config : testAccGitlabProjectConfigInitializeWithReadme (rInt ),
225
293
Check : resource .ComposeTestCheckFunc (
226
294
testAccCheckGitlabProjectExists ("gitlab_project.foo" , & project ),
227
- testAccCheckGitlabProjectInitializeWithReadme (& project , & testAccGitlabProjectExpectedAttributes {
295
+ testAccCheckGitlabProjectDefaultBranch (& project , & testAccGitlabProjectExpectedAttributes {
228
296
DefaultBranch : "master" ,
229
297
}),
230
298
),
@@ -437,6 +505,22 @@ func TestAccGitlabProject_importURL(t *testing.T) {
437
505
})
438
506
}
439
507
508
+ func TestAccGitlabProjec_templateMutualExclusiveNameAndID (t * testing.T ) {
509
+ rInt := acctest .RandInt ()
510
+
511
+ resource .Test (t , resource.TestCase {
512
+ PreCheck : func () { testAccPreCheck (t ) },
513
+ Providers : testAccProviders ,
514
+ Steps : []resource.TestStep {
515
+ {
516
+ Config : testAccCheckMutualExclusiveNameAndID (rInt ),
517
+ SkipFunc : isRunningInCE ,
518
+ ExpectError : regexp .MustCompile (regexp .QuoteMeta (`"template_project_id": conflicts with template_name` )),
519
+ },
520
+ },
521
+ })
522
+ }
523
+
440
524
func testAccCheckGitlabProjectExists (n string , project * gitlab.Project ) resource.TestCheckFunc {
441
525
return func (s * terraform.State ) error {
442
526
var err error
@@ -506,10 +590,10 @@ func testAccCheckAggregateGitlabProject(expected, received *gitlab.Project) reso
506
590
return resource .ComposeAggregateTestCheckFunc (checks ... )
507
591
}
508
592
509
- func testAccCheckGitlabProjectInitializeWithReadme (project * gitlab.Project , want * testAccGitlabProjectExpectedAttributes ) resource.TestCheckFunc {
593
+ func testAccCheckGitlabProjectDefaultBranch (project * gitlab.Project , want * testAccGitlabProjectExpectedAttributes ) resource.TestCheckFunc {
510
594
return func (s * terraform.State ) error {
511
595
if project .DefaultBranch != want .DefaultBranch {
512
- return fmt .Errorf ("got description %q; want %q" , project .DefaultBranch , want .DefaultBranch )
596
+ return fmt .Errorf ("got default branch %q; want %q" , project .DefaultBranch , want .DefaultBranch )
513
597
}
514
598
515
599
return nil
@@ -765,3 +849,61 @@ resource "gitlab_project" "foo" {
765
849
}
766
850
` , rInt , pushRules )
767
851
}
852
+
853
+ func testAccGitlabProjectConfigTemplateName (rInt int ) string {
854
+ return fmt .Sprintf (`
855
+ resource "gitlab_project" "template-name" {
856
+ name = "template-name-%d"
857
+ path = "template-name.%d"
858
+ description = "Terraform acceptance tests"
859
+ template_name = "rails"
860
+ default_branch = "master"
861
+ }
862
+ ` , rInt , rInt )
863
+ }
864
+
865
+ // 2020-09-07: Currently Gitlab (version 13.3.6 ) doesn't allow in admin API
866
+ // ability to set a group as instance level templates.
867
+ // To test resource_gitlab_project_test template features we add
868
+ // group, project myrails and admin settings directly in scripts/start-gitlab.sh
869
+ // Once Gitlab add admin template in API we could manage group/project/settings
870
+ // directly in tests like TestAccGitlabProject_basic.
871
+ func testAccGitlabProjectConfigTemplateNameCustom (rInt int ) string {
872
+ return fmt .Sprintf (`
873
+ resource "gitlab_project" "template-name-custom" {
874
+ name = "template-name-custom-%d"
875
+ path = "template-name-custom.%d"
876
+ description = "Terraform acceptance tests"
877
+ template_name = "myrails"
878
+ use_custom_template = true
879
+ default_branch = "master"
880
+ }
881
+ ` , rInt , rInt )
882
+ }
883
+
884
+ func testAccGitlabProjectConfigTemplateProjectID (rInt int ) string {
885
+ return fmt .Sprintf (`
886
+ resource "gitlab_project" "template-id" {
887
+ name = "template-id-%d"
888
+ path = "template-id.%d"
889
+ description = "Terraform acceptance tests"
890
+ template_project_id = 999
891
+ use_custom_template = true
892
+ default_branch = "master"
893
+ }
894
+ ` , rInt , rInt )
895
+ }
896
+
897
+ func testAccCheckMutualExclusiveNameAndID (rInt int ) string {
898
+ return fmt .Sprintf (`
899
+ resource "gitlab_project" "template-mutual-exclusive" {
900
+ name = "template-mutual-exclusive-%d"
901
+ path = "template-mutual-exclusive.%d"
902
+ description = "Terraform acceptance tests"
903
+ template_name = "rails"
904
+ template_project_id = 999
905
+ use_custom_template = true
906
+ default_branch = "master"
907
+ }
908
+ ` , rInt , rInt )
909
+ }
0 commit comments