pulumi/tests/init_test.go
Matt Ellis bac02f1df1 Remove the need to pulumi init for the local backend
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.
2018-04-18 04:53:49 -07:00

101 lines
2.9 KiB
Go

// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
package tests
import (
"os"
"path"
"testing"
ptesting "github.com/pulumi/pulumi/pkg/testing"
"github.com/pulumi/pulumi/pkg/testing/integration"
"github.com/pulumi/pulumi/pkg/workspace"
"github.com/stretchr/testify/assert"
)
func TestPulumiInit(t *testing.T) {
t.Run("SanityTest", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer func() {
if !t.Failed() {
e.DeleteEnvironment()
}
}()
// With a .git folder in the test root, `pulumi init` sets up shop there.
const dirName = workspace.BookkeepingDir
e.RunCommand("git", "init")
assert.False(t, e.PathExists(dirName), "expecting no %s folder yet", dirName)
e.RunCommand("pulumi", "init")
assert.True(t, e.PathExists(dirName), "expecting %s folder to be created", dirName)
})
t.Run("WalkUpToGitFolder", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer func() {
if !t.Failed() {
e.DeleteEnvironment()
}
}()
// Create a git repo in the root.
e.RunCommand("git", "init")
assert.True(t, e.PathExists(".git"), "expecting .git folder")
// Create a subdirectory and CD into it.,
subdir := path.Join(e.RootPath, "/foo/bar/baz/")
err := os.MkdirAll(subdir, os.ModePerm)
assert.NoError(t, err, "error creating subdirectory")
e.CWD = subdir
// Confirm we are in the new location (no .git folder found.)
assert.False(t, e.PathExists(".git"), "expecting no .git folder (in new dir)")
// pulumi init won't create the folder here, but rather along side .git.
const dirName = workspace.BookkeepingDir
assert.False(t, e.PathExists(dirName), "expecting no %s folder", dirName)
e.RunCommand("pulumi", "init")
assert.False(t, e.PathExists(dirName), "expecting no %s folder. still.", dirName)
e.CWD = e.RootPath
assert.True(t, e.PathExists(dirName), "expecting %s folder to exist (next to .git)", dirName)
})
t.Run("DefaultRepositoryInfo", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer func() {
if !t.Failed() {
e.DeleteEnvironment()
}
}()
e.RunCommand("git", "init")
e.RunCommand("pulumi", "init")
// Defaults
repo := integration.GetRepository(e)
testRootName := path.Base(e.RootPath)
assert.Equal(t, os.Getenv("USER"), repo.Owner)
assert.Equal(t, testRootName, repo.Name)
})
t.Run("ReadRemoteInfo", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer func() {
if !t.Failed() {
e.DeleteEnvironment()
}
}()
e.RunCommand("git", "init")
e.RunCommand("git", "remote", "add", "not-origin", "git@github.com:moolumi/pasture.git")
e.RunCommand("git", "remote", "add", "origin", "git@github.com:pulumi/pulumi-cloud.git")
e.RunCommand("pulumi", "init")
// We pick up the settings from "origin", not any other remote name.
repo := integration.GetRepository(e)
assert.Equal(t, "pulumi", repo.Owner)
assert.Equal(t, "pulumi-cloud", repo.Name)
})
}