Skip to content

Commit

Permalink
NR-191066 moved part of logic to separate functions and added version…
Browse files Browse the repository at this point in the history
…s to testnames
  • Loading branch information
sairaj18 committed Nov 6, 2024
1 parent 84200dc commit 315f660
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 141 deletions.
106 changes: 49 additions & 57 deletions tests/integration/cassandra_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,74 +50,63 @@ func (s *CassandraSSLTestSuite) TearDownSuite() {
s.cancelComposeCtx()
}

func (s *CassandraSSLTestSuite) TestCassandraIntegration_SSL() {
t := s.T()

func testCassandraIntegration_SSL(t *testing.T, ctx context.Context, cassandraConfig testutils.CassandraConfig) {
testName := t.Name()

ctx, cancelFn := context.WithTimeout(context.Background(), 30*time.Second)
defer cancelFn()

testCases := []struct {
env map[string]string
cassandraVersion string
}{}
for _, cassandraConfig := range testutils.CassandraConfigs {
testCases = append(testCases, struct {
env map[string]string
cassandraVersion string
}{
env: map[string]string{
"METRICS": "true",
"TIMEOUT": SSLConnectionTimeout,

"HOSTNAME": cassandraConfig.Hostname,
"USERNAME": testutils.JMXUsername,
"PASSWORD": testutils.JMXPassword,

"TRUST_STORE": testutils.TruststoreFile,
"TRUST_STORE_PASSWORD": testutils.TruststorePassword,
"KEY_STORE": testutils.KeystoreFile,
"KEY_STORE_PASSWORD": testutils.KeystorePassword,

"NRIA_CACHE_PATH": fmt.Sprintf("/tmp/%v.json", testName),
},
cassandraVersion: cassandraConfig.Version,
})
testCase := struct {
name string
env map[string]string
}{
name: fmt.Sprintf("CassandraIntegrationSSL - %s", cassandraConfig.Version),
env: map[string]string{
"METRICS": "true",
"TIMEOUT": SSLConnectionTimeout,

"HOSTNAME": cassandraConfig.Hostname,
"USERNAME": testutils.JMXUsername,
"PASSWORD": testutils.JMXPassword,

"TRUST_STORE": testutils.TruststoreFile,
"TRUST_STORE_PASSWORD": testutils.TruststorePassword,
"KEY_STORE": testutils.KeystoreFile,
"KEY_STORE_PASSWORD": testutils.KeystorePassword,

"NRIA_CACHE_PATH": fmt.Sprintf("/tmp/%v.json", testName),
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
stdout, stderr, err := testutils.RunDockerExecCommand(ctx, t, integrationContainerName, []string{integrationBinPath}, testCase.env)
assert.NoError(t, err, "It isn't possible to execute Cassandra integration binary.")

assert.Empty(t, testutils.FilterStderr(stderr))

schemaDir := fmt.Sprintf("json-schema-files-%s", testCase.cassandraVersion)
schemaDir := fmt.Sprintf("json-schema-files-%s", cassandraConfig.Version)
schemaPath := filepath.Join(schemaDir, "cassandra-schema-metrics.json")

err = jsonschema.Validate(schemaPath, stdout)
assert.NoError(t, err, "The output of Cassandra integration doesn't have expected format.")
}
})
}

func (s *CassandraSSLTestSuite) TestCassandraIntegration_WrongConfig() {
func (s *CassandraSSLTestSuite) TestCassandraIntegration_SSL() {
t := s.T()

testName := t.Name()
ctx, cancelFn := context.WithTimeout(context.Background(), 30*time.Second)
defer cancelFn()

for _, cassandraConfig := range testutils.CassandraConfigs {
testCassandraIntegration_SSL(t, ctx, cassandraConfig)
}
}

func testCassandraIntegration_WrongConfig(t *testing.T, cassandraConfig testutils.CassandraConfig) {
testName := t.Name()
testCases := []struct {
name string
config map[string]string
expectedError string
}{}

for _, cassandraConfig := range testutils.CassandraConfigs {
testCases = append(testCases, struct {
name string
config map[string]string
expectedError string
}{
name: "WrongPassword",
}{
{
name: fmt.Sprintf("WrongPasword - %s", cassandraConfig.Version),
config: map[string]string{
"METRICS": "true",
"TIMEOUT": SSLConnectionTimeout,
Expand All @@ -134,13 +123,9 @@ func (s *CassandraSSLTestSuite) TestCassandraIntegration_WrongConfig() {
"NRIA_CACHE_PATH": fmt.Sprintf("/tmp/%v.json", testName),
},
expectedError: "Authentication failed! Invalid username or password",
})
testCases = append(testCases, struct {
name string
config map[string]string
expectedError string
}{
name: "WrongKeyStorePassword",
},
{
name: fmt.Sprintf("WrongKeyStorePassword - %s", cassandraConfig.Version),
config: map[string]string{
"METRICS": "true",
"TIMEOUT": SSLConnectionTimeout,
Expand All @@ -157,9 +142,8 @@ func (s *CassandraSSLTestSuite) TestCassandraIntegration_WrongConfig() {
"NRIA_CACHE_PATH": fmt.Sprintf("/tmp/%v.json", testName),
},
expectedError: "java.security.NoSuchAlgorithmException",
})
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
ctx, cancelFn := context.WithTimeout(context.Background(), 30*time.Second)
Expand All @@ -173,3 +157,11 @@ func (s *CassandraSSLTestSuite) TestCassandraIntegration_WrongConfig() {
})
}
}

func (s *CassandraSSLTestSuite) TestCassandraIntegration_WrongConfig() {
t := s.T()

for _, cassandraConfig := range testutils.CassandraConfigs {
testCassandraIntegration_WrongConfig(t, cassandraConfig)
}
}
76 changes: 36 additions & 40 deletions tests/integration/cassandra_long_running_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,46 +45,31 @@ func (s *CassandraLongRunningTestSuite) TearDownSuite() {
s.cancelComposeCtx()
}

func (s *CassandraLongRunningTestSuite) TestCassandraIntegration_LongRunningIntegration() {
t := s.T()

func testCassandraIntegration_LongRunningIntegration(t *testing.T, ctx context.Context, cassandraConfig testutils.CassandraConfig) {
testName := t.Name()

ctx, cancelFn := context.WithTimeout(context.Background(), 600*time.Second)
defer cancelFn()

testCases := []struct {
env map[string]string
cassandraVersion string
containerName string
}{}
for _, cassandraConfig := range testutils.CassandraConfigs {
testCases = append(testCases, struct {
env map[string]string
cassandraVersion string
containerName string
}{
env: map[string]string{
"METRICS": "true",
"HOSTNAME": cassandraConfig.Hostname,
"TIMEOUT": timeout,

"LONG_RUNNING": "true",
"INTERVAL": "2",
"HEARTBEAT_INTERVAL": "2",

"NRIA_CACHE_PATH": fmt.Sprintf("/tmp/%v.json", testName),

// Uncomment those for troubleshooting.
// "VERBOSE": "true",
// "ENABLE_INTERNAL_STATS": "true",
},
cassandraVersion: cassandraConfig.Version,
containerName: cassandraConfig.ContainerName,
})
testCase := struct {
name string
env map[string]string
}{
name: fmt.Sprintf("CassandraIntegrationLongRunningIntegration - %s", cassandraConfig.Version),
env: map[string]string{
"METRICS": "true",
"HOSTNAME": cassandraConfig.Hostname,
"TIMEOUT": timeout,

"LONG_RUNNING": "true",
"INTERVAL": "2",
"HEARTBEAT_INTERVAL": "2",

"NRIA_CACHE_PATH": fmt.Sprintf("/tmp/%v.json", testName),

// Uncomment those for troubleshooting.
// "VERBOSE": "true",
// "ENABLE_INTERNAL_STATS": "true",
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
cmd := testutils.NewDockerExecCommand(ctx, t, integrationContainerName, []string{integrationBinPath}, testCase.env)

output, err := testutils.StartLongRunningProcess(ctx, t, cmd)
Expand All @@ -99,11 +84,11 @@ func (s *CassandraLongRunningTestSuite) TestCassandraIntegration_LongRunningInte
}
}()

schemaDir := fmt.Sprintf("json-schema-files-%s", testCase.cassandraVersion)
schemaDir := fmt.Sprintf("json-schema-files-%s", cassandraConfig.Version)
schemaFile := filepath.Join(schemaDir, "cassandra-schema-metrics.json")
testutils.AssertReceivedPayloadsMatchSchema(t, ctx, output, schemaFile, 30*time.Second)

err = testutils.RunDockerCommandForContainer(t, "stop", testCase.containerName)
err = testutils.RunDockerCommandForContainer(t, "stop", cassandraConfig.ContainerName)
require.NoError(t, err)

// Wait for the jmx connection to fail. We need to give it time as it might
Expand All @@ -112,7 +97,7 @@ func (s *CassandraLongRunningTestSuite) TestCassandraIntegration_LongRunningInte
log.Info("Waiting for jmx connection to fail")
time.Sleep(60 * time.Second)

err = testutils.RunDockerCommandForContainer(t, "start", testCase.containerName)
err = testutils.RunDockerCommandForContainer(t, "start", cassandraConfig.ContainerName)
require.NoError(t, err)

log.Info("Waiting for cassandra server to be up again")
Expand All @@ -123,5 +108,16 @@ func (s *CassandraLongRunningTestSuite) TestCassandraIntegration_LongRunningInte
testutils.AssertReceivedErrors(t, "connection error", stderr...)

testutils.AssertReceivedPayloadsMatchSchema(t, ctx, output, schemaFile, 30*time.Second)
})
}

func (s *CassandraLongRunningTestSuite) TestCassandraIntegration_LongRunningIntegration() {
t := s.T()

ctx, cancelFn := context.WithTimeout(context.Background(), 600*time.Second)
defer cancelFn()

for _, cassandraConfig := range testutils.CassandraConfigs {
testCassandraIntegration_LongRunningIntegration(t, ctx, cassandraConfig)
}
}
70 changes: 31 additions & 39 deletions tests/integration/cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,52 +54,44 @@ func (s *CassandraTestSuite) TearDownSuite() {
s.cancelComposeCtx()
}

func (s *CassandraTestSuite) TestCassandraIntegration_Success() {
t := s.T()

func testCassandraIntegrationSuccess(t *testing.T, ctx context.Context, cassandraConfig testutils.CassandraConfig) {
testName := t.Name()
testCase := struct {
name string
config map[string]string
schemaFile string
}{
name: fmt.Sprintf("MetricsAndInventoryAreCollected - %s", cassandraConfig.Version),
config: map[string]string{
"CONFIG_PATH": "/etc/cassandra/cassandra-" + cassandraConfig.Version + ".yaml",
"HOSTNAME": cassandraConfig.Hostname,
"TIMEOUT": timeout,
"NRIA_CACHE_PATH": fmt.Sprintf("/tmp/%v.json", testName),
},
schemaFile: "cassandra-schema.json",
}
t.Run(testCase.name, func(t *testing.T) {
stdout, stderr, err := testutils.RunDockerExecCommand(ctx, t, integrationContainerName, []string{integrationBinPath}, testCase.config)
assert.NoError(t, err, "It isn't possible to execute Cassandra integration binary.")

ctx, cancelFn := context.WithTimeout(context.Background(), 30*time.Second)
defer cancelFn()
assert.Empty(t, testutils.FilterStderr(stderr), "Unexpected stderr")

testCases := []struct {
name string
config map[string]string
schemaFile string
cassandraVersion string
}{}
for _, cassandraConfig := range testutils.CassandraConfigs {
testCases = append(testCases, struct {
name string
config map[string]string
schemaFile string
cassandraVersion string
}{
name: "MetricsAndInventoryAreCollected",
config: map[string]string{
"CONFIG_PATH": "/etc/cassandra/cassandra-" + cassandraConfig.Version + ".yaml",
"HOSTNAME": cassandraConfig.Hostname,
"TIMEOUT": timeout,
"NRIA_CACHE_PATH": fmt.Sprintf("/tmp/%v.json", testName),
},
schemaFile: "cassandra-schema.json",
cassandraVersion: cassandraConfig.Version,
})
}
schemaDir := fmt.Sprintf("json-schema-files-%s", cassandraConfig.Version)
schemaPath := filepath.Join(schemaDir, testCase.schemaFile)

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
stdout, stderr, err := testutils.RunDockerExecCommand(ctx, t, integrationContainerName, []string{integrationBinPath}, testCase.config)
assert.NoError(t, err, "It isn't possible to execute Cassandra integration binary.")
err = jsonschema.Validate(schemaPath, stdout)
assert.NoError(t, err, "The output of Cassandra integration doesn't have expected format.")
})
}

assert.Empty(t, testutils.FilterStderr(stderr), "Unexpected stderr")
func (s *CassandraTestSuite) TestCassandraIntegration_Success() {
t := s.T()

schemaDir := fmt.Sprintf("json-schema-files-%s", testCase.cassandraVersion)
schemaPath := filepath.Join(schemaDir, testCase.schemaFile)
ctx, cancelFn := context.WithTimeout(context.Background(), 30*time.Second)
defer cancelFn()

err = jsonschema.Validate(schemaPath, stdout)
assert.NoError(t, err, "The output of Cassandra integration doesn't have expected format.")
})
for _, cassandraConfig := range testutils.CassandraConfigs {
testCassandraIntegrationSuccess(t, ctx, cassandraConfig)
}
}

Expand Down
12 changes: 7 additions & 5 deletions tests/integration/testutils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ const (
)

var (
CassandraConfigs = []struct {
Version string
ContainerName string
Hostname string // Hostname for the Cassandra service. (Will be the cassandra service inside the docker-compose file).
}{
CassandraConfigs = []CassandraConfig{
{
Version: "3.11.0",
ContainerName: "cassandra-3-11-0",
Expand All @@ -57,6 +53,12 @@ var (
}
)

type CassandraConfig struct {
Version string
ContainerName string
Hostname string // Hostname for the Cassandra service. (Will be the cassandra service inside the docker-compose file).
}

// GetIntegrationTestsPath return the absolute path to this project's integration tests.
func GetIntegrationTestsPath() (testsPath string) {
var err error
Expand Down

0 comments on commit 315f660

Please sign in to comment.