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.
110 lines
3.4 KiB
Go
110 lines
3.4 KiB
Go
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
|
|
|
|
package testing
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/pulumi/pulumi/pkg/util/fsutil"
|
|
"github.com/pulumi/pulumi/pkg/workspace"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// Environment is an extension of the testing.T type that provides support for a test environment
|
|
// on the local disk. The Environment has a root directory (e.g. a newly created temp folder) and
|
|
// a current working directory (to virtually change directories).
|
|
type Environment struct {
|
|
*testing.T
|
|
|
|
// RootPath is a new temp directory where the environment starts.
|
|
RootPath string
|
|
// Current working directory.
|
|
CWD string
|
|
}
|
|
|
|
// NewEnvironment returns a new Environment object, located in a temp directory.
|
|
func NewEnvironment(t *testing.T) *Environment {
|
|
root, err := ioutil.TempDir("", "test-env")
|
|
assert.NoError(t, err, "creating temp directory")
|
|
|
|
t.Logf("Created new test environment: %v", root)
|
|
return &Environment{
|
|
T: t,
|
|
RootPath: root,
|
|
CWD: root,
|
|
}
|
|
}
|
|
|
|
// ImportDirectory copies a folder into the test environment.
|
|
func (e *Environment) ImportDirectory(path string) {
|
|
err := fsutil.CopyFile(e.RootPath, path, nil)
|
|
if err != nil {
|
|
e.T.Fatalf("error importing directory: %v", err)
|
|
}
|
|
}
|
|
|
|
// DeleteEnvironment deletes the environment's RootPath, and everything underneath it.
|
|
func (e *Environment) DeleteEnvironment() {
|
|
e.Helper()
|
|
err := os.RemoveAll(e.RootPath)
|
|
assert.NoError(e, err, "cleaning up the test directory")
|
|
}
|
|
|
|
// PathExists returns whether or not a file or directory exists relative to Environment's working directory.
|
|
func (e *Environment) PathExists(p string) bool {
|
|
fullPath := path.Join(e.CWD, p)
|
|
_, err := os.Stat(fullPath)
|
|
return err == nil
|
|
}
|
|
|
|
// RunCommand invokes the command-line argument in the environment, returning stdout and stderr.
|
|
// Fails on non-zero exit code.
|
|
func (e *Environment) RunCommand(cmd string, args ...string) (string, string) {
|
|
e.Helper()
|
|
return e.runCommand(e.T, true, cmd, e.CWD, args...)
|
|
}
|
|
|
|
// RunCommandExpectError runs the command expecting a non-zero exit code.
|
|
func (e *Environment) RunCommandExpectError(cmd string, args ...string) (string, string) {
|
|
e.Helper()
|
|
return e.runCommand(e.T, false, cmd, e.CWD, args...)
|
|
}
|
|
|
|
// LocalURL returns a URL that uses the "fire and forget", storing its data inside the test folder (so multiple tests)
|
|
// may reuse stack names.
|
|
func (e *Environment) LocalURL() string {
|
|
return "local://" + filepath.Join(e.RootPath, workspace.BookkeepingDir)
|
|
}
|
|
|
|
func (e *Environment) runCommand(t *testing.T, expectSuccess bool, command,
|
|
cwd string, args ...string) (string, string) {
|
|
t.Helper()
|
|
t.Logf("Running command %v %v", command, strings.Join(args, " "))
|
|
|
|
// Buffer STDOUT and STDERR so we can return them later.
|
|
var outBuffer bytes.Buffer
|
|
var errBuffer bytes.Buffer
|
|
|
|
// nolint: gas
|
|
cmd := exec.Command(command, args...)
|
|
cmd.Dir = cwd
|
|
cmd.Stdout = &outBuffer
|
|
cmd.Stderr = &errBuffer
|
|
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", workspace.PulumiCredentialsPathEnvVar, e.RootPath))
|
|
|
|
runErr := cmd.Run()
|
|
if (runErr == nil) != expectSuccess {
|
|
t.Errorf("Finished with unexpected result. Expected success: %v, got error: %v", expectSuccess, runErr)
|
|
t.Logf("STDOUT: %v", outBuffer.String())
|
|
t.Logf("STDERR: %v", errBuffer.String())
|
|
}
|
|
return outBuffer.String(), errBuffer.String()
|
|
}
|