@@ -11,14 +11,186 @@ import (
1111func TestAccInsertFunctionInstanceResource (t * testing.T ) {
1212 t .Parallel ()
1313
14- updated := 0
15- fakeServer := httptest .NewServer (
16- http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
17- w .Header ().Set ("content-type" , "application/json" )
18-
19- payload := ""
20- if req .URL .Path == "/insert-function-instances" && req .Method == http .MethodPost {
21- payload = `
14+ t .Run ("happy path" , func (t * testing.T ) {
15+ t .Parallel ()
16+
17+ updated := 0
18+ fakeServer := httptest .NewServer (
19+ http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
20+ w .Header ().Set ("content-type" , "application/json" )
21+
22+ payload := ""
23+ if req .URL .Path == "/insert-function-instances" && req .Method == http .MethodPost {
24+ payload = `
25+ {
26+ "data": {
27+ "insertFunctionInstance": {
28+ "id": "my-instance-id",
29+ "name": "My instance name",
30+ "integrationId": "my-integration-id",
31+ "classId": "my-function-id",
32+ "enabled": true,
33+ "createdAt": "2023-11-01T18:38:01.349Z",
34+ "updatedAt": "2023-11-01T18:41:29.318Z",
35+ "settings": {
36+ "apiKey": "abc123"
37+ },
38+ "encryptedSettings": {}
39+ }
40+ }
41+ }
42+ `
43+ } else if req .URL .Path == "/insert-function-instances/my-instance-id" && req .Method == http .MethodPatch {
44+ payload = `
45+ {
46+ "data": {
47+ "insertFunctionInstance": {
48+ "id": "my-instance-id",
49+ "name": "My new instance name",
50+ "integrationId": "my-integration-id",
51+ "classId": "my-function-id",
52+ "enabled": false,
53+ "createdAt": "2023-11-01T18:38:01.349Z",
54+ "updatedAt": "2023-11-01T18:41:29.318Z",
55+ "settings": {
56+ "apiKey": "cba321"
57+ },
58+ "encryptedSettings": {}
59+ }
60+ }
61+ }
62+ `
63+ updated ++
64+ } else if req .URL .Path == "/insert-function-instances/my-instance-id" && req .Method == http .MethodGet {
65+ if updated == 0 {
66+ payload = `
67+ {
68+ "data": {
69+ "insertFunctionInstance": {
70+ "id": "my-instance-id",
71+ "name": "My instance name",
72+ "integrationId": "my-integration-id",
73+ "classId": "my-function-id",
74+ "enabled": true,
75+ "createdAt": "2023-11-01T18:38:01.349Z",
76+ "updatedAt": "2023-11-01T18:41:29.318Z",
77+ "settings": {
78+ "apiKey": "abc123"
79+ },
80+ "encryptedSettings": {}
81+ }
82+ }
83+ }
84+ `
85+ } else {
86+ payload = `
87+ {
88+ "data": {
89+ "insertFunctionInstance": {
90+ "id": "my-instance-id",
91+ "name": "My new instance name",
92+ "integrationId": "my-integration-id",
93+ "classId": "my-function-id",
94+ "enabled": false,
95+ "createdAt": "2023-11-01T18:38:01.349Z",
96+ "updatedAt": "2023-11-01T18:41:29.318Z",
97+ "settings": {
98+ "apiKey": "cba321"
99+ },
100+ "encryptedSettings": {}
101+ }
102+ }
103+ }
104+ `
105+ }
106+ }
107+
108+ _ , _ = w .Write ([]byte (payload ))
109+ }),
110+ )
111+ defer fakeServer .Close ()
112+
113+ providerConfig := `
114+ provider "segment" {
115+ url = "` + fakeServer .URL + `"
116+ token = "abc123"
117+ }
118+ `
119+
120+ resource .Test (t , resource.TestCase {
121+ ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
122+ Steps : []resource.TestStep {
123+ // Create and Read testing
124+ {
125+ Config : providerConfig + `
126+ resource "segment_insert_function_instance" "test" {
127+ integration_id = "my-integration-id"
128+ function_id = "my-function-id"
129+ name = "My instance name"
130+ enabled = true
131+ settings = jsonencode({"apiKey": "abc123"})
132+ }
133+ ` ,
134+ Check : resource .ComposeAggregateTestCheckFunc (
135+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "id" , "my-instance-id" ),
136+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "integration_id" , "my-integration-id" ),
137+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "name" , "My instance name" ),
138+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "enabled" , "true" ),
139+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "function_id" , "my-function-id" ),
140+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "settings" , "{\" apiKey\" :\" abc123\" }" ),
141+ ),
142+ },
143+ // ImportState testing
144+ {
145+ ResourceName : "segment_insert_function_instance.test" ,
146+ Config : providerConfig + `
147+ resource "segment_insert_function_instance" "test" {
148+ integration_id = "my-integration-id"
149+ function_id = "my-function-id"
150+ name = "My instance name"
151+ enabled = true
152+ settings = jsonencode({"apiKey": "abc123"})
153+ }
154+ ` ,
155+ ImportState : true ,
156+ ImportStateVerify : true ,
157+ },
158+ // Update and Read testing
159+ {
160+ Config : providerConfig + `
161+ resource "segment_insert_function_instance" "test" {
162+ integration_id = "my-integration-id"
163+ function_id = "my-function-id"
164+ name = "My new instance name"
165+ enabled = false
166+ settings = jsonencode({"apiKey": "cba321"})
167+ }
168+ ` ,
169+ Check : resource .ComposeAggregateTestCheckFunc (
170+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "id" , "my-instance-id" ),
171+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "integration_id" , "my-integration-id" ),
172+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "name" , "My new instance name" ),
173+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "enabled" , "false" ),
174+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "function_id" , "my-function-id" ),
175+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "settings" , "{\" apiKey\" :\" cba321\" }" ),
176+ ),
177+ },
178+ // Delete testing automatically occurs in TestCase
179+ },
180+ })
181+ })
182+
183+ t .Run ("with prefix" , func (t * testing.T ) {
184+ t .Parallel ()
185+
186+ updated := 0
187+ fakeServer := httptest .NewServer (
188+ http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
189+ w .Header ().Set ("content-type" , "application/json" )
190+
191+ payload := ""
192+ if req .URL .Path == "/insert-function-instances" && req .Method == http .MethodPost {
193+ payload = `
22194 {
23195 "data": {
24196 "insertFunctionInstance": {
@@ -37,8 +209,8 @@ func TestAccInsertFunctionInstanceResource(t *testing.T) {
37209 }
38210 }
39211 `
40- } else if req .URL .Path == "/insert-function-instances/my-instance-id" && req .Method == http .MethodPatch {
41- payload = `
212+ } else if req .URL .Path == "/insert-function-instances/my-instance-id" && req .Method == http .MethodPatch {
213+ payload = `
42214 {
43215 "data": {
44216 "insertFunctionInstance": {
@@ -57,10 +229,10 @@ func TestAccInsertFunctionInstanceResource(t *testing.T) {
57229 }
58230 }
59231 `
60- updated ++
61- } else if req .URL .Path == "/insert-function-instances/my-instance-id" && req .Method == http .MethodGet {
62- if updated == 0 {
63- payload = `
232+ updated ++
233+ } else if req .URL .Path == "/insert-function-instances/my-instance-id" && req .Method == http .MethodGet {
234+ if updated == 0 {
235+ payload = `
64236 {
65237 "data": {
66238 "insertFunctionInstance": {
@@ -79,8 +251,8 @@ func TestAccInsertFunctionInstanceResource(t *testing.T) {
79251 }
80252 }
81253 `
82- } else {
83- payload = `
254+ } else {
255+ payload = `
84256 {
85257 "data": {
86258 "insertFunctionInstance": {
@@ -99,80 +271,46 @@ func TestAccInsertFunctionInstanceResource(t *testing.T) {
99271 }
100272 }
101273 `
274+ }
102275 }
103- }
104276
105- _ , _ = w .Write ([]byte (payload ))
106- }),
107- )
108- defer fakeServer .Close ()
277+ _ , _ = w .Write ([]byte (payload ))
278+ }),
279+ )
280+ defer fakeServer .Close ()
109281
110- providerConfig := `
282+ providerConfig := `
111283 provider "segment" {
112284 url = "` + fakeServer .URL + `"
113285 token = "abc123"
114286 }
115287 `
116288
117- resource .Test (t , resource.TestCase {
118- ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
119- Steps : []resource.TestStep {
120- // Create and Read testing
121- {
122- Config : providerConfig + `
123- resource "segment_insert_function_instance" "test " {
289+ resource .Test (t , resource.TestCase {
290+ ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
291+ Steps : []resource.TestStep {
292+ // Create and Read testing for 'ifnd_' prefixed fn
293+ {
294+ Config : providerConfig + `
295+ resource "segment_insert_function_instance" "test_ifnd " {
124296 integration_id = "my-integration-id"
125- function_id = "my -function-id"
297+ function_id = "ifnd_my -function-id"
126298 name = "My instance name"
127299 enabled = true
128300 settings = jsonencode({"apiKey": "abc123"})
129301 }
130302 ` ,
131- Check : resource .ComposeAggregateTestCheckFunc (
132- resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "id" , "my-instance-id" ),
133- resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "integration_id" , "my-integration-id" ),
134- resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "name" , "My instance name" ),
135- resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "enabled" , "true" ),
136- resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "function_id" , "my-function-id" ),
137- resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "settings" , "{\" apiKey\" :\" abc123\" }" ),
138- ),
303+ Check : resource .ComposeAggregateTestCheckFunc (
304+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test_ifnd" , "id" , "my-instance-id" ),
305+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test_ifnd" , "integration_id" , "my-integration-id" ),
306+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test_ifnd" , "name" , "My instance name" ),
307+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test_ifnd" , "enabled" , "true" ),
308+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test_ifnd" , "function_id" , "ifnd_my-function-id" ),
309+ resource .TestCheckResourceAttr ("segment_insert_function_instance.test_ifnd" , "settings" , "{\" apiKey\" :\" abc123\" }" ),
310+ ),
311+ },
139312 },
140- // ImportState testing
141- {
142- ResourceName : "segment_insert_function_instance.test" ,
143- Config : providerConfig + `
144- resource "segment_insert_function_instance" "test" {
145- integration_id = "my-integration-id"
146- function_id = "my-function-id"
147- name = "My instance name"
148- enabled = true
149- settings = jsonencode({"apiKey": "abc123"})
150- }
151- ` ,
152- ImportState : true ,
153- ImportStateVerify : true ,
154- },
155- // Update and Read testing
156- {
157- Config : providerConfig + `
158- resource "segment_insert_function_instance" "test" {
159- integration_id = "my-integration-id"
160- function_id = "my-function-id"
161- name = "My new instance name"
162- enabled = false
163- settings = jsonencode({"apiKey": "cba321"})
164- }
165- ` ,
166- Check : resource .ComposeAggregateTestCheckFunc (
167- resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "id" , "my-instance-id" ),
168- resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "integration_id" , "my-integration-id" ),
169- resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "name" , "My new instance name" ),
170- resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "enabled" , "false" ),
171- resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "function_id" , "my-function-id" ),
172- resource .TestCheckResourceAttr ("segment_insert_function_instance.test" , "settings" , "{\" apiKey\" :\" cba321\" }" ),
173- ),
174- },
175- // Delete testing automatically occurs in TestCase
176- },
313+ })
177314 })
315+
178316}
0 commit comments