2016-10-19 16:25:12 +02:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-26 19:44:05 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2016-10-19 16:25:12 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/xanzy/go-gitlab"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccGitlabProject_basic(t *testing.T) {
|
|
|
|
var project gitlab.Project
|
2017-04-26 19:44:05 +02:00
|
|
|
rInt := acctest.RandInt()
|
2016-10-19 16:25:12 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckGitlabProjectDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
// Create a project with all the features on
|
2017-04-26 19:44:05 +02:00
|
|
|
{
|
|
|
|
Config: testAccGitlabProjectConfig(rInt),
|
2016-10-19 16:25:12 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
|
|
|
|
testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{
|
2017-04-26 19:44:05 +02:00
|
|
|
Name: fmt.Sprintf("foo-%d", rInt),
|
2016-10-19 16:25:12 +02:00
|
|
|
Description: "Terraform acceptance tests",
|
|
|
|
IssuesEnabled: true,
|
|
|
|
MergeRequestsEnabled: true,
|
|
|
|
WikiEnabled: true,
|
|
|
|
SnippetsEnabled: true,
|
|
|
|
VisibilityLevel: 20,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
// Update the project to turn the features off
|
2017-04-26 19:44:05 +02:00
|
|
|
{
|
|
|
|
Config: testAccGitlabProjectUpdateConfig(rInt),
|
2016-10-19 16:25:12 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
|
|
|
|
testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{
|
2017-04-26 19:44:05 +02:00
|
|
|
Name: fmt.Sprintf("foo-%d", rInt),
|
2016-10-19 16:25:12 +02:00
|
|
|
Description: "Terraform acceptance tests!",
|
|
|
|
VisibilityLevel: 20,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
},
|
2017-04-26 19:44:05 +02:00
|
|
|
//Update the project to turn the features on again
|
|
|
|
{
|
|
|
|
Config: testAccGitlabProjectConfig(rInt),
|
2016-10-19 16:25:12 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
|
|
|
|
testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{
|
2017-04-26 19:44:05 +02:00
|
|
|
Name: fmt.Sprintf("foo-%d", rInt),
|
2016-10-19 16:25:12 +02:00
|
|
|
Description: "Terraform acceptance tests",
|
|
|
|
IssuesEnabled: true,
|
|
|
|
MergeRequestsEnabled: true,
|
|
|
|
WikiEnabled: true,
|
|
|
|
SnippetsEnabled: true,
|
|
|
|
VisibilityLevel: 20,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGitlabProjectExists(n string, project *gitlab.Project) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not Found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
repoName := rs.Primary.ID
|
|
|
|
if repoName == "" {
|
|
|
|
return fmt.Errorf("No project ID is set")
|
|
|
|
}
|
|
|
|
conn := testAccProvider.Meta().(*gitlab.Client)
|
|
|
|
|
|
|
|
gotProject, _, err := conn.Projects.GetProject(repoName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*project = *gotProject
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testAccGitlabProjectExpectedAttributes struct {
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
DefaultBranch string
|
|
|
|
IssuesEnabled bool
|
|
|
|
MergeRequestsEnabled bool
|
|
|
|
WikiEnabled bool
|
|
|
|
SnippetsEnabled bool
|
|
|
|
VisibilityLevel gitlab.VisibilityLevelValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGitlabProjectAttributes(project *gitlab.Project, want *testAccGitlabProjectExpectedAttributes) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if project.Name != want.Name {
|
|
|
|
return fmt.Errorf("got repo %q; want %q", project.Name, want.Name)
|
|
|
|
}
|
|
|
|
if project.Description != want.Description {
|
|
|
|
return fmt.Errorf("got description %q; want %q", project.Description, want.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
if project.DefaultBranch != want.DefaultBranch {
|
|
|
|
return fmt.Errorf("got default_branch %q; want %q", project.DefaultBranch, want.DefaultBranch)
|
|
|
|
}
|
|
|
|
|
|
|
|
if project.IssuesEnabled != want.IssuesEnabled {
|
|
|
|
return fmt.Errorf("got issues_enabled %t; want %t", project.IssuesEnabled, want.IssuesEnabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
if project.MergeRequestsEnabled != want.MergeRequestsEnabled {
|
|
|
|
return fmt.Errorf("got merge_requests_enabled %t; want %t", project.MergeRequestsEnabled, want.MergeRequestsEnabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
if project.WikiEnabled != want.WikiEnabled {
|
|
|
|
return fmt.Errorf("got wiki_enabled %t; want %t", project.WikiEnabled, want.WikiEnabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
if project.SnippetsEnabled != want.SnippetsEnabled {
|
|
|
|
return fmt.Errorf("got snippets_enabled %t; want %t", project.SnippetsEnabled, want.SnippetsEnabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
if project.VisibilityLevel != want.VisibilityLevel {
|
|
|
|
return fmt.Errorf("got default branch %q; want %q", project.VisibilityLevel, want.VisibilityLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGitlabProjectDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*gitlab.Client)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "gitlab_project" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID)
|
|
|
|
if err == nil {
|
|
|
|
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Repository still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 404 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-26 19:44:05 +02:00
|
|
|
func testAccGitlabProjectConfig(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-10-19 16:25:12 +02:00
|
|
|
resource "gitlab_project" "foo" {
|
2017-04-26 19:44:05 +02:00
|
|
|
name = "foo-%d"
|
2016-10-19 16:25:12 +02:00
|
|
|
description = "Terraform acceptance tests"
|
|
|
|
|
|
|
|
# So that acceptance tests can be run in a gitlab organization
|
|
|
|
# with no billing
|
|
|
|
visibility_level = "public"
|
|
|
|
}
|
2017-04-26 19:44:05 +02:00
|
|
|
`, rInt)
|
|
|
|
}
|
2016-10-19 16:25:12 +02:00
|
|
|
|
2017-04-26 19:44:05 +02:00
|
|
|
func testAccGitlabProjectUpdateConfig(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-10-19 16:25:12 +02:00
|
|
|
resource "gitlab_project" "foo" {
|
2017-04-26 19:44:05 +02:00
|
|
|
name = "foo-%d"
|
2016-10-19 16:25:12 +02:00
|
|
|
description = "Terraform acceptance tests!"
|
|
|
|
|
|
|
|
# So that acceptance tests can be run in a gitlab organization
|
|
|
|
# with no billing
|
|
|
|
visibility_level = "public"
|
|
|
|
|
2017-05-24 14:05:27 +02:00
|
|
|
issues_enabled = false
|
|
|
|
merge_requests_enabled = false
|
|
|
|
wiki_enabled = false
|
|
|
|
snippets_enabled = false
|
2016-10-19 16:25:12 +02:00
|
|
|
}
|
2017-04-26 19:44:05 +02:00
|
|
|
`, rInt)
|
|
|
|
}
|