github.com/Elate-DevOps/migrate/v4@v4.0.12/database/cassandra/cassandra_test.go (about)

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