2015-05-05 20:08:42 +00:00
|
|
|
package physical
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
"github.com/hashicorp/vault/helper/logformat"
|
|
|
|
log "github.com/mgutz/logxi/v1"
|
|
|
|
|
2015-05-05 20:08:42 +00:00
|
|
|
"github.com/samuel/go-zookeeper/zk"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestZookeeperBackend(t *testing.T) {
|
|
|
|
addr := os.Getenv("ZOOKEEPER_ADDR")
|
|
|
|
if addr == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
client, _, err := zk.Connect([]string{addr}, time.Second)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
randPath := fmt.Sprintf("/vault-%d", time.Now().Unix())
|
|
|
|
acl := zk.WorldACL(zk.PermAll)
|
|
|
|
_, err = client.Create(randPath, []byte("hi"), int32(0), acl)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2016-10-04 13:28:48 +00:00
|
|
|
client.Delete(randPath+"/foo/nested1/nested2/nested3", -1)
|
|
|
|
client.Delete(randPath+"/foo/nested1/nested2", -1)
|
|
|
|
client.Delete(randPath+"/foo/nested1", -1)
|
2015-07-13 09:33:23 +00:00
|
|
|
client.Delete(randPath+"/foo/bar/baz", -1)
|
|
|
|
client.Delete(randPath+"/foo/bar", -1)
|
|
|
|
client.Delete(randPath+"/foo", -1)
|
2015-05-05 20:08:42 +00:00
|
|
|
client.Delete(randPath, -1)
|
2015-05-26 04:12:16 +00:00
|
|
|
client.Close()
|
2015-05-05 20:08:42 +00:00
|
|
|
}()
|
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
|
|
|
|
2016-04-26 03:10:32 +00:00
|
|
|
b, err := NewBackend("zookeeper", logger, map[string]string{
|
2015-05-05 20:08:42 +00:00
|
|
|
"address": addr + "," + addr,
|
|
|
|
"path": randPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testBackend(t, b)
|
|
|
|
testBackend_ListPrefix(t, b)
|
|
|
|
}
|
2015-05-26 04:12:16 +00:00
|
|
|
|
|
|
|
func TestZookeeperHABackend(t *testing.T) {
|
|
|
|
addr := os.Getenv("ZOOKEEPER_ADDR")
|
|
|
|
if addr == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
client, _, err := zk.Connect([]string{addr}, time.Second)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
randPath := fmt.Sprintf("/vault-ha-%d", time.Now().Unix())
|
|
|
|
acl := zk.WorldACL(zk.PermAll)
|
|
|
|
_, err = client.Create(randPath, []byte("hi"), int32(0), acl)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2015-07-13 09:33:23 +00:00
|
|
|
client.Delete(randPath+"/foo", -1)
|
2015-05-26 04:12:16 +00:00
|
|
|
client.Delete(randPath, -1)
|
|
|
|
client.Close()
|
|
|
|
}()
|
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
|
|
|
|
2016-04-26 03:10:32 +00:00
|
|
|
b, err := NewBackend("zookeeper", logger, map[string]string{
|
2015-05-26 04:12:16 +00:00
|
|
|
"address": addr + "," + addr,
|
|
|
|
"path": randPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ha, ok := b.(HABackend)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("zookeeper does not implement HABackend")
|
|
|
|
}
|
|
|
|
testHABackend(t, ha, ha)
|
|
|
|
}
|