pulumi/tests/history_test.go
Joe Duffy 01d0d64e84
Correctly rename stack files during a rename (#5812)
* Correctly rename stack files during a rename

This fixes pulumi/pulumi#4463, by renaming a stack's configuration
file based on its stack-part, and ignoring the owner-part. Our
workspace system doesn't recognize configuration files with fully
qualified names. That, by the way, causes problems if we have
multiple stacks in different organizations that share a stack-part.

The fix here is simple: propagate the new StackReference from the
Rename operation and rely on the backend's normalization to a
simple name, and then use that the same way we are using a
StackReference to determine the path for the origin stack.

An alternative fix is to recognize fully qualified config files,
however, there's a fair bit of cleanup we will be doing as part of
https://github.com/pulumi/pulumi/issues/2522 and
https://github.com/pulumi/pulumi/issues/4605, so figured it is best
to make this work the way the system expects first, and revisit it
as part of those overall workstreams. I also suspect we may want to
consider changing the default behavior here as part of
https://github.com/pulumi/pulumi/issues/5731.

Tests TBD; need some advice on how best to test this since it
only happens with our HTTP state backend -- all integration tests
appear to use the local filestate backend at the moment.

* Add a changelog entry for bug fix

* Add some stack rename tests

* Fix a typo

* Address CR feedback

* Make some logic clearer

Use "parsedName" instead of "qn", add a comment explaining why
we're doing this, and also explicitly ignore the error rather
than implicitly doing so with _.
2020-12-01 16:55:48 -08:00

90 lines
3.5 KiB
Go

// Copyright 2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tests
import (
"testing"
"github.com/pulumi/pulumi/pkg/v2/testing/integration"
ptesting "github.com/pulumi/pulumi/sdk/v2/go/common/testing"
"github.com/stretchr/testify/assert"
)
// deleteIfNotFailed deletes the files in the testing environment if the testcase has
// not failed. (Otherwise they are left to aid debugging.)
func deleteIfNotFailed(e *ptesting.Environment) {
if !e.T.Failed() {
e.DeleteEnvironment()
}
}
// assertHasNoHistory runs `pulumi history` and confirms an error that the stack has not
// ever been updated.
func assertHasNoHistory(e *ptesting.Environment) {
// NOTE: pulumi returns with exit code 0 in this scenario.
out, err := e.RunCommand("pulumi", "history")
assert.Equal(e.T, "", err)
assert.Equal(e.T, "Stack has never been updated\n", out)
}
func TestHistoryCommand(t *testing.T) {
// We fail if no stack is selected.
t.Run("NoStackSelected", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer deleteIfNotFailed(e)
integration.CreateBasicPulumiRepo(e)
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
out, err := e.RunCommandExpectError("pulumi", "history")
assert.Equal(t, "", out)
assert.Contains(t, err, "error: no stack selected")
})
// We don't display any history for a stack that has never been updated.
t.Run("NoUpdates", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer deleteIfNotFailed(e)
integration.CreateBasicPulumiRepo(e)
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
e.RunCommand("pulumi", "stack", "init", "no-updates-test")
assertHasNoHistory(e)
})
// The "history" command uses the currently selected stack.
t.Run("CurrentlySelectedStack", func(t *testing.T) {
e := ptesting.NewEnvironment(t)
defer deleteIfNotFailed(e)
integration.CreateBasicPulumiRepo(e)
e.ImportDirectory("integration/stack_dependencies")
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
e.ImportDirectory("integration/stack_outputs")
e.RunCommand("pulumi", "stack", "init", "stack-without-updates")
e.RunCommand("pulumi", "stack", "init", "history-test")
e.RunCommand("yarn", "install")
e.RunCommand("yarn", "link", "@pulumi/pulumi")
// Update the history-test stack.
e.RunCommand("pulumi", "up", "--non-interactive", "--yes", "--skip-preview", "-m", "this is an updated stack")
// Confirm we see the update message in thie history output.
out, err := e.RunCommand("pulumi", "history")
assert.Equal(t, "", err)
assert.Contains(t, out, "this is an updated stack")
// Change stack and confirm the history command honors the selected stack.
e.RunCommand("pulumi", "stack", "select", "stack-without-updates")
assertHasNoHistory(e)
// Change stack back, and confirm still has history.
e.RunCommand("pulumi", "stack", "select", "history-test")
out, err = e.RunCommand("pulumi", "history")
assert.Equal(t, "", err)
assert.Contains(t, out, "this is an updated stack")
})
}