forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
etcd.go
72 lines (65 loc) · 1.64 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
package server
import (
"encoding/json"
"flag"
"os"
"strings"
"testing"
etcdclientv3 "github.com/coreos/etcd/clientv3"
"github.com/coreos/pkg/capnslog"
"golang.org/x/net/context"
"k8s.io/kubernetes/pkg/capabilities"
"github.com/openshift/origin/test/util"
)
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))
}
}
func dumpEtcd3(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(util.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, etcd3 *etcdclientv3.Client) {
if !t.Failed() {
return
}
name := t.Name()
name = strings.Replace(name, "/", "_", -1)
if etcd3 != nil {
if err := dumpEtcd3(name, etcd3); err != nil {
t.Logf("Unable to dump etcd3: %v", err)
}
}
}