/
data_source_scalr_policy_group_test.go
149 lines (137 loc) · 4.1 KB
/
data_source_scalr_policy_group_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
package scalr
import (
"fmt"
"log"
"regexp"
"testing"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
scalr "github.com/scalr/go-scalr"
)
func TestAccPolicyGroupDataSource_basic(t *testing.T) {
rInt := GetRandomInteger()
resource.Test(t, resource.TestCase{
PreCheck: func() {
// TODO: delete skip after SCALRCORE-19891
t.Skip("Works with personal token but does not work with github action token.")
testVcsAccGithubTokenPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccPolicyGroupConfig(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("scalr_policy_group.test", "id"),
),
},
{
PreConfig: waitForPolicyGroupFetch(fmt.Sprintf("test-pg-%d", rInt)),
Config: testAccPolicyGroupDataSourceConfig(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.scalr_policy_group.test", "id"),
resource.TestCheckResourceAttr(
"data.scalr_policy_group.test",
"name",
fmt.Sprintf("test-pg-%d", rInt),
),
resource.TestCheckResourceAttr(
"data.scalr_policy_group.test",
"status",
string(scalr.PolicyGroupStatusActive),
),
resource.TestCheckResourceAttr(
"data.scalr_policy_group.test",
"error_message",
"",
),
resource.TestCheckResourceAttrSet("data.scalr_policy_group.test", "opa_version"),
resource.TestCheckResourceAttr(
"data.scalr_policy_group.test",
"account_id",
defaultAccount,
),
resource.TestCheckResourceAttrSet("data.scalr_policy_group.test", "vcs_provider_id"),
resource.TestCheckResourceAttr(
"data.scalr_policy_group.test",
"vcs_repo.0.identifier",
policyGroupVcsRepoID,
),
resource.TestCheckResourceAttr(
"data.scalr_policy_group.test",
"vcs_repo.0.path",
policyGroupVcsRepoPath,
),
resource.TestCheckResourceAttrSet("data.scalr_policy_group.test", "vcs_repo.0.branch"),
resource.TestCheckResourceAttrSet("data.scalr_policy_group.test", "policies.#"),
resource.TestCheckResourceAttrSet("data.scalr_policy_group.test", "environments.#"),
),
},
{
Config: fmt.Sprintf(`
data "scalr_policy_group" "test" {
name = "not-exists"
account_id = "%s"
}
`, defaultAccount),
ExpectError: regexp.MustCompile(fmt.Sprintf(
"policy group %s/%s not found", defaultAccount, "not-exists",
)),
PlanOnly: true,
},
},
})
}
func waitForPolicyGroupFetch(name string) func() {
return func() {
scalrClient := testAccProvider.Meta().(*scalr.Client)
pgl, err := scalrClient.PolicyGroups.List(ctx, scalr.PolicyGroupListOptions{
Account: defaultAccount,
Name: name,
})
if err != nil || len(pgl.Items) == 0 {
log.Fatalf("The test policy group on account %s was not created: %v", defaultAccount, err)
}
var pgID = pgl.Items[0].ID
for i := 0; i < 60; i++ {
pg, err := scalrClient.PolicyGroups.Read(ctx, pgID)
if err != nil {
log.Fatalf("Error polling policy group %s: %v", pgID, err)
}
if pg.Status != scalr.PolicyGroupStatusFetching {
if pg.Status != scalr.PolicyGroupStatusActive {
log.Fatalf("Invalid policy group status: '%s'", pg.Status)
}
return
}
time.Sleep(time.Second)
}
log.Fatal("Policy group has not become active after 60 seconds")
}
}
func testAccPolicyGroupConfig(rInt int) string {
return fmt.Sprintf(`
resource "scalr_vcs_provider" "test" {
name = "test-github-%d"
vcs_type = "%s"
token = "%s"
}
resource "scalr_policy_group" "test" {
name = "test-pg-%[1]d"
account_id = "%[4]s"
vcs_provider_id = scalr_vcs_provider.test.id
vcs_repo {
identifier = "%s"
path = "%s"
}
}
`, rInt, string(scalr.Github), GITHUB_TOKEN, defaultAccount, policyGroupVcsRepoID, policyGroupVcsRepoPath)
}
func testAccPolicyGroupDataSourceConfig(rInt int) string {
return fmt.Sprintf(`
%s
data "scalr_policy_group" "test" {
name = scalr_policy_group.test.name
account_id = "%s"
}
`, testAccPolicyGroupConfig(rInt), defaultAccount)
}