1 Star 0 Fork 0

xingyp/cn-infra

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
embeded_etcd.go 1.70 KB
一键复制 编辑 原始数据 按行查看 历史
Ondrej Fabry 提交于 7年前 . Rename etcdv3 to etcd
package mocks
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"testing"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/embed"
"github.com/coreos/etcd/etcdserver/api/v3client"
"github.com/ligato/cn-infra/logging/logrus"
)
const etcdStartTimeout = 30
// Embedded ETCD instance with tmp directory for serialized key&vals and etcd client.
type Embedded struct {
tmpDir string
ETCD *embed.Etcd
client *clientv3.Client
}
// Start starts embedded ETCD.
func (embd *Embedded) Start(t *testing.T) {
dir, err := ioutil.TempDir("", "ETCD")
if err != nil {
t.Error(err)
t.FailNow()
}
cfg := embed.NewConfig()
cfg.Dir = dir
lpurl, _ := url.Parse("http://localhost:0")
lcurl, _ := url.Parse("http://localhost:0")
cfg.LPUrls = []url.URL{*lpurl}
cfg.LCUrls = []url.URL{*lcurl}
embd.ETCD, err = embed.StartEtcd(cfg)
if err != nil {
t.Error(err)
t.FailNow()
}
select {
case <-embd.ETCD.Server.ReadyNotify():
logrus.DefaultLogger().Debug("Server is ready!")
case <-time.After(etcdStartTimeout * time.Second):
embd.ETCD.Server.Stop() // trigger a shutdown
t.Error("Server took too long to start!")
t.FailNow()
}
embd.client = v3client.New(embd.ETCD.Server)
}
// Stop stops the embedded ETCD & cleanups the tmp dir.
func (embd *Embedded) Stop() {
embd.ETCD.Close()
os.RemoveAll(embd.tmpDir)
}
// CleanDs deletes all stored key-value pairs.
func (embd *Embedded) CleanDs() {
if embd.client != nil {
resp, err := embd.client.Delete(context.Background(), "", clientv3.WithPrefix())
if err != nil {
panic(err)
}
fmt.Printf("resp: %+v\n", resp)
}
}
// Client is a getter for embedded ETCD client.
func (embd *Embedded) Client() *clientv3.Client {
return embd.client
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xingyp/cn-infra.git
git@gitee.com:xingyp/cn-infra.git
xingyp
cn-infra
cn-infra
v1.6.0

搜索帮助