2017-08-03 17:24:27 +00:00
|
|
|
package cassandra
|
2017-01-07 16:30:25 +00:00
|
|
|
|
|
|
|
import (
|
2017-07-31 14:58:38 +00:00
|
|
|
"fmt"
|
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"
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-22 16:26:10 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/docker"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2018-07-11 21:49:13 +00:00
|
|
|
"github.com/ory/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-07-31 14:58:38 +00:00
|
|
|
cleanup, hosts := prepareCassandraTestContainer(t)
|
|
|
|
defer cleanup()
|
2017-01-07 17:05:07 +00:00
|
|
|
|
2017-01-07 16:30:25 +00:00
|
|
|
// Run vault tests
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2017-08-03 17:24:27 +00:00
|
|
|
b, err := NewCassandraBackend(map[string]string{
|
2017-06-09 01:56:46 +00:00
|
|
|
"hosts": hosts,
|
2017-08-03 17:24:27 +00:00
|
|
|
"protocol_version": "3",
|
|
|
|
}, logger)
|
2017-01-07 16:30:25 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create new backend: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseBackend(t, b)
|
|
|
|
physical.ExerciseBackend_ListPrefix(t, b)
|
2017-01-07 16:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-07-31 14:58:38 +00:00
|
|
|
func prepareCassandraTestContainer(t *testing.T) (func(), string) {
|
2017-01-07 17:05:07 +00:00
|
|
|
if os.Getenv("CASSANDRA_HOSTS") != "" {
|
2017-07-31 14:58:38 +00:00
|
|
|
return func() {}, os.Getenv("CASSANDRA_HOSTS")
|
2017-01-07 17:05:07 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 14:58:38 +00:00
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cassandra: failed to connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-09-08 03:04:40 +00:00
|
|
|
resource, err := pool.Run("cassandra", "3.11", []string{"CASSANDRA_BROADCAST_ADDRESS=127.0.0.1"})
|
2017-07-31 14:58:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cassandra: could not start container: %s", err)
|
|
|
|
}
|
2017-01-07 17:05:07 +00:00
|
|
|
|
2017-09-08 03:04:40 +00:00
|
|
|
cleanup := func() {
|
2019-04-22 16:26:10 +00:00
|
|
|
docker.CleanupResource(t, pool, resource)
|
2017-09-08 03:04:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 14:58:38 +00:00
|
|
|
setup := func() error {
|
|
|
|
cluster := gocql.NewCluster("127.0.0.1")
|
|
|
|
p, _ := strconv.Atoi(resource.GetPort("9042/tcp"))
|
|
|
|
cluster.Port = p
|
2019-07-23 17:26:36 +00:00
|
|
|
cluster.Timeout = 15 * time.Second
|
2017-01-07 17:05:07 +00:00
|
|
|
sess, err := cluster.CreateSession()
|
|
|
|
if err != nil {
|
2017-07-31 14:58:38 +00:00
|
|
|
return err
|
2017-01-07 17:05:07 +00:00
|
|
|
}
|
2017-09-08 03:04:40 +00:00
|
|
|
defer sess.Close()
|
2017-01-07 17:05:07 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2017-07-31 14:58:38 +00:00
|
|
|
return nil
|
2017-01-07 17:05:07 +00:00
|
|
|
}
|
2017-07-31 14:58:38 +00:00
|
|
|
if pool.Retry(setup); err != nil {
|
2017-09-08 03:04:40 +00:00
|
|
|
cleanup()
|
2017-07-31 14:58:38 +00:00
|
|
|
t.Fatalf("cassandra: could not setup container: %s", err)
|
2017-01-07 17:05:07 +00:00
|
|
|
}
|
2017-07-31 14:58:38 +00:00
|
|
|
|
|
|
|
return cleanup, fmt.Sprintf("127.0.0.1:%s", resource.GetPort("9042/tcp"))
|
2017-01-07 17:05:07 +00:00
|
|
|
}
|