2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
package zookeeper
|
2015-05-05 20:08:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-02-10 16:56:27 +00:00
|
|
|
"github.com/go-zookeeper/zk"
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2015-05-05 20:08:42 +00:00
|
|
|
)
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
func TestZooKeeperBackend(t *testing.T) {
|
2015-05-05 20:08:42 +00:00
|
|
|
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
|
|
|
}()
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
b, err := NewZooKeeperBackend(map[string]string{
|
2015-05-05 20:08:42 +00:00
|
|
|
"address": addr + "," + addr,
|
|
|
|
"path": randPath,
|
2017-08-03 17:24:27 +00:00
|
|
|
}, logger)
|
2015-05-05 20:08:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseBackend(t, b)
|
|
|
|
physical.ExerciseBackend_ListPrefix(t, b)
|
2015-05-05 20:08:42 +00:00
|
|
|
}
|
2015-05-26 04:12:16 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
func TestZooKeeperHABackend(t *testing.T) {
|
2015-05-26 04:12:16 +00:00
|
|
|
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()
|
|
|
|
}()
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2018-11-01 17:31:09 +00:00
|
|
|
config := map[string]string{
|
2015-05-26 04:12:16 +00:00
|
|
|
"address": addr + "," + addr,
|
|
|
|
"path": randPath,
|
2018-11-01 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := NewZooKeeperBackend(config, logger)
|
2015-05-26 04:12:16 +00:00
|
|
|
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)
|
2015-05-26 04:12:16 +00:00
|
|
|
}
|
2018-11-01 17:31:09 +00:00
|
|
|
|
|
|
|
physical.ExerciseHABackend(t, b.(physical.HABackend), b2.(physical.HABackend))
|
2015-05-26 04:12:16 +00:00
|
|
|
}
|