-
Notifications
You must be signed in to change notification settings - Fork 147
/
data_source_organization_membership_test.go
124 lines (110 loc) · 3.89 KB
/
data_source_organization_membership_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package tfe
import (
"fmt"
"math/rand"
"regexp"
"testing"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
func TestAccTFEOrganizationMembershipDataSource_basic(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
orgName := fmt.Sprintf("tst-terraform-%d", rInt)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccTFEOrganizationMembershipDataSourceConfig(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.tfe_organization_membership.foobar", "email", "example@hashicorp.com"),
resource.TestCheckResourceAttr(
"data.tfe_organization_membership.foobar", "username", ""),
resource.TestCheckResourceAttr(
"data.tfe_organization_membership.foobar", "organization", orgName),
resource.TestCheckResourceAttrSet("data.tfe_organization_membership.foobar", "user_id"),
),
},
},
})
}
func TestAccTFEOrganizationMembershipDataSource_findByName(t *testing.T) {
// This test requires a user that exists in a TFC organization called "hashicorp".
// Our CI instance has a default organization "hashicorp" and prepopulates it
// with users (i.e TFE_USER1, etc) since we are unable to create users via the API.
// In order to run this against your own organization, simply modify the organization
// attribute in the test step config and set TFE_USER1 to the desired user you want to fetch.
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
if TFE_USER1 == "" {
t.Skip("Please set TFE_USER1 to run this test")
}
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccTFEOrganizationMembershipDataSourceSearchUsername(TFE_USER1),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.tfe_organization_membership.foobar", "email"),
resource.TestCheckResourceAttr(
"data.tfe_organization_membership.foobar", "username", TFE_USER1),
resource.TestCheckResourceAttr(
"data.tfe_organization_membership.foobar", "organization", "hashicorp"),
resource.TestCheckResourceAttrSet("data.tfe_organization_membership.foobar", "user_id"),
),
},
},
})
}
func TestAccTFEOrganizationMembershipDataSource_missingParams(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccTFEOrganizationMembershipDataSourceMissingParams(rInt),
ExpectError: regexp.MustCompile("you must specify a username or email"),
},
},
})
}
func testAccTFEOrganizationMembershipDataSourceConfig(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "admin@company.com"
}
resource "tfe_organization_membership" "foobar" {
email = "example@hashicorp.com"
organization = tfe_organization.foobar.id
}
data "tfe_organization_membership" "foobar" {
email = tfe_organization_membership.foobar.email
organization = tfe_organization.foobar.name
}`, rInt)
}
func testAccTFEOrganizationMembershipDataSourceSearchUsername(username string) string {
return fmt.Sprintf(`
data "tfe_organization_membership" "foobar" {
username = "%s"
organization = "hashicorp"
}`, username)
}
func testAccTFEOrganizationMembershipDataSourceMissingParams(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "admin@company.com"
}
resource "tfe_organization_membership" "foobar" {
email = "example@hashicorp.com"
organization = tfe_organization.foobar.id
}
data "tfe_organization_membership" "foobar" {
organization = tfe_organization.foobar.name
}`, rInt)
}