forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
etcd.go
139 lines (117 loc) · 3.21 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
package util
import (
"encoding/json"
"flag"
"os"
goruntime "runtime"
"strings"
"testing"
"golang.org/x/net/context"
"github.com/coreos/pkg/capnslog"
etcdclient "github.com/coreos/etcd/client"
etcdclientv3 "github.com/coreos/etcd/clientv3"
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", "5")
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
// RequireEtcd verifies if the etcd is running and accessible for testing
func RequireEtcd(t *testing.T) *etcdtest.EtcdTestServer {
s := etcdtest.NewUnsecuredEtcdTestClientServer(t)
url = s.Client.Endpoints()[0]
return 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 DumpEtcdOnFailure(t *testing.T) {
if !t.Failed() {
return
}
pc := make([]uintptr, 10)
goruntime.Callers(2, pc)
f := goruntime.FuncForPC(pc[0])
last := strings.LastIndex(f.Name(), "Test")
if last == -1 {
last = 0
}
name := f.Name()[last:]
client := NewEtcdClient()
keyClient := etcdclient.NewKeysAPI(client)
response, err := keyClient.Get(context.Background(), "/", &etcdclient.GetOptions{Recursive: true, Sort: true})
if err != nil {
t.Logf("error dumping etcd: %v", err)
return
}
jsonResponse, err := json.Marshal(response.Node)
if err != nil {
t.Logf("error encoding etcd dump: %v", err)
return
}
t.Logf("dumping etcd to %q", GetBaseDir()+"/etcd-dump-"+name+".json")
dumpFile, err := os.OpenFile(GetBaseDir()+"/etcd-dump-"+name+".json", os.O_WRONLY|os.O_CREATE, 0444)
if err != nil {
t.Logf("error writing etcd dump: %v", err)
return
}
defer dumpFile.Close()
_, err = dumpFile.Write(jsonResponse)
if err != nil {
t.Logf("error writing etcd dump: %v", err)
return
}
}