open-vault/physical/zookeeper/zookeeper_test.go

105 lines
2.2 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package zookeeper
import (
"fmt"
"os"
"testing"
"time"
"github.com/go-zookeeper/zk"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/helper/logging"
"github.com/hashicorp/vault/sdk/physical"
)
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() {
client.Delete(randPath+"/foo/nested1/nested2/nested3", -1)
client.Delete(randPath+"/foo/nested1/nested2", -1)
client.Delete(randPath+"/foo/nested1", -1)
client.Delete(randPath+"/foo/bar/baz", -1)
client.Delete(randPath+"/foo/bar", -1)
client.Delete(randPath+"/foo", -1)
client.Delete(randPath, -1)
client.Close()
}()
logger := logging.NewVaultLogger(log.Debug)
2016-08-19 20:45:17 +00:00
b, err := NewZooKeeperBackend(map[string]string{
"address": addr + "," + addr,
"path": randPath,
}, logger)
if err != nil {
t.Fatalf("err: %s", err)
}
physical.ExerciseBackend(t, b)
physical.ExerciseBackend_ListPrefix(t, b)
}
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() {
client.Delete(randPath+"/foo", -1)
client.Delete(randPath, -1)
client.Close()
}()
logger := logging.NewVaultLogger(log.Debug)
2018-11-01 17:31:09 +00:00
config := map[string]string{
"address": addr + "," + addr,
"path": randPath,
2018-11-01 17:31:09 +00:00
}
b, err := NewZooKeeperBackend(config, logger)
if err != nil {
t.Fatalf("err: %s", err)
}
2018-11-01 17:31:09 +00:00
b2, err := NewZooKeeperBackend(config, logger)
if err != nil {
t.Fatalf("err: %s", err)
}
2018-11-01 17:31:09 +00:00
physical.ExerciseHABackend(t, b.(physical.HABackend), b2.(physical.HABackend))
}