github.com/bdollma-te/migrate/v4@v4.17.0-clickv2/database/cassandra/cassandra_test.go (about)

     1  package cassandra
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/bdollma-te/migrate/v4"
    10  	"github.com/dhui/dktest"
    11  	"github.com/gocql/gocql"
    12  
    13  	dt "github.com/bdollma-te/migrate/v4/database/testing"
    14  	"github.com/bdollma-te/migrate/v4/dktesting"
    15  
    16  	_ "github.com/bdollma-te/migrate/v4/source/file"
    17  )
    18  
    19  var (
    20  	opts = dktest.Options{PortRequired: true, ReadyFunc: isReady}
    21  	// Supported versions: http://cassandra.apache.org/download/
    22  	// Although Cassandra 2.x is supported by the Apache Foundation,
    23  	// the migrate db driver only supports Cassandra 3.x since it uses
    24  	// the system_schema keyspace.
    25  	// last ScyllaDB version tested is 5.1.11
    26  	specs = []dktesting.ContainerSpec{
    27  		{ImageName: "cassandra:3.0", Options: opts},
    28  		{ImageName: "cassandra:3.11", Options: opts},
    29  		{ImageName: "scylladb/scylla:5.1.11", Options: opts},
    30  	}
    31  )
    32  
    33  func isReady(ctx context.Context, c dktest.ContainerInfo) bool {
    34  	// Cassandra exposes 5 ports (7000, 7001, 7199, 9042 & 9160)
    35  	// We only need the port bound to 9042
    36  	ip, portStr, err := c.Port(9042)
    37  	if err != nil {
    38  		return false
    39  	}
    40  	port, err := strconv.Atoi(portStr)
    41  	if err != nil {
    42  		return false
    43  	}
    44  
    45  	cluster := gocql.NewCluster(ip)
    46  	cluster.Port = port
    47  	cluster.Consistency = gocql.All
    48  	p, err := cluster.CreateSession()
    49  	if err != nil {
    50  		return false
    51  	}
    52  	defer p.Close()
    53  	// Create keyspace for tests
    54  	if err = p.Query("CREATE KEYSPACE testks WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor':1}").Exec(); err != nil {
    55  		return false
    56  	}
    57  	return true
    58  }
    59  
    60  func Test(t *testing.T) {
    61  	dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
    62  		ip, port, err := c.Port(9042)
    63  		if err != nil {
    64  			t.Fatal("Unable to get mapped port:", err)
    65  		}
    66  		addr := fmt.Sprintf("cassandra://%v:%v/testks", ip, port)
    67  		p := &Cassandra{}
    68  		d, err := p.Open(addr)
    69  		if err != nil {
    70  			t.Fatal(err)
    71  		}
    72  		defer func() {
    73  			if err := d.Close(); err != nil {
    74  				t.Error(err)
    75  			}
    76  		}()
    77  		dt.Test(t, d, []byte("SELECT table_name from system_schema.tables"))
    78  	})
    79  }
    80  
    81  func TestMigrate(t *testing.T) {
    82  	dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
    83  		ip, port, err := c.Port(9042)
    84  		if err != nil {
    85  			t.Fatal("Unable to get mapped port:", err)
    86  		}
    87  		addr := fmt.Sprintf("cassandra://%v:%v/testks", ip, port)
    88  		p := &Cassandra{}
    89  		d, err := p.Open(addr)
    90  		if err != nil {
    91  			t.Fatal(err)
    92  		}
    93  		defer func() {
    94  			if err := d.Close(); err != nil {
    95  				t.Error(err)
    96  			}
    97  		}()
    98  
    99  		m, err := migrate.NewWithDatabaseInstance("file://./examples/migrations", "testks", d)
   100  		if err != nil {
   101  			t.Fatal(err)
   102  		}
   103  		dt.TestMigrate(t, m)
   104  	})
   105  }