11package provider
22
33import (
4+ "fmt"
45 "testing"
6+ "time"
57
68 "github.com/hashicorp/terraform-plugin-testing/helper/resource"
79)
810
911func TestResources (t * testing.T ) {
12+ testID := fmt .Sprintf ("test-%d" , time .Now ().Unix ())
1013 resource .Test (t , resource.TestCase {
1114 ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
1215 Steps : []resource.TestStep {
1316 // Read testing
1417 {
15- Config : providerConfig +
16- `resource "permitio_resource" "document" {
17- key = "document"
18- name = "document"
18+ Config : providerConfig + fmt .Sprintf (`resource "permitio_resource" "document" {
19+ key = "document-%s"
20+ name = "document-%s"
1921 description = "a new document"
2022 actions = {
2123 "read" = {
@@ -31,58 +33,57 @@ func TestResources(t *testing.T) {
3133 "type" = "time"
3234 }
3335 }
34- }` + " \n " +
35- ` resource "permitio_role" "admin" {
36- key = "admin"
36+ }
37+ resource "permitio_role" "admin" {
38+ key = "admin-%s "
3739 name = "admin"
3840 description = "a new admin"
39- permissions = ["document:read"]
41+ permissions = ["document-%s :read"]
4042 depends_on = [
4143 "permitio_resource.document"
4244 ]
43- }` + " \n " +
44- ` resource "permitio_role" "writer" {
45- key = "writer"
45+ }
46+ resource "permitio_role" "writer" {
47+ key = "writer-%s "
4648 name = "writer"
4749 description = "a new writer"
4850 permissions = [
49- "document:write"
51+ "document-%s :write"
5052 ]
5153 extends = [
52- "admin"
54+ "admin-%s "
5355 ]
5456 depends_on = [
5557 "permitio_role.admin",
5658 "permitio_resource.document"
5759 ]
58- }` ,
60+ }` , testID , testID , testID , testID , testID , testID , testID ),
5961 Check : resource .ComposeAggregateTestCheckFunc (
6062 // Document Resource tests
61- resource .TestCheckResourceAttr ("permitio_resource.document" , "key" , "document" ),
62- resource .TestCheckResourceAttr ("permitio_resource.document" , "name" , "document" ),
63+ resource .TestCheckResourceAttr ("permitio_resource.document" , "key" , fmt . Sprintf ( "document-%s" , testID ) ),
64+ resource .TestCheckResourceAttr ("permitio_resource.document" , "name" , fmt . Sprintf ( "document-%s" , testID ) ),
6365 resource .TestCheckResourceAttr ("permitio_resource.document" , "description" , "a new document" ),
6466 resource .TestCheckResourceAttr ("permitio_resource.document" , "actions.read.name" , "read" ),
6567 resource .TestCheckResourceAttr ("permitio_resource.document" , "attributes.created_at.type" , "time" ),
6668 resource .TestCheckResourceAttr ("permitio_resource.document" , "attributes.created_at.description" , "creation time of the document" ),
6769 // Admin Role tests
68- resource .TestCheckResourceAttr ("permitio_role.admin" , "key" , "admin" ),
70+ resource .TestCheckResourceAttr ("permitio_role.admin" , "key" , fmt . Sprintf ( "admin-%s" , testID ) ),
6971 resource .TestCheckResourceAttr ("permitio_role.admin" , "name" , "admin" ),
7072 resource .TestCheckResourceAttr ("permitio_role.admin" , "description" , "a new admin" ),
7173 resource .TestCheckResourceAttr ("permitio_role.admin" , "permissions.#" , "1" ),
72- resource .TestCheckResourceAttr ("permitio_role.admin" , "permissions.0" , "document:read" ),
74+ resource .TestCheckResourceAttr ("permitio_role.admin" , "permissions.0" , fmt . Sprintf ( "document-%s :read" , testID ) ),
7375 // Writer Role tests
74- resource .TestCheckResourceAttr ("permitio_role.writer" , "key" , "writer" ),
76+ resource .TestCheckResourceAttr ("permitio_role.writer" , "key" , fmt . Sprintf ( "writer-%s" , testID ) ),
7577 resource .TestCheckResourceAttr ("permitio_role.writer" , "name" , "writer" ),
7678 resource .TestCheckResourceAttr ("permitio_role.writer" , "description" , "a new writer" ),
7779 resource .TestCheckResourceAttr ("permitio_role.writer" , "permissions.#" , "1" ),
78- resource .TestCheckResourceAttr ("permitio_role.writer" , "permissions.0" , "document:write" ),
80+ resource .TestCheckResourceAttr ("permitio_role.writer" , "permissions.0" , fmt . Sprintf ( "document-%s :write" , testID ) ),
7981 ),
8082 },
8183 {
82- Config : providerConfig +
83- `resource "permitio_resource" "document" {
84- key = "document"
85- name = "document"
84+ Config : providerConfig + fmt .Sprintf (`resource "permitio_resource" "document" {
85+ key = "document-%s"
86+ name = "document-%s"
8687 description = "a new document"
8788 actions = {
8889 "read" = {
@@ -106,34 +107,33 @@ func TestResources(t *testing.T) {
106107 "type" = "string"
107108 }
108109 }
109- }` + " \n " +
110- ` resource "permitio_role" "admin" {
111- key = "admin"
110+ }
111+ resource "permitio_role" "admin" {
112+ key = "admin-%s "
112113 name = "admin"
113114 description = "a new admin"
114- permissions = ["document:read", "document:write", "document:delete"]
115+ permissions = ["document-%s :read", "document-%s :write", "document-%s :delete"]
115116 depends_on = [
116117 "permitio_resource.document"
117118 ]
118- }` ,
119+ }` , testID , testID , testID , testID , testID , testID ),
119120 Check : resource .ComposeAggregateTestCheckFunc (
120121 // Document Resource tests
121- resource .TestCheckResourceAttr ("permitio_resource.document" , "key" , "document" ),
122+ resource .TestCheckResourceAttr ("permitio_resource.document" , "key" , fmt . Sprintf ( "document-%s" , testID ) ),
122123 resource .TestCheckResourceAttr ("permitio_resource.document" , "actions.delete.name" , "delete" ),
123124 resource .TestCheckResourceAttr ("permitio_resource.document" , "actions.delete.description" , "delete a document" ),
124125 resource .TestCheckResourceAttr ("permitio_resource.document" , "attributes.created_at.type" , "number" ),
125126 resource .TestCheckResourceAttr ("permitio_resource.document" , "attributes.created_at.description" , "creation time of the document" ),
126127 resource .TestCheckResourceAttr ("permitio_resource.document" , "attributes.content.type" , "string" ),
127128 resource .TestCheckResourceAttr ("permitio_resource.document" , "attributes.content.description" , "the content of the document" ),
128129 // Admin Role tests
129- resource .TestCheckResourceAttr ("permitio_role.admin" , "key" , "admin" ),
130+ resource .TestCheckResourceAttr ("permitio_role.admin" , "key" , fmt . Sprintf ( "admin-%s" , testID ) ),
130131 resource .TestCheckResourceAttr ("permitio_role.admin" , "permissions.#" , "3" ),
131132 ),
132133 },
133134 {
134- Config : providerConfig +
135- `resource "permitio_proxy_config" "foaz" {
136- key = "foaz"
135+ Config : providerConfig + fmt .Sprintf (`resource "permitio_proxy_config" "foaz" {
136+ key = "foaz-%s"
137137 name = "Boaz"
138138 auth_mechanism = "Basic"
139139 auth_secret = {
@@ -143,30 +143,29 @@ func TestResources(t *testing.T) {
143143 {
144144 url = "https://example.com/documents"
145145 http_method = "post"
146- resource = "document"
146+ resource = "document-%s "
147147 action = "read"
148148 },
149149 {
150150 url = "https://example.com/documents/{project_id}"
151151 http_method = "delete"
152- resource = "document"
152+ resource = "document-%s "
153153 action = "delete"
154154 }
155155 ]
156- }` ,
156+ }` , testID , testID , testID ),
157157 Check : resource .ComposeAggregateTestCheckFunc (
158158 // Proxy Config tests
159- resource .TestCheckResourceAttr ("permitio_proxy_config.foaz" , "key" , "foaz" ),
159+ resource .TestCheckResourceAttr ("permitio_proxy_config.foaz" , "key" , fmt . Sprintf ( "foaz-%s" , testID ) ),
160160 resource .TestCheckResourceAttr ("permitio_proxy_config.foaz" , "name" , "Boaz" ),
161161 resource .TestCheckResourceAttr ("permitio_proxy_config.foaz" , "auth_mechanism" , "Basic" ),
162162 resource .TestCheckResourceAttr ("permitio_proxy_config.foaz" , "auth_secret.basic" , "hello:world" ),
163163 resource .TestCheckResourceAttr ("permitio_proxy_config.foaz" , "mapping_rules.#" , "2" ),
164164 ),
165165 },
166166 {
167- Config : providerConfig +
168- `resource "permitio_proxy_config" "foaz" {
169- key = "foaz"
167+ Config : providerConfig + fmt .Sprintf (`resource "permitio_proxy_config" "foaz" {
168+ key = "foaz-%s"
170169 name = "Boaz"
171170 auth_mechanism = "Basic"
172171 auth_secret = {
@@ -176,19 +175,19 @@ func TestResources(t *testing.T) {
176175 {
177176 url = "https://example.com/documents"
178177 http_method = "post"
179- resource = "document"
178+ resource = "document-%s "
180179 action = "read"
181180 },
182181 {
183182 url = "https://example.com/documents/{project_id}"
184183 http_method = "get"
185- resource = "document"
184+ resource = "document-%s "
186185 action = "read"
187186 },
188187 {
189188 url = "https://example.com/documents/{project_id}"
190189 http_method = "put"
191- resource = "document"
190+ resource = "document-%s "
192191 action = "update"
193192 headers = {
194193 "x-update-id" : "foaz"
@@ -197,27 +196,26 @@ func TestResources(t *testing.T) {
197196 {
198197 url = "https://example.com/documents/{project_id}"
199198 http_method = "delete"
200- resource = "document"
199+ resource = "document-%s "
201200 action = "delete"
202201 }
203202 ]
204203 depends_on = [
205204 permitio_resource.document
206205 ]
207- }` ,
206+ }` , testID , testID , testID , testID , testID ),
208207 Check : resource .ComposeAggregateTestCheckFunc (
209208 // Proxy Config tests
210- resource .TestCheckResourceAttr ("permitio_proxy_config.foaz" , "key" , "foaz" ),
209+ resource .TestCheckResourceAttr ("permitio_proxy_config.foaz" , "key" , fmt . Sprintf ( "foaz-%s" , testID ) ),
211210 resource .TestCheckResourceAttr ("permitio_proxy_config.foaz" , "name" , "Boaz" ),
212211 resource .TestCheckResourceAttr ("permitio_proxy_config.foaz" , "auth_mechanism" , "Basic" ),
213212 resource .TestCheckResourceAttr ("permitio_proxy_config.foaz" , "auth_secret.basic" , "hello:world" ),
214213 resource .TestCheckResourceAttr ("permitio_proxy_config.foaz" , "mapping_rules.#" , "4" ),
215214 ),
216215 },
217- {Config : providerConfig +
218- `resource "permitio_resource" "file" {
219- key = "file"
220- name = "file"
216+ {Config : providerConfig + fmt .Sprintf (`resource "permitio_resource" "file" {
217+ key = "file-%s"
218+ name = "file-%s"
221219 actions = {
222220 "create" = {
223221 "name" = "Create"
@@ -240,8 +238,8 @@ func TestResources(t *testing.T) {
240238 }
241239 }
242240 resource "permitio_resource" "folder" {
243- key = "folder"
244- name = "folder"
241+ key = "folder-%s "
242+ name = "folder-%s "
245243 actions = {
246244 "create" = {
247245 "name" = "Create"
@@ -265,14 +263,14 @@ func TestResources(t *testing.T) {
265263 }
266264
267265 resource "permitio_relation" "parent" {
268- key = "parent"
266+ key = "parent-%s "
269267 name = "parent of"
270268 subject_resource = permitio_resource.folder.key
271269 object_resource = permitio_resource.file.key
272270 }
273271
274272 resource "permitio_role" "fileAdmin" {
275- key = "admin"
273+ key = "admin-%s "
276274 name = "Administrator"
277275 description = "Administrator access to files"
278276 permissions = ["read", "create", "update", "delete"]
@@ -284,7 +282,7 @@ func TestResources(t *testing.T) {
284282 }
285283
286284 resource "permitio_role" "folderAdmin" {
287- key = "admin"
285+ key = "admin-%s "
288286 name = "Administrator"
289287 description = "Administrator access to folders"
290288 permissions = ["create", "list", "modify", "delete"]
@@ -301,7 +299,7 @@ func TestResources(t *testing.T) {
301299 on_resource = permitio_resource.folder.key
302300 role = permitio_role.folderAdmin.key
303301 linked_by = permitio_relation.parent.key
304- }` ,
302+ }` , testID , testID , testID , testID , testID , testID , testID ),
305303 Check : resource .ComposeAggregateTestCheckFunc (),
306304 },
307305 },
0 commit comments