@@ -2,6 +2,7 @@ package gitlab
22
33import (
44 "fmt"
5+ "os"
56 "regexp"
67 "testing"
78
@@ -352,18 +353,55 @@ func TestAccGitlabProject_transfer(t *testing.T) {
352353 })
353354}
354355
355- func TestAccImportURL (t * testing.T ) {
356- var received gitlab.Project
356+ func TestAccGitlabProject_importURL (t * testing.T ) {
357+ // Since we do some manual setup in this test, we need to handle the test skip first.
358+ if os .Getenv (resource .TestEnvVar ) == "" {
359+ t .Skip (fmt .Sprintf ("Acceptance tests skipped unless env '%s' set" , resource .TestEnvVar ))
360+ }
361+
362+ client := testAccProvider .Meta ().(* gitlab.Client )
363+ rInt := acctest .RandInt ()
364+
365+ // Create a base project for importing.
366+ baseProject , _ , err := client .Projects .CreateProject (& gitlab.CreateProjectOptions {
367+ Name : gitlab .String (fmt .Sprintf ("base-%d" , rInt )),
368+ Visibility : gitlab .Visibility (gitlab .PublicVisibility ),
369+ })
370+ if err != nil {
371+ t .Fatalf ("failed to create base project: %v" , err )
372+ }
373+
374+ defer client .Projects .DeleteProject (baseProject .ID )
375+
376+ // Add a file to the base project, for later verifying the import.
377+ _ , _ , err = client .RepositoryFiles .CreateFile (baseProject .ID , "foo.txt" , & gitlab.CreateFileOptions {
378+ Branch : gitlab .String ("master" ),
379+ CommitMessage : gitlab .String ("add file" ),
380+ Content : gitlab .String ("" ),
381+ })
382+ if err != nil {
383+ t .Fatalf ("failed to commit file to base project: %v" , err )
384+ }
385+
357386 resource .Test (t , resource.TestCase {
358387 PreCheck : func () { testAccPreCheck (t ) },
359388 Providers : testAccProviders ,
360389 CheckDestroy : testAccCheckGitlabProjectDestroy ,
361390 Steps : []resource.TestStep {
362391 {
363- Config : testImportURLOptions ( ),
392+ Config : testAccGitlabProjectConfigImportURL ( rInt , baseProject . HTTPURLToRepo ),
364393 Check : resource .ComposeTestCheckFunc (
365- testAccCheckGitlabProjectExists ("gitlab_project.import_url" , & received ),
366- testAccCheckImportURL ("gitlab/resource_gitlab_project.go" , & received ),
394+ resource .TestCheckResourceAttr ("gitlab_project.imported" , "import_url" , baseProject .HTTPURLToRepo ),
395+ func (state * terraform.State ) error {
396+ projectID := state .RootModule ().Resources ["gitlab_project.imported" ].Primary .ID
397+
398+ _ , _ , err := client .RepositoryFiles .GetFile (projectID , "foo.txt" , & gitlab.GetFileOptions {Ref : gitlab .String ("master" )}, nil )
399+ if err != nil {
400+ return fmt .Errorf ("failed to get file from imported project: %w" , err )
401+ }
402+
403+ return nil
404+ },
367405 ),
368406 },
369407 },
@@ -686,48 +724,16 @@ resource "gitlab_project" "foo" {
686724 ` , rInt , rInt )
687725}
688726
689- func testImportURLOptions ( ) string {
727+ func testAccGitlabProjectConfigImportURL ( rInt int , importURL string ) string {
690728 return fmt .Sprintf (`
691- resource "gitlab_project" "import_url " {
692- name = "import "
693- path = "import "
694- description = "Terraform Import URL Acceptance test "
729+ resource "gitlab_project" "imported " {
730+ name = "imported-%d "
731+ default_branch = "master "
732+ import_url = "%s "
695733
696734 # So that acceptance tests can be run in a gitlab organization
697735 # with no billing
698736 visibility_level = "public"
699- merge_method = "ff"
700- only_allow_merge_if_pipeline_succeeds = true
701- only_allow_merge_if_all_discussions_are_resolved = true
702-
703- issues_enabled = false
704- merge_requests_enabled = false
705- approvals_before_merge = 0
706- wiki_enabled = false
707- snippets_enabled = false
708- container_registry_enabled = false
709- shared_runners_enabled = false
710- archived = false
711- import_url = "https://github.com/terraform-providers/terraform-provider-gitlab.git"
712- default_branch = "master"
713737}
714- ` )
715- }
716-
717- func testAccCheckImportURL (fp string , project * gitlab.Project ) resource.TestCheckFunc {
718- return func (s * terraform.State ) error {
719- conn := testAccProvider .Meta ().(* gitlab.Client )
720- ref := & gitlab.GetFileOptions {
721- Ref : gitlab .String ("master" ),
722- }
723- f , _ , err := conn .RepositoryFiles .GetFile (project .ID , fp , ref , nil )
724- if err != nil {
725- return fmt .Errorf ("Cannot find file %s, error %s\n " , fp , err )
726- }
727- if f == nil {
728- return fmt .Errorf ("Did not find file\n " )
729- }
730-
731- return nil
732- }
738+ ` , rInt , importURL )
733739}
0 commit comments