Merge pull request #103 from cgilling/better_cassandra_uri_errors

cassandra: add error checking for bad connection urls
This commit is contained in:
Matthias Kadenbach 2016-12-21 20:30:43 -08:00 committed by GitHub
commit 7910a55d0e
3 changed files with 39 additions and 2 deletions

View File

@ -21,6 +21,6 @@ mysql:
image: mysql
environment:
MYSQL_DATABASE: migratetest
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
cassandra:
image: cassandra:2.2

View File

@ -49,6 +49,13 @@ const (
// cassandra://localhost/SpaceOfKeys?consistency=quorum
func (driver *Driver) Initialize(rawurl string) error {
u, err := url.Parse(rawurl)
if err != nil {
return fmt.Errorf("failed to parse connectil url: %v", err)
}
if u.Path == "" {
return fmt.Errorf("no keyspace provided in connection url")
}
cluster := gocql.NewCluster(u.Host)
cluster.Keyspace = u.Path[1:len(u.Path)]

View File

@ -112,5 +112,35 @@ func TestMigrate(t *testing.T) {
if err := d.Close(); err != nil {
t.Fatal(err)
}
}
func TestInitializeReturnsErrorsForBadUrls(t *testing.T) {
var session *gocql.Session
host := os.Getenv("CASSANDRA_PORT_9042_TCP_ADDR")
port := os.Getenv("CASSANDRA_PORT_9042_TCP_PORT")
cluster := gocql.NewCluster(host)
cluster.Consistency = gocql.All
cluster.Timeout = 1 * time.Minute
session, err := cluster.CreateSession()
if err != nil {
t.Fatal(err)
}
defer session.Close()
if err := session.Query(`CREATE KEYSPACE IF NOT EXISTS migrate WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};`).Exec(); err != nil {
t.Fatal(err)
}
d := &Driver{}
invalidURL := "sdf://asdf://as?df?a"
if err := d.Initialize(invalidURL); err == nil {
t.Errorf("expected an error to be returned if url could not be parsed")
}
noKeyspace := "cassandra://" + host + ":" + port
if err := d.Initialize(noKeyspace); err == nil {
t.Errorf("expected an error to be returned if no keyspace provided")
}
}