Skip to content

Commit

Permalink
Fix backupTarget (#9476)
Browse files Browse the repository at this point in the history
* Fix backupTarget

src and dst arguments to bucket.Copy were the wrong way round.

* Add test

* Add to CHANGELOG
  • Loading branch information
Frassle committed Apr 26, 2022
1 parent 77eb530 commit c8a3688
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@
[#9443](https://github.com/pulumi/pulumi/pull/9443)

- [auto/go] Fixed a race condition that could cause `Preview` to fail with "failed to get preview summary".
[#9467](https://github.com/pulumi/pulumi/pull/9467)
[#9467](https://github.com/pulumi/pulumi/pull/9467)

- [backend/filestate] - Fix a bug creating `stack.json.bak` files.
[#9476](https://github.com/pulumi/pulumi/pull/9476)
44 changes: 44 additions & 0 deletions pkg/backend/filestate/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,47 @@ func TestCancel(t *testing.T) {
err = lb.checkForLock(ctx, aStackRef)
assert.NoError(t, err)
}

func TestRemoveMakesBackups(t *testing.T) {
t.Parallel()

// Login to a temp dir filestate backend
tmpDir, err := ioutil.TempDir("", "filestatebackend")
assert.NoError(t, err)
b, err := New(cmdutil.Diag(), "file://"+filepath.ToSlash(tmpDir))
assert.NoError(t, err)
ctx := context.Background()

// Grab the bucket interface to test with
lb, ok := b.(*localBackend)
assert.True(t, ok)
assert.NotNil(t, lb)

// Check that creating a new stack doesn't make a backup file
aStackRef, err := b.ParseStackReference("a")
assert.NoError(t, err)
aStack, err := b.CreateStack(ctx, aStackRef, nil)
assert.NoError(t, err)
assert.NotNil(t, aStack)

// Check the stack file now exists, but the backup file doesn't
stackFileExists, err := lb.bucket.Exists(ctx, lb.stackPath(aStackRef.Name()))
assert.NoError(t, err)
assert.True(t, stackFileExists)
backupFileExists, err := lb.bucket.Exists(ctx, lb.stackPath(aStackRef.Name())+".bak")
assert.NoError(t, err)
assert.False(t, backupFileExists)

// Now remove the stack
removed, err := b.RemoveStack(ctx, aStack, false)
assert.NoError(t, err)
assert.False(t, removed)

// Check the stack file is now gone, but the backup file exists
stackFileExists, err = lb.bucket.Exists(ctx, lb.stackPath(aStackRef.Name()))
assert.NoError(t, err)
assert.False(t, stackFileExists)
backupFileExists, err = lb.bucket.Exists(ctx, lb.stackPath(aStackRef.Name())+".bak")
assert.NoError(t, err)
assert.True(t, backupFileExists)
}
2 changes: 1 addition & 1 deletion pkg/backend/filestate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func backupTarget(bucket Bucket, file string, keepOriginal bool) string {
contract.Require(file != "", "file")
bck := file + ".bak"

err := bucket.Copy(context.TODO(), file, bck, nil)
err := bucket.Copy(context.TODO(), bck, file, nil)
if err != nil {
logging.V(5).Infof("error copying %s to %s: %s", file, bck, err)
}
Expand Down

0 comments on commit c8a3688

Please sign in to comment.