Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a function for workspace selection, or creation #164

Merged
merged 3 commits into from
Oct 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions modules/terraform/workspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package terraform

import (
"fmt"
"regexp"
"strings"
"testing"
)

// WorkspaceSelectOrNew runs terraform workspace with the given options and the workspace name
// and returns a name of the current workspace. It tries to select a workspace with the given
// name, or it creates a new one if it doesn't exist.
func WorkspaceSelectOrNew(t *testing.T, options *Options, name string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the return value?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value is the name of the current workspace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add that to the docs!

out, err := WorkspaceSelectOrNewE(t, options, name)
if err != nil {
t.Fatal(err)
}
return out
}

// WorkspaceSelectOrNewE runs terraform workspace with the given options and the workspace name
// and returns a name of the current workspace. It tries to select a workspace with the given
// name, or it creates a new one if it doesn't exist.
func WorkspaceSelectOrNewE(t *testing.T, options *Options, name string) (string, error) {
out, err := RunTerraformCommandE(t, options, "workspace", "list")
if err != nil {
return "", err
}

if isExistingWorkspace(out, name) {
_, err = RunTerraformCommandE(t, options, "workspace", "select", name)
} else {
_, err = RunTerraformCommandE(t, options, "workspace", "new", name)
}
if err != nil {
return "", err
}

return RunTerraformCommandE(t, options, "workspace", "show")
}

func isExistingWorkspace(out string, name string) bool {
workspaces := strings.Split(out, "\n")
for _, ws := range workspaces {
if nameMatchesWorkspace(name, ws) {
return true
}
}
return false
}

func nameMatchesWorkspace(name string, workspace string) bool {
match, _ := regexp.MatchString(fmt.Sprintf("^\\*?\\s*%s$", name), workspace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment to explain what format you expect workspace to have? It'll make it easier to understand the regex.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regex documented.

return match
}
132 changes: 132 additions & 0 deletions modules/terraform/workspace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package terraform

import (
"testing"

"github.com/gruntwork-io/terratest/modules/files"
"github.com/stretchr/testify/assert"
)

func TestWorkspaceNew(t *testing.T) {
t.Parallel()

testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", t.Name())
if err != nil {
t.Fatal(err)
}

options := &Options{
TerraformDir: testFolder,
}

out := WorkspaceSelectOrNew(t, options, "terratest")

assert.Equal(t, "terratest", out)
}

func TestWorkspaceIllegalName(t *testing.T) {
t.Parallel()

testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", t.Name())
if err != nil {
t.Fatal(err)
}

options := &Options{
TerraformDir: testFolder,
}

out, err := WorkspaceSelectOrNewE(t, options, "###@@@&&&")

assert.Error(t, err)
assert.Equal(t, "", out, "%q should be an empty string", out)
}

func TestWorkspaceSelect(t *testing.T) {
t.Parallel()

testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", t.Name())
if err != nil {
t.Fatal(err)
}

options := &Options{
TerraformDir: testFolder,
}

out := WorkspaceSelectOrNew(t, options, "terratest")
assert.Equal(t, "terratest", out)

out = WorkspaceSelectOrNew(t, options, "default")
assert.Equal(t, "default", out)
}

func TestWorkspaceApply(t *testing.T) {
t.Parallel()

testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", t.Name())
if err != nil {
t.Fatal(err)
}

options := &Options{
TerraformDir: testFolder,
}

WorkspaceSelectOrNew(t, options, "Terratest")
out := InitAndApply(t, options)

assert.Contains(t, out, "Hello, Terratest")
}

func TestIsExistingWorkspace(t *testing.T) {
t.Parallel()

testCases := []struct {
out string
name string
expected bool
}{
{" default\n* foo\n", "default", true},
{"* default\n foo\n", "default", true},
{" foo\n* default\n", "default", true},
{"* foo\n default\n", "default", true},
{" foo\n* bar\n", "default", false},
{"* foo\n bar\n", "default", false},
{" default\n* foobar\n", "foo", false},
{"* default\n foobar\n", "foo", false},
{" default\n* foo\n", "foobar", false},
{"* default\n foo\n", "foobar", false},
{"* default\n foo\n", "foo", true},
}

for _, testCase := range testCases {
actual := isExistingWorkspace(testCase.out, testCase.name)
assert.Equal(t, testCase.expected, actual, "Out: %q, Name: %q", testCase.out, testCase.name)
}
}

func TestNameMatchesWorkspace(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
workspace string
expected bool
}{
{"default", " default", true},
{"default", "* default", true},
{"default", "", false},
{"foo", " foobar", false},
{"foo", "* foobar", false},
{"foobar", " foo", false},
{"foobar", "* foo", false},
{"foo", " foo", true},
{"foo", "* foo", true},
}

for _, testCase := range testCases {
actual := nameMatchesWorkspace(testCase.name, testCase.workspace)
assert.Equal(t, testCase.expected, actual, "Name: %q, Workspace: %q", testCase.name, testCase.workspace)
}
}
3 changes: 3 additions & 0 deletions test/fixtures/terraform-workspace/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "test" {
value = "Hello, ${terraform.workspace}"
}