diff --git a/cassandra.go b/cassandra.go index 8d6ae45b..20844ccb 100644 --- a/cassandra.go +++ b/cassandra.go @@ -10,7 +10,7 @@ import ( // SetupCassandraContainer sets up a real Cassandra node for testing purposes, // using a Docker container. It returns the container ID and its IP address, // or makes the test fail on error. -func SetupCassandraContainer(versionTag string) (c ContainerID, ip string, port int, err error) { +func SetupCassandraContainer(versionTag string, optionalParams ...string) (c ContainerID, ip string, port int, err error) { port = RandomPort() // Forward for the CQL port. @@ -22,15 +22,15 @@ func SetupCassandraContainer(versionTag string) (c ContainerID, ip string, port imageName := fmt.Sprintf("%s:%s", CassandraImageName, versionTag) c, ip, err = SetupContainer(imageName, port, 10*time.Second, func() (string, error) { - return run("--name", GenerateContainerID(), "-d", "-P", "-p", forward, imageName) + return run(append(optionalParams, "--name", GenerateContainerID(), "-d", "-p", forward, imageName)...) }) return } // ConnectToCassandra starts a Cassandra image and passes the nodes connection string to the connector callback function. // The connection string will match the ip:port pattern, where port is the mapped CQL port. -func ConnectToCassandra(versionTag string, tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error) { - c, ip, port, err := SetupCassandraContainer(versionTag) +func ConnectToCassandra(versionTag string, tries int, delay time.Duration, connector func(url string) bool, optionalParams ...string) (c ContainerID, err error) { + c, ip, port, err := SetupCassandraContainer(versionTag, optionalParams...) if err != nil { return c, fmt.Errorf("Could not setup Cassandra container: %v", err) } diff --git a/docker_integration_test.go b/docker_integration_test.go index 2021fd83..1aed9e99 100644 --- a/docker_integration_test.go +++ b/docker_integration_test.go @@ -302,8 +302,13 @@ func TestConnectToCassandra(t *testing.T) { // See: http://stackoverflow.com/questions/34645846/cannot-connect-to-cassandra-docker-with-cqlsh BindDockerToLocalhost = "true" + env := []string{ + "-e", "CASSANDRA_RACK=TEST_RACK", + "-e", "CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch", + } + // Cassandra takes a while to start up, so we have a longer retry window - c, err := ConnectToCassandra("3.7", 15, time.Second*5, func(url string) bool { + c, err := ConnectToCassandra("3.7", 20, time.Second*5, func(url string) bool { cluster := gocql.NewCluster(url) cluster.Keyspace = "system" cluster.ProtoVersion = 4 // Required for cassandra 3.x @@ -314,16 +319,20 @@ func TestConnectToCassandra(t *testing.T) { } defer session.Close() - // Query one of the system tables as a basic test. - it := session.Query("select * from local").Iter() + // Verify that the environment property was applied correctly, + // and that querying the node works. + it := session.Query("select rack from local").Iter() defer it.Close() - if it.NumRows() == 0 { - return false + var rackName string + for it.Scan(&rackName) { + if rackName != "TEST_RACK" { + return false + } } return true - }) + }, env...) assert.Nil(t, err) defer c.KillRemove() }