Skip to content

Commit

Permalink
[exporter/cassandra] add test for authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
go-follow committed Nov 18, 2023
1 parent c2492f0 commit d77c91a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 27 deletions.
4 changes: 3 additions & 1 deletion exporter/cassandraexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

package cassandraexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/cassandraexporter"
import "go.opentelemetry.io/collector/config/configopaque"
import (
"go.opentelemetry.io/collector/config/configopaque"
)

type Config struct {
DSN string `mapstructure:"dsn"`
Expand Down
29 changes: 16 additions & 13 deletions exporter/cassandraexporter/exporter_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,10 @@ type logsExporter struct {
}

func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) {
cluster := gocql.NewCluster(cfg.DSN)
if cfg.Auth.UserName != "" && cfg.Auth.Password != "" {
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: cfg.Auth.UserName,
Password: string(cfg.Auth.Password),
}
}
session, err := cluster.CreateSession()
cluster := newCluster(cfg)
cluster.Keyspace = cfg.Keyspace
cluster.Consistency = gocql.Quorum
cluster.Port = cfg.Port

session, err := cluster.CreateSession()
if err != nil {
return nil, err
}
Expand All @@ -45,9 +37,7 @@ func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) {

func initializeLogKernel(cfg *Config) error {
ctx := context.Background()
cluster := gocql.NewCluster(cfg.DSN)
cluster.Consistency = gocql.Quorum
cluster.Port = cfg.Port
cluster := newCluster(cfg)

session, err := cluster.CreateSession()
if err != nil {
Expand All @@ -68,6 +58,19 @@ func initializeLogKernel(cfg *Config) error {
return nil
}

func newCluster(cfg *Config) *gocql.ClusterConfig {
cluster := gocql.NewCluster(cfg.DSN)
if cfg.Auth.UserName != "" && cfg.Auth.Password != "" {
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: cfg.Auth.UserName,
Password: string(cfg.Auth.Password),
}
}
cluster.Consistency = gocql.Quorum
cluster.Port = cfg.Port
return cluster
}

func (e *logsExporter) Start(_ context.Context, _ component.Host) error {
initializeErr := initializeLogKernel(e.cfg)
return initializeErr
Expand Down
59 changes: 59 additions & 0 deletions exporter/cassandraexporter/exporter_logs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package cassandraexporter

import (
"github.com/gocql/gocql"
"testing"

"github.com/stretchr/testify/require"
)

func TestNewCluster(t *testing.T) {
testCases := map[string]struct {
cfg *Config
expectedAuthenticator gocql.Authenticator
}{
"empty_auth": {
cfg: withDefaultConfig(),
expectedAuthenticator: nil,
},
"empty_username": {
cfg: withDefaultConfig(func(config *Config) {
config.Auth.Password = "pass"
}),
expectedAuthenticator: nil,
},
"empty_password": {
cfg: withDefaultConfig(func(config *Config) {
config.Auth.UserName = "user"
}),
expectedAuthenticator: nil,
},
"success_auth": {
cfg: withDefaultConfig(func(config *Config) {
config.Auth.UserName = "user"
config.Auth.Password = "pass"
}),
expectedAuthenticator: gocql.PasswordAuthenticator{
Username: "user",
Password: "pass",
},
},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
c := newCluster(test.cfg)
require.Equal(t, test.expectedAuthenticator, c.Authenticator)
})
}
}

func withDefaultConfig(fns ...func(*Config)) *Config {
cfg := createDefaultConfig().(*Config)
for _, fn := range fns {
fn(cfg)
}
return cfg
}
16 changes: 3 additions & 13 deletions exporter/cassandraexporter/exporter_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,10 @@ type tracesExporter struct {
}

func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) {
cluster := gocql.NewCluster(cfg.DSN)
if cfg.Auth.UserName != "" && cfg.Auth.Password != "" {
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: cfg.Auth.UserName,
Password: string(cfg.Auth.Password),
}
}
session, err := cluster.CreateSession()
cluster := newCluster(cfg)
cluster.Keyspace = cfg.Keyspace
cluster.Consistency = gocql.Quorum
cluster.Port = cfg.Port

session, err := cluster.CreateSession()
if err != nil {
return nil, err
}
Expand All @@ -44,9 +36,7 @@ func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error)

func initializeTraceKernel(cfg *Config) error {
ctx := context.Background()
cluster := gocql.NewCluster(cfg.DSN)
cluster.Consistency = gocql.Quorum
cluster.Port = cfg.Port
cluster := newCluster(cfg)

session, err := cluster.CreateSession()
if err != nil {
Expand Down

0 comments on commit d77c91a

Please sign in to comment.