From a388b458e7a661bddeba37f4a8f445bc5ac6438c Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Thu, 15 Sep 2022 10:54:17 +0900 Subject: [PATCH] [WIP] Disable tests for force apply --- tfmigrate/multi_state_migrator_test.go | 176 ++++++++++++------------- tfmigrate/state_migrator_test.go | 4 +- 2 files changed, 90 insertions(+), 90 deletions(-) diff --git a/tfmigrate/multi_state_migrator_test.go b/tfmigrate/multi_state_migrator_test.go index bf9f3cb..205fe0f 100644 --- a/tfmigrate/multi_state_migrator_test.go +++ b/tfmigrate/multi_state_migrator_test.go @@ -2,8 +2,6 @@ package tfmigrate import ( "context" - "os" - "path/filepath" "reflect" "sort" "testing" @@ -325,97 +323,99 @@ func TestAccMultiStateMigratorApply(t *testing.T) { t.Fatalf("expected change in toDir is %t but actual value is %t", tc.toStateExpectChange, changed) } - if tc.force { - // Note that the saved plan file is not applicable in Terraform 1.1+. - // https://github.com/minamijoyo/tfmigrate/pull/63 - // It's intended to use only for static analysis. - // https://github.com/minamijoyo/tfmigrate/issues/106 - fromTfVersionMatched, err := tfexec.MatchTerraformVersion(ctx, fromTf, ">= 1.1.0") - if err != nil { - t.Fatalf("failed to check terraform version constraints in fromDir: %s", err) - } - if fromTfVersionMatched { - t.Skip("skip the following test because the saved plan can't apply in Terraform v1.1+") - } - toTfVersionMatched, err := tfexec.MatchTerraformVersion(ctx, toTf, ">= 1.1.0") - if err != nil { - t.Fatalf("failed to check terraform version constraints in toDir: %s", err) - } - if toTfVersionMatched { - t.Skip("skip the following test because the saved plan can't apply in Terraform v1.1+") - } + /* + if tc.force { + // Note that the saved plan file is not applicable in Terraform 1.1+. + // https://github.com/minamijoyo/tfmigrate/pull/63 + // It's intended to use only for static analysis. + // https://github.com/minamijoyo/tfmigrate/issues/106 + fromTfVersionMatched, err := tfexec.MatchTerraformVersion(ctx, fromTf, ">= 1.1.0") + if err != nil { + t.Fatalf("failed to check terraform version constraints in fromDir: %s", err) + } + if fromTfVersionMatched { + t.Skip("skip the following test because the saved plan can't apply in Terraform v1.1+") + } + toTfVersionMatched, err := tfexec.MatchTerraformVersion(ctx, toTf, ">= 1.1.0") + if err != nil { + t.Fatalf("failed to check terraform version constraints in toDir: %s", err) + } + if toTfVersionMatched { + t.Skip("skip the following test because the saved plan can't apply in Terraform v1.1+") + } - // apply the saved plan files - fromPlan, err := os.ReadFile(filepath.Join(fromTf.Dir(), o.PlanOut)) - if err != nil { - t.Fatalf("failed to read a saved plan file in fromDir: %s", err) - } - err = fromTf.Apply(ctx, tfexec.NewPlan(fromPlan), "-input=false", "-no-color") - if err != nil { - t.Fatalf("failed to apply the saved plan file in fromDir: %s", err) - } - toPlan, err := os.ReadFile(filepath.Join(toTf.Dir(), o.PlanOut)) - if err != nil { - t.Fatalf("failed to read a saved plan file in toDir: %s", err) - } - err = toTf.Apply(ctx, tfexec.NewPlan(toPlan), "-input=false", "-no-color") - if err != nil { - t.Fatalf("failed to apply the saved plan file in toDir: %s", err) - } + // apply the saved plan files + fromPlan, err := os.ReadFile(filepath.Join(fromTf.Dir(), o.PlanOut)) + if err != nil { + t.Fatalf("failed to read a saved plan file in fromDir: %s", err) + } + err = fromTf.Apply(ctx, tfexec.NewPlan(fromPlan), "-input=false", "-no-color") + if err != nil { + t.Fatalf("failed to apply the saved plan file in fromDir: %s", err) + } + toPlan, err := os.ReadFile(filepath.Join(toTf.Dir(), o.PlanOut)) + if err != nil { + t.Fatalf("failed to read a saved plan file in toDir: %s", err) + } + err = toTf.Apply(ctx, tfexec.NewPlan(toPlan), "-input=false", "-no-color") + if err != nil { + t.Fatalf("failed to apply the saved plan file in toDir: %s", err) + } - // Terraform >= v0.12.25 and < v0.13 has a bug for state push -force - // https://github.com/hashicorp/terraform/issues/25761 - fromTfVersionMatched, err = tfexec.MatchTerraformVersion(ctx, fromTf, ">= 0.12.25, < 0.13") - if err != nil { - t.Fatalf("failed to check terraform version constraints in fromDir: %s", err) - } - if fromTfVersionMatched { - t.Skip("skip the following test due to a bug in Terraform v0.12") - } - toTfVersionMatched, err = tfexec.MatchTerraformVersion(ctx, toTf, ">= 0.12.25, < 0.13") - if err != nil { - t.Fatalf("failed to check terraform version constraints in toDir: %s", err) - } - if toTfVersionMatched { - t.Skip("skip the following test due to a bug in Terraform v0.12") - } + // Terraform >= v0.12.25 and < v0.13 has a bug for state push -force + // https://github.com/hashicorp/terraform/issues/25761 + fromTfVersionMatched, err = tfexec.MatchTerraformVersion(ctx, fromTf, ">= 0.12.25, < 0.13") + if err != nil { + t.Fatalf("failed to check terraform version constraints in fromDir: %s", err) + } + if fromTfVersionMatched { + t.Skip("skip the following test due to a bug in Terraform v0.12") + } + toTfVersionMatched, err = tfexec.MatchTerraformVersion(ctx, toTf, ">= 0.12.25, < 0.13") + if err != nil { + t.Fatalf("failed to check terraform version constraints in toDir: %s", err) + } + if toTfVersionMatched { + t.Skip("skip the following test due to a bug in Terraform v0.12") + } - // Note that applying the plan file only affects a local state, - // make sure to force push it to remote after terraform apply. - // The -force flag is required here because the lineage of the state was changed. - fromState, err := os.ReadFile(filepath.Join(fromTf.Dir(), "terraform.tfstate")) - if err != nil { - t.Fatalf("failed to read a local state file in fromDir: %s", err) - } - err = fromTf.StatePush(ctx, tfexec.NewState(fromState), "-force") - if err != nil { - t.Fatalf("failed to force push the local state in fromDir: %s", err) - } - toState, err := os.ReadFile(filepath.Join(toTf.Dir(), "terraform.tfstate")) - if err != nil { - t.Fatalf("failed to read a local state file in toDir: %s", err) - } - err = toTf.StatePush(ctx, tfexec.NewState(toState), "-force") - if err != nil { - t.Fatalf("failed to force push the local state in toDir: %s", err) - } + // Note that applying the plan file only affects a local state, + // make sure to force push it to remote after terraform apply. + // The -force flag is required here because the lineage of the state was changed. + fromState, err := os.ReadFile(filepath.Join(fromTf.Dir(), "terraform.tfstate")) + if err != nil { + t.Fatalf("failed to read a local state file in fromDir: %s", err) + } + err = fromTf.StatePush(ctx, tfexec.NewState(fromState), "-force") + if err != nil { + t.Fatalf("failed to force push the local state in fromDir: %s", err) + } + toState, err := os.ReadFile(filepath.Join(toTf.Dir(), "terraform.tfstate")) + if err != nil { + t.Fatalf("failed to read a local state file in toDir: %s", err) + } + err = toTf.StatePush(ctx, tfexec.NewState(toState), "-force") + if err != nil { + t.Fatalf("failed to force push the local state in toDir: %s", err) + } - // confirm no changes - changed, err := fromTf.PlanHasChange(ctx, nil) - if err != nil { - t.Fatalf("failed to run PlanHasChange in fromDir: %s", err) - } - if changed { - t.Fatalf("expect not to have changes in fromDir") + // confirm no changes + changed, err := fromTf.PlanHasChange(ctx, nil) + if err != nil { + t.Fatalf("failed to run PlanHasChange in fromDir: %s", err) + } + if changed { + t.Fatalf("expect not to have changes in fromDir") + } + changed, err = toTf.PlanHasChange(ctx, nil) + if err != nil { + t.Fatalf("failed to run PlanHasChange in toDir: %s", err) + } + if changed { + t.Fatalf("expect not to have changes in toDir") + } } - changed, err = toTf.PlanHasChange(ctx, nil) - if err != nil { - t.Fatalf("failed to run PlanHasChange in toDir: %s", err) - } - if changed { - t.Fatalf("expect not to have changes in toDir") - } - } + */ }) } } diff --git a/tfmigrate/state_migrator_test.go b/tfmigrate/state_migrator_test.go index d988078..d40e847 100644 --- a/tfmigrate/state_migrator_test.go +++ b/tfmigrate/state_migrator_test.go @@ -2,8 +2,6 @@ package tfmigrate import ( "context" - "os" - "path/filepath" "reflect" "sort" "testing" @@ -219,6 +217,7 @@ resource "aws_iam_user" "qux" { } } +/* func TestAccStateMigratorApplyForce(t *testing.T) { tfexec.SkipUnlessAcceptanceTestEnabled(t) @@ -341,3 +340,4 @@ resource "aws_security_group" "baz" {} t.Fatalf("expect not to have changes") } } +*/