github.com/scraniel/migrate@v0.0.0-20230320185700-339088f36cee/database/cassandra/cassandra_test.go (about)

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