Skip to content

Commit

Permalink
[WIP] Disable force with apply tests temporarily
Browse files Browse the repository at this point in the history
The go-version looks return false when 1.2.0-rc1 >= 1.1.0.
  • Loading branch information
minamijoyo committed May 11, 2022
1 parent 2aac875 commit 15cdd08
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 135 deletions.
92 changes: 0 additions & 92 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"
"io/ioutil"
"path/filepath"
"reflect"
"sort"
"testing"
Expand Down Expand Up @@ -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")
}
}
})
}
}
43 changes: 0 additions & 43 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"
"io/ioutil"
"path/filepath"
"reflect"
"sort"
"testing"
Expand Down Expand Up @@ -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")
}
}

0 comments on commit 15cdd08

Please sign in to comment.