2017-05-24 09:59:18 +02:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-05-16 15:12:36 -07:00
|
|
|
"strconv"
|
2017-05-24 09:59:18 +02:00
|
|
|
"testing"
|
2018-05-16 15:12:36 -07:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gocql/gocql"
|
|
|
|
)
|
|
|
|
|
|
|
|
import (
|
2018-01-19 10:56:55 -08:00
|
|
|
dt "github.com/golang-migrate/migrate/database/testing"
|
|
|
|
mt "github.com/golang-migrate/migrate/testing"
|
2017-05-24 09:59:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var versions = []mt.Version{
|
2017-06-24 00:32:09 -07:00
|
|
|
{Image: "cassandra:3.0.10"},
|
|
|
|
{Image: "cassandra:3.0"},
|
2017-05-24 09:59:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func isReady(i mt.Instance) bool {
|
|
|
|
// Cassandra exposes 5 ports (7000, 7001, 7199, 9042 & 9160)
|
|
|
|
// We only need the port bound to 9042, but we can only access to the first one
|
|
|
|
// through 'i.Port()' (which calls DockerContainer.firstPortMapping())
|
|
|
|
// So we need to get port mapping to retrieve correct port number bound to 9042
|
|
|
|
portMap := i.NetworkSettings().Ports
|
|
|
|
port, _ := strconv.Atoi(portMap["9042/tcp"][0].HostPort)
|
|
|
|
|
|
|
|
cluster := gocql.NewCluster(i.Host())
|
|
|
|
cluster.Port = port
|
|
|
|
//cluster.ProtoVersion = 4
|
|
|
|
cluster.Consistency = gocql.All
|
2018-05-16 15:12:36 -07:00
|
|
|
cluster.Timeout = 10 * time.Second
|
2017-05-24 09:59:18 +02:00
|
|
|
p, err := cluster.CreateSession()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2018-05-16 15:12:36 -07:00
|
|
|
defer p.Close()
|
2017-05-24 09:59:18 +02:00
|
|
|
// Create keyspace for tests
|
|
|
|
p.Query("CREATE KEYSPACE testks WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor':1}").Exec()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test(t *testing.T) {
|
|
|
|
mt.ParallelTest(t, versions, isReady,
|
|
|
|
func(t *testing.T, i mt.Instance) {
|
|
|
|
p := &Cassandra{}
|
|
|
|
portMap := i.NetworkSettings().Ports
|
|
|
|
port, _ := strconv.Atoi(portMap["9042/tcp"][0].HostPort)
|
|
|
|
addr := fmt.Sprintf("cassandra://%v:%v/testks", i.Host(), port)
|
|
|
|
d, err := p.Open(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
dt.Test(t, d, []byte("SELECT table_name from system_schema.tables"))
|
|
|
|
})
|
2017-06-24 00:32:09 -07:00
|
|
|
}
|