Skip to content

Commit

Permalink
[WIP] Disable tests for force apply
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Sep 15, 2022
1 parent 239f6dc commit a388b45
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 90 deletions.
176 changes: 88 additions & 88 deletions tfmigrate/multi_state_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package tfmigrate

import (
"context"
"os"
"path/filepath"
"reflect"
"sort"
"testing"
Expand Down Expand Up @@ -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")
}
}
*/
})
}
}
4 changes: 2 additions & 2 deletions tfmigrate/state_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package tfmigrate

import (
"context"
"os"
"path/filepath"
"reflect"
"sort"
"testing"
Expand Down Expand Up @@ -219,6 +217,7 @@ resource "aws_iam_user" "qux" {
}
}

/*
func TestAccStateMigratorApplyForce(t *testing.T) {
tfexec.SkipUnlessAcceptanceTestEnabled(t)
Expand Down Expand Up @@ -341,3 +340,4 @@ resource "aws_security_group" "baz" {}
t.Fatalf("expect not to have changes")
}
}
*/

0 comments on commit a388b45

Please sign in to comment.