-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathresource_acceptance_test.go
More file actions
215 lines (192 loc) · 7.16 KB
/
resource_acceptance_test.go
File metadata and controls
215 lines (192 loc) · 7.16 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package notification_setting_test
import (
"fmt"
"regexp"
"strconv"
"strings"
"testing"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)
func TestAccDbtCloudNotificationSettingResource(t *testing.T) {
settingName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
settingNameUpdated := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
projectName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resourceName := "dbtcloud_notification_setting.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest_helper.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest_helper.TestAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckDbtCloudNotificationSettingDestroy,
Steps: []resource.TestStep{
// CREATE - one Teams channel, one rule scoped to a job
{
Config: testAccDbtCloudNotificationSettingResourceCreate(projectName, settingName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbtCloudNotificationSettingExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", settingName),
resource.TestCheckResourceAttr(resourceName, "description", "initial"),
resource.TestCheckResourceAttr(resourceName, "channels.#", "1"),
resource.TestCheckResourceAttr(resourceName, "channels.0.channel_type", "teams"),
resource.TestCheckResourceAttr(
resourceName,
"channels.0.teams_team_id",
"19:abcdef0123456789@thread.tacv2",
),
resource.TestCheckResourceAttr(
resourceName,
"channels.0.teams_channel_id",
"19:fedcba9876543210@thread.tacv2",
),
resource.TestCheckResourceAttrSet(resourceName, "channels.0.id"),
resource.TestCheckResourceAttr(resourceName, "rules.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rules.0.trigger_on", "run_errored"),
resource.TestCheckResourceAttrSet(resourceName, "rules.0.id"),
resource.TestCheckResourceAttrSet(resourceName, "rules.0.job_id"),
),
},
// UPDATE - rename, flip is_active, add a Teams warning rule
{
Config: testAccDbtCloudNotificationSettingResourceUpdate(projectName, settingNameUpdated),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbtCloudNotificationSettingExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", settingNameUpdated),
resource.TestCheckResourceAttr(resourceName, "description", "updated"),
resource.TestCheckResourceAttr(resourceName, "channels.#", "1"),
resource.TestCheckResourceAttr(resourceName, "channels.0.channel_type", "teams"),
resource.TestCheckResourceAttr(resourceName, "rules.#", "2"),
resource.TestCheckResourceAttr(resourceName, "rules.0.trigger_on", "run_errored"),
resource.TestCheckResourceAttr(resourceName, "rules.1.trigger_on", "run_warning"),
// Second rule has no job_id — fires for all jobs.
resource.TestCheckNoResourceAttr(resourceName, "rules.1.job_id"),
),
},
// IMPORT
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccDbtCloudNotificationSettingResourceProjectAndJob(projectName string) string {
return fmt.Sprintf(`
resource "dbtcloud_project" "test_notification_setting_project" {
name = "%s"
}
resource "dbtcloud_environment" "test_notification_setting_environment" {
project_id = dbtcloud_project.test_notification_setting_project.id
name = "Test Env Notification Setting"
dbt_version = "%s"
type = "development"
}
resource "dbtcloud_job" "test_notification_setting_job" {
name = "Notification Setting Test Job"
project_id = dbtcloud_project.test_notification_setting_project.id
environment_id = dbtcloud_environment.test_notification_setting_environment.environment_id
execute_steps = ["dbt compile"]
triggers = {
"github_webhook" = false
"git_provider_webhook" = false
"schedule" = false
}
}
`, projectName, acctest_config.AcceptanceTestConfig.DbtCloudVersion)
}
func testAccDbtCloudNotificationSettingResourceCreate(projectName, settingName string) string {
settingConfig := fmt.Sprintf(`
resource "dbtcloud_notification_setting" "test" {
name = "%s"
description = "initial"
channels = [
{
channel_type = "teams"
teams_team_id = "19:abcdef0123456789@thread.tacv2"
teams_channel_id = "19:fedcba9876543210@thread.tacv2"
},
]
rules = [
{
trigger_on = "run_errored"
job_id = dbtcloud_job.test_notification_setting_job.id
},
]
}
`, settingName)
return testAccDbtCloudNotificationSettingResourceProjectAndJob(projectName) + settingConfig
}
func testAccDbtCloudNotificationSettingResourceUpdate(projectName, settingName string) string {
settingConfig := fmt.Sprintf(`
resource "dbtcloud_notification_setting" "test" {
name = "%s"
description = "updated"
channels = [
{
channel_type = "teams"
teams_team_id = "19:abcdef0123456789@thread.tacv2"
teams_channel_id = "19:fedcba9876543210@thread.tacv2"
},
]
rules = [
{
trigger_on = "run_errored"
job_id = dbtcloud_job.test_notification_setting_job.id
},
{
trigger_on = "run_warning"
},
]
}
`, settingName)
return testAccDbtCloudNotificationSettingResourceProjectAndJob(projectName) + settingConfig
}
func testAccCheckDbtCloudNotificationSettingExists(resourceName string) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Record ID is set")
}
id, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
if err != nil {
return fmt.Errorf("invalid notification setting ID %q: %v", rs.Primary.ID, err)
}
apiClient, err := acctest_helper.SharedClient()
if err != nil {
return fmt.Errorf("Issue getting the client: %v", err)
}
if _, err := apiClient.GetNotificationSetting(id); err != nil {
return fmt.Errorf("error fetching notification setting %s: %v", rs.Primary.ID, err)
}
return nil
}
}
func testAccCheckDbtCloudNotificationSettingDestroy(s *terraform.State) error {
apiClient, err := acctest_helper.SharedClient()
if err != nil {
return fmt.Errorf("Issue getting the client: %v", err)
}
for _, rs := range s.RootModule().Resources {
if rs.Type != "dbtcloud_notification_setting" {
continue
}
id, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
if err != nil {
return fmt.Errorf("invalid notification setting ID %q: %v", rs.Primary.ID, err)
}
_, err = apiClient.GetNotificationSetting(id)
if err == nil {
return fmt.Errorf("Notification setting %s still exists", rs.Primary.ID)
}
expectedErr := regexp.MustCompile(`resource-not-found`)
if !expectedErr.Match([]byte(err.Error())) {
return fmt.Errorf("expected resource-not-found error, got %s", err)
}
}
return nil
}