Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit bd65ddb

Browse files
committed
Add test for apply/refresh on locked state files
Verify that these operations fail when a state file is locked.
1 parent fb60b6f commit bd65ddb

3 files changed

Lines changed: 84 additions & 9 deletions

File tree

command/apply_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ import (
88
"net/http"
99
"net/url"
1010
"os"
11-
"os/exec"
1211
"path/filepath"
1312
"reflect"
1413
"strings"
1514
"sync"
16-
"syscall"
1715
"testing"
1816
"time"
1917

@@ -65,13 +63,11 @@ func TestApply(t *testing.T) {
6563
func TestApply_lockedState(t *testing.T) {
6664
statePath := testTempFile(t)
6765

68-
locker := exec.Command("go", "run", "testadata/statelocker.go", statePath)
69-
locker.Stderr = os.Stderr
70-
if err := locker.Start(); err != nil {
66+
unlock, err := testLockState(statePath)
67+
if err != nil {
7168
t.Fatal(err)
7269
}
73-
74-
defer locker.Process.Signal(syscall.SIGTERM)
70+
defer unlock()
7571

7672
p := testProvider()
7773
ui := new(cli.MockUi)
@@ -86,8 +82,13 @@ func TestApply_lockedState(t *testing.T) {
8682
"-state", statePath,
8783
testFixturePath("apply"),
8884
}
89-
if code := c.Run(args); code != 0 {
90-
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
85+
if code := c.Run(args); code == 0 {
86+
t.Fatal("expected error")
87+
}
88+
89+
output := ui.ErrorWriter.String()
90+
if !strings.Contains(output, "locked") {
91+
t.Fatal("command output does not look like a lock error:", output)
9192
}
9293
}
9394

command/command_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import (
66
"encoding/base64"
77
"encoding/json"
88
"flag"
9+
"fmt"
910
"io"
1011
"io/ioutil"
1112
"log"
1213
"net/http"
1314
"net/http/httptest"
1415
"os"
16+
"os/exec"
1517
"path/filepath"
1618
"strings"
19+
"syscall"
1720
"testing"
1821

1922
"github.com/hashicorp/go-getter"
@@ -529,3 +532,37 @@ func testRemoteState(t *testing.T, s *terraform.State, c int) (*terraform.Remote
529532

530533
return remote, srv
531534
}
535+
536+
// testlockState calls a separate process to the lock the state file at path.
537+
// deferFunc should be called in the caller to properly unlock the file.
538+
func testLockState(path string) (func(), error) {
539+
locker := exec.Command("go", "run", "testdata/statelocker.go", path)
540+
pr, pw, err := os.Pipe()
541+
if err != nil {
542+
return nil, err
543+
}
544+
defer pr.Close()
545+
defer pw.Close()
546+
locker.Stderr = pw
547+
locker.Stdout = pw
548+
549+
if err := locker.Start(); err != nil {
550+
return nil, err
551+
}
552+
deferFunc := func() {
553+
locker.Process.Signal(syscall.SIGTERM)
554+
locker.Wait()
555+
}
556+
557+
// wait for the process to lock
558+
buf := make([]byte, 1024)
559+
n, err := pr.Read(buf)
560+
if err != nil {
561+
return deferFunc, fmt.Errorf("read from statelocker returned: %s", err)
562+
}
563+
564+
if string(buf[:n]) != "LOCKED" {
565+
return deferFunc, fmt.Errorf("statelocker wrote", string(buf[:n]))
566+
}
567+
return deferFunc, nil
568+
}

command/refresh_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,43 @@ func TestRefresh(t *testing.T) {
5959
}
6060
}
6161

62+
func TestRefresh_lockedState(t *testing.T) {
63+
state := testState()
64+
statePath := testStateFile(t, state)
65+
66+
unlock, err := testLockState(statePath)
67+
if err != nil {
68+
t.Fatal(err)
69+
}
70+
defer unlock()
71+
72+
p := testProvider()
73+
ui := new(cli.MockUi)
74+
c := &RefreshCommand{
75+
Meta: Meta{
76+
ContextOpts: testCtxConfig(p),
77+
Ui: ui,
78+
},
79+
}
80+
81+
p.RefreshFn = nil
82+
p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
83+
84+
args := []string{
85+
"-state", statePath,
86+
testFixturePath("refresh"),
87+
}
88+
89+
if code := c.Run(args); code == 0 {
90+
t.Fatal("expected error")
91+
}
92+
93+
output := ui.ErrorWriter.String()
94+
if !strings.Contains(output, "locked") {
95+
t.Fatal("command output does not look like a lock error:", output)
96+
}
97+
}
98+
6299
func TestRefresh_badState(t *testing.T) {
63100
p := testProvider()
64101
ui := new(cli.MockUi)

0 commit comments

Comments
 (0)