-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresource_global_role_test.go
148 lines (132 loc) · 4.72 KB
/
resource_global_role_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
package platform_test
import (
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/jfrog/terraform-provider-shared/testutil"
"github.com/jfrog/terraform-provider-shared/util"
)
func TestAccGlobalRole_full(t *testing.T) {
_, fqrn, roleName := testutil.MkNames("test-global-role", "platform_global_role")
temp := `
resource "platform_global_role" "{{ .name }}" {
name = "{{ .name }}"
description = "Test description"
type = "{{ .type }}"
environments = ["{{ .environment }}"]
actions = ["{{ .action }}"]
}`
testData := map[string]string{
"name": roleName,
"type": "CUSTOM_GLOBAL",
"environment": "DEV",
"action": "READ_REPOSITORY",
}
config := util.ExecuteTemplate(roleName, temp, testData)
updatedTemp := `
resource "platform_global_role" "{{ .name }}" {
name = "{{ .name }}"
description = "Test description"
type = "{{ .type }}"
environments = ["{{ .environment }}", "{{ .environment2 }}"]
actions = ["{{ .action }}", "{{ .action2 }}"]
}`
updatedTestData := map[string]string{
"name": roleName,
"type": "CUSTOM_GLOBAL",
"environment": "DEV",
"environment2": "PROD",
"action": "READ_REPOSITORY",
"action2": "READ_BUILD",
}
updatedConfig := util.ExecuteTemplate(roleName, updatedTemp, updatedTestData)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProviders(),
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "name", testData["name"]),
resource.TestCheckResourceAttr(fqrn, "type", testData["type"]),
resource.TestCheckResourceAttr(fqrn, "environments.#", "1"),
resource.TestCheckResourceAttr(fqrn, "environments.0", "DEV"),
resource.TestCheckResourceAttr(fqrn, "actions.#", "1"),
resource.TestCheckResourceAttr(fqrn, "actions.0", "READ_REPOSITORY"),
),
},
{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "name", testData["name"]),
resource.TestCheckResourceAttr(fqrn, "type", testData["type"]),
resource.TestCheckResourceAttr(fqrn, "environments.#", "2"),
resource.TestCheckTypeSetElemAttr(fqrn, "environments.*", "DEV"),
resource.TestCheckTypeSetElemAttr(fqrn, "environments.*", "PROD"),
resource.TestCheckResourceAttr(fqrn, "actions.#", "2"),
resource.TestCheckTypeSetElemAttr(fqrn, "actions.*", "READ_REPOSITORY"),
resource.TestCheckTypeSetElemAttr(fqrn, "actions.*", "READ_BUILD"),
),
},
{
ResourceName: fqrn,
ImportState: true,
ImportStateId: roleName,
ImportStateVerify: true,
ImportStateVerifyIdentifierAttribute: "name",
},
},
})
}
func TestAccGlobalRole_name_change(t *testing.T) {
_, fqrn, roleName := testutil.MkNames("test-global-role", "platform_global_role")
temp := `
resource "platform_global_role" "{{ .name }}" {
name = "{{ .name }}"
description = "Test description"
type = "{{ .type }}"
environments = ["{{ .environment }}"]
actions = ["{{ .action }}"]
}`
testData := map[string]string{
"name": roleName,
"type": "CUSTOM_GLOBAL",
"environment": "DEV",
"action": "READ_REPOSITORY",
}
config := util.ExecuteTemplate(roleName, temp, testData)
nameChangeTemp := `
resource "platform_global_role" "{{ .name }}" {
name = "foobar"
description = "Test description"
type = "{{ .type }}"
environments = ["{{ .environment }}"]
actions = ["{{ .action }}"]
}`
updatedConfig := util.ExecuteTemplate(roleName, nameChangeTemp, testData)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProviders(),
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "name", testData["name"]),
resource.TestCheckResourceAttr(fqrn, "type", testData["type"]),
resource.TestCheckResourceAttr(fqrn, "environments.#", "1"),
resource.TestCheckResourceAttr(fqrn, "environments.0", "DEV"),
resource.TestCheckResourceAttr(fqrn, "actions.#", "1"),
resource.TestCheckResourceAttr(fqrn, "actions.0", "READ_REPOSITORY"),
),
},
{
Config: updatedConfig,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(fqrn, plancheck.ResourceActionDestroyBeforeCreate),
},
},
},
},
})
}