Skip to content

Commit

Permalink
Split workspace tests
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Dec 23, 2022
1 parent 90bb6db commit 9d2a69c
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 86 deletions.
6 changes: 4 additions & 2 deletions tfmigrate/state_import_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ resource "time_static" "baz" {
triggers = {}
}
`
tf := tfexec.SetupTestAccWithApply(t, "default", backend+source)

workspace := "default"
tf := tfexec.SetupTestAccWithApply(t, workspace, backend+source)
ctx := context.Background()

_, err := tf.StateRm(ctx, nil, []string{"time_static.foo", "time_static.baz"})
Expand All @@ -44,7 +46,7 @@ resource "time_static" "baz" {
NewStateImportAction("time_static.baz", "2006-01-02T15:04:05Z"),
}

m := NewStateMigrator(tf.Dir(), "default", actions, &MigratorOption{}, false)
m := NewStateMigrator(tf.Dir(), workspace, actions, &MigratorOption{}, false)
err = m.Plan(ctx)
if err != nil {
t.Fatalf("failed to run migrator plan: %s", err)
Expand Down
206 changes: 128 additions & 78 deletions tfmigrate/state_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,106 +120,154 @@ func TestStateMigratorConfigNewMigrator(t *testing.T) {
}
}

func TestAccStateMigratorApply(t *testing.T) {
func TestAccStateMigratorApplySimple(t *testing.T) {
tfexec.SkipUnlessAcceptanceTestEnabled(t)

cases := []struct {
desc string
workspace string
}{
{
desc: "default workspace",
workspace: "default",
},
{
desc: "non-default workspace",
workspace: "workspace1",
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
backend := tfexec.GetTestAccBackendS3Config(t.Name())
backend := tfexec.GetTestAccBackendS3Config(t.Name())

source := `
source := `
resource "null_resource" "foo" {}
resource "null_resource" "bar" {}
resource "null_resource" "baz" {}
resource "time_static" "qux" {
triggers = {}
}
resource "time_static" "qux" { triggers = {} }
`
tf := tfexec.SetupTestAccWithApply(t, tc.workspace, backend+source)
ctx := context.Background()

updatedSource := `
workspace := "default"
tf := tfexec.SetupTestAccWithApply(t, workspace, backend+source)
ctx := context.Background()

updatedSource := `
resource "null_resource" "foo2" {}
resource "null_resource" "baz" {}
resource "time_static" "qux" {
triggers = {}
resource "time_static" "qux" { triggers = {} }
`

tfexec.UpdateTestAccSource(t, tf, backend+updatedSource)

_, err := tf.StateRm(ctx, nil, []string{"time_static.qux"})
if err != nil {
t.Fatalf("failed to run terraform state rm: %s", err)
}

changed, err := tf.PlanHasChange(ctx, nil)
if err != nil {
t.Fatalf("failed to run PlanHasChange: %s", err)
}
if !changed {
t.Fatalf("expect to have changes")
}

actions := []StateAction{
NewStateMvAction("null_resource.foo", "null_resource.foo2"),
NewStateRmAction([]string{"null_resource.bar"}),
NewStateImportAction("time_static.qux", "2006-01-02T15:04:05Z"),
}

m := NewStateMigrator(tf.Dir(), workspace, actions, &MigratorOption{}, false)
err = m.Plan(ctx)
if err != nil {
t.Fatalf("failed to run migrator plan: %s", err)
}

err = m.Apply(ctx)
if err != nil {
t.Fatalf("failed to run migrator apply: %s", err)
}

got, err := tf.StateList(ctx, nil, nil)
if err != nil {
t.Fatalf("failed to run terraform state list: %s", err)
}

want := []string{
"null_resource.foo2",
"null_resource.baz",
"time_static.qux",
}
sort.Strings(got)
sort.Strings(want)
if !reflect.DeepEqual(got, want) {
t.Errorf("got state: %v, want state: %v", got, want)
}

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")
}
}

func TestAccStateMigratorApplyWithWorkspace(t *testing.T) {
tfexec.SkipUnlessAcceptanceTestEnabled(t)

backend := tfexec.GetTestAccBackendS3Config(t.Name())

source := `
resource "null_resource" "foo" {}
resource "null_resource" "bar" {}
`

tfexec.UpdateTestAccSource(t, tf, backend+updatedSource)
workspace := "workspace1"
tf := tfexec.SetupTestAccWithApply(t, workspace, backend+source)
ctx := context.Background()

_, err := tf.StateRm(ctx, nil, []string{"time_static.qux"})
if err != nil {
t.Fatalf("failed to run terraform state rm: %s", err)
}
updatedSource := `
resource "null_resource" "foo2" {}
resource "null_resource" "bar" {}
`

changed, err := tf.PlanHasChange(ctx, nil)
if err != nil {
t.Fatalf("failed to run PlanHasChange: %s", err)
}
if !changed {
t.Fatalf("expect to have changes")
}
tfexec.UpdateTestAccSource(t, tf, backend+updatedSource)

actions := []StateAction{
NewStateMvAction("null_resource.foo", "null_resource.foo2"),
NewStateRmAction([]string{"null_resource.bar"}),
NewStateImportAction("time_static.qux", "2006-01-02T15:04:05Z"),
}
changed, err := tf.PlanHasChange(ctx, nil)
if err != nil {
t.Fatalf("failed to run PlanHasChange: %s", err)
}
if !changed {
t.Fatalf("expect to have changes")
}

m := NewStateMigrator(tf.Dir(), tc.workspace, actions, &MigratorOption{}, false)
err = m.Plan(ctx)
if err != nil {
t.Fatalf("failed to run migrator plan: %s", err)
}
actions := []StateAction{
NewStateMvAction("null_resource.foo", "null_resource.foo2"),
}

err = m.Apply(ctx)
if err != nil {
t.Fatalf("failed to run migrator apply: %s", err)
}
m := NewStateMigrator(tf.Dir(), workspace, actions, &MigratorOption{}, false)
err = m.Plan(ctx)
if err != nil {
t.Fatalf("failed to run migrator plan: %s", err)
}

got, err := tf.StateList(ctx, nil, nil)
if err != nil {
t.Fatalf("failed to run terraform state list: %s", err)
}
err = m.Apply(ctx)
if err != nil {
t.Fatalf("failed to run migrator apply: %s", err)
}

want := []string{
"null_resource.foo2",
"null_resource.baz",
"time_static.qux",
}
sort.Strings(got)
sort.Strings(want)
if !reflect.DeepEqual(got, want) {
t.Errorf("got state: %v, want state: %v", got, want)
}
got, err := tf.StateList(ctx, nil, nil)
if err != nil {
t.Fatalf("failed to run terraform state list: %s", err)
}

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")
}
})
want := []string{
"null_resource.foo2",
"null_resource.bar",
}
sort.Strings(got)
sort.Strings(want)
if !reflect.DeepEqual(got, want) {
t.Errorf("got state: %v, want state: %v", got, want)
}

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")
}
}

func TestAccStateMigratorApplyForce(t *testing.T) {
func TestAccStateMigratorApplyWithForce(t *testing.T) {
tfexec.SkipUnlessAcceptanceTestEnabled(t)

backend := tfexec.GetTestAccBackendS3Config(t.Name())
Expand All @@ -228,7 +276,9 @@ func TestAccStateMigratorApplyForce(t *testing.T) {
resource "null_resource" "foo" {}
resource "null_resource" "bar" {}
`
tf := tfexec.SetupTestAccWithApply(t, "default", backend+source)

workspace := "default"
tf := tfexec.SetupTestAccWithApply(t, workspace, backend+source)
ctx := context.Background()

updatedSource := `
Expand All @@ -254,7 +304,7 @@ resource "null_resource" "baz" {}
o := &MigratorOption{}
o.PlanOut = "foo.tfplan"

m := NewStateMigrator(tf.Dir(), "default", actions, o, true)
m := NewStateMigrator(tf.Dir(), workspace, actions, o, true)
err = m.Plan(ctx)
if err != nil {
t.Fatalf("failed to run migrator plan: %s", err)
Expand Down
6 changes: 4 additions & 2 deletions tfmigrate/state_mv_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ resource "null_resource" "foo" {}
resource "null_resource" "bar" {}
resource "null_resource" "baz" {}
`
tf := tfexec.SetupTestAccWithApply(t, "default", backend+source)

workspace := "default"
tf := tfexec.SetupTestAccWithApply(t, workspace, backend+source)
ctx := context.Background()

updatedSource := `
Expand All @@ -41,7 +43,7 @@ resource "null_resource" "baz" {}
NewStateMvAction("null_resource.bar", "null_resource.bar2"),
}

m := NewStateMigrator(tf.Dir(), "default", actions, &MigratorOption{}, false)
m := NewStateMigrator(tf.Dir(), workspace, actions, &MigratorOption{}, false)
err = m.Plan(ctx)
if err != nil {
t.Fatalf("failed to run migrator plan: %s", err)
Expand Down
6 changes: 4 additions & 2 deletions tfmigrate/state_rm_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ resource "null_resource" "bar" {}
resource "null_resource" "baz" {}
resource "null_resource" "qux" {}
`
tf := tfexec.SetupTestAccWithApply(t, "default", backend+source)

workspace := "default"
tf := tfexec.SetupTestAccWithApply(t, workspace, backend+source)
ctx := context.Background()

updatedSource := `
Expand All @@ -40,7 +42,7 @@ resource "null_resource" "baz" {}
NewStateRmAction([]string{"null_resource.qux"}),
}

m := NewStateMigrator(tf.Dir(), "default", actions, &MigratorOption{}, false)
m := NewStateMigrator(tf.Dir(), workspace, actions, &MigratorOption{}, false)
err = m.Plan(ctx)
if err != nil {
t.Fatalf("failed to run migrator plan: %s", err)
Expand Down
6 changes: 4 additions & 2 deletions tfmigrate/state_xmv_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func TestAccStateMvActionWildcardRenameSecurityGroupResourceNamesFromDocs(t *tes
resource "null_resource" "foo" {}
resource "null_resource" "bar" {}
`
tf := tfexec.SetupTestAccWithApply(t, "default", backend+source)

workspace := "default"
tf := tfexec.SetupTestAccWithApply(t, workspace, backend+source)
ctx := context.Background()

updatedSource := `
Expand All @@ -39,7 +41,7 @@ resource "null_resource" "bar2" {}
NewStateXMvAction("null_resource.*", "null_resource.${1}2"),
}

m := NewStateMigrator(tf.Dir(), "default", actions, &MigratorOption{}, false)
m := NewStateMigrator(tf.Dir(), workspace, actions, &MigratorOption{}, false)
err = m.Plan(ctx)
if err != nil {
t.Fatalf("failed to run migrator plan: %s", err)
Expand Down

0 comments on commit 9d2a69c

Please sign in to comment.