Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm committed Aug 20, 2024
1 parent c83b18c commit f5428ec
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 41 deletions.
40 changes: 3 additions & 37 deletions pkg/cassdcutil/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,12 @@ func ClientEncryptionEnabled(dc *cassdcapi.CassandraDatacenter) bool {
func SubSectionOfCassYaml(dc *cassdcapi.CassandraDatacenter, section string) map[string]*gabs.Container {
config, err := gabs.ParseJSON(dc.Spec.Config)
if err != nil {
return nil
}

cassYaml := config.Path("cassandra-yaml")
if cassYaml == nil {
return make(map[string]*gabs.Container)
}

return cassYaml.Path(section).ChildrenMap()
}

/*
func (dc *CassandraDatacenter) LegacyInternodeEnabled() bool {
config, err := gabs.ParseJSON(dc.Spec.Config)
if err != nil {
return false
}
hasOldKeyStore := func(gobContainer map[string]*gabs.Container) bool {
if gobContainer == nil {
return false
}
if keystorePath, found := gobContainer["keystore"]; found {
if strings.TrimSpace(keystorePath.Data().(string)) == "/etc/encryption/node-keystore.jks" {
return true
}
}
return false
}
if config.Exists("cassandra-yaml", "client_encryption_options") || config.Exists("cassandra-yaml", "server_encryption_options") {
serverContainer := config.Path("cassandra-yaml.server_encryption_options").ChildrenMap()
clientContainer := config.Path("cassandra-yaml.client_encryption_options").ChildrenMap()
if hasOldKeyStore(clientContainer) || hasOldKeyStore(serverContainer) {
return true
}
if !config.Exists("cassandra-yaml") {
return make(map[string]*gabs.Container)
}

return false
return config.Path("cassandra-yaml").Path(section).ChildrenMap()
}
*/
66 changes: 66 additions & 0 deletions pkg/cassdcutil/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,69 @@ func TestClientEncryptionEnabled(t *testing.T) {
assert := assert.New(t)
assert.True(ClientEncryptionEnabled(dc))
}

func TestEmptySubSection(t *testing.T) {
dc := &cassdcapi.CassandraDatacenter{
Spec: cassdcapi.CassandraDatacenterSpec{},
}

assert := assert.New(t)
section := SubSectionOfCassYaml(dc, "client_encryption_options")
assert.NotNil(section)
assert.Equal(0, len(section))

dc.Spec.Config = json.RawMessage(``)
section = SubSectionOfCassYaml(dc, "client_encryption_options")
assert.NotNil(section)
assert.Equal(0, len(section))
}

func TestSubSectionNotMatch(t *testing.T) {
dc := &cassdcapi.CassandraDatacenter{
Spec: cassdcapi.CassandraDatacenterSpec{
Config: json.RawMessage(clientEncryptionEnabled),
},
}

assert := assert.New(t)
section := SubSectionOfCassYaml(dc, "server_encryption_options")
assert.NotNil(section)
assert.Equal(0, len(section))
}

func TestSubSectionPart(t *testing.T) {
dc := &cassdcapi.CassandraDatacenter{
Spec: cassdcapi.CassandraDatacenterSpec{
Config: json.RawMessage(clientEncryptionEnabled),
},
}

assert := assert.New(t)
section := SubSectionOfCassYaml(dc, "client_encryption_options")
assert.NotNil(section)
assert.Equal(6, len(section))

enabled, ok := section["enabled"].Data().(bool)
assert.True(ok)
assert.True(enabled)

keystore, ok := section["keystore"].Data().(string)
assert.True(ok)
assert.Equal("/etc/encryption/node-keystore.jks", keystore)

keystorePassword, ok := section["keystore_password"].Data().(string)
assert.True(ok)
assert.Equal("dc2", keystorePassword)

truststore, ok := section["truststore"].Data().(string)
assert.True(ok)
assert.Equal("/etc/encryption/node-keystore.jks", truststore)

truststorePassword, ok := section["truststore_password"].Data().(string)
assert.True(ok)
assert.Equal("dc2", truststorePassword)

optional, ok := section["optional"].Data().(bool)
assert.True(ok)
assert.False(optional)
}
9 changes: 5 additions & 4 deletions pkg/cassdcutil/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cassdcutil

import (
"context"
"strings"

corev1 "k8s.io/api/core/v1"

Expand Down Expand Up @@ -32,10 +33,10 @@ func (c *CassManager) CassandraAuthDetails(ctx context.Context, cassdc *cassdcap

if ClientEncryptionEnabled(cassdc) {
encryptionOptions := SubSectionOfCassYaml(cassdc, "client_encryption_options")
auth.KeystorePath = encryptionOptions["keystore"].Data().(string)
auth.KeystorePassword = encryptionOptions["keystore_password"].Data().(string)
auth.TruststorePath = encryptionOptions["truststore"].Data().(string)
auth.TruststorePassword = encryptionOptions["truststore_password"].Data().(string)
auth.KeystorePath = strings.TrimSpace(encryptionOptions["keystore"].Data().(string))
auth.KeystorePassword = strings.TrimSpace(encryptionOptions["keystore_password"].Data().(string))
auth.TruststorePath = strings.TrimSpace(encryptionOptions["truststore"].Data().(string))
auth.TruststorePassword = strings.TrimSpace(encryptionOptions["truststore_password"].Data().(string))
}

return auth, nil
Expand Down
57 changes: 57 additions & 0 deletions pkg/cassdcutil/secrets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cassdcutil

import (
"context"
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1"
)

func TestCassandraAuthDetails(t *testing.T) {
scheme := runtime.NewScheme()
clientgoscheme.AddToScheme(scheme)

Check failure on line 20 in pkg/cassdcutil/secrets_test.go

View workflow job for this annotation

GitHub Actions / Unit testing and linting

Error return value is not checked (errcheck)
cassdcapi.AddToScheme(scheme)

Check failure on line 21 in pkg/cassdcutil/secrets_test.go

View workflow job for this annotation

GitHub Actions / Unit testing and linting

Error return value is not checked (errcheck)
assert := assert.New(t)

cassdc := &cassdcapi.CassandraDatacenter{
ObjectMeta: metav1.ObjectMeta{
Name: "test-dc",
},
Spec: cassdcapi.CassandraDatacenterSpec{
ClusterName: "test-cluster",
SuperuserSecretName: "test-secret",
Config: json.RawMessage(clientEncryptionEnabled),
},
}
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "test-secret",
},
Data: map[string][]byte{
"username": []byte("test-cluster-superuser"),
"password": []byte("cryptic-password"),
},
}

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(cassdc, secret).Build()
cassManager := &CassManager{client: client}

authDetails, err := cassManager.CassandraAuthDetails(context.TODO(), cassdc)
assert.NoError(err)
assert.NotNil(authDetails)

assert.Equal("test-cluster-superuser", authDetails.Username)
assert.Equal("cryptic-password", authDetails.Password)
assert.Equal("/etc/encryption/node-keystore.jks", authDetails.KeystorePath)
assert.Equal("dc2", authDetails.KeystorePassword)
assert.Equal("/etc/encryption/node-keystore.jks", authDetails.TruststorePath)
assert.Equal("dc2", authDetails.TruststorePassword)
}

0 comments on commit f5428ec

Please sign in to comment.