-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
statemgr_fake.go
132 lines (105 loc) · 3.23 KB
/
statemgr_fake.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package statemgr
import (
"errors"
"sync"
"github.com/hashicorp/terraform/internal/states"
)
// NewFullFake returns a full state manager that really only supports transient
// snapshots. This is primarily intended for testing and is not suitable for
// general use.
//
// The persistent part of the interface is stubbed out as an in-memory store,
// and so its snapshots are effectively also transient.
//
// The given Transient implementation is used to implement the transient
// portion of the interface. If nil is given, NewTransientInMemory is
// automatically called to create an in-memory transient manager with no
// initial transient snapshot.
//
// If the given initial state is non-nil then a copy of it will be used as
// the initial persistent snapshot.
//
// The Locker portion of the returned manager uses a local mutex to simulate
// mutually-exclusive access to the fake persistent portion of the object.
func NewFullFake(t Transient, initial *states.State) Full {
if t == nil {
t = NewTransientInMemory(nil)
}
// The "persistent" part of our manager is actually just another in-memory
// transient used to fake a secondary storage layer.
fakeP := NewTransientInMemory(initial.DeepCopy())
return &fakeFull{
t: t,
fakeP: fakeP,
}
}
type fakeFull struct {
t Transient
fakeP Transient
lockLock sync.Mutex
locked bool
}
var _ Full = (*fakeFull)(nil)
func (m *fakeFull) State() *states.State {
return m.t.State()
}
func (m *fakeFull) WriteState(s *states.State) error {
return m.t.WriteState(s)
}
func (m *fakeFull) RefreshState() error {
return m.t.WriteState(m.fakeP.State())
}
func (m *fakeFull) PersistState() error {
return m.fakeP.WriteState(m.t.State())
}
func (m *fakeFull) Lock(info *LockInfo) (string, error) {
m.lockLock.Lock()
defer m.lockLock.Unlock()
if m.locked {
return "", &LockError{
Err: errors.New("fake state manager is locked"),
Info: info,
}
}
m.locked = true
return "placeholder", nil
}
func (m *fakeFull) Unlock(id string) error {
m.lockLock.Lock()
defer m.lockLock.Unlock()
if !m.locked {
return errors.New("fake state manager is not locked")
}
if id != "placeholder" {
return errors.New("wrong lock id for fake state manager")
}
m.locked = false
return nil
}
// NewUnlockErrorFull returns a state manager that is useful for testing errors
// (mostly Unlock errors) when used with the clistate.Locker interface. Lock()
// does not return an error because clistate.Locker Lock()s the state at the
// start of Unlock(), so Lock() must succeeded for Unlock() to get called.
func NewUnlockErrorFull(t Transient, initial *states.State) Full {
return &fakeErrorFull{}
}
type fakeErrorFull struct{}
var _ Full = (*fakeErrorFull)(nil)
func (m *fakeErrorFull) State() *states.State {
return nil
}
func (m *fakeErrorFull) WriteState(s *states.State) error {
return errors.New("fake state manager error")
}
func (m *fakeErrorFull) RefreshState() error {
return errors.New("fake state manager error")
}
func (m *fakeErrorFull) PersistState() error {
return errors.New("fake state manager error")
}
func (m *fakeErrorFull) Lock(info *LockInfo) (string, error) {
return "placeholder", nil
}
func (m *fakeErrorFull) Unlock(id string) error {
return errors.New("fake state manager error")
}