/
backend_test.go
90 lines (69 loc) · 1.93 KB
/
backend_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package inmem
import (
"flag"
"os"
"testing"
"github.com/hashicorp/terraform/internal/backend"
statespkg "github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/states/remote"
_ "github.com/hashicorp/terraform/internal/logging"
)
func TestMain(m *testing.M) {
flag.Parse()
os.Exit(m.Run())
}
func TestBackend_impl(t *testing.T) {
var _ backend.Backend = new(Backend)
}
func TestBackendConfig(t *testing.T) {
defer Reset()
testID := "test_lock_id"
config := map[string]interface{}{
"lock_id": testID,
}
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)).(*Backend)
s, err := b.StateMgr(backend.DefaultStateName)
if err != nil {
t.Fatal(err)
}
c := s.(*remote.State).Client.(*RemoteClient)
if c.Name != backend.DefaultStateName {
t.Fatal("client name is not configured")
}
if err := locks.unlock(backend.DefaultStateName, testID); err != nil {
t.Fatalf("default state should have been locked: %s", err)
}
}
func TestBackend(t *testing.T) {
defer Reset()
b := backend.TestBackendConfig(t, New(), hcl.EmptyBody()).(*Backend)
backend.TestBackendStates(t, b)
}
func TestBackendLocked(t *testing.T) {
defer Reset()
b1 := backend.TestBackendConfig(t, New(), hcl.EmptyBody()).(*Backend)
b2 := backend.TestBackendConfig(t, New(), hcl.EmptyBody()).(*Backend)
backend.TestBackendStateLocks(t, b1, b2)
}
// use the this backen to test the remote.State implementation
func TestRemoteState(t *testing.T) {
defer Reset()
b := backend.TestBackendConfig(t, New(), hcl.EmptyBody())
workspace := "workspace"
// create a new workspace in this backend
s, err := b.StateMgr(workspace)
if err != nil {
t.Fatal(err)
}
// force overwriting the remote state
newState := statespkg.NewState()
if err := s.WriteState(newState); err != nil {
t.Fatal(err)
}
if err := s.PersistState(nil); err != nil {
t.Fatal(err)
}
if err := s.RefreshState(); err != nil {
t.Fatal(err)
}
}