github.com/SmoothieNoIce/migrate@v3.5.4+incompatible/database/cassandra/cassandra_test.go (about)

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