github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/gitlab/resource_gitlab_group_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 TestAccGitlabGroup_basic(t *testing.T) { 14 var group gitlab.Group 15 rInt := acctest.RandInt() 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckGitlabGroupDestroy, 21 Steps: []resource.TestStep{ 22 // Create a group 23 { 24 Config: testAccGitlabGroupConfig(rInt), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckGitlabGroupExists("gitlab_group.foo", &group), 27 testAccCheckGitlabGroupAttributes(&group, &testAccGitlabGroupExpectedAttributes{ 28 Name: fmt.Sprintf("foo-name-%d", rInt), 29 Path: fmt.Sprintf("foo-path-%d", rInt), 30 Description: "Terraform acceptance tests", 31 LFSEnabled: true, 32 }), 33 ), 34 }, 35 // Update the group to change the description 36 { 37 Config: testAccGitlabGroupUpdateConfig(rInt), 38 Check: resource.ComposeTestCheckFunc( 39 testAccCheckGitlabGroupExists("gitlab_group.foo", &group), 40 testAccCheckGitlabGroupAttributes(&group, &testAccGitlabGroupExpectedAttributes{ 41 Name: fmt.Sprintf("bar-name-%d", rInt), 42 Path: fmt.Sprintf("bar-path-%d", rInt), 43 Description: "Terraform acceptance tests! Updated description", 44 RequestAccessEnabled: true, 45 }), 46 ), 47 }, 48 // Update the group to put the anem and description back 49 { 50 Config: testAccGitlabGroupConfig(rInt), 51 Check: resource.ComposeTestCheckFunc( 52 testAccCheckGitlabGroupExists("gitlab_group.foo", &group), 53 testAccCheckGitlabGroupAttributes(&group, &testAccGitlabGroupExpectedAttributes{ 54 Name: fmt.Sprintf("foo-name-%d", rInt), 55 Path: fmt.Sprintf("foo-path-%d", rInt), 56 Description: "Terraform acceptance tests", 57 LFSEnabled: true, 58 }), 59 ), 60 }, 61 }, 62 }) 63 } 64 65 func testAccCheckGitlabGroupExists(n string, group *gitlab.Group) resource.TestCheckFunc { 66 return func(s *terraform.State) error { 67 rs, ok := s.RootModule().Resources[n] 68 if !ok { 69 return fmt.Errorf("Not Found: %s", n) 70 } 71 72 groupID := rs.Primary.ID 73 if groupID == "" { 74 return fmt.Errorf("No group ID is set") 75 } 76 conn := testAccProvider.Meta().(*gitlab.Client) 77 78 gotGroup, _, err := conn.Groups.GetGroup(groupID) 79 if err != nil { 80 return err 81 } 82 *group = *gotGroup 83 return nil 84 } 85 } 86 87 type testAccGitlabGroupExpectedAttributes struct { 88 Name string 89 Path string 90 Description string 91 LFSEnabled bool 92 RequestAccessEnabled bool 93 } 94 95 func testAccCheckGitlabGroupAttributes(group *gitlab.Group, want *testAccGitlabGroupExpectedAttributes) resource.TestCheckFunc { 96 return func(s *terraform.State) error { 97 if group.Name != want.Name { 98 return fmt.Errorf("got repo %q; want %q", group.Name, want.Name) 99 } 100 101 if group.Path != want.Path { 102 return fmt.Errorf("got path %q; want %q", group.Path, want.Path) 103 } 104 105 if group.Description != want.Description { 106 return fmt.Errorf("got description %q; want %q", group.Description, want.Description) 107 } 108 109 if group.LFSEnabled != want.LFSEnabled { 110 return fmt.Errorf("got lfs_enabled %t; want %t", group.LFSEnabled, want.LFSEnabled) 111 } 112 113 if group.RequestAccessEnabled != want.RequestAccessEnabled { 114 return fmt.Errorf("got request_access_enabled %t; want %t", group.RequestAccessEnabled, want.RequestAccessEnabled) 115 } 116 117 return nil 118 } 119 } 120 121 func testAccCheckGitlabGroupDestroy(s *terraform.State) error { 122 conn := testAccProvider.Meta().(*gitlab.Client) 123 124 for _, rs := range s.RootModule().Resources { 125 if rs.Type != "gitlab_group" { 126 continue 127 } 128 129 group, resp, err := conn.Groups.GetGroup(rs.Primary.ID) 130 if err == nil { 131 if group != nil && fmt.Sprintf("%d", group.ID) == rs.Primary.ID { 132 return fmt.Errorf("Group still exists") 133 } 134 } 135 if resp.StatusCode != 404 { 136 return err 137 } 138 return nil 139 } 140 return nil 141 } 142 143 func testAccGitlabGroupConfig(rInt int) string { 144 return fmt.Sprintf(` 145 resource "gitlab_group" "foo" { 146 name = "foo-name-%d" 147 path = "foo-path-%d" 148 description = "Terraform acceptance tests" 149 150 # So that acceptance tests can be run in a gitlab organization 151 # with no billing 152 visibility_level = "public" 153 } 154 `, rInt, rInt) 155 } 156 157 func testAccGitlabGroupUpdateConfig(rInt int) string { 158 return fmt.Sprintf(` 159 resource "gitlab_group" "foo" { 160 name = "bar-name-%d" 161 path = "bar-path-%d" 162 description = "Terraform acceptance tests! Updated description" 163 lfs_enabled = false 164 request_access_enabled = true 165 166 # So that acceptance tests can be run in a gitlab organization 167 # with no billing 168 visibility_level = "public" 169 } 170 `, rInt, rInt) 171 }