This change removes the need to `pulumi init` when targeting the local backend. A fair amount of the change lays the foundation that the next set of changes to stop having `pulumi init` be used for cloud stacks as well. Previously, `pulumi init` logically did two things: 1. It created the bookkeeping directory for local stacks, this was stored in `<repository-root>/.pulumi`, where `<repository-root>` was the path to what we belived the "root" of your project was. In the case of git repositories, this was the directory that contained your `.git` folder. 2. It recorded repository information in `<repository-root>/.pulumi/repository.json`. This was used by the cloud backend when computing what project to interact with on Pulumi.com The new identity model will remove the need for (2), since we only need an owner and stack name to fully qualify a stack on pulumi.com, so it's easy enough to stop creating a folder just for that. However, for the local backend, we need to continue to retain some information about stacks (e.g. checkpoints, history, etc). In addition, we need to store our workspace settings (which today just contains the selected stack) somehere. For state stored by the local backend, we change the URL scheme from `local://` to `local://<optional-root-path>`. When `<optional-root-path>` is unset, it defaults to `$HOME`. We create our `.pulumi` folder in that directory. This is important because stack names now must be unique within the backend, but we have some tests using local stacks which use fixed stack names, so each integration test really wants its own "view" of the world. For the workspace settings, we introduce a new `workspaces` directory in `~/.pulumi`. In this folder we write the workspace settings file for each project. The file name is the name of the project, combined with the SHA1 of the path of the project file on disk, to ensure that multiple pulumi programs with the same project name have different workspace settings. This does mean that moving a project's location on disk will cause the CLI to "forget" what the selected stack was, which is unfortunate, but not the end of the world. If this ends up being a big pain point, we can certianly try to play games in the future (for example, if we saw a .git folder in a parent folder, we could store data in there). With respect to compatibility, we don't attempt to migrate older files to their newer locations. For long lived stacks managed using the local backend, we can provide information on where to move things to. For all stacks (regardless of backend) we'll require the user to `pulumi stack select` their stack again, but that seems like the correct trade-off vs writing complicated upgrade code.
91 lines
3 KiB
Go
91 lines
3 KiB
Go
package integration
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"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) {
|
|
e.RunCommand("git", "init")
|
|
|
|
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)
|
|
}
|
|
|
|
// 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, _ := e.RunCommand("pulumi", "stack", "ls")
|
|
|
|
outLines := strings.Split(out, "\n")
|
|
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.True(e, strings.HasPrefix(outLines[0], "NAME"))
|
|
|
|
var stackNames []string
|
|
var currentStack *string
|
|
stackSummaries := outLines[1:]
|
|
for _, summary := range stackSummaries {
|
|
if summary == "" {
|
|
break
|
|
}
|
|
firstSpace := strings.Index(summary, " ")
|
|
if firstSpace != -1 {
|
|
stackName := strings.TrimSpace(summary[:firstSpace])
|
|
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, intentionally using a test token
|
|
const TestAccountAccessToken = "gGo1WLk2Jh_NZlqo2FFvjPtBvo73IW3xsJvBRmDOPW4="
|