2017-01-07 16:30:25 +00:00
|
|
|
package physical
|
|
|
|
|
|
|
|
import (
|
2017-01-07 17:05:07 +00:00
|
|
|
"net"
|
2017-01-07 16:30:25 +00:00
|
|
|
"os"
|
|
|
|
"reflect"
|
2017-01-07 17:05:07 +00:00
|
|
|
"strconv"
|
2017-01-07 16:30:25 +00:00
|
|
|
"testing"
|
2017-01-07 17:05:07 +00:00
|
|
|
"time"
|
2017-01-07 16:30:25 +00:00
|
|
|
|
2017-01-07 17:05:07 +00:00
|
|
|
"github.com/gocql/gocql"
|
2017-01-07 16:30:25 +00:00
|
|
|
"github.com/hashicorp/vault/helper/logformat"
|
|
|
|
log "github.com/mgutz/logxi/v1"
|
2017-01-07 17:05:07 +00:00
|
|
|
"github.com/ory-am/dockertest"
|
2017-01-07 16:30:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCassandraBackend(t *testing.T) {
|
2017-01-07 17:05:07 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skipf("skipping in short mode")
|
2017-01-07 16:30:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 17:05:07 +00:00
|
|
|
cid, hosts := prepareCassandraTestContainer(t)
|
|
|
|
defer cleanupCassandraTestContainer(t, cid)
|
|
|
|
|
2017-01-07 16:30:25 +00:00
|
|
|
// Run vault tests
|
|
|
|
logger := logformat.NewVaultLogger(log.LevelTrace)
|
|
|
|
b, err := NewBackend("cassandra", logger, map[string]string{
|
|
|
|
"hosts": hosts,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create new backend: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testBackend(t, b)
|
|
|
|
testBackend_ListPrefix(t, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCassandraBackendBuckets(t *testing.T) {
|
|
|
|
expectations := map[string][]string{
|
|
|
|
"": {"."},
|
|
|
|
"a": {"."},
|
|
|
|
"a/b": {".", "a"},
|
|
|
|
"a/b/c/d/e": {".", "a", "a/b", "a/b/c", "a/b/c/d"}}
|
|
|
|
|
|
|
|
b := &CassandraBackend{}
|
|
|
|
for input, expected := range expectations {
|
|
|
|
actual := b.buckets(input)
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Errorf("bad: %v expected: %v", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-07 17:05:07 +00:00
|
|
|
|
|
|
|
func prepareCassandraTestContainer(t *testing.T) (dockertest.ContainerID, string) {
|
|
|
|
if os.Getenv("CASSANDRA_HOSTS") != "" {
|
|
|
|
return "", os.Getenv("CASSANDRA_HOSTS")
|
|
|
|
}
|
|
|
|
|
|
|
|
dockertest.Pull(dockertest.CassandraImageName)
|
|
|
|
hosts := ""
|
|
|
|
cid, connErr := dockertest.ConnectToCassandra("3.9", 90, time.Second, func(connAddress string) bool {
|
|
|
|
host, _port, _ := net.SplitHostPort(connAddress)
|
|
|
|
port, _ := strconv.Atoi(_port)
|
|
|
|
|
|
|
|
cluster := gocql.NewCluster(host)
|
|
|
|
cluster.Port = port
|
|
|
|
cluster.Timeout = 5 * time.Second
|
|
|
|
sess, err := cluster.CreateSession()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create keyspace
|
|
|
|
q := sess.Query(`CREATE KEYSPACE "vault" WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };`)
|
|
|
|
if err := q.Exec(); err != nil {
|
|
|
|
t.Fatalf("could not create cassandra keyspace: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create table
|
|
|
|
q = sess.Query(`CREATE TABLE "vault"."entries" (
|
|
|
|
bucket text,
|
|
|
|
key text,
|
|
|
|
value blob,
|
|
|
|
PRIMARY KEY (bucket, key)
|
|
|
|
) WITH CLUSTERING ORDER BY (key ASC);`)
|
|
|
|
if err := q.Exec(); err != nil {
|
|
|
|
t.Fatalf("could not create cassandra table: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts = connAddress
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
if connErr != nil {
|
|
|
|
t.Fatalf("could not connect to cassandra: %v", connErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cid, hosts
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupCassandraTestContainer(t *testing.T, cid dockertest.ContainerID) {
|
|
|
|
if cid == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := cid.KillRemove(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|