-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
config_entries_testing.go
55 lines (43 loc) · 1.08 KB
/
config_entries_testing.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
package consul
import (
"sync"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/go-hclog"
)
var _ ConfigAPI = (*MockConfigsAPI)(nil)
type MockConfigsAPI struct {
logger hclog.Logger
lock sync.Mutex
state struct {
error error
entries map[string]api.ConfigEntry
}
}
func NewMockConfigsAPI(l hclog.Logger) *MockConfigsAPI {
return &MockConfigsAPI{
logger: l.Named("mock_consul"),
state: struct {
error error
entries map[string]api.ConfigEntry
}{entries: make(map[string]api.ConfigEntry)},
}
}
// Set is a mock of ConfigAPI.Set
func (m *MockConfigsAPI) Set(entry api.ConfigEntry, w *api.WriteOptions) (bool, *api.WriteMeta, error) {
m.lock.Lock()
defer m.lock.Unlock()
if m.state.error != nil {
return false, nil, m.state.error
}
m.state.entries[entry.GetName()] = entry
return true, &api.WriteMeta{
RequestTime: 1,
}, nil
}
// SetError is a helper method for configuring an error that will be returned
// on future calls to mocked methods.
func (m *MockConfigsAPI) SetError(err error) {
m.lock.Lock()
defer m.lock.Unlock()
m.state.error = err
}