Skip to content

Commit

Permalink
Update cassandra files
Browse files Browse the repository at this point in the history
Signed-off-by: nilayasiktoprak <nilayasiktoprak@gmail.com>
  • Loading branch information
nilayasiktoprak committed Oct 23, 2021
1 parent a073d6a commit 4671de2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
7 changes: 4 additions & 3 deletions pkg/scalers/cassandra_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ func ParseCassandraMetadata(config *ScalerConfig) (*CassandraMetadata, error) {
}

if val, ok := config.TriggerMetadata["clusterIPAddress"]; ok {
if meta.port > 0 {
switch p := meta.port; {
case p > 0:
meta.clusterIPAddress = fmt.Sprintf("%s:%d", val, meta.port)
} else if strings.Contains(val, ":") {
case strings.Contains(val, ":"):
meta.clusterIPAddress = val
} else {
default:
return nil, fmt.Errorf("no port given")
}
} else {
Expand Down
44 changes: 18 additions & 26 deletions pkg/scalers/cassandra_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,105 +3,97 @@ package scalers
import (
"errors"
"fmt"
"github.com/gocql/gocql"
"testing"

"github.com/gocql/gocql"
)

type cassandraTestData struct {
//test inputs
// test inputs
metadata map[string]string
authParams map[string]string

//expected outputs
// expected outputs
expectedMetricName string
expectedConsistency gocql.Consistency
expectedProtocolVersion int
expectedClusterIPAddress string
expectedPort int
expectedError error
}

var testCassandraInputs = []cassandraTestData{
//metricName written
// metricName written
{
metadata: map[string]string{"query": "SELECT COUNT(*) FROM test_keyspace.test_table;", "targetQueryValue": "1", "username": "cassandra", "clusterIPAddress": "cassandra.test:9042", "keyspace": "test_keyspace", "metricName": "myMetric"},
authParams: map[string]string{"password": "Y2Fzc2FuZHJhCg=="},
expectedMetricName: "cassandra-myMetric",
},

//keyspace written, no metricName
// keyspace written, no metricName
{
metadata: map[string]string{"query": "SELECT COUNT(*) FROM test_keyspace.test_table;", "targetQueryValue": "1", "username": "cassandra", "clusterIPAddress": "cassandra.test:9042", "keyspace": "test_keyspace"},
authParams: map[string]string{"password": "Y2Fzc2FuZHJhCg=="},
expectedMetricName: "cassandra-test_keyspace",
},

//metricName and keyspace written
// metricName and keyspace written
{
metadata: map[string]string{"query": "SELECT COUNT(*) FROM test_keyspace.test_table;", "targetQueryValue": "1", "username": "cassandra", "clusterIPAddress": "cassandra.test:9042", "metricName": "myMetric", "keyspace": "test_keyspace"},
authParams: map[string]string{"password": "Y2Fzc2FuZHJhCg=="},
expectedMetricName: "cassandra-myMetric",
},

//consistency and protocolVersion not written
// consistency and protocolVersion not written
{
metadata: map[string]string{"query": "SELECT COUNT(*) FROM test_keyspace.test_table;", "targetQueryValue": "1", "username": "cassandra", "clusterIPAddress": "cassandra.test:9042", "keyspace": "test_keyspace"},
authParams: map[string]string{"password": "Y2Fzc2FuZHJhCg=="},
expectedConsistency: gocql.One,
expectedProtocolVersion: 4,
},

//clusterIPAddress and port written separately
{
metadata: map[string]string{"query": "SELECT COUNT(*) FROM test_keyspace.test_table;", "port": "9042", "clusterIPAddress": "cassandra.test", "targetQueryValue": "1", "username": "cassandra", "keyspace": "test_keyspace"},
authParams: map[string]string{"password": "Y2Fzc2FuZHJhCg=="},
expectedClusterIPAddress: "cassandra.test:9042",
expectedPort: 9042,
},

//Error: keyspace not written
// Error: keyspace not written
{
metadata: map[string]string{"query": "SELECT COUNT(*) FROM test_keyspace.test_table;", "targetQueryValue": "1", "username": "cassandra", "clusterIPAddress": "cassandra.test:9042", "metricName": "myMetric"},
authParams: map[string]string{"password": "Y2Fzc2FuZHJhCg=="},
expectedError: errors.New("no keyspace given"),
},

//Error: missing query
// Error: missing query
{
metadata: map[string]string{"targetQueryValue": "1", "username": "cassandra", "clusterIPAddress": "cassandra.test:9042", "keyspace": "test_keyspace"},
authParams: map[string]string{"password": "Y2Fzc2FuZHJhCg=="},
expectedError: errors.New("no query given"),
},

//Error: missing targetQueryValue
// Error: missing targetQueryValue
{
metadata: map[string]string{"query": "SELECT COUNT(*) FROM test_keyspace.test_table;", "username": "cassandra", "clusterIPAddress": "cassandra.test:9042", "keyspace": "test_keyspace"},
authParams: map[string]string{"password": "Y2Fzc2FuZHJhCg=="},
expectedError: errors.New("no targetQueryValue given"),
},

//Error: missing username
// Error: missing username
{
metadata: map[string]string{"query": "SELECT COUNT(*) FROM test_keyspace.test_table;", "targetQueryValue": "1", "clusterIPAddress": "cassandra.test:9042", "keyspace": "test_keyspace"},
authParams: map[string]string{"password": "Y2Fzc2FuZHJhCg=="},
expectedError: errors.New("no username given"),
},

//Error: missing clusterIPAddress
// Error: missing clusterIPAddress
{
metadata: map[string]string{"query": "SELECT COUNT(*) FROM test_keyspace.test_table;", "targetQueryValue": "1", "username": "cassandra", "keyspace": "test_keyspace"},
authParams: map[string]string{"password": "Y2Fzc2FuZHJhCg=="},
expectedError: errors.New("no cluster IP address given"),
},

//Error: missing port
// Error: missing port
{
metadata: map[string]string{"query": "SELECT COUNT(*) FROM test_keyspace.test_table;", "clusterIPAddress": "cassandra.test", "targetQueryValue": "1", "username": "cassandra", "keyspace": "test_keyspace"},
authParams: map[string]string{"password": "Y2Fzc2FuZHJhCg=="},
expectedError: errors.New("no port given"),
},

//Error: missing password
// Error: missing password
{
metadata: map[string]string{"query": "SELECT COUNT(*) FROM test_keyspace.test_table;", "targetQueryValue": "1", "username": "cassandra", "clusterIPAddress": "cassandra.test:9042", "keyspace": "test_keyspace"},
authParams: map[string]string{},
Expand Down Expand Up @@ -140,17 +132,17 @@ func TestParseCassandraMetadata(t *testing.T) {
}

expectedConsistency := gocql.One
if outputMetadata.consistency != expectedConsistency {
if testData.expectedConsistency != 0 && testData.expectedConsistency != outputMetadata.consistency {
t.Errorf("Wrong consistency. Expected %d but got %d", expectedConsistency, outputMetadata.consistency)
}

expectedProtocolVersion := 4
if outputMetadata.protocolVersion != expectedProtocolVersion {
if testData.expectedProtocolVersion != 0 && testData.expectedProtocolVersion != outputMetadata.protocolVersion {
t.Errorf("Wrong protocol version. Expected %d but got %d", expectedProtocolVersion, outputMetadata.protocolVersion)
}

expectedClusterIPAddress := "cassandra.test:9042"
if outputMetadata.clusterIPAddress != expectedClusterIPAddress {
if testData.expectedClusterIPAddress != "" && testData.expectedClusterIPAddress != outputMetadata.clusterIPAddress {
t.Errorf("Wrong clusterIPAddress. Expected %s but got %s", expectedClusterIPAddress, outputMetadata.clusterIPAddress)
}

Expand Down

0 comments on commit 4671de2

Please sign in to comment.