forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
etcd.go
198 lines (174 loc) · 4.83 KB
/
etcd.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package util
import (
"encoding/json"
"flag"
"os"
"strings"
"testing"
etcdclient "github.com/coreos/etcd/client"
etcdclientv3 "github.com/coreos/etcd/clientv3"
"github.com/coreos/pkg/capnslog"
"golang.org/x/net/context"
etcdtest "k8s.io/apiserver/pkg/storage/etcd/testing"
"k8s.io/apiserver/pkg/storage/storagebackend"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/capabilities"
serveretcd "github.com/openshift/origin/pkg/cmd/server/etcd"
)
func init() {
capabilities.SetForTests(capabilities.Capabilities{
AllowPrivileged: true,
})
flag.Set("v", "4")
if len(os.Getenv("OS_TEST_VERBOSE_ETCD")) > 0 {
capnslog.SetGlobalLogLevel(capnslog.DEBUG)
capnslog.SetFormatter(capnslog.NewGlogFormatter(os.Stderr))
} else {
capnslog.SetGlobalLogLevel(capnslog.INFO)
capnslog.SetFormatter(capnslog.NewGlogFormatter(os.Stderr))
}
}
// url is the url for the launched etcd server
var url string
type EtcdTestServer struct {
*etcdtest.EtcdTestServer
}
// RequireEtcd verifies if the etcd is running and accessible for testing
func RequireEtcd(t *testing.T) EtcdTestServer {
s, _ := etcdtest.NewUnsecuredEtcd3TestClientServer(t, kapi.Scheme)
t.Logf("endpoints: %v", s.V3Client.Endpoints())
url = s.V3Client.Endpoints()[0]
return EtcdTestServer{s}
}
// RequireEtcd verifies if the etcd is running and accessible for testing
// TODO: remove use of etcd2 specific apis in 1.6
func RequireEtcd2(t *testing.T) EtcdTestServer {
s := etcdtest.NewUnsecuredEtcdTestClientServer(t)
url = s.Client.Endpoints()[0]
return EtcdTestServer{s}
}
func RequireEtcd3(t *testing.T) (*etcdtest.EtcdTestServer, *storagebackend.Config) {
s, c := etcdtest.NewUnsecuredEtcd3TestClientServer(t, kapi.Scheme)
url = s.V3Client.Endpoints()[0]
return s, c
}
func NewEtcdClient() etcdclient.Client {
client, _ := MakeNewEtcdClient()
return client
}
func MakeNewEtcdClient() (etcdclient.Client, error) {
etcdServers := []string{GetEtcdURL()}
cfg := etcdclient.Config{
Endpoints: etcdServers,
}
client, err := etcdclient.New(cfg)
if err != nil {
return nil, err
}
return client, serveretcd.TestEtcdClient(client)
}
func NewEtcd3Client() *etcdclientv3.Client {
client, _ := MakeNewEtcd3Client()
return client
}
func MakeNewEtcd3Client() (*etcdclientv3.Client, error) {
etcdServers := []string{GetEtcdURL()}
cfg := etcdclientv3.Config{
Endpoints: etcdServers,
}
client, err := etcdclientv3.New(cfg)
if err != nil {
return nil, err
}
return client, serveretcd.TestEtcdClientV3(client)
}
func GetEtcdURL() string {
if len(url) == 0 {
panic("can't invoke GetEtcdURL prior to calling RequireEtcd")
}
return url
}
func (s EtcdTestServer) DumpEtcdOnFailure(t *testing.T) {
defer func() {
s.Terminate(t)
dir := s.DataDir
if len(dir) > 0 {
t.Logf("Removing etcd dir %s", dir)
if err := os.RemoveAll(dir); err != nil {
t.Errorf("Unable to remove contents of etcd data directory %s: %v", dir, err)
}
} else {
t.Logf("No data directory, nothing to clean up")
}
}()
if !t.Failed() {
return
}
DumpEtcdOnFailure(t, s.Client, s.V3Client)
}
func dumpEtcd2(t *testing.T, name string, etcd2 etcdclient.Client) error {
keyClient := etcdclient.NewKeysAPI(etcd2)
response, err := keyClient.Get(context.Background(), "/", &etcdclient.GetOptions{Recursive: true, Sort: true})
if err != nil {
return err
}
jsonResponse, err := json.Marshal(response.Node)
if err != nil {
return err
}
t.Logf("dumping etcd to %q", GetBaseDir()+"/etcd-dump-"+name+"-v2.json")
dumpFile, err := os.OpenFile(GetBaseDir()+"/etcd-dump-"+name+"-v2.json", os.O_WRONLY|os.O_CREATE, 0444)
if err != nil {
return err
}
defer dumpFile.Close()
_, err = dumpFile.Write(jsonResponse)
if err != nil {
return err
}
return nil
}
func dumpEtcd3(t *testing.T, name string, etcd3 *etcdclientv3.Client) error {
r, err := etcd3.KV.Get(context.Background(), "\x00", etcdclientv3.WithFromKey())
if err != nil {
return err
}
dumpFile, err := os.OpenFile(GetBaseDir()+"/etcd-dump-"+name+"-v3.json", os.O_WRONLY|os.O_CREATE, 0444)
if err != nil {
return err
}
defer dumpFile.Close()
w := json.NewEncoder(dumpFile)
result := struct {
Key string
Value []byte
CreateRevision int64
ModRevision int64
}{}
for _, v := range r.Kvs {
result.Key = string(v.Key)
result.Value = v.Value
result.CreateRevision, result.ModRevision = v.CreateRevision, v.ModRevision
if err := w.Encode(result); err != nil {
return err
}
}
return nil
}
func DumpEtcdOnFailure(t *testing.T, etcd2 etcdclient.Client, etcd3 *etcdclientv3.Client) {
if !t.Failed() {
return
}
name := t.Name()
name = strings.Replace(name, "/", "_", -1)
if etcd2 != nil {
if err := dumpEtcd2(t, name, etcd2); err != nil {
t.Logf("Unable to dump etcd2: %v", err)
}
}
if etcd3 != nil {
if err := dumpEtcd3(t, name, etcd3); err != nil {
t.Logf("Unable to dump etcd3: %v", err)
}
}
}