|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 13 | + "github.com/xanzy/go-gitlab" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAccGitlabProjectMirror_basic(t *testing.T) { |
| 17 | + var hook gitlab.ProjectMirror |
| 18 | + rInt := acctest.RandInt() |
| 19 | + |
| 20 | + resource.Test(t, resource.TestCase{ |
| 21 | + PreCheck: func() { testAccPreCheck(t) }, |
| 22 | + Providers: testAccProviders, |
| 23 | + CheckDestroy: testAccCheckGitlabProjectMirrorDestroy, |
| 24 | + Steps: []resource.TestStep{ |
| 25 | + // Create a project and hook with default options |
| 26 | + { |
| 27 | + Config: testAccGitlabProjectMirrorConfig(rInt), |
| 28 | + Check: resource.ComposeTestCheckFunc( |
| 29 | + testAccCheckGitlabProjectMirrorExists("gitlab_project_mirror.foo", &hook), |
| 30 | + testAccCheckGitlabProjectMirrorAttributes(&hook, &testAccGitlabProjectMirrorExpectedAttributes{ |
| 31 | + URL: fmt.Sprintf("https://example.com/hook-%d", rInt), |
| 32 | + Enabled: true, |
| 33 | + OnlyProtectedBranches: true, |
| 34 | + KeepDivergentRefs: true, |
| 35 | + }), |
| 36 | + ), |
| 37 | + }, |
| 38 | + // Update the project hook to toggle all the values to their inverse |
| 39 | + { |
| 40 | + Config: testAccGitlabProjectMirrorUpdateConfig(rInt), |
| 41 | + Check: resource.ComposeTestCheckFunc( |
| 42 | + testAccCheckGitlabProjectMirrorExists("gitlab_project_mirror.foo", &hook), |
| 43 | + testAccCheckGitlabProjectMirrorAttributes(&hook, &testAccGitlabProjectMirrorExpectedAttributes{ |
| 44 | + URL: fmt.Sprintf("https://example.com/hook-%d", rInt), |
| 45 | + Enabled: false, |
| 46 | + OnlyProtectedBranches: false, |
| 47 | + KeepDivergentRefs: false, |
| 48 | + }), |
| 49 | + ), |
| 50 | + }, |
| 51 | + // Update the project hook to toggle the options back |
| 52 | + { |
| 53 | + Config: testAccGitlabProjectMirrorConfig(rInt), |
| 54 | + Check: resource.ComposeTestCheckFunc( |
| 55 | + testAccCheckGitlabProjectMirrorExists("gitlab_project_mirror.foo", &hook), |
| 56 | + testAccCheckGitlabProjectMirrorAttributes(&hook, &testAccGitlabProjectMirrorExpectedAttributes{ |
| 57 | + URL: fmt.Sprintf("https://example.com/hook-%d", rInt), |
| 58 | + Enabled: true, |
| 59 | + OnlyProtectedBranches: true, |
| 60 | + KeepDivergentRefs: true, |
| 61 | + }), |
| 62 | + ), |
| 63 | + }, |
| 64 | + }, |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func TestAccGitlabProjectMirror_import(t *testing.T) { |
| 69 | + rInt := acctest.RandInt() |
| 70 | + |
| 71 | + resource.Test(t, resource.TestCase{ |
| 72 | + PreCheck: func() { testAccPreCheck(t) }, |
| 73 | + Providers: testAccProviders, |
| 74 | + CheckDestroy: testAccCheckGitlabProjectMirrorDestroy, |
| 75 | + Steps: []resource.TestStep{ |
| 76 | + { |
| 77 | + Config: testAccGitlabProjectMirrorConfig(rInt), |
| 78 | + }, |
| 79 | + { |
| 80 | + ResourceName: "gitlab_project_mirror.foo", |
| 81 | + ImportState: true, |
| 82 | + ImportStateVerify: true, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +func testAccCheckGitlabProjectMirrorExists(n string, mirror *gitlab.ProjectMirror) resource.TestCheckFunc { |
| 89 | + return func(s *terraform.State) error { |
| 90 | + rs, ok := s.RootModule().Resources[n] |
| 91 | + if !ok { |
| 92 | + return fmt.Errorf("Not Found: %s", n) |
| 93 | + } |
| 94 | + |
| 95 | + splitID := strings.Split(rs.Primary.ID, ":") |
| 96 | + |
| 97 | + mirrorID, err := strconv.Atoi(splitID[len(splitID)-1]) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + repoName := rs.Primary.Attributes["project"] |
| 102 | + if repoName == "" { |
| 103 | + return fmt.Errorf("No project ID is set") |
| 104 | + } |
| 105 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 106 | + |
| 107 | + mirrors, _, err := conn.ProjectMirrors.ListProjectMirror(repoName, nil) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + for _, m := range mirrors { |
| 113 | + if m.ID == mirrorID { |
| 114 | + *mirror = *m |
| 115 | + break |
| 116 | + } |
| 117 | + return errors.New("unable to find mirror.") |
| 118 | + } |
| 119 | + return nil |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +type testAccGitlabProjectMirrorExpectedAttributes struct { |
| 124 | + URL string |
| 125 | + Enabled bool |
| 126 | + OnlyProtectedBranches bool |
| 127 | + KeepDivergentRefs bool |
| 128 | +} |
| 129 | + |
| 130 | +func testAccCheckGitlabProjectMirrorAttributes(mirror *gitlab.ProjectMirror, want *testAccGitlabProjectMirrorExpectedAttributes) resource.TestCheckFunc { |
| 131 | + return func(s *terraform.State) error { |
| 132 | + if mirror.URL != want.URL { |
| 133 | + return fmt.Errorf("got url %q; want %q", mirror.URL, want.URL) |
| 134 | + } |
| 135 | + |
| 136 | + if mirror.Enabled != want.Enabled { |
| 137 | + return fmt.Errorf("got enabled %t; want %t", mirror.Enabled, want.Enabled) |
| 138 | + } |
| 139 | + if mirror.OnlyProtectedBranches != want.OnlyProtectedBranches { |
| 140 | + return fmt.Errorf("got only protected branches %t; want %t", mirror.OnlyProtectedBranches, want.OnlyProtectedBranches) |
| 141 | + } |
| 142 | + if mirror.KeepDivergentRefs != want.KeepDivergentRefs { |
| 143 | + return fmt.Errorf("got keep divergent refs %t; want %t", mirror.KeepDivergentRefs, want.KeepDivergentRefs) |
| 144 | + } |
| 145 | + return nil |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func testAccCheckGitlabProjectMirrorDestroy(s *terraform.State) error { |
| 150 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 151 | + |
| 152 | + for _, rs := range s.RootModule().Resources { |
| 153 | + if rs.Type != "gitlab_project" { |
| 154 | + continue |
| 155 | + } |
| 156 | + |
| 157 | + gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil) |
| 158 | + if err == nil { |
| 159 | + if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID { |
| 160 | + if gotRepo.MarkedForDeletionAt == nil { |
| 161 | + return fmt.Errorf("Repository still exists") |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + if resp.StatusCode != 404 { |
| 166 | + return err |
| 167 | + } |
| 168 | + return nil |
| 169 | + } |
| 170 | + return nil |
| 171 | +} |
| 172 | + |
| 173 | +func testAccGitlabProjectMirrorConfig(rInt int) string { |
| 174 | + return fmt.Sprintf(` |
| 175 | +resource "gitlab_project" "foo" { |
| 176 | + name = "foo-%d" |
| 177 | + description = "Terraform acceptance tests" |
| 178 | +
|
| 179 | + # So that acceptance tests can be run in a gitlab organization |
| 180 | + # with no billing |
| 181 | + visibility_level = "public" |
| 182 | +} |
| 183 | +
|
| 184 | +resource "gitlab_project_mirror" "foo" { |
| 185 | + project = "${gitlab_project.foo.id}" |
| 186 | + url = "https://example.com/hook-%d" |
| 187 | +} |
| 188 | + `, rInt, rInt) |
| 189 | +} |
| 190 | + |
| 191 | +func testAccGitlabProjectMirrorUpdateConfig(rInt int) string { |
| 192 | + return fmt.Sprintf(` |
| 193 | +resource "gitlab_project" "foo" { |
| 194 | + name = "foo-%d" |
| 195 | + description = "Terraform acceptance tests" |
| 196 | +
|
| 197 | + # So that acceptance tests can be run in a gitlab organization |
| 198 | + # with no billing |
| 199 | + visibility_level = "public" |
| 200 | +} |
| 201 | +
|
| 202 | +resource "gitlab_project_mirror" "foo" { |
| 203 | + project = "${gitlab_project.foo.id}" |
| 204 | + url = "https://example.com/hook-%d" |
| 205 | + enabled = false |
| 206 | + only_protected_branches = false |
| 207 | + keep_divergent_refs = false |
| 208 | +} |
| 209 | + `, rInt, rInt) |
| 210 | +} |
0 commit comments