-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
156 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cassandra | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/gocql/gocql" | ||
) | ||
|
||
// Config is the Cassandra checker configuration settings container. | ||
type Config struct { | ||
// Hosts is a list of Cassandra hosts. At least one is required. | ||
Hosts []string | ||
// Keyspace is the Cassandra keyspace to which you want to connect. Required. | ||
Keyspace string | ||
} | ||
|
||
// New creates new Cassandra health check that verifies the following: | ||
// - that a connection can be established through creating a session | ||
// - that queries can be executed by describing keyspaces | ||
func New(config Config) func(ctx context.Context) error { | ||
return func(ctx context.Context) error { | ||
if len(config.Hosts) < 1 || len(config.Keyspace) < 1 { | ||
return errors.New("keyspace name and hosts are required to initialize cassandra health check") | ||
} | ||
|
||
cluster := gocql.NewCluster(config.Hosts...) | ||
cluster.Keyspace = config.Keyspace | ||
|
||
session, err := cluster.CreateSession() | ||
if err != nil { | ||
return fmt.Errorf("cassandra health check failed on connect: %w", err) | ||
} | ||
|
||
defer session.Close() | ||
|
||
err = session.Query("DESCRIBE KEYSPACES;").WithContext(ctx).Exec() | ||
if err != nil { | ||
return fmt.Errorf("cassandra health check failed on describe: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cassandra | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/gocql/gocql" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const HOST = "HEALTH_GO_CASSANDRA_HOST" | ||
const KEYSPACE = "default" | ||
|
||
func TestNew(t *testing.T) { | ||
initDB(t) | ||
|
||
check := New(Config{ | ||
Hosts: getHosts(t), | ||
Keyspace: KEYSPACE, | ||
}) | ||
|
||
err := check(context.Background()) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestNewWithError(t *testing.T) { | ||
check := New(Config{}) | ||
|
||
err := check(context.Background()) | ||
require.Error(t, err) | ||
} | ||
|
||
func initDB(t *testing.T) { | ||
t.Helper() | ||
|
||
cluster := gocql.NewCluster(getHosts(t)[0]) | ||
|
||
session, err := cluster.CreateSession() | ||
require.NoError(t, err) | ||
|
||
defer session.Close() | ||
|
||
err = session.Query(fmt.Sprintf("CREATE KEYSPACE IF NOT EXISTS %s WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};", KEYSPACE)).Exec() | ||
require.NoError(t, err) | ||
|
||
err = session.Query(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.test (id UUID PRIMARY KEY, name text);", KEYSPACE)).Exec() | ||
require.NoError(t, err) | ||
} | ||
|
||
func getHosts(t *testing.T) []string { | ||
t.Helper() | ||
|
||
host, ok := os.LookupEnv(HOST) | ||
require.True(t, ok, fmt.Sprintf("Host is: %s", host)) | ||
|
||
// "docker-compose port <service> <port>" returns 0.0.0.0:XXXX locally, change it to local port | ||
host = strings.Replace(host, "0.0.0.0:", "127.0.0.1:", 1) | ||
|
||
return []string{host} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters