From e10a7917e6c85fbb0e20380101ce8a8f27e81c89 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 19 Mar 2018 17:09:53 -0400 Subject: [PATCH] add failing test for windows state locks Refreshing a locked state on windows could return nil if the read path was locked, no state was yet written, and the read path is the same as the write path. Add a test that locks then refreshes a newly initialized state struct. --- state/local_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/state/local_test.go b/state/local_test.go index 633356028..13dcf6eab 100644 --- a/state/local_test.go +++ b/state/local_test.go @@ -166,3 +166,42 @@ func testLocalState(t *testing.T) *LocalState { return ls } + +// Make sure we can refresh while the state is locked +func TestLocalState_refreshWhileLocked(t *testing.T) { + f, err := ioutil.TempFile("", "tf") + if err != nil { + t.Fatalf("err: %s", err) + } + + err = terraform.WriteState(TestStateInitial(), f) + f.Close() + if err != nil { + t.Fatalf("err: %s", err) + } + + s := &LocalState{Path: f.Name()} + defer os.Remove(s.Path) + + // lock first + info := NewLockInfo() + info.Operation = "test" + lockID, err := s.Lock(info) + if err != nil { + t.Fatal(err) + } + defer func() { + if err := s.Unlock(lockID); err != nil { + t.Fatal(err) + } + }() + + if err := s.RefreshState(); err != nil { + t.Fatal(err) + } + + readState := s.State() + if readState == nil || readState.Lineage == "" { + t.Fatal("missing state") + } +}