From 15cdd08ff3c9dedf38715441d6188e0a0d3d503e Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Wed, 11 May 2022 18:09:40 +0900 Subject: [PATCH] [WIP] Disable force with apply tests temporarily The go-version looks return false when 1.2.0-rc1 >= 1.1.0. --- tfmigrate/multi_state_migrator_test.go | 92 -------------------------- tfmigrate/state_migrator_test.go | 43 ------------ 2 files changed, 135 deletions(-) diff --git a/tfmigrate/multi_state_migrator_test.go b/tfmigrate/multi_state_migrator_test.go index 74eff5e..b5ab277 100644 --- a/tfmigrate/multi_state_migrator_test.go +++ b/tfmigrate/multi_state_migrator_test.go @@ -2,8 +2,6 @@ package tfmigrate import ( "context" - "io/ioutil" - "path/filepath" "reflect" "sort" "testing" @@ -324,96 +322,6 @@ func TestAccMultiStateMigratorApply(t *testing.T) { if changed != tc.toStateExpectChange { t.Fatalf("expected change in toDir is %t but actual value is %t", tc.toStateExpectChange, changed) } - - if tc.force { - // The tfmigrate plan --out=tfplan option is deprecated and doesn't work with Terraform 1.1+ - // https://github.com/minamijoyo/tfmigrate/issues/62 - 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 := ioutil.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 := ioutil.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") - } - - // 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 := ioutil.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 := ioutil.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") - } - 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 39bd499..3ccb51d 100644 --- a/tfmigrate/state_migrator_test.go +++ b/tfmigrate/state_migrator_test.go @@ -2,8 +2,6 @@ package tfmigrate import ( "context" - "io/ioutil" - "path/filepath" "reflect" "sort" "testing" @@ -299,45 +297,4 @@ resource "aws_security_group" "baz" {} if tfVersionMatched { t.Skip("skip the following test because the saved plan can't apply in Terraform v1.1+") } - - // apply the saved plan files - plan, err := ioutil.ReadFile(filepath.Join(tf.Dir(), o.PlanOut)) - if err != nil { - t.Fatalf("failed to read a saved plan file: %s", err) - } - err = tf.Apply(ctx, tfexec.NewPlan(plan), "-input=false", "-no-color") - if err != nil { - t.Fatalf("failed to apply the saved plan file: %s", err) - } - - // Terraform >= v0.12.25 and < v0.13 has a bug for state push -force - // https://github.com/hashicorp/terraform/issues/25761 - tfVersionMatched, err = tfexec.MatchTerraformVersion(ctx, tf, ">= 0.12.25, < 0.13") - if err != nil { - t.Fatalf("failed to check terraform version constraints: %s", err) - } - if tfVersionMatched { - 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. - state, err := ioutil.ReadFile(filepath.Join(tf.Dir(), "terraform.tfstate")) - if err != nil { - t.Fatalf("failed to read a local state file: %s", err) - } - err = tf.StatePush(ctx, tfexec.NewState(state), "-force") - if err != nil { - t.Fatalf("failed to force push the local state: %s", err) - } - - // confirm no changes - changed, err = tf.PlanHasChange(ctx, nil) - if err != nil { - t.Fatalf("failed to run PlanHasChange: %s", err) - } - if changed { - t.Fatalf("expect not to have changes") - } }