forked from PagerDuty/terraform-provider-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_pagerduty_schedule_test.go
92 lines (75 loc) · 2.5 KB
/
data_source_pagerduty_schedule_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
package pagerduty
import (
"fmt"
"testing"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)
func TestAccDataSourcePagerDutySchedule_Basic(t *testing.T) {
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%s@foo.com", username)
schedule := fmt.Sprintf("tf-%s", acctest.RandString(5))
location := "Europe/Berlin"
start := timeNowInLoc(location).Add(24 * time.Hour).Round(1 * time.Hour).Format(time.RFC3339)
rotationVirtualStart := timeNowInLoc(location).Add(24 * time.Hour).Round(1 * time.Hour).Format(time.RFC3339)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyScheduleConfig(username, email, schedule, location, start, rotationVirtualStart),
Check: resource.ComposeTestCheckFunc(
testAccDataSourcePagerDutySchedule("pagerduty_schedule.test", "data.pagerduty_schedule.by_name"),
),
},
},
})
}
func testAccDataSourcePagerDutySchedule(src, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
srcR := s.RootModule().Resources[src]
srcA := srcR.Primary.Attributes
r := s.RootModule().Resources[n]
a := r.Primary.Attributes
if a["id"] == "" {
return fmt.Errorf("Expected to get a schedule ID from PagerDuty")
}
testAtts := []string{"id", "name"}
for _, att := range testAtts {
if a[att] != srcA[att] {
return fmt.Errorf("Expected the schedule %s to be: %s, but got: %s", att, srcA[att], a[att])
}
}
return nil
}
}
func testAccDataSourcePagerDutyScheduleConfig(username, email, schedule, location, start, rotationVirtualStart string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "test" {
name = "%s"
email = "%s"
}
resource "pagerduty_schedule" "test" {
name = "%s"
time_zone = "%s"
layer {
name = "foo"
start = "%s"
rotation_virtual_start = "%s"
rotation_turn_length_seconds = 86400
users = [pagerduty_user.test.id]
restriction {
type = "weekly_restriction"
start_time_of_day = "08:00:00"
start_day_of_week = 5
duration_seconds = 32101
}
}
}
data "pagerduty_schedule" "by_name" {
name = pagerduty_schedule.test.name
}
`, username, email, schedule, location, start, rotationVirtualStart)
}