From e2695e83850dd5fd8f96947321eeef883a3fcace Mon Sep 17 00:00:00 2001 From: Krista LaFentres Date: Fri, 12 Nov 2021 14:16:54 -0600 Subject: [PATCH 1/2] Add support for the workspace show command --- tfexec/internal/e2etest/workspace_test.go | 24 ++++++++++++++++++++++- tfexec/workspace_show.go | 21 ++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tfexec/workspace_show.go diff --git a/tfexec/internal/e2etest/workspace_test.go b/tfexec/internal/e2etest/workspace_test.go index 43702f39..35964ecc 100644 --- a/tfexec/internal/e2etest/workspace_test.go +++ b/tfexec/internal/e2etest/workspace_test.go @@ -16,6 +16,7 @@ const defaultWorkspace = "default" func TestWorkspace_default_only(t *testing.T) { runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { assertWorkspaceList(t, tf, defaultWorkspace) + assertWorkspaceShow(t, tf, defaultWorkspace) t.Run("select default when already on default", func(t *testing.T) { err := tf.WorkspaceSelect(context.Background(), defaultWorkspace) @@ -24,6 +25,7 @@ func TestWorkspace_default_only(t *testing.T) { } assertWorkspaceList(t, tf, defaultWorkspace) + assertWorkspaceShow(t, tf, defaultWorkspace) }) t.Run("create new workspace", func(t *testing.T) { @@ -34,6 +36,7 @@ func TestWorkspace_default_only(t *testing.T) { } assertWorkspaceList(t, tf, newWorkspace, newWorkspace) + assertWorkspaceShow(t, tf, newWorkspace) }) }) } @@ -62,6 +65,7 @@ func TestWorkspace_already_exists(t *testing.T) { } assertWorkspaceList(t, tf, newWorkspace, newWorkspace) + assertWorkspaceShow(t, tf, newWorkspace) }) t.Run("create existing workspace", func(t *testing.T) { @@ -81,6 +85,7 @@ func TestWorkspace_already_exists(t *testing.T) { func TestWorkspace_multiple(t *testing.T) { runTest(t, "workspaces", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { assertWorkspaceList(t, tf, "foo", "foo") + assertWorkspaceShow(t, tf, "foo") const newWorkspace = "new1" @@ -91,6 +96,7 @@ func TestWorkspace_multiple(t *testing.T) { } assertWorkspaceList(t, tf, newWorkspace, "foo", newWorkspace) + assertWorkspaceShow(t, tf, newWorkspace) }) t.Run("select non-default workspace", func(t *testing.T) { @@ -100,13 +106,15 @@ func TestWorkspace_multiple(t *testing.T) { } assertWorkspaceList(t, tf, "foo", "foo", newWorkspace) + assertWorkspaceShow(t, tf, "foo") }) }) } func TestWorkspace_deletion(t *testing.T) { runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { - assertWorkspaceList(t, tf, "default") + assertWorkspaceList(t, tf, defaultWorkspace) + assertWorkspaceShow(t, tf, defaultWorkspace) const testWorkspace = "testws" @@ -117,18 +125,22 @@ func TestWorkspace_deletion(t *testing.T) { } assertWorkspaceList(t, tf, testWorkspace, testWorkspace) + assertWorkspaceShow(t, tf, testWorkspace) err = tf.WorkspaceSelect(context.Background(), defaultWorkspace) if err != nil { t.Fatalf("got error selecting workspace: %s", err) } + assertWorkspaceShow(t, tf, defaultWorkspace) + err = tf.WorkspaceDelete(context.Background(), testWorkspace) if err != nil { t.Fatalf("got error deleting workspace: %s", err) } assertWorkspaceList(t, tf, defaultWorkspace) + assertWorkspaceShow(t, tf, defaultWorkspace) }) }) } @@ -146,3 +158,13 @@ func assertWorkspaceList(t *testing.T, tf *tfexec.Terraform, expectedCurrent str t.Fatalf("expected %#v, got %#v", actualWorkspaces, expectedWorkspaces) } } + +func assertWorkspaceShow(t *testing.T, tf *tfexec.Terraform, expectedWorkspace string) { + actualWorkspace, err := tf.WorkspaceShow(context.Background()) + if err != nil { + t.Fatalf("got error querying workspace show: %s", err) + } + if actualWorkspace != expectedWorkspace { + t.Fatalf("expected %q workspace, got %q workspace", expectedWorkspace, actualWorkspace) + } +} diff --git a/tfexec/workspace_show.go b/tfexec/workspace_show.go new file mode 100644 index 00000000..fed9930a --- /dev/null +++ b/tfexec/workspace_show.go @@ -0,0 +1,21 @@ +package tfexec + +import ( + "context" + "strings" +) + +// WorkspaceShow represents the workspace show subcommand to the Terraform CLI. +func (tf *Terraform) WorkspaceShow(ctx context.Context) (string, error) { + workspaceShowCmd := tf.buildTerraformCmd(ctx, nil, "workspace", "show", "-no-color") + + var outBuffer strings.Builder + workspaceShowCmd.Stdout = &outBuffer + + err := tf.runTerraformCmd(ctx, workspaceShowCmd) + if err != nil { + return "", err + } + + return strings.TrimSpace(outBuffer.String()), nil +} From 1b115f5ec54b0fce076ff5f28ca59953ec2a4c80 Mon Sep 17 00:00:00 2001 From: Krista LaFentres Date: Wed, 17 Nov 2021 11:16:12 -0600 Subject: [PATCH 2/2] Add 0.10.0 version check for workspace show command --- tfexec/version.go | 1 + tfexec/workspace_show.go | 18 ++++++++++++++++-- tfexec/workspace_show_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tfexec/workspace_show_test.go diff --git a/tfexec/version.go b/tfexec/version.go index d9c57cd0..f42fbfc3 100644 --- a/tfexec/version.go +++ b/tfexec/version.go @@ -15,6 +15,7 @@ import ( var ( tf0_7_7 = version.Must(version.NewVersion("0.7.7")) + tf0_10_0 = version.Must(version.NewVersion("0.10.0")) tf0_12_0 = version.Must(version.NewVersion("0.12.0")) tf0_13_0 = version.Must(version.NewVersion("0.13.0")) tf0_14_0 = version.Must(version.NewVersion("0.14.0")) diff --git a/tfexec/workspace_show.go b/tfexec/workspace_show.go index fed9930a..7d5a267f 100644 --- a/tfexec/workspace_show.go +++ b/tfexec/workspace_show.go @@ -2,20 +2,34 @@ package tfexec import ( "context" + "fmt" + "os/exec" "strings" ) // WorkspaceShow represents the workspace show subcommand to the Terraform CLI. func (tf *Terraform) WorkspaceShow(ctx context.Context) (string, error) { - workspaceShowCmd := tf.buildTerraformCmd(ctx, nil, "workspace", "show", "-no-color") + workspaceShowCmd, err := tf.workspaceShowCmd(ctx) + if err != nil { + return "", err + } var outBuffer strings.Builder workspaceShowCmd.Stdout = &outBuffer - err := tf.runTerraformCmd(ctx, workspaceShowCmd) + err = tf.runTerraformCmd(ctx, workspaceShowCmd) if err != nil { return "", err } return strings.TrimSpace(outBuffer.String()), nil } + +func (tf *Terraform) workspaceShowCmd(ctx context.Context) (*exec.Cmd, error) { + err := tf.compatible(ctx, tf0_10_0, nil) + if err != nil { + return nil, fmt.Errorf("workspace show was first introduced in Terraform 0.10.0: %w", err) + } + + return tf.buildTerraformCmd(ctx, nil, "workspace", "show", "-no-color"), nil +} diff --git a/tfexec/workspace_show_test.go b/tfexec/workspace_show_test.go new file mode 100644 index 00000000..5c21a000 --- /dev/null +++ b/tfexec/workspace_show_test.go @@ -0,0 +1,31 @@ +package tfexec + +import ( + "context" + "errors" + "testing" +) + +func TestWorkspaceShowCmd(t *testing.T) { + td := t.TempDir() + + tf, err := NewTerraform(td, tfVersion(t, "0.9.11")) + if err != nil { + t.Fatal(err) + } + + // empty env, to avoid environ mismatch in testing + tf.SetEnv(map[string]string{}) + + t.Run("too old version", func(t *testing.T) { + _, err := tf.workspaceShowCmd(context.Background()) + if err == nil { + t.Fatal("expected old version to fail") + } + + var expectedErr *ErrVersionMismatch + if !errors.As(err, &expectedErr) { + t.Fatalf("error doesn't match: %#v", err) + } + }) +}