-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathdata_source_pagerduty_license_test.go
154 lines (135 loc) · 4.1 KB
/
data_source_pagerduty_license_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package pagerduty
import (
"fmt"
"os"
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)
func TestAccDataSourcePagerDutyLicense_Basic(t *testing.T) {
reference := "full_user"
description := ""
name := os.Getenv("PAGERDUTY_ACC_LICENSE_NAME")
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckLicenseNameTests(t, name)
},
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyLicenseConfig(reference, name, description),
Check: resource.ComposeTestCheckFunc(
testAccDataSourcePagerDutyLicense(fmt.Sprintf("data.pagerduty_license.%s", reference)),
),
},
},
})
}
func TestAccDataSourcePagerDutyLicense_Empty(t *testing.T) {
// Note that this test does not actually set any values for the name or
// description of the license. An accounts license data changes over time and
// per account. So, this test only verifies that a license can be found with
// an empty pagerduty_license datasource
reference := "full_user"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourceEmptyPagerDutyLicenseConfig(reference),
Check: resource.ComposeTestCheckFunc(
testAccDataSourcePagerDutyLicense(fmt.Sprintf("data.pagerduty_license.%s", reference)),
),
},
},
})
}
func TestAccDataSourcePagerDutyLicense_Error(t *testing.T) {
reference := "testing_reference_missing_license"
expectedErrorString := "Unable to locate any license"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyLicenseConfigError(reference),
ExpectError: regexp.MustCompile(expectedErrorString),
},
},
})
}
func TestAccDataSourcePagerDutyLicense_ErrorWithID(t *testing.T) {
reference := "testing_reference_missing_license"
expectedErrorString := "Unable to locate any license"
// Even with an expected name, if the configured ID is not found there
// should be an error
name := "Full User"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyLicenseConfigErrorWithID(reference, name),
ExpectError: regexp.MustCompile(expectedErrorString),
},
},
})
}
func testAccPreCheckLicenseNameTests(t *testing.T, name string) {
if name == "" {
t.Skip("PAGERDUTY_ACC_LICENSE_NAME not set. Skipping tests requiring license names")
}
}
func testAccDataSourcePagerDutyLicense(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
r := s.RootModule().Resources[n]
a := r.Primary.Attributes
testAtts := []string{
"id",
"name",
"summary",
"description",
"role_group",
"current_value",
"allocations_available",
"html_url",
"self",
}
for _, att := range testAtts {
if _, ok := a[att]; !ok {
return fmt.Errorf("Expected the required attribute %s to exist", att)
}
}
return nil
}
}
func testAccDataSourcePagerDutyLicenseConfig(reference string, name string, description string) string {
return fmt.Sprintf(`
data "pagerduty_license" "%s" {
name = "%s"
description = "%s"
}
`, reference, name, description)
}
func testAccDataSourceEmptyPagerDutyLicenseConfig(reference string) string {
return fmt.Sprintf(`
data "pagerduty_license" "%s" {}
`, reference)
}
func testAccDataSourcePagerDutyLicenseConfigError(reference string) string {
return fmt.Sprintf(`
data "pagerduty_license" "%s" {
name = "%s"
}
`, reference, reference)
}
func testAccDataSourcePagerDutyLicenseConfigErrorWithID(reference, name string) string {
return fmt.Sprintf(`
data "pagerduty_license" "%s" {
id = "%s"
name = "%s"
}
`, reference, reference, name)
}