Skip to content

Commit 5933a83

Browse files
authored
chore(logpush_jobs): Switch to Plan and State Checks from legacy Checks for logpush_jobs resource (#6083)
This switches to Pland nad State Checks from legacy Checks for `logpush_jobs` resource. - Reference: https://developer.hashicorp.com/terraform/plugin/testing/acceptance-tests/teststep - This check resource action taken was whether create or update on each step. Applicable tests pass locally: ``` go test ./internal/services/logpush_job -run "^TestAccCloudflareLogpushJob_" -v -count 1 ``` ``` === RUN TestAccCloudflareLogpushJob_Basic --- PASS: TestAccCloudflareLogpushJob_Basic (8.07s) === RUN TestAccCloudflareLogpushJob_BasicOutputOptions --- PASS: TestAccCloudflareLogpushJob_BasicOutputOptions (6.40s) === RUN TestAccCloudflareLogpushJob_Full --- PASS: TestAccCloudflareLogpushJob_Full (6.82s) === RUN TestAccCloudflareLogpushJob_ImmutableFields --- PASS: TestAccCloudflareLogpushJob_ImmutableFields (4.79s) PASS ok github.com/cloudflare/terraform-provider-cloudflare/internal/services/logpush_job 26.101s ```
1 parent e62250c commit 5933a83

File tree

1 file changed

+109
-57
lines changed

1 file changed

+109
-57
lines changed

internal/services/logpush_job/resource_test.go

Lines changed: 109 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
1515
"github.com/hashicorp/terraform-plugin-log/tflog"
1616
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
17+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
18+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
19+
"github.com/hashicorp/terraform-plugin-testing/statecheck"
20+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
1721
"github.com/pkg/errors"
1822
)
1923

@@ -148,17 +152,29 @@ func TestAccCloudflareLogpushJob_Basic(t *testing.T) {
148152
Steps: []resource.TestStep{
149153
{
150154
Config: testCloudflareLogpushJobBasic(rnd, logpushJobConfigCreate),
151-
Check: resource.ComposeTestCheckFunc(
152-
resource.TestCheckResourceAttr(resourceName, "dataset", toString(logpushJobConfigCreate.dataset)),
153-
resource.TestCheckResourceAttr(resourceName, "destination_conf", toString(logpushJobConfigCreate.destinationConf)),
154-
),
155+
ConfigPlanChecks: resource.ConfigPlanChecks{
156+
PreApply: []plancheck.PlanCheck{
157+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionCreate),
158+
plancheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfigCreate.destinationConf))),
159+
},
160+
},
161+
ConfigStateChecks: []statecheck.StateCheck{
162+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("dataset"), knownvalue.StringExact(toString(logpushJobConfigCreate.dataset))),
163+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfigCreate.destinationConf))),
164+
},
155165
},
156166
{
157167
Config: testCloudflareLogpushJobBasic(rnd, logpushJobConfigUpdate),
158-
Check: resource.ComposeTestCheckFunc(
159-
resource.TestCheckResourceAttr(resourceName, "dataset", toString(logpushJobConfigUpdate.dataset)),
160-
resource.TestCheckResourceAttr(resourceName, "destination_conf", toString(logpushJobConfigUpdate.destinationConf)),
161-
),
168+
ConfigPlanChecks: resource.ConfigPlanChecks{
169+
PreApply: []plancheck.PlanCheck{
170+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
171+
plancheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfigUpdate.destinationConf))),
172+
},
173+
},
174+
ConfigStateChecks: []statecheck.StateCheck{
175+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("dataset"), knownvalue.StringExact(toString(logpushJobConfigUpdate.dataset))),
176+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfigUpdate.destinationConf))),
177+
},
162178
},
163179
},
164180
})
@@ -211,23 +227,35 @@ func TestAccCloudflareLogpushJob_BasicOutputOptions(t *testing.T) {
211227
Steps: []resource.TestStep{
212228
{
213229
Config: testCloudflareLogpushJobBasicOutputOptions(rnd, logpushJobConfigCreate),
214-
Check: resource.ComposeTestCheckFunc(
215-
resource.TestCheckResourceAttr(resourceName, "dataset", toString(logpushJobConfigCreate.dataset)),
216-
resource.TestCheckResourceAttr(resourceName, "destination_conf", toString(logpushJobConfigCreate.destinationConf)),
217-
resource.TestCheckResourceAttr(resourceName, "output_options.output_type", toString(logpushJobConfigCreate.outputOptions.outputType)),
218-
resource.TestCheckResourceAttr(resourceName, "output_options.sample_rate", toString(logpushJobConfigCreate.outputOptions.sampleRate)),
219-
resource.TestCheckResourceAttr(resourceName, "output_options.timestamp_format", toString(logpushJobConfigCreate.outputOptions.timestampFormat)),
220-
),
230+
ConfigPlanChecks: resource.ConfigPlanChecks{
231+
PreApply: []plancheck.PlanCheck{
232+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionCreate),
233+
plancheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfigCreate.destinationConf))),
234+
},
235+
},
236+
ConfigStateChecks: []statecheck.StateCheck{
237+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("dataset"), knownvalue.StringExact(toString(logpushJobConfigCreate.dataset))),
238+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfigCreate.destinationConf))),
239+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("output_type"), knownvalue.StringExact(toString(logpushJobConfigCreate.outputOptions.outputType))),
240+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("sample_rate"), knownvalue.Float64Exact(logpushJobConfigCreate.outputOptions.sampleRate)),
241+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("timestamp_format"), knownvalue.StringExact(toString(logpushJobConfigCreate.outputOptions.timestampFormat))),
242+
},
221243
},
222244
{
223245
Config: testCloudflareLogpushJobBasicOutputOptions(rnd, logpushJobConfigUpdate),
224-
Check: resource.ComposeTestCheckFunc(
225-
resource.TestCheckResourceAttr(resourceName, "dataset", toString(logpushJobConfigUpdate.dataset)),
226-
resource.TestCheckResourceAttr(resourceName, "destination_conf", toString(logpushJobConfigUpdate.destinationConf)),
227-
resource.TestCheckResourceAttr(resourceName, "output_options.output_type", toString(logpushJobConfigUpdate.outputOptions.outputType)),
228-
resource.TestCheckResourceAttr(resourceName, "output_options.sample_rate", toString(logpushJobConfigUpdate.outputOptions.sampleRate)),
229-
resource.TestCheckResourceAttr(resourceName, "output_options.timestamp_format", toString(logpushJobConfigUpdate.outputOptions.timestampFormat)),
230-
),
246+
ConfigPlanChecks: resource.ConfigPlanChecks{
247+
PreApply: []plancheck.PlanCheck{
248+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
249+
plancheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfigUpdate.destinationConf))),
250+
},
251+
},
252+
ConfigStateChecks: []statecheck.StateCheck{
253+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("dataset"), knownvalue.StringExact(toString(logpushJobConfigUpdate.dataset))),
254+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfigUpdate.destinationConf))),
255+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("output_type"), knownvalue.StringExact(toString(logpushJobConfigUpdate.outputOptions.outputType))),
256+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("sample_rate"), knownvalue.Float64Exact(logpushJobConfigUpdate.outputOptions.sampleRate)),
257+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("timestamp_format"), knownvalue.StringExact(toString(logpushJobConfigUpdate.outputOptions.timestampFormat))),
258+
},
231259
},
232260
},
233261
})
@@ -305,47 +333,65 @@ func TestAccCloudflareLogpushJob_Full(t *testing.T) {
305333
Steps: []resource.TestStep{
306334
{
307335
Config: testCloudflareLogpushJobFull(rnd, logpushJobConfigCreate),
308-
Check: resource.ComposeTestCheckFunc(getTestCheckResourceAttrs(resourceName, logpushJobConfigCreate)...),
336+
ConfigPlanChecks: resource.ConfigPlanChecks{
337+
PreApply: []plancheck.PlanCheck{
338+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionCreate),
339+
plancheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfigCreate.destinationConf))),
340+
},
341+
},
342+
ConfigStateChecks: getStateChecks(resourceName, logpushJobConfigCreate),
309343
},
310344
{
311345
Config: testCloudflareLogpushJobFull(rnd, logpushJobConfigUpdate),
312-
Check: resource.ComposeTestCheckFunc(getTestCheckResourceAttrs(resourceName, logpushJobConfigUpdate)...),
346+
ConfigPlanChecks: resource.ConfigPlanChecks{
347+
PreApply: []plancheck.PlanCheck{
348+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
349+
plancheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfigUpdate.destinationConf))),
350+
},
351+
},
352+
ConfigStateChecks: getStateChecks(resourceName, logpushJobConfigUpdate),
313353
},
314354
},
315355
})
316356
}
317357

318-
func getTestCheckResourceAttrs(resourceName string, logpushJobConfig *logpushJobConfig) []resource.TestCheckFunc {
319-
testCheckFuncs := []resource.TestCheckFunc{
320-
resource.TestCheckResourceAttr(resourceName, "dataset", toString(logpushJobConfig.dataset)),
321-
resource.TestCheckResourceAttr(resourceName, "destination_conf", toString(logpushJobConfig.destinationConf)),
322-
resource.TestCheckResourceAttr(resourceName, "enabled", toString(logpushJobConfig.enabled)),
323-
resource.TestCheckResourceAttr(resourceName, "name", toString(logpushJobConfig.name)),
324-
resource.TestCheckResourceAttr(resourceName, "filter", toString(logpushJobConfig.filter)),
325-
resource.TestCheckResourceAttr(resourceName, "kind", toString(logpushJobConfig.kind)),
326-
resource.TestCheckResourceAttr(resourceName, "max_upload_bytes", toString(logpushJobConfig.maxUploadBytes)),
327-
resource.TestCheckResourceAttr(resourceName, "max_upload_records", toString(logpushJobConfig.maxUploadRecords)),
328-
resource.TestCheckResourceAttr(resourceName, "max_upload_interval_seconds", toString(logpushJobConfig.maxUploadIntervalSeconds)),
329-
resource.TestCheckResourceAttr(resourceName, "frequency", toString(logpushJobConfig.frequency)),
330-
resource.TestCheckResourceAttr(resourceName, "logpull_options", toString(logpushJobConfig.logpullOptions)),
331-
resource.TestCheckResourceAttr(resourceName, "output_options.batch_prefix", unquote(toString(logpushJobConfig.outputOptions.batchPrefix))),
332-
resource.TestCheckResourceAttr(resourceName, "output_options.batch_suffix", unquote(toString(logpushJobConfig.outputOptions.batchSuffix))),
333-
resource.TestCheckResourceAttr(resourceName, "output_options.cve_2021_44228", toString(logpushJobConfig.outputOptions.cve2021_44228)),
334-
resource.TestCheckResourceAttr(resourceName, "output_options.field_delimiter", unquote(toString(logpushJobConfig.outputOptions.fieldDelimiter))),
335-
resource.TestCheckResourceAttr(resourceName, "output_options.field_names.#", toString(len(logpushJobConfig.outputOptions.fieldNames))),
336-
resource.TestCheckResourceAttr(resourceName, "output_options.output_type", toString(logpushJobConfig.outputOptions.outputType)),
337-
resource.TestCheckResourceAttr(resourceName, "output_options.record_delimiter", unquote(toString(logpushJobConfig.outputOptions.recordDelimiter))),
338-
resource.TestCheckResourceAttr(resourceName, "output_options.record_prefix", unquote(toString(logpushJobConfig.outputOptions.recordPrefix))),
339-
resource.TestCheckResourceAttr(resourceName, "output_options.record_suffix", unquote(toString(logpushJobConfig.outputOptions.recordSuffix))),
340-
resource.TestCheckResourceAttr(resourceName, "output_options.record_template", unquote(toString(logpushJobConfig.outputOptions.recordTemplate))),
341-
resource.TestCheckResourceAttr(resourceName, "output_options.sample_rate", toString(logpushJobConfig.outputOptions.sampleRate)),
342-
resource.TestCheckResourceAttr(resourceName, "output_options.timestamp_format", toString(logpushJobConfig.outputOptions.timestampFormat)),
358+
func getStateChecks(resourceName string, logpushJobConfig *logpushJobConfig) []statecheck.StateCheck {
359+
stateChecks := []statecheck.StateCheck{
360+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("dataset"), knownvalue.StringExact(toString(logpushJobConfig.dataset))),
361+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfig.destinationConf))),
362+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("enabled"), knownvalue.Bool(logpushJobConfig.enabled)),
363+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("name"), knownvalue.StringExact(toString(logpushJobConfig.name))),
364+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("filter"), knownvalue.StringExact(toString(logpushJobConfig.filter))),
365+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("kind"), knownvalue.StringExact(toString(logpushJobConfig.kind))),
366+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("max_upload_bytes"), knownvalue.Int64Exact(int64(logpushJobConfig.maxUploadBytes))),
367+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("max_upload_records"), knownvalue.Int64Exact(int64(logpushJobConfig.maxUploadRecords))),
368+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("max_upload_interval_seconds"), knownvalue.Int64Exact(int64(logpushJobConfig.maxUploadIntervalSeconds))),
369+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("frequency"), knownvalue.StringExact(toString(logpushJobConfig.frequency))),
370+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("logpull_options"), knownvalue.StringExact(toString(logpushJobConfig.logpullOptions))),
371+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("batch_prefix"), knownvalue.StringExact(unquote(toString(logpushJobConfig.outputOptions.batchPrefix)))),
372+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("batch_suffix"), knownvalue.StringExact(unquote(toString(logpushJobConfig.outputOptions.batchSuffix)))),
373+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("cve_2021_44228"), knownvalue.Bool(logpushJobConfig.outputOptions.cve2021_44228)),
374+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("field_delimiter"), knownvalue.StringExact(unquote(toString(logpushJobConfig.outputOptions.fieldDelimiter)))),
375+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("output_type"), knownvalue.StringExact(toString(logpushJobConfig.outputOptions.outputType))),
376+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("record_delimiter"), knownvalue.StringExact(unquote(toString(logpushJobConfig.outputOptions.recordDelimiter)))),
377+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("record_prefix"), knownvalue.StringExact(unquote(toString(logpushJobConfig.outputOptions.recordPrefix)))),
378+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("record_suffix"), knownvalue.StringExact(unquote(toString(logpushJobConfig.outputOptions.recordSuffix)))),
379+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("record_template"), knownvalue.StringExact(unquote(toString(logpushJobConfig.outputOptions.recordTemplate)))),
380+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("sample_rate"), knownvalue.Float64Exact(logpushJobConfig.outputOptions.sampleRate)),
381+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("timestamp_format"), knownvalue.StringExact(toString(logpushJobConfig.outputOptions.timestampFormat))),
343382
}
344-
for i, fieldName := range logpushJobConfig.outputOptions.fieldNames {
345-
testCheckFuncs = append(testCheckFuncs, resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("output_options.field_names.%d", i), fieldName))
383+
384+
if len(logpushJobConfig.outputOptions.fieldNames) == 0 {
385+
stateChecks = append(stateChecks, statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("field_names"), knownvalue.Null()))
386+
} else {
387+
list := []knownvalue.Check{}
388+
for _, fieldName := range logpushJobConfig.outputOptions.fieldNames {
389+
list = append(list, knownvalue.StringExact(fieldName))
390+
}
391+
stateChecks = append(stateChecks, statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("output_options").AtMapKey("field_names"), knownvalue.ListExact(list)))
346392
}
347393

348-
return testCheckFuncs
394+
return stateChecks
349395
}
350396

351397
func testCloudflareLogpushJobFull(resourceID string, logpushJobConfig *logpushJobConfig) string {
@@ -410,11 +456,17 @@ func TestAccCloudflareLogpushJob_ImmutableFields(t *testing.T) {
410456
Steps: []resource.TestStep{
411457
{
412458
Config: testCloudflareLogpushJobImmutableFields(rnd, logpushJobConfigCreate),
413-
Check: resource.ComposeTestCheckFunc(
414-
resource.TestCheckResourceAttr(resourceName, "dataset", toString(logpushJobConfigCreate.dataset)),
415-
resource.TestCheckResourceAttr(resourceName, "destination_conf", toString(logpushJobConfigCreate.destinationConf)),
416-
resource.TestCheckResourceAttr(resourceName, "kind", toString(logpushJobConfigCreate.kind)),
417-
),
459+
ConfigPlanChecks: resource.ConfigPlanChecks{
460+
PreApply: []plancheck.PlanCheck{
461+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionCreate),
462+
plancheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfigCreate.destinationConf))),
463+
},
464+
},
465+
ConfigStateChecks: []statecheck.StateCheck{
466+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("dataset"), knownvalue.StringExact(toString(logpushJobConfigCreate.dataset))),
467+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("destination_conf"), knownvalue.StringExact(toString(logpushJobConfigCreate.destinationConf))),
468+
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("kind"), knownvalue.StringExact(toString(logpushJobConfigCreate.kind))),
469+
},
418470
},
419471
{
420472
Config: testCloudflareLogpushJobImmutableFields(rnd, logpushJobConfigUpdate),

0 commit comments

Comments
 (0)