-
Notifications
You must be signed in to change notification settings - Fork 6
/
mock.go
94 lines (79 loc) · 2.16 KB
/
mock.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
package kv
import (
"os"
"os/exec"
"time"
"github.com/cerana/cerana/acomm"
"github.com/cerana/cerana/internal/tests/common"
"github.com/cerana/cerana/provider"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// Mock is a mock KV provider
type Mock struct {
*KV
dir string
cmd *exec.Cmd
}
// NewMock starts up a kv backend server and instantiates a new kv.KV provider.
// The kv backend is started on the port provided as part of config.Address().
// Mock.Stop() should be called when testing is done in order to clean up.
func NewMock(config *provider.Config, tracker *acomm.Tracker) (*Mock, error) {
s := common.Suite{}
s.KVPrefix = "mock-kv-provider"
s.KVCmdMaker = common.ConsulMaker
s.SetupSuite()
v := viper.New()
flagset := pflag.NewFlagSet("mock-kv-provider", pflag.PanicOnError)
conf := NewConfig(flagset, v)
s.Require().NoError(flagset.Parse(nil))
v.Set("service_name", "mock-kv-provider")
v.Set("socket_dir", config.SocketDir())
v.Set("coordinator_url", config.CoordinatorURL())
v.Set("log_level", "fatal")
v.Set("address", s.KVURL)
s.Require().NoError(config.LoadConfig())
s.Require().NoError(config.SetupLogging())
kv, err := New(conf, tracker)
s.Require().NoError(err)
err = errorKVDown
for range [5]struct{}{} {
if !kv.kvDown() {
err = nil
break
}
time.Sleep(1 * time.Second)
}
if err != nil {
return nil, err
}
return &Mock{KV: kv, cmd: s.KVCmd, dir: s.KVDir}, nil
}
// Stop will stop the kv and remove the temporary directory used for it's data
func (m *Mock) Stop() {
_ = m.cmd.Process.Kill()
_ = m.cmd.Wait()
_ = os.RemoveAll(m.dir)
}
// Get will perform a Get operation directly on the kv store.
func (m *Mock) Get(key string) (Value, error) {
if m.kvDown() {
return Value{}, errorKVDown
}
kvV, err := m.KV.kv.Get(key)
return Value(kvV), err
}
// Set will perform a Set operation directly on the kv store.
func (m *Mock) Set(key, value string) error {
if m.kvDown() {
return errorKVDown
}
return m.KV.kv.Set(key, value)
}
// Clean will perform a recursive Delete operation directly on the kv store.
func (m *Mock) Clean(prefix string) error {
if m.kvDown() {
return nil
}
return m.KV.kv.Delete(prefix, true)
}