Skip to content

Commit

Permalink
core: add optional parameters to cassandra (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Cheng authored and Aeneas committed Jul 14, 2016
1 parent c066e73 commit e6ba58a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
8 changes: 4 additions & 4 deletions cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand Down
21 changes: 15 additions & 6 deletions docker_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
Expand Down

0 comments on commit e6ba58a

Please sign in to comment.