Skip to content

Commit

Permalink
Refactor etcd logic so we can run the master etcd
Browse files Browse the repository at this point in the history
  • Loading branch information
smarterclayton committed Jul 31, 2017
1 parent e8ca110 commit 94e95c3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 44 deletions.
91 changes: 48 additions & 43 deletions test/util/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"flag"
"os"
goruntime "runtime"
"strings"
"testing"

Expand Down Expand Up @@ -124,54 +123,42 @@ func (s EtcdTestServer) DumpEtcdOnFailure(t *testing.T) {
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:]
DumpEtcdOnFailure(t, s.Client, s.V3Client)
}

if s.Client != nil {
keyClient := etcdclient.NewKeysAPI(s.Client)
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 {
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
}
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+".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
}
return
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
}

client := s.V3Client
r, err := client.KV.Get(context.Background(), "\x00", etcdclientv3.WithFromKey())
func dumpEtcd3(t *testing.T, name string, etcd3 *etcdclientv3.Client) error {
r, err := etcd3.KV.Get(context.Background(), "\x00", etcdclientv3.WithFromKey())
if err != nil {
t.Logf("error reading all keys: %v", err)
return
return err
}
dumpFile, err := os.OpenFile(GetBaseDir()+"/etcd-dump-"+name+".json", os.O_WRONLY|os.O_CREATE, 0444)
dumpFile, err := os.OpenFile(GetBaseDir()+"/etcd-dump-"+name+"-v3.json", os.O_WRONLY|os.O_CREATE, 0444)
if err != nil {
t.Logf("error writing etcd dump: %v", err)
return
return err
}
defer dumpFile.Close()
w := json.NewEncoder(dumpFile)
Expand All @@ -186,8 +173,26 @@ func (s EtcdTestServer) DumpEtcdOnFailure(t *testing.T) {
result.Value = v.Value
result.CreateRevision, result.ModRevision = v.CreateRevision, v.ModRevision
if err := w.Encode(result); err != nil {
t.Logf("error writing etcd dump: %v", err)
return
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)
}
}
}
36 changes: 35 additions & 1 deletion test/util/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import (
"net/url"
"os"
"path"
"sync"
"testing"
"time"

"github.com/golang/glog"
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"

etcdclient "github.com/coreos/etcd/client"
etcdclientv3 "github.com/coreos/etcd/clientv3"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
knet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -24,6 +29,8 @@ import (
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/server/admin"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
"github.com/openshift/origin/pkg/cmd/server/etcd"
kubernetes "github.com/openshift/origin/pkg/cmd/server/kubernetes/node"
"github.com/openshift/origin/pkg/cmd/server/start"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
Expand Down Expand Up @@ -366,6 +373,33 @@ func StartTestAllInOne() (*configapi.MasterConfig, *configapi.NodeConfig, string
return master, node, adminKubeConfigFile, err
}

func MasterEtcdClients(config *configapi.MasterConfig) (etcdclient.Client, *etcdclientv3.Client, error) {
etcd2, err := etcd.MakeEtcdClient(config.EtcdClientInfo)
if err != nil {
return nil, nil, err
}
etcd3, err := etcd.MakeEtcdClientV3(config.EtcdClientInfo)
if err != nil {
return nil, nil, err
}
return etcd2, etcd3, nil
}

func CleanupMasterEtcd(t *testing.T, config *configapi.MasterConfig) {
etcd2, etcd3, err := MasterEtcdClients(config)
if err != nil {
t.Logf("Unable to get etcd client available for master: %v", err)
}
util.DumpEtcdOnFailure(t, etcd2, etcd3)
if config.EtcdConfig != nil {
if len(config.EtcdConfig.StorageDir) > 0 {
if err := os.RemoveAll(config.EtcdConfig.StorageDir); err != nil {
t.Logf("Unable to clean up the config storage directory %s: %v", config.EtcdConfig.StorageDir, err)
}
}
}
}

type TestOptions struct {
EnableControllers bool
}
Expand Down

0 comments on commit 94e95c3

Please sign in to comment.