github.com/postmates/migrate@v3.0.2-0.20200730201548-1a6ead3e680d+incompatible/database/cassandra/cassandra_test.go (about)

     1  package cassandra
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	dt "github.com/mattes/migrate/database/testing"
     7  	mt "github.com/mattes/migrate/testing"
     8  	"github.com/gocql/gocql"
     9  	"time"
    10  	"strconv"
    11  )
    12  
    13  var versions = []mt.Version{
    14  	{"cassandra:3.0.10", []string{}},
    15  	{"cassandra:3.0", []string{}},
    16  }
    17  
    18  func isReady(i mt.Instance) bool {
    19  	// Cassandra exposes 5 ports (7000, 7001, 7199, 9042 & 9160)
    20  	// We only need the port bound to 9042, but we can only access to the first one
    21  	// through 'i.Port()' (which calls DockerContainer.firstPortMapping())
    22  	// So we need to get port mapping to retrieve correct port number bound to 9042
    23  	portMap := i.NetworkSettings().Ports
    24  	port, _ := strconv.Atoi(portMap["9042/tcp"][0].HostPort)
    25  
    26  	cluster := gocql.NewCluster(i.Host())
    27  	cluster.Port = port
    28  	//cluster.ProtoVersion = 4
    29  	cluster.Consistency = gocql.All
    30  	cluster.Timeout = 1 * time.Minute
    31  	p, err := cluster.CreateSession()
    32  	if err != nil {
    33  		return false
    34  	}
    35  	// Create keyspace for tests
    36  	p.Query("CREATE KEYSPACE testks WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor':1}").Exec()
    37  	return true
    38  }
    39  
    40  func Test(t *testing.T) {
    41  	mt.ParallelTest(t, versions, isReady,
    42  		func(t *testing.T, i mt.Instance) {
    43  			p := &Cassandra{}
    44  			portMap := i.NetworkSettings().Ports
    45  			port, _ := strconv.Atoi(portMap["9042/tcp"][0].HostPort)
    46  			addr := fmt.Sprintf("cassandra://%v:%v/testks", i.Host(), port)
    47  			d, err := p.Open(addr)
    48  			if err != nil {
    49  				t.Fatalf("%v", err)
    50  			}
    51  			dt.Test(t, d, []byte("SELECT table_name from system_schema.tables"))
    52  		})
    53  }