github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/gitlab/resource_gitlab_project_test.go (about)

     1  package gitlab
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/xanzy/go-gitlab"
    11  )
    12  
    13  func TestAccGitlabProject_basic(t *testing.T) {
    14  	var project gitlab.Project
    15  	rInt := acctest.RandInt()
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckGitlabProjectDestroy,
    21  		Steps: []resource.TestStep{
    22  			// Create a project with all the features on
    23  			{
    24  				Config: testAccGitlabProjectConfig(rInt),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
    27  					testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{
    28  						Name:                 fmt.Sprintf("foo-%d", rInt),
    29  						Description:          "Terraform acceptance tests",
    30  						IssuesEnabled:        true,
    31  						MergeRequestsEnabled: true,
    32  						WikiEnabled:          true,
    33  						SnippetsEnabled:      true,
    34  						VisibilityLevel:      20,
    35  					}),
    36  				),
    37  			},
    38  			// Update the project to turn the features off
    39  			{
    40  				Config: testAccGitlabProjectUpdateConfig(rInt),
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
    43  					testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{
    44  						Name:            fmt.Sprintf("foo-%d", rInt),
    45  						Description:     "Terraform acceptance tests!",
    46  						VisibilityLevel: 20,
    47  					}),
    48  				),
    49  			},
    50  			//Update the project to turn the features on again
    51  			{
    52  				Config: testAccGitlabProjectConfig(rInt),
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
    55  					testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{
    56  						Name:                 fmt.Sprintf("foo-%d", rInt),
    57  						Description:          "Terraform acceptance tests",
    58  						IssuesEnabled:        true,
    59  						MergeRequestsEnabled: true,
    60  						WikiEnabled:          true,
    61  						SnippetsEnabled:      true,
    62  						VisibilityLevel:      20,
    63  					}),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func testAccCheckGitlabProjectExists(n string, project *gitlab.Project) resource.TestCheckFunc {
    71  	return func(s *terraform.State) error {
    72  		rs, ok := s.RootModule().Resources[n]
    73  		if !ok {
    74  			return fmt.Errorf("Not Found: %s", n)
    75  		}
    76  
    77  		repoName := rs.Primary.ID
    78  		if repoName == "" {
    79  			return fmt.Errorf("No project ID is set")
    80  		}
    81  		conn := testAccProvider.Meta().(*gitlab.Client)
    82  
    83  		gotProject, _, err := conn.Projects.GetProject(repoName)
    84  		if err != nil {
    85  			return err
    86  		}
    87  		*project = *gotProject
    88  		return nil
    89  	}
    90  }
    91  
    92  type testAccGitlabProjectExpectedAttributes struct {
    93  	Name                 string
    94  	Description          string
    95  	DefaultBranch        string
    96  	IssuesEnabled        bool
    97  	MergeRequestsEnabled bool
    98  	WikiEnabled          bool
    99  	SnippetsEnabled      bool
   100  	VisibilityLevel      gitlab.VisibilityLevelValue
   101  }
   102  
   103  func testAccCheckGitlabProjectAttributes(project *gitlab.Project, want *testAccGitlabProjectExpectedAttributes) resource.TestCheckFunc {
   104  	return func(s *terraform.State) error {
   105  		if project.Name != want.Name {
   106  			return fmt.Errorf("got repo %q; want %q", project.Name, want.Name)
   107  		}
   108  		if project.Description != want.Description {
   109  			return fmt.Errorf("got description %q; want %q", project.Description, want.Description)
   110  		}
   111  
   112  		if project.DefaultBranch != want.DefaultBranch {
   113  			return fmt.Errorf("got default_branch %q; want %q", project.DefaultBranch, want.DefaultBranch)
   114  		}
   115  
   116  		if project.IssuesEnabled != want.IssuesEnabled {
   117  			return fmt.Errorf("got issues_enabled %t; want %t", project.IssuesEnabled, want.IssuesEnabled)
   118  		}
   119  
   120  		if project.MergeRequestsEnabled != want.MergeRequestsEnabled {
   121  			return fmt.Errorf("got merge_requests_enabled %t; want %t", project.MergeRequestsEnabled, want.MergeRequestsEnabled)
   122  		}
   123  
   124  		if project.WikiEnabled != want.WikiEnabled {
   125  			return fmt.Errorf("got wiki_enabled %t; want %t", project.WikiEnabled, want.WikiEnabled)
   126  		}
   127  
   128  		if project.SnippetsEnabled != want.SnippetsEnabled {
   129  			return fmt.Errorf("got snippets_enabled %t; want %t", project.SnippetsEnabled, want.SnippetsEnabled)
   130  		}
   131  
   132  		if project.VisibilityLevel != want.VisibilityLevel {
   133  			return fmt.Errorf("got default branch %q; want %q", project.VisibilityLevel, want.VisibilityLevel)
   134  		}
   135  
   136  		return nil
   137  	}
   138  }
   139  
   140  func testAccCheckGitlabProjectDestroy(s *terraform.State) error {
   141  	conn := testAccProvider.Meta().(*gitlab.Client)
   142  
   143  	for _, rs := range s.RootModule().Resources {
   144  		if rs.Type != "gitlab_project" {
   145  			continue
   146  		}
   147  
   148  		gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID)
   149  		if err == nil {
   150  			if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
   151  				return fmt.Errorf("Repository still exists")
   152  			}
   153  		}
   154  		if resp.StatusCode != 404 {
   155  			return err
   156  		}
   157  		return nil
   158  	}
   159  	return nil
   160  }
   161  
   162  func testAccGitlabProjectConfig(rInt int) string {
   163  	return fmt.Sprintf(`
   164  resource "gitlab_project" "foo" {
   165    name = "foo-%d"
   166    description = "Terraform acceptance tests"
   167  
   168    # So that acceptance tests can be run in a gitlab organization
   169    # with no billing
   170    visibility_level = "public"
   171  }
   172  	`, rInt)
   173  }
   174  
   175  func testAccGitlabProjectUpdateConfig(rInt int) string {
   176  	return fmt.Sprintf(`
   177  resource "gitlab_project" "foo" {
   178    name = "foo-%d"
   179    description = "Terraform acceptance tests!"
   180  
   181    # So that acceptance tests can be run in a gitlab organization
   182    # with no billing
   183    visibility_level = "public"
   184  
   185    issues_enabled = false
   186    merge_requests_enabled = false
   187    wiki_enabled = false
   188    snippets_enabled = false
   189  }
   190  	`, rInt)
   191  }