|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/helper/acctest" |
| 9 | + "github.com/hashicorp/terraform/helper/resource" |
| 10 | + "github.com/hashicorp/terraform/terraform" |
| 11 | + "github.com/xanzy/go-gitlab" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAccGitlabUser_basic(t *testing.T) { |
| 15 | + var user gitlab.User |
| 16 | + rInt := acctest.RandInt() |
| 17 | + |
| 18 | + resource.Test(t, resource.TestCase{ |
| 19 | + PreCheck: func() { testAccPreCheck(t) }, |
| 20 | + Providers: testAccProviders, |
| 21 | + CheckDestroy: testAccCheckGitlabGroupDestroy, |
| 22 | + Steps: []resource.TestStep{ |
| 23 | + // Create a user |
| 24 | + { |
| 25 | + Config: testAccGitlabUserConfig(rInt), |
| 26 | + Check: resource.ComposeTestCheckFunc( |
| 27 | + testAccCheckGitlabUserExists("gitlab_user.foo", &user), |
| 28 | + testAccCheckGitlabUserAttributes(&user, &testAccGitlabUserExpectedAttributes{ |
| 29 | + Email: "listest%[email protected]", |
| 30 | + Password: fmt.Sprintf("test%dtt", rInt), |
| 31 | + Username: fmt.Sprintf("listest%d", rInt), |
| 32 | + Name: fmt.Sprintf("foo %d", rInt), |
| 33 | + ProjectsLimit: 0, |
| 34 | + Admin: false, |
| 35 | + CanCreateGroup: false, |
| 36 | + SkipConfirmation: true, |
| 37 | + }), |
| 38 | + ), |
| 39 | + }, |
| 40 | + // Update the user to change the name |
| 41 | + { |
| 42 | + Config: testAccGitlabUserUpdateConfig(rInt), |
| 43 | + Check: resource.ComposeTestCheckFunc( |
| 44 | + testAccCheckGitlabUserExists("gitlab_user.foo", &user), |
| 45 | + testAccCheckGitlabUserAttributes(&user, &testAccGitlabUserExpectedAttributes{ |
| 46 | + Email: "listest%[email protected]", |
| 47 | + Password: fmt.Sprintf("test%dtt", rInt), |
| 48 | + Username: fmt.Sprintf("listest%d", rInt), |
| 49 | + Name: fmt.Sprintf("bar %d", rInt), |
| 50 | + ProjectsLimit: 10, |
| 51 | + Admin: true, |
| 52 | + CanCreateGroup: true, |
| 53 | + SkipConfirmation: false, |
| 54 | + }), |
| 55 | + ), |
| 56 | + }, |
| 57 | + // Update the user to put the name back |
| 58 | + { |
| 59 | + Config: testAccGitlabUserConfig(rInt), |
| 60 | + Check: resource.ComposeTestCheckFunc( |
| 61 | + testAccCheckGitlabUserExists("gitlab_user.foo", &user), |
| 62 | + testAccCheckGitlabUserAttributes(&user, &testAccGitlabUserExpectedAttributes{ |
| 63 | + Email: "listest%[email protected]", |
| 64 | + Password: fmt.Sprintf("test%dtt", rInt), |
| 65 | + Username: fmt.Sprintf("listest%d", rInt), |
| 66 | + Name: fmt.Sprintf("foo %d", rInt), |
| 67 | + ProjectsLimit: 0, |
| 68 | + Admin: false, |
| 69 | + CanCreateGroup: false, |
| 70 | + SkipConfirmation: false, |
| 71 | + }), |
| 72 | + ), |
| 73 | + }, |
| 74 | + }, |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +func testAccCheckGitlabUserExists(n string, user *gitlab.User) resource.TestCheckFunc { |
| 79 | + return func(s *terraform.State) error { |
| 80 | + rs, ok := s.RootModule().Resources[n] |
| 81 | + if !ok { |
| 82 | + return fmt.Errorf("Not Found: %s", n) |
| 83 | + } |
| 84 | + |
| 85 | + userID := rs.Primary.ID |
| 86 | + if userID == "" { |
| 87 | + return fmt.Errorf("No user ID is set") |
| 88 | + } |
| 89 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 90 | + |
| 91 | + id, _ := strconv.Atoi(userID) |
| 92 | + |
| 93 | + gotUser, _, err := conn.Users.GetUser(id) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + *user = *gotUser |
| 98 | + return nil |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +type testAccGitlabUserExpectedAttributes struct { |
| 103 | + Email string |
| 104 | + Password string |
| 105 | + Username string |
| 106 | + Name string |
| 107 | + ProjectsLimit int |
| 108 | + Admin bool |
| 109 | + CanCreateGroup bool |
| 110 | + SkipConfirmation bool |
| 111 | +} |
| 112 | + |
| 113 | +func testAccCheckGitlabUserAttributes(user *gitlab.User, want *testAccGitlabUserExpectedAttributes) resource.TestCheckFunc { |
| 114 | + return func(s *terraform.State) error { |
| 115 | + if user.Name != want.Name { |
| 116 | + return fmt.Errorf("got name %q; want %q", user.Name, want.Name) |
| 117 | + } |
| 118 | + |
| 119 | + if user.Username != want.Username { |
| 120 | + return fmt.Errorf("got username %q; want %q", user.Username, want.Username) |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func testAccCheckGitlabUserDestroy(s *terraform.State) error { |
| 128 | + conn := testAccProvider.Meta().(*gitlab.Client) |
| 129 | + |
| 130 | + for _, rs := range s.RootModule().Resources { |
| 131 | + if rs.Type != "gitlab_user" { |
| 132 | + continue |
| 133 | + } |
| 134 | + |
| 135 | + id, _ := strconv.Atoi(rs.Primary.ID) |
| 136 | + |
| 137 | + user, resp, err := conn.Users.GetUser(id) |
| 138 | + if err == nil { |
| 139 | + if user != nil && fmt.Sprintf("%d", user.ID) == rs.Primary.ID { |
| 140 | + return fmt.Errorf("User still exists") |
| 141 | + } |
| 142 | + } |
| 143 | + if resp.StatusCode != 404 { |
| 144 | + return err |
| 145 | + } |
| 146 | + return nil |
| 147 | + } |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +func testAccGitlabUserConfig(rInt int) string { |
| 152 | + return fmt.Sprintf(` |
| 153 | +resource "gitlab_user" "foo" { |
| 154 | + name = "foo %d" |
| 155 | + username = "listest%d" |
| 156 | + password = "test%dtt" |
| 157 | + email = "listest%[email protected]" |
| 158 | + is_admin = false |
| 159 | + projects_limit = 0 |
| 160 | + can_create_group = false |
| 161 | +} |
| 162 | + `, rInt, rInt, rInt, rInt) |
| 163 | +} |
| 164 | + |
| 165 | +func testAccGitlabUserUpdateConfig(rInt int) string { |
| 166 | + return fmt.Sprintf(` |
| 167 | +resource "gitlab_user" "foo" { |
| 168 | + name = "bar %d" |
| 169 | + username = "listest%d" |
| 170 | + password = "test%dtt" |
| 171 | + email = "listest%[email protected]" |
| 172 | + is_admin = true |
| 173 | + projects_limit = 10 |
| 174 | + can_create_group = true |
| 175 | +} |
| 176 | + `, rInt, rInt, rInt, rInt) |
| 177 | +} |
0 commit comments