The first exception relates to how we launch plugins. Plugin paths are calculated using a well-known set of rules; this makes `gas` suspicious due to the need to use a variable to store the path of the plugin. The second and third are in test code and aren't terribly concerning. The latter exception asks `gas` to ignore the access key we hard-code into the integration tests for our Pulumi test account. The fourth exception allows use to use more permissive permissions for the `.pulumi` directory than `gas` would prefer. We use `755`; `gas` wants `700` or stricter. `755` is the default for `mkdir` and `.git` and so seems like a reasonable choice for us.
101 lines
3.5 KiB
Go
101 lines
3.5 KiB
Go
package integration
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/pulumi/pulumi/pkg/testing"
|
|
"github.com/pulumi/pulumi/pkg/workspace"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// CreateBasicPulumiRepo will initialize the environment with a basic Pulumi repository and
|
|
// project file definition. Returns the repo owner and name used.
|
|
func CreateBasicPulumiRepo(e *testing.Environment) (string, string) {
|
|
e.RunCommand("git", "init")
|
|
|
|
// We need to specify a well-known owner so that commands don't fail for reasons like
|
|
// the organization isn't in the private beta. The repository name is randomly generated
|
|
// so that testcases can assume no stacks, etc.
|
|
const owner = "moolumi"
|
|
name := fmt.Sprintf("integration-test-%x", time.Now().Unix())
|
|
e.RunCommand("pulumi", "init", "--owner", owner, "--name", name)
|
|
|
|
contents := "name: pulumi-test\ndescription: a test\nruntime: nodejs\n"
|
|
filePath := fmt.Sprintf("%s.yaml", workspace.ProjectFile)
|
|
filePath = path.Join(e.CWD, filePath)
|
|
err := ioutil.WriteFile(filePath, []byte(contents), os.ModePerm)
|
|
assert.NoError(e, err, "writing %s file", filePath)
|
|
|
|
return owner, name
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// TestAccountAccessToken is a Pulumi access token of a well-known test account. Most importantly, we can
|
|
// rely on this test account existing in all Pulumi environments. GitHub user "lumi-test-1". See
|
|
// pulumi-service/cmd/service/model/test.go for more information.
|
|
// nolint: gas
|
|
const TestAccountAccessToken = "gGo1WLk2Jh_NZlqo2FFvjPtBvo73IW3xsJvBRmDOPW4="
|