|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 12 | + "github.com/xanzy/go-gitlab" |
| 13 | +) |
| 14 | + |
| 15 | +func TestAccGitlabProjectBadge_basic(t *testing.T) { |
| 16 | + var badge gitlab.ProjectBadge |
| 17 | + rInt := acctest.RandInt() |
| 18 | + |
| 19 | + resource.Test(t, resource.TestCase{ |
| 20 | + PreCheck: func() { testAccPreCheck(t) }, |
| 21 | + Providers: testAccProviders, |
| 22 | + CheckDestroy: testAccCheckGitlabProjectBadgeDestroy, |
| 23 | + Steps: []resource.TestStep{ |
| 24 | + // Create a project and badge |
| 25 | + { |
| 26 | + Config: testAccGitlabProjectBadgeConfig(rInt), |
| 27 | + Check: resource.ComposeTestCheckFunc( |
| 28 | + testAccCheckGitlabProjectBadgeExists("gitlab_project_badge.foo", &badge), |
| 29 | + testAccCheckGitlabProjectBadgeAttributes(&badge, &testAccGitlabProjectBadgeExpectedAttributes{ |
| 30 | + LinkURL: fmt.Sprintf("https://example.com/badge-%d", rInt), |
| 31 | + ImageURL: fmt.Sprintf("https://example.com/badge-%d.svg", rInt), |
| 32 | + }), |
| 33 | + ), |
| 34 | + }, |
| 35 | + // Test ImportState |
| 36 | + { |
| 37 | + ResourceName: "gitlab_project_badge.foo", |
| 38 | + ImportState: true, |
| 39 | + ImportStateVerify: true, |
| 40 | + }, |
| 41 | + // Update the project badge |
| 42 | + { |
| 43 | + Config: testAccGitlabProjectBadgeUpdateConfig(rInt), |
| 44 | + Check: resource.ComposeTestCheckFunc( |
| 45 | + testAccCheckGitlabProjectBadgeExists("gitlab_project_badge.foo", &badge), |
| 46 | + testAccCheckGitlabProjectBadgeAttributes(&badge, &testAccGitlabProjectBadgeExpectedAttributes{ |
| 47 | + LinkURL: fmt.Sprintf("https://example.com/badge-%d", rInt), |
| 48 | + ImageURL: fmt.Sprintf("https://example.com/badge-%d.svg", rInt), |
| 49 | + }), |
| 50 | + ), |
| 51 | + }, |
| 52 | + }, |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +func testAccCheckGitlabProjectBadgeExists(n string, badge *gitlab.ProjectBadge) resource.TestCheckFunc { |
| 57 | + return func(s *terraform.State) error { |
| 58 | + rs, ok := s.RootModule().Resources[n] |
| 59 | + if !ok { |
| 60 | + return fmt.Errorf("Not Found: %s", n) |
| 61 | + } |
| 62 | + |
| 63 | + splitID := strings.Split(rs.Primary.ID, ":") |
| 64 | + |
| 65 | + badgeID, err := strconv.Atoi(splitID[len(splitID)-1]) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + repoName := rs.Primary.Attributes["project"] |
| 70 | + if repoName == "" { |
| 71 | + return fmt.Errorf("No project ID is set") |
| 72 | + } |
| 73 | + |
| 74 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 75 | + |
| 76 | + gotBadge, _, err := conn.ProjectBadges.GetProjectBadge(repoName, badgeID) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + *badge = *gotBadge |
| 81 | + return nil |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +type testAccGitlabProjectBadgeExpectedAttributes struct { |
| 86 | + LinkURL string |
| 87 | + ImageURL string |
| 88 | +} |
| 89 | + |
| 90 | +func testAccCheckGitlabProjectBadgeAttributes(badge *gitlab.ProjectBadge, want *testAccGitlabProjectBadgeExpectedAttributes) resource.TestCheckFunc { |
| 91 | + return func(s *terraform.State) error { |
| 92 | + if badge.LinkURL != want.LinkURL { |
| 93 | + return fmt.Errorf("got link_url %q; want %q", badge.LinkURL, want.LinkURL) |
| 94 | + } |
| 95 | + |
| 96 | + if badge.ImageURL != want.ImageURL { |
| 97 | + return fmt.Errorf("got image_url %s; want %s", badge.ImageURL, want.ImageURL) |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func testAccCheckGitlabProjectBadgeDestroy(s *terraform.State) error { |
| 105 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 106 | + |
| 107 | + for _, rs := range s.RootModule().Resources { |
| 108 | + if rs.Type != "gitlab_project" { |
| 109 | + continue |
| 110 | + } |
| 111 | + |
| 112 | + gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil) |
| 113 | + if err == nil { |
| 114 | + if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID { |
| 115 | + if gotRepo.MarkedForDeletionAt == nil { |
| 116 | + return fmt.Errorf("Repository still exists") |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + if resp.StatusCode != 404 { |
| 121 | + return err |
| 122 | + } |
| 123 | + return nil |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func testAccGitlabProjectBadgeConfig(rInt int) string { |
| 129 | + return fmt.Sprintf(` |
| 130 | +resource "gitlab_project" "foo" { |
| 131 | + name = "foo-%d" |
| 132 | + description = "Terraform acceptance tests" |
| 133 | +
|
| 134 | + # So that acceptance tests can be run in a gitlab organization |
| 135 | + # with no billing |
| 136 | + visibility_level = "public" |
| 137 | +} |
| 138 | +
|
| 139 | +resource "gitlab_project_badge" "foo" { |
| 140 | + project = "${gitlab_project.foo.id}" |
| 141 | + link_url = "https://example.com/badge-%d" |
| 142 | + image_url = "https://example.com/badge-%d.svg" |
| 143 | +} |
| 144 | + `, rInt, rInt, rInt) |
| 145 | +} |
| 146 | + |
| 147 | +func testAccGitlabProjectBadgeUpdateConfig(rInt int) string { |
| 148 | + return fmt.Sprintf(` |
| 149 | +resource "gitlab_project" "foo" { |
| 150 | + name = "foo-%d" |
| 151 | + description = "Terraform acceptance tests" |
| 152 | +
|
| 153 | + # So that acceptance tests can be run in a gitlab organization |
| 154 | + # with no billing |
| 155 | + visibility_level = "public" |
| 156 | +} |
| 157 | +
|
| 158 | +resource "gitlab_project_badge" "foo" { |
| 159 | + project = "${gitlab_project.foo.id}" |
| 160 | + link_url = "https://example.com/badge-%d" |
| 161 | + image_url = "https://example.com/badge-%d.svg" |
| 162 | +} |
| 163 | + `, rInt, rInt, rInt) |
| 164 | +} |
0 commit comments