5
5
"encoding/json"
6
6
"fmt"
7
7
"os"
8
+ "os/exec"
8
9
"path/filepath"
9
10
"strings"
10
11
"testing"
@@ -16,6 +17,8 @@ import (
16
17
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
17
18
"github.com/hashicorp/terraform-plugin-framework/providerserver"
18
19
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
20
+ "github.com/hashicorp/terraform-plugin-testing/config"
21
+ "github.com/hashicorp/terraform-plugin-testing/helper/resource"
19
22
"github.com/hashicorp/terraform-plugin-testing/plancheck"
20
23
"github.com/hashicorp/terraform-plugin-testing/terraform"
21
24
)
@@ -317,3 +320,83 @@ var LogResourceDrift = []plancheck.PlanCheck{pc}
317
320
func PtrTo [T any ](v T ) * T {
318
321
return & v
319
322
}
323
+
324
+ // RunMigrationCommand runs the migration script to transform config and state
325
+ func RunMigrationCommand (t * testing.T , v4Config string , tmpDir string ) {
326
+ t .Helper ()
327
+
328
+ // Get the current working directory to find the migration binary
329
+ cwd , err := os .Getwd ()
330
+ if err != nil {
331
+ t .Fatalf ("Failed to get current working directory: %v" , err )
332
+ }
333
+
334
+ // Build the path to the migration binary
335
+ // The test runs from internal/services/zone, so we need to go up to the root
336
+ projectRoot := filepath .Join (cwd , ".." , ".." , ".." )
337
+ migratePath := filepath .Join (projectRoot , "cmd" , "migrate" )
338
+ t .Logf ("Migrate path: %s" , migratePath )
339
+
340
+ // Write the v4 config to tmpDir/test_migration.tf
341
+ testConfigPath := filepath .Join (tmpDir , "test_migration.tf" )
342
+ t .Logf ("Writing v4 config to: %s" , testConfigPath )
343
+
344
+ err = os .WriteFile (testConfigPath , []byte (v4Config ), 0644 )
345
+ if err != nil {
346
+ t .Fatalf ("Failed to write test config file: %v" , err )
347
+ }
348
+ t .Logf ("Successfully wrote v4 config (%d bytes)" , len (v4Config ))
349
+
350
+ // Find state file in tmpDir
351
+ entries , err := os .ReadDir (tmpDir )
352
+ var stateDir string
353
+ if err != nil {
354
+ t .Logf ("Failed to read test directory: %v" , err )
355
+ } else {
356
+ for _ , entry := range entries {
357
+ if entry .IsDir () {
358
+ inner_entries , _ := os .ReadDir (filepath .Join (tmpDir , entry .Name ()))
359
+ for _ , inner_entry := range inner_entries {
360
+ if inner_entry .Name () == "terraform.tfstate" {
361
+ stateDir = filepath .Join (tmpDir , entry .Name ())
362
+ }
363
+ }
364
+ }
365
+
366
+ }
367
+ }
368
+
369
+ // Run the migration command on tmpDir (for config) and terraform.tfstate (for state)
370
+ t .Logf ("StateDir: %s" , stateDir )
371
+ state , err := os .ReadFile (filepath .Join (stateDir , "terraform.tfstate" ))
372
+ if err != nil {
373
+ t .Fatalf ("Failed to read state file: %v" , err )
374
+ }
375
+ t .Logf ("State is: %s" , string (state ))
376
+ cmd := exec .Command ("go" , "run" , "-C" , migratePath , "." , "-config" , tmpDir , "-state" , filepath .Join (stateDir , "terraform.tfstate" ))
377
+ cmd .Dir = tmpDir
378
+ // Set environment variable so the migrate command can find local grit patterns
379
+ patternsDir := filepath .Join (migratePath , "patterns" )
380
+ cmd .Env = append (os .Environ (), fmt .Sprintf ("TF_MIGRATE_PATTERNS_DIR=%s" , patternsDir ))
381
+
382
+ // Capture output for debugging
383
+ output , err := cmd .CombinedOutput ()
384
+ if err != nil {
385
+ t .Logf ("Migration command failed: %v" , err )
386
+ }
387
+
388
+ t .Logf ("Migration output:\n %s" , string (output ))
389
+ }
390
+
391
+ // MigrationTestStep creates a test step that runs the migration command and validates with v5 provider
392
+ func MigrationTestStep (t * testing.T , v4Config string , tmpDir string ) resource.TestStep {
393
+ return resource.TestStep {
394
+ PreConfig : func () {
395
+ // Run the migration command to transform config and state
396
+ RunMigrationCommand (t , v4Config , tmpDir )
397
+ },
398
+ ProtoV6ProviderFactories : TestAccProtoV6ProviderFactories ,
399
+ ConfigDirectory : config .StaticDirectory (tmpDir ),
400
+ PlanOnly : true , // Verify no changes needed after migration
401
+ }
402
+ }
0 commit comments