pulumi/pkg/testing/integration/pulumi.go
Chris Smith defe0038ca
Add integration tests for pulumi CLI (#493)
This PR adds integration tests for exercising `pulumi init` and the `pulumi stack *` commands. The only functional change is merging in https://github.com/pulumi/pulumi/pull/492 , which I found while writing the tests and (of course 😁 ) wrote a regression for.

To do this I introduce a new test driver called `PulumiProgram`. This is different from the one found in the `testing/integration`package in that it doesn't try to prescribe a workflow. It really just deals in executing commands, and confirming strings are in the output.

While it doesn't hurt to have more tests for `pulumi`, my motivation here was so that I could reuse these to ensure I keep the same behavior for my pending PR that implements Cloud-enabled variants of some of these commands.
2017-10-30 15:17:13 -07:00

72 lines
2.2 KiB
Go

package integration
import (
"encoding/json"
"fmt"
"io/ioutil"
"path"
"strings"
"github.com/pulumi/pulumi/pkg/testing"
"github.com/pulumi/pulumi/pkg/workspace"
"github.com/stretchr/testify/assert"
)
// GetRepository returns the contents of the workspace's repository settings file. Assumes the
// bookkeeping dir (.pulumi) is in the CWD. Any IO errors fails the test.
func GetRepository(e *testing.Environment) workspace.Repository {
relativePathToRepoFile := fmt.Sprintf("%s/%s", workspace.BookkeepingDir, workspace.RepoFile)
if !e.PathExists(relativePathToRepoFile) {
e.Fatalf("did not find .pulumi/settings.json")
}
path := path.Join(e.CWD, relativePathToRepoFile)
contents, err := ioutil.ReadFile(path)
if err != nil {
e.Fatalf("error reading %s: %v", workspace.RepoFile, err)
}
var repo workspace.Repository
err = json.Unmarshal(contents, &repo)
if err != nil {
e.Fatalf("error unmarshalling JSON: %v", err)
}
return repo
}
// GetStacks returns the list of stacks and current stack by scraping `pulumi stack ls`.
// Assumes .pulumi is in the current working directory. Fails the test on IO errors.
func GetStacks(e *testing.Environment) ([]string, *string) {
out, err := e.RunCommand("pulumi", "stack", "ls")
assert.Equal(e, "", err, "expected nothing written to stderr")
outLines := strings.Split(out, "\n")
var stackNames []string
var currentStack *string
if len(outLines) == 0 {
e.Fatalf("command didn't output as expected")
}
// Confirm header row matches.
// TODO(pulumi/pulumi/issues/496): Provide structured output for pulumi commands. e.g., so we can avoid this
// err-prone scraping with just deserializings a JSON object.
assert.Equal(e, outLines[0], "NAME LAST UPDATE RESOURCE COUNT")
if len(outLines) >= 2 {
stackSummaries := outLines[1:]
for _, summary := range stackSummaries {
if summary == "" {
continue // Last line of stdout is "".
}
stackName := strings.TrimSpace(summary[:20])
if strings.HasSuffix(stackName, "*") {
currentStack = &stackName
stackName = strings.TrimSuffix(stackName, "*")
}
stackNames = append(stackNames, stackName)
}
}
return stackNames, currentStack
}