From 0a0eece15ce313cf6b5040594ac42ea4eb67d5b9 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 16 Oct 2018 20:47:23 -0400 Subject: [PATCH 1/3] add backend cli options after configuration The cli should override the config --- backend/local/cli.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/backend/local/cli.go b/backend/local/cli.go index f0221b7bd..3385e6639 100644 --- a/backend/local/cli.go +++ b/backend/local/cli.go @@ -14,10 +14,16 @@ func (b *Local) CLIInit(opts *backend.CLIOpts) error { b.OpValidation = opts.Validation b.RunningInAutomation = opts.RunningInAutomation - // Only configure state paths if we didn't do so via the configure func. - if b.StatePath == "" { + // configure any new cli options + if opts.StatePath != "" { b.StatePath = opts.StatePath + } + + if opts.StateOutPath != "" { b.StateOutPath = opts.StateOutPath + } + + if opts.StateBackupPath != "" { b.StateBackupPath = opts.StateBackupPath } From d707049f724bc6f6e44ef3ae5f9899a1c8abf9b4 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 16 Oct 2018 20:50:09 -0400 Subject: [PATCH 2/3] don't make a backup of a nil state This makes sure we don't create a backup of an intermediate state if the first read state was empty. --- states/statemgr/filesystem.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/states/statemgr/filesystem.go b/states/statemgr/filesystem.go index 51cc31037..c9011162e 100644 --- a/states/statemgr/filesystem.go +++ b/states/statemgr/filesystem.go @@ -240,6 +240,12 @@ func (s *Filesystem) RefreshState() error { } f, err := statefile.Read(reader) + + // nothing to backup if there's no initial state + if f == nil { + s.writtenBackup = true + } + // if there's no state we just assign the nil return value if err != nil && err != statefile.ErrNoState { return err From fe9ed37dfc604be0fefa872ef5d5dc9ab3898dd7 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 16 Oct 2018 20:51:47 -0400 Subject: [PATCH 3/3] minor fixes for command Apply tests --- command/apply_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/command/apply_test.go b/command/apply_test.go index 8195de61c..2e35c749f 100644 --- a/command/apply_test.go +++ b/command/apply_test.go @@ -585,8 +585,9 @@ func TestApply_plan_backup(t *testing.T) { if err != nil { t.Fatal(err) } + args := []string{ - "-state-out", statePath, + "-state", statePath, "-backup", backupPath, planPath, } @@ -964,7 +965,7 @@ func TestApply_state(t *testing.T) { Name: "foo", }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance), &states.ResourceInstanceObjectSrc{ - AttrsJSON: []byte(`{"ami":"bar"}`), + AttrsJSON: []byte(`{"ami":"foo"}`), Status: states.ObjectReady, }, addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance), @@ -1006,7 +1007,7 @@ func TestApply_state(t *testing.T) { actual := p.PlanResourceChangeRequest.PriorState expected := cty.ObjectVal(map[string]cty.Value{ "id": cty.NullVal(cty.String), - "ami": cty.StringVal("bar"), + "ami": cty.StringVal("foo"), }) if !expected.RawEquals(actual) { t.Fatalf("wrong prior state during plan\ngot: %#v\nwant: %#v", actual, expected) @@ -1015,9 +1016,9 @@ func TestApply_state(t *testing.T) { actual = p.ApplyResourceChangeRequest.PriorState expected = cty.ObjectVal(map[string]cty.Value{ "id": cty.NullVal(cty.String), - "ami": cty.StringVal("bar"), + "ami": cty.StringVal("foo"), }) - if actual != expected { + if !expected.RawEquals(actual) { t.Fatalf("wrong prior state during apply\ngot: %#v\nwant: %#v", actual, expected) }