-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a comment to explain what format you expect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regex documented. |
||
return match | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "test" { | ||
value = "Hello, ${terraform.workspace}" | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!