-
Notifications
You must be signed in to change notification settings - Fork 759
Add logpush_job migrations tests #6456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ssicard
wants to merge
4
commits into
next
Choose a base branch
from
ssicard/logpush-job
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6449c82
add migration test for logpush_job
ssicard dcc4397
add zone level logpush jobs to sweeper
ssicard 30b69ac
use MigrationV2TestStep, use zone level job for instant-logs test
ssicard 00a15e7
handle instant-logs being returned from the API despite not being a v…
ssicard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| package logpush_job_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/config" | ||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
| "github.com/hashicorp/terraform-plugin-testing/statecheck" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" | ||
|
|
||
| "github.com/cloudflare/terraform-provider-cloudflare/internal/acctest" | ||
| "github.com/cloudflare/terraform-provider-cloudflare/internal/utils" | ||
| ) | ||
|
|
||
| // TestMigrateCloudflareLogpushJob_Migration_Basic_MultiVersion tests the most fundamental | ||
| // logpush job migration scenario with output_options block to attribute transformation. | ||
| // This test ensures that: | ||
| // 1. output_options block { ... } → output_options = { ... } (block to attribute syntax) | ||
| // 2. cve20214428 field is renamed to cve_2021_44228 | ||
| // 3. kind = "instant-logs" is converted to kind = "" | ||
| // 4. Numeric fields are properly converted (max_upload_* fields) | ||
| // 5. The migration tool successfully transforms both configuration and state files | ||
| func TestMigrateCloudflareLogpushJob_Migration_Basic_MultiVersion(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| version string | ||
| configFn func(accountID, rnd string) string | ||
| }{ | ||
| { | ||
| name: "from_v4_52_1", | ||
| version: "4.52.1", | ||
| configFn: testAccCloudflareLogpushJobMigrationConfigV4Basic, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| accountID := acctest.TestAccCloudflareAccountID | ||
| rnd := utils.GenerateRandomResourceName() | ||
| resourceName := "cloudflare_logpush_job." + rnd | ||
| testConfig := tc.configFn(accountID, rnd) | ||
| tmpDir := t.TempDir() | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { | ||
| acctest.TestAccPreCheck(t) | ||
| acctest.TestAccPreCheck_AccountID(t) | ||
| }, | ||
| WorkingDir: tmpDir, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| // Step 1: Create logpush job with v4 provider | ||
| ExternalProviders: map[string]resource.ExternalProvider{ | ||
| "cloudflare": { | ||
| VersionConstraint: tc.version, | ||
| Source: "cloudflare/cloudflare", | ||
| }, | ||
| }, | ||
| Config: testConfig, | ||
| ConfigStateChecks: []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("account_id"), knownvalue.StringExact(accountID)), | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("dataset"), knownvalue.StringExact("audit_logs")), | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("enabled"), knownvalue.Bool(true)), | ||
| }, | ||
| }, | ||
| // Step 2: Migrate to v5 provider | ||
| acctest.MigrationV2TestStep(t, testConfig, tmpDir, tc.version, "v4", "v5", []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("account_id"), knownvalue.StringExact(accountID)), | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("dataset"), knownvalue.StringExact("audit_logs")), | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("enabled"), knownvalue.Bool(true)), | ||
| }), | ||
| { | ||
| // Step 3: Apply migrated config with v5 provider | ||
| ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories, | ||
| ConfigDirectory: config.StaticDirectory(tmpDir), | ||
| ConfigStateChecks: []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("account_id"), knownvalue.StringExact(accountID)), | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("dataset"), knownvalue.StringExact("audit_logs")), | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("enabled"), knownvalue.Bool(true)), | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestMigrateCloudflareLogpushJob_Migration_OutputOptions tests migration of logpush jobs | ||
| // with output_options blocks. This test verifies that: | ||
| // 1. output_options block syntax is converted to attribute syntax with = | ||
| // 2. cve20214428 field is properly renamed to cve_2021_44228 | ||
| // 3. All nested fields within output_options are preserved | ||
| // 4. State transformation converts array [{...}] to object {...} | ||
| func TestMigrateCloudflareLogpushJob_Migration_OutputOptions(t *testing.T) { | ||
| accountID := acctest.TestAccCloudflareAccountID | ||
| rnd := utils.GenerateRandomResourceName() | ||
| resourceName := "cloudflare_logpush_job." + rnd | ||
| v4Config := testAccCloudflareLogpushJobMigrationConfigV4OutputOptions(accountID, rnd) | ||
| tmpDir := t.TempDir() | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { | ||
| acctest.TestAccPreCheck(t) | ||
| acctest.TestAccPreCheck_AccountID(t) | ||
| }, | ||
| WorkingDir: tmpDir, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| // Step 1: Create logpush job with v4 provider | ||
| ExternalProviders: map[string]resource.ExternalProvider{ | ||
| "cloudflare": { | ||
| VersionConstraint: "4.52.1", | ||
| Source: "cloudflare/cloudflare", | ||
| }, | ||
| }, | ||
| Config: v4Config, | ||
| ConfigStateChecks: []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("dataset"), knownvalue.StringExact("audit_logs")), | ||
| }, | ||
| }, | ||
| // Step 2: Migrate to v5 provider | ||
| acctest.MigrationV2TestStep(t, v4Config, tmpDir, "4.52.1", "v4", "v5", []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("dataset"), knownvalue.StringExact("audit_logs")), | ||
| }), | ||
| { | ||
| // Step 3: Apply migrated config with v5 provider | ||
| ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories, | ||
| ConfigDirectory: config.StaticDirectory(tmpDir), | ||
| ConfigStateChecks: []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("dataset"), knownvalue.StringExact("audit_logs")), | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| // TestMigrateCloudflareLogpushJob_Migration_InstantLogs tests migration of logpush jobs | ||
| // with kind = "instant-logs" which needs to be converted to empty string in v5. | ||
| func TestMigrateCloudflareLogpushJob_Migration_InstantLogs(t *testing.T) { | ||
| zoneID := acctest.TestAccCloudflareZoneID | ||
| rnd := utils.GenerateRandomResourceName() | ||
| resourceName := "cloudflare_logpush_job." + rnd | ||
| v4Config := testAccCloudflareLogpushJobMigrationConfigV4InstantLogs(zoneID, rnd) | ||
| tmpDir := t.TempDir() | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { | ||
| acctest.TestAccPreCheck(t) | ||
| acctest.TestAccPreCheck_ZoneID(t) | ||
| }, | ||
| WorkingDir: tmpDir, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| // Step 1: Create logpush job with v4 provider | ||
| ExternalProviders: map[string]resource.ExternalProvider{ | ||
| "cloudflare": { | ||
| VersionConstraint: "4.52.1", | ||
| Source: "cloudflare/cloudflare", | ||
| }, | ||
| }, | ||
| Config: v4Config, | ||
| ConfigStateChecks: []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("kind"), knownvalue.StringExact("instant-logs")), | ||
| }, | ||
| }, | ||
| // Step 2: Migrate to v5 provider | ||
| acctest.MigrationV2TestStep(t, v4Config, tmpDir, "4.52.1", "v4", "v5", []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("kind"), knownvalue.StringExact("")), | ||
| }), | ||
| { | ||
| // Step 3: Apply migrated config with v5 provider | ||
| ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories, | ||
| ConfigDirectory: config.StaticDirectory(tmpDir), | ||
| ConfigStateChecks: []statecheck.StateCheck{ | ||
| statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("kind"), knownvalue.StringExact("")), | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| // V4 Configuration Functions | ||
|
|
||
| func testAccCloudflareLogpushJobMigrationConfigV4Basic(accountID, rnd string) string { | ||
| return fmt.Sprintf(` | ||
| resource "cloudflare_logpush_job" "%[2]s" { | ||
| account_id = "%[1]s" | ||
| dataset = "audit_logs" | ||
| destination_conf = "https://logpush-receiver.sd.cfplat.com" | ||
| enabled = true | ||
| } | ||
| `, accountID, rnd) | ||
| } | ||
|
|
||
| func testAccCloudflareLogpushJobMigrationConfigV4OutputOptions(accountID, rnd string) string { | ||
| return fmt.Sprintf(` | ||
| resource "cloudflare_logpush_job" "%[2]s" { | ||
| account_id = "%[1]s" | ||
| dataset = "audit_logs" | ||
| destination_conf = "https://logpush-receiver.sd.cfplat.com" | ||
| enabled = true | ||
|
|
||
| output_options { | ||
| cve20214428 = true | ||
| output_type = "ndjson" | ||
| field_names = ["ClientIP", "EdgeStartTimestamp"] | ||
| } | ||
| } | ||
| `, accountID, rnd) | ||
| } | ||
|
|
||
| func testAccCloudflareLogpushJobMigrationConfigV4InstantLogs(zoneID, rnd string) string { | ||
| return fmt.Sprintf(` | ||
| resource "cloudflare_logpush_job" "%[2]s" { | ||
| zone_id = "%[1]s" | ||
| dataset = "http_requests" | ||
| destination_conf = "https://logpush-receiver.sd.cfplat.com" | ||
| enabled = true | ||
| kind = "instant-logs" | ||
| } | ||
| `, zoneID, rnd) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updates are being made in resource.go to account for an update in
kindin logpush_job resource.instant-logswas a valid kind option in v4, but is not valid in v5. The API was adding instant-logs back to the state even though it was not valid, so we explicitly ignore that as a value.