Skip to content

Commit e10a499

Browse files
author
Jordan Caussat
committed
Update users datasource: improve tests
1 parent 50b6ae1 commit e10a499

File tree

1 file changed

+95
-8
lines changed

1 file changed

+95
-8
lines changed
Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,114 @@
11
package gitlab
22

33
import (
4-
"regexp"
4+
"fmt"
55
"testing"
66

7+
"github.com/hashicorp/terraform/helper/acctest"
78
"github.com/hashicorp/terraform/helper/resource"
89
)
910

1011
func TestAccDataSourceGitlabUsers_basic(t *testing.T) {
12+
rInt := acctest.RandInt()
13+
rInt2 := acctest.RandInt()
14+
user2 := fmt.Sprintf("user%[email protected]", rInt2)
15+
1116
resource.Test(t, resource.TestCase{
1217
PreCheck: func() { testAccPreCheck(t) },
1318
Providers: testAccProviders,
1419
Steps: []resource.TestStep{
15-
resource.TestStep{
16-
Config: testAccComputeLbIpRangesConfig,
20+
{
21+
Config: testAccDataSourceGitlabUsersConfig(rInt, rInt2),
22+
Check: resource.ComposeTestCheckFunc(
23+
resource.TestCheckResourceAttr("gitlab_user.foo", "name", "footest1"),
24+
resource.TestCheckResourceAttr("gitlab_user.foo2", "name", "footest2"),
25+
),
26+
},
27+
{
28+
Config: testAccDataSourceGitlabUsersConfigSort(rInt, rInt2),
1729
Check: resource.ComposeTestCheckFunc(
18-
resource.TestMatchResourceAttr("data.gitlab_users.test",
19-
"users.#", regexp.MustCompile("^[1-9]*$"))),
30+
resource.TestCheckResourceAttr("data.gitlab_users.foo", "users.#", "2"),
31+
resource.TestCheckResourceAttr("data.gitlab_users.foo", "users.0.email", user2),
32+
resource.TestCheckResourceAttr("data.gitlab_users.foo", "users.0.projects_limit", "2"),
33+
),
34+
},
35+
{
36+
Config: testAccDataSourceGitlabUsersConfigSearch(rInt, rInt2),
37+
Check: resource.ComposeTestCheckFunc(
38+
resource.TestCheckResourceAttr("data.gitlab_users.foo", "users.#", "1"),
39+
// resource.TestCheckResourceAttr("data.gitlab_users.foo", "users.0.email", user2),
40+
),
2041
},
2142
},
2243
})
2344
}
2445

25-
const testAccComputeLbIpRangesConfig = `
26-
data "gitlab_users" "test" {}
27-
`
46+
func testAccDataSourceGitlabUsersConfig(rInt int, rInt2 int) string {
47+
return fmt.Sprintf(`
48+
resource "gitlab_user" "foo" {
49+
name = "footest1"
50+
username = "listest%d"
51+
password = "test%dtt"
52+
email = "user%[email protected]"
53+
projects_limit = 3
54+
}
55+
56+
resource "gitlab_user" "foo2" {
57+
name = "footest2"
58+
username = "listest%d"
59+
password = "test%dtt"
60+
email = "user%[email protected]"
61+
projects_limit = 2
62+
}
63+
`, rInt, rInt, rInt, rInt2, rInt2, rInt2)
64+
}
65+
66+
func testAccDataSourceGitlabUsersConfigSort(rInt int, rInt2 int) string {
67+
return fmt.Sprintf(`
68+
resource "gitlab_user" "foo" {
69+
name = "footest1"
70+
username = "listest%d"
71+
password = "test%dtt"
72+
email = "user%[email protected]"
73+
projects_limit = 3
74+
}
75+
76+
resource "gitlab_user" "foo2" {
77+
name = "footest2"
78+
username = "listest%d"
79+
password = "test%dtt"
80+
email = "user%[email protected]"
81+
projects_limit = 2
82+
}
83+
84+
data "gitlab_users" "foo" {
85+
sort = "desc"
86+
search = "footest"
87+
order_by = "name"
88+
}
89+
`, rInt, rInt, rInt, rInt2, rInt2, rInt2)
90+
}
91+
92+
func testAccDataSourceGitlabUsersConfigSearch(rInt int, rInt2 int) string {
93+
return fmt.Sprintf(`
94+
resource "gitlab_user" "foo" {
95+
name = "footest1"
96+
username = "listest%d"
97+
password = "test%dtt"
98+
email = "user%[email protected]"
99+
projects_limit = 3
100+
}
101+
102+
resource "gitlab_user" "foo2" {
103+
name = "footest2"
104+
username = "listest%d"
105+
password = "test%dtt"
106+
email = "user%[email protected]"
107+
projects_limit = 2
108+
}
109+
110+
data "gitlab_users" "foo" {
111+
search = "user%[email protected]"
112+
}
113+
`, rInt, rInt, rInt, rInt2, rInt2, rInt2, rInt2)
114+
}

0 commit comments

Comments
 (0)