|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 10 | + gitlab "github.com/xanzy/go-gitlab" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccGitlabServiceGithub_basic(t *testing.T) { |
| 14 | + var githubService gitlab.GithubService |
| 15 | + rInt := acctest.RandInt() |
| 16 | + githubResourceName := "gitlab_service_github.github" |
| 17 | + |
| 18 | + resource.Test(t, resource.TestCase{ |
| 19 | + PreCheck: func() { testAccPreCheck(t) }, |
| 20 | + Providers: testAccProviders, |
| 21 | + CheckDestroy: testAccCheckGitlabServiceGithubDestroy, |
| 22 | + Steps: []resource.TestStep{ |
| 23 | + // Create a project and a github service |
| 24 | + { |
| 25 | + SkipFunc: isRunningInCE, |
| 26 | + Config: testAccGitlabServiceGithubConfig(rInt), |
| 27 | + Check: resource.ComposeTestCheckFunc( |
| 28 | + testAccCheckGitlabServiceGithubExists(githubResourceName, &githubService), |
| 29 | + resource.TestCheckResourceAttr(githubResourceName, "repository_url", "https://github.com/terraform-providers/terraform-provider-gitlab"), |
| 30 | + resource.TestCheckResourceAttr(githubResourceName, "static_context", "true"), |
| 31 | + ), |
| 32 | + }, |
| 33 | + // Update the github service |
| 34 | + { |
| 35 | + SkipFunc: isRunningInCE, |
| 36 | + Config: testAccGitlabServiceGithubUpdateConfig(rInt), |
| 37 | + Check: resource.ComposeTestCheckFunc( |
| 38 | + testAccCheckGitlabServiceGithubExists(githubResourceName, &githubService), |
| 39 | + resource.TestCheckResourceAttr(githubResourceName, "repository_url", "https://github.com/terraform-providers/terraform-provider-github"), |
| 40 | + resource.TestCheckResourceAttr(githubResourceName, "static_context", "false"), |
| 41 | + ), |
| 42 | + }, |
| 43 | + // Update the github service to get back to previous settings |
| 44 | + { |
| 45 | + SkipFunc: isRunningInCE, |
| 46 | + Config: testAccGitlabServiceGithubConfig(rInt), |
| 47 | + Check: resource.ComposeTestCheckFunc( |
| 48 | + testAccCheckGitlabServiceGithubExists(githubResourceName, &githubService), |
| 49 | + resource.TestCheckResourceAttr(githubResourceName, "repository_url", "https://github.com/terraform-providers/terraform-provider-gitlab"), |
| 50 | + resource.TestCheckResourceAttr(githubResourceName, "static_context", "true"), |
| 51 | + ), |
| 52 | + }, |
| 53 | + }, |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +func TestAccGitlabServiceGithub_import(t *testing.T) { |
| 58 | + githubResourceName := "gitlab_service_github.github" |
| 59 | + rInt := acctest.RandInt() |
| 60 | + |
| 61 | + resource.Test(t, resource.TestCase{ |
| 62 | + PreCheck: func() { testAccPreCheck(t) }, |
| 63 | + Providers: testAccProviders, |
| 64 | + CheckDestroy: testAccCheckGitlabServiceGithubDestroy, |
| 65 | + Steps: []resource.TestStep{ |
| 66 | + { |
| 67 | + SkipFunc: isRunningInCE, |
| 68 | + Config: testAccGitlabServiceGithubConfig(rInt), |
| 69 | + }, |
| 70 | + { |
| 71 | + SkipFunc: isRunningInCE, |
| 72 | + ResourceName: githubResourceName, |
| 73 | + ImportStateIdFunc: getGithubProjectID(githubResourceName), |
| 74 | + ImportState: true, |
| 75 | + ImportStateVerify: true, |
| 76 | + ImportStateVerifyIgnore: []string{ |
| 77 | + "token", |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +func testAccCheckGitlabServiceGithubExists(n string, service *gitlab.GithubService) resource.TestCheckFunc { |
| 85 | + return func(s *terraform.State) error { |
| 86 | + rs, ok := s.RootModule().Resources[n] |
| 87 | + if !ok { |
| 88 | + return fmt.Errorf("Not Found: %s", n) |
| 89 | + } |
| 90 | + |
| 91 | + project := rs.Primary.Attributes["project"] |
| 92 | + if project == "" { |
| 93 | + return fmt.Errorf("No project ID is set") |
| 94 | + } |
| 95 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 96 | + |
| 97 | + githubService, _, err := conn.Services.GetGithubService(project) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("Github service does not exist in project %s: %v", project, err) |
| 100 | + } |
| 101 | + *service = *githubService |
| 102 | + |
| 103 | + return nil |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func testAccCheckGitlabServiceGithubDestroy(s *terraform.State) error { |
| 108 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 109 | + |
| 110 | + for _, rs := range s.RootModule().Resources { |
| 111 | + if rs.Type != "gitlab_project" { |
| 112 | + continue |
| 113 | + } |
| 114 | + |
| 115 | + gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil) |
| 116 | + if err == nil { |
| 117 | + if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID { |
| 118 | + if gotRepo.MarkedForDeletionAt == nil { |
| 119 | + return fmt.Errorf("Repository still exists") |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + if resp.StatusCode != 404 { |
| 124 | + return err |
| 125 | + } |
| 126 | + return nil |
| 127 | + } |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func getGithubProjectID(n string) resource.ImportStateIdFunc { |
| 132 | + return func(s *terraform.State) (string, error) { |
| 133 | + rs, ok := s.RootModule().Resources[n] |
| 134 | + if !ok { |
| 135 | + return "", fmt.Errorf("Not Found: %s", n) |
| 136 | + } |
| 137 | + |
| 138 | + project := rs.Primary.Attributes["project"] |
| 139 | + if project == "" { |
| 140 | + return "", fmt.Errorf("No project ID is set") |
| 141 | + } |
| 142 | + |
| 143 | + return project, nil |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func testAccGitlabServiceGithubConfig(rInt int) string { |
| 148 | + return fmt.Sprintf(` |
| 149 | +# Requires billing with Silver or above |
| 150 | +resource "gitlab_project" "foo" { |
| 151 | + name = "foo-%d" |
| 152 | + description = "Terraform acceptance tests" |
| 153 | +} |
| 154 | +
|
| 155 | +resource "gitlab_service_github" "github" { |
| 156 | + project = "${gitlab_project.foo.id}" |
| 157 | + token = "test" |
| 158 | + repository_url = "https://github.com/terraform-providers/terraform-provider-gitlab" |
| 159 | +} |
| 160 | +`, rInt) |
| 161 | +} |
| 162 | + |
| 163 | +func testAccGitlabServiceGithubUpdateConfig(rInt int) string { |
| 164 | + return fmt.Sprintf(` |
| 165 | +# Requires billing with Silver or above |
| 166 | +resource "gitlab_project" "foo" { |
| 167 | + name = "foo-%d" |
| 168 | + description = "Terraform acceptance tests" |
| 169 | +} |
| 170 | +
|
| 171 | +resource "gitlab_service_github" "github" { |
| 172 | + project = "${gitlab_project.foo.id}" |
| 173 | + token = "test" |
| 174 | + repository_url = "https://github.com/terraform-providers/terraform-provider-github" |
| 175 | + static_context = false |
| 176 | +} |
| 177 | +`, rInt) |
| 178 | +} |
0 commit comments