|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 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 | + gitlab "github.com/xanzy/go-gitlab" |
| 13 | +) |
| 14 | + |
| 15 | +func TestAccGitlabServicePipelinesEmail_basic(t *testing.T) { |
| 16 | + var pipelinesEmailService gitlab.PipelinesEmailService |
| 17 | + rInt := acctest.RandInt() |
| 18 | + pipelinesEmailResourceName := "gitlab_service_pipelines_email.email" |
| 19 | + |
| 20 | + resource.Test(t, resource.TestCase{ |
| 21 | + PreCheck: func() { testAccPreCheck(t) }, |
| 22 | + Providers: testAccProviders, |
| 23 | + CheckDestroy: testAccCheckGitlabServicePipelinesEmailDestroy, |
| 24 | + Steps: []resource.TestStep{ |
| 25 | + // Create a project and a pipelines email service |
| 26 | + { |
| 27 | + Config: testAccGitlabServicePipelinesEmailConfig(rInt), |
| 28 | + Check: resource.ComposeTestCheckFunc( |
| 29 | + testAccCheckGitlabServicePipelinesEmailExists(pipelinesEmailResourceName, &pipelinesEmailService), |
| 30 | + testRecipients( &pipelinesEmailService, [] string{ "[email protected]"}), |
| 31 | + resource.TestCheckResourceAttr(pipelinesEmailResourceName, "notify_only_broken_pipelines", "true"), |
| 32 | + resource.TestCheckResourceAttr(pipelinesEmailResourceName, "branches_to_be_notified", "default"), |
| 33 | + ), |
| 34 | + }, |
| 35 | + // Update the pipelinesEmail service |
| 36 | + { |
| 37 | + Config: testAccGitlabServicePipelinesEmailUpdateConfig(rInt), |
| 38 | + Check: resource.ComposeTestCheckFunc( |
| 39 | + testAccCheckGitlabServicePipelinesEmailExists(pipelinesEmailResourceName, &pipelinesEmailService), |
| 40 | + testRecipients( &pipelinesEmailService, [] string{ "[email protected]", "[email protected]"}), |
| 41 | + resource.TestCheckResourceAttr(pipelinesEmailResourceName, "notify_only_broken_pipelines", "false"), |
| 42 | + resource.TestCheckResourceAttr(pipelinesEmailResourceName, "branches_to_be_notified", "all"), |
| 43 | + ), |
| 44 | + }, |
| 45 | + // Update the pipelinesEmail service to get back to previous settings |
| 46 | + { |
| 47 | + Config: testAccGitlabServicePipelinesEmailConfig(rInt), |
| 48 | + Check: resource.ComposeTestCheckFunc( |
| 49 | + testAccCheckGitlabServicePipelinesEmailExists(pipelinesEmailResourceName, &pipelinesEmailService), |
| 50 | + testRecipients( &pipelinesEmailService, [] string{ "[email protected]"}), |
| 51 | + resource.TestCheckResourceAttr(pipelinesEmailResourceName, "notify_only_broken_pipelines", "true"), |
| 52 | + resource.TestCheckResourceAttr(pipelinesEmailResourceName, "branches_to_be_notified", "default"), |
| 53 | + ), |
| 54 | + }, |
| 55 | + }, |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func TestAccGitlabServicePipelinesEmail_import(t *testing.T) { |
| 60 | + pipelinesEmailResourceName := "gitlab_service_pipelines_email.email" |
| 61 | + rInt := acctest.RandInt() |
| 62 | + |
| 63 | + resource.Test(t, resource.TestCase{ |
| 64 | + PreCheck: func() { testAccPreCheck(t) }, |
| 65 | + Providers: testAccProviders, |
| 66 | + CheckDestroy: testAccCheckGitlabServicePipelinesEmailDestroy, |
| 67 | + Steps: []resource.TestStep{ |
| 68 | + { |
| 69 | + Config: testAccGitlabServicePipelinesEmailConfig(rInt), |
| 70 | + }, |
| 71 | + { |
| 72 | + ResourceName: pipelinesEmailResourceName, |
| 73 | + ImportState: true, |
| 74 | + ImportStateVerify: true, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +func testAccCheckGitlabServicePipelinesEmailExists(n string, service *gitlab.PipelinesEmailService) resource.TestCheckFunc { |
| 81 | + return func(s *terraform.State) error { |
| 82 | + rs, ok := s.RootModule().Resources[n] |
| 83 | + if !ok { |
| 84 | + return fmt.Errorf("Not Found: %s", n) |
| 85 | + } |
| 86 | + |
| 87 | + project := rs.Primary.Attributes["project"] |
| 88 | + if project == "" { |
| 89 | + return fmt.Errorf("No project ID is set") |
| 90 | + } |
| 91 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 92 | + |
| 93 | + pipelinesEmailService, _, err := conn.Services.GetPipelinesEmailService(project) |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("PipelinesEmail service does not exist in project %s: %v", project, err) |
| 96 | + } |
| 97 | + *service = *pipelinesEmailService |
| 98 | + |
| 99 | + return nil |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func testRecipients(service *gitlab.PipelinesEmailService, expected []string) resource.TestCheckFunc { |
| 104 | + return func(s *terraform.State) error { |
| 105 | + res_string := service.Properties.Recipients |
| 106 | + res := strings.Split(res_string, ",") |
| 107 | + if len(res) != len(expected) { |
| 108 | + return fmt.Errorf("'recipients' field does not have the correct size expected: %d, found: %d", len(expected), len(res)) |
| 109 | + } |
| 110 | + sort.Strings(res) |
| 111 | + sort.Strings(expected) |
| 112 | + for i, r := range res { |
| 113 | + e := expected[i] |
| 114 | + if r != e { |
| 115 | + return fmt.Errorf("expected: %s, found: %s", r, e) |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + return nil |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func testAccCheckGitlabServicePipelinesEmailDestroy(s *terraform.State) error { |
| 124 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 125 | + |
| 126 | + for _, rs := range s.RootModule().Resources { |
| 127 | + if rs.Type != "gitlab_project" { |
| 128 | + continue |
| 129 | + } |
| 130 | + |
| 131 | + gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil) |
| 132 | + if err == nil { |
| 133 | + if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID { |
| 134 | + if gotRepo.MarkedForDeletionAt == nil { |
| 135 | + return fmt.Errorf("Repository still exists") |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + if resp.StatusCode != 404 { |
| 140 | + return err |
| 141 | + } |
| 142 | + return nil |
| 143 | + } |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +func testAccGitlabServicePipelinesEmailConfig(rInt int) string { |
| 148 | + return fmt.Sprintf(` |
| 149 | +resource "gitlab_project" "foo" { |
| 150 | + name = "foo-%d" |
| 151 | + description = "Terraform acceptance tests" |
| 152 | +} |
| 153 | +
|
| 154 | +resource "gitlab_service_pipelines_email" "email" { |
| 155 | + project = gitlab_project.foo.id |
| 156 | + recipients = ["[email protected]"] |
| 157 | +} |
| 158 | +`, rInt) |
| 159 | +} |
| 160 | + |
| 161 | +func testAccGitlabServicePipelinesEmailUpdateConfig(rInt int) string { |
| 162 | + return fmt.Sprintf(` |
| 163 | +resource "gitlab_project" "foo" { |
| 164 | + name = "foo-%d" |
| 165 | + description = "Terraform acceptance tests" |
| 166 | +} |
| 167 | +
|
| 168 | +resource "gitlab_service_pipelines_email" "email" { |
| 169 | + project = gitlab_project.foo.id |
| 170 | + |
| 171 | + notify_only_broken_pipelines = false |
| 172 | + branches_to_be_notified = "all" |
| 173 | +} |
| 174 | +`, rInt) |
| 175 | +} |
0 commit comments