pulumi/tests/stack_test.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

134 lines
4.1 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package tests
import (
"io/ioutil"
"os"
"path"
"testing"
"github.com/pulumi/pulumi/pkg/testing/integration"
"github.com/stretchr/testify/assert"
ptesting "github.com/pulumi/pulumi/pkg/testing"
)
func createBasicPulumiRepo(e *ptesting.Environment) {
e.RunCommand("git", "init")
e.RunCommand("pulumi", "init")
contents := "name: pulumi-test\ndescription: a test\nruntime: nodejs\n"
pulumiFile := path.Join(e.CWD, "Pulumi.yaml")
err := ioutil.WriteFile(pulumiFile, []byte(contents), os.ModePerm)
assert.NoError(e, err, "writing Pulumi.yaml file")
}
func TestStackWithoutInit(t *testing.T) {
t.Run("SanityTest", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer e.DeleteEnvironment()
stdout, stderr := e.RunCommandExpectError("pulumi", "stack", "ls")
assert.Empty(t, stdout, "expected nothing to be written to stdout")
assert.Contains(t, stderr, "error: no repository")
})
}
func TestStackCommands(t *testing.T) {
// stack init, stack ls, stack rm, stack ls
t.Run("SanityTest", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer e.DeleteEnvironment()
createBasicPulumiRepo(e)
e.RunCommand("pulumi", "stack", "init", "foo")
stacks, current := integration.GetStacks(e)
assert.Equal(t, 1, len(stacks))
assert.NotNil(t, current)
assert.Equal(t, "foo", *current)
assert.Contains(t, stacks, "foo")
e.RunCommand("pulumi", "stack", "rm", "foo", "--yes")
stacks, _ = integration.GetStacks(e)
assert.Equal(t, 0, len(stacks))
})
t.Run("Errors", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer e.DeleteEnvironment()
// Have not ran `pulumi init` yet.
out, err := e.RunCommandExpectError("pulumi", "stack", "ls")
assert.Empty(t, out)
assert.Contains(t, err, "error: no repository")
// Create git repo so the .pulumi folder doesn't wind up on a parent directory.
// `pulumi stack ls` now fails because it cannot locate a Pulumi.yaml project file.
e.RunCommand("git", "init")
e.RunCommand("pulumi", "init")
out, err = e.RunCommandExpectError("pulumi", "stack", "ls")
assert.Empty(t, out)
assert.Contains(t, err, "no Pulumi project file found, are you missing a Pulumi.yaml file?")
})
t.Run("StackSelect", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer e.DeleteEnvironment()
createBasicPulumiRepo(e)
e.RunCommand("pulumi", "stack", "init", "blighttown")
e.RunCommand("pulumi", "stack", "init", "majula")
e.RunCommand("pulumi", "stack", "init", "lothric")
// Last one created is always selected.
_, current := integration.GetStacks(e)
assert.Equal(t, "lothric", *current)
// Select works
e.RunCommand("pulumi", "stack", "select", "blighttown")
_, current = integration.GetStacks(e)
assert.Equal(t, "blighttown", *current)
// Error
out, err := e.RunCommandExpectError("pulumi", "stack", "select", "anor-londo")
assert.Empty(t, out)
assert.Contains(t, err, ".pulumi/stacks/pulumi-test/anor-londo.json: no such file or directory")
})
t.Run("StackRm", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer e.DeleteEnvironment()
createBasicPulumiRepo(e)
e.RunCommand("pulumi", "stack", "init", "blighttown")
e.RunCommand("pulumi", "stack", "init", "majula")
e.RunCommand("pulumi", "stack", "init", "lothric")
stacks, _ := integration.GetStacks(e)
assert.Equal(t, 3, len(stacks))
e.RunCommand("pulumi", "stack", "rm", "majula", "--yes")
stacks, _ = integration.GetStacks(e)
assert.Equal(t, 2, len(stacks))
assert.Contains(t, stacks, "blighttown")
assert.Contains(t, stacks, "lothric")
e.RunCommand("pulumi", "stack", "rm", "lothric", "--yes")
stacks, _ = integration.GetStacks(e)
assert.Equal(t, 1, len(stacks))
assert.Contains(t, stacks, "blighttown")
e.RunCommand("pulumi", "stack", "rm", "blighttown", "--yes")
stacks, _ = integration.GetStacks(e)
assert.Equal(t, 0, len(stacks))
// Error
out, err := e.RunCommandExpectError("pulumi", "stack", "rm", "anor-londo", "--yes")
assert.Empty(t, out)
assert.Contains(t, err, ".pulumi/stacks/pulumi-test/anor-londo.json: no such file or directory")
})
}