Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
Signed-off-by: Karol Szwaj <[email protected]>
  • Loading branch information
cnvergence committed Nov 28, 2023
1 parent 55e2646 commit dc08a94
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
6 changes: 3 additions & 3 deletions apis/k8ssandra/v1alpha1/k8ssandracluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,9 @@ const (
ServerDistributionDse = ServerDistribution("dse")
)

func (kc *K8ssandraCluster) DefaultNumTokens(serverVersion *semver.Version) int64 {
func (kc *K8ssandraCluster) DefaultNumTokens(serverVersion *semver.Version) float64 {
if kc.Spec.Cassandra.ServerType == ServerDistributionCassandra && serverVersion.Major() == 3 {
return int64(256)
return float64(256)
}
return int64(16)
return float64(16)

Check warning on line 533 in apis/k8ssandra/v1alpha1/k8ssandracluster_types.go

View check run for this annotation

Codecov / codecov/patch

apis/k8ssandra/v1alpha1/k8ssandracluster_types.go#L533

Added line #L533 was not covered by tests
}
7 changes: 5 additions & 2 deletions apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,20 @@ func (r *K8ssandraCluster) ValidateUpdate(old runtime.Object) error {
if oldCassConfig != nil && newCassConfig != nil {
oldNumTokens, oldNumTokensExists := oldCassConfig.CassandraYaml["num_tokens"]
newNumTokens, newNumTokensExists := newCassConfig.CassandraYaml["num_tokens"]

// Check if num_tokens is not present in old configuration
if !oldNumTokensExists {
// Determine the default num_tokens based on Cassandra version
cassVersion, err := semver.NewVersion(oldCluster.Spec.Cassandra.ServerVersion)
if err != nil {
return err
}
defaultNumTokens := oldCluster.DefaultNumTokens(cassVersion)
if newNumTokensExists && int64(newNumTokens.(float64)) != defaultNumTokens {
// If new configuration has num_tokens and it's not equal to the default, return an error
if newNumTokensExists && newNumTokens.(float64) != defaultNumTokens {
return ErrNumTokens
}

Check warning on line 132 in apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go

View check run for this annotation

Codecov / codecov/patch

apis/k8ssandra/v1alpha1/k8ssandracluster_webhook.go#L131-L132

Added lines #L131 - L132 were not covered by tests
} else {
// If num_tokens is present in both old and new configurations, check if they are equal
if oldNumTokens != newNumTokens {
return ErrNumTokens
}
Expand Down
13 changes: 13 additions & 0 deletions apis/k8ssandra/v1alpha1/k8ssandracluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ func testNumTokensInUpdate(t *testing.T) {
// This should be acceptable change, since 3.11.10 defaulted to 256 and so it is the same value
err = k8sClient.Update(ctx, cluster)
require.NoError(err)
cluster2 := createMinimalClusterObj("numtokens-wrong-test-update", "numtokensupdate-namespace")
cluster2.Spec.Cassandra.ServerVersion = "3.11.10"
cluster2.Spec.Cassandra.DatacenterOptions.CassandraConfig = &CassandraConfig{}
err = k8sClient.Create(ctx, cluster2)
require.NoError(err)

// Now update to 4.1.3
cluster.Spec.Cassandra.DatacenterOptions.CassandraConfig.CassandraYaml = unstructured.Unstructured{"num_tokens": 33}
cluster.Spec.Cassandra.ServerVersion = "4.1.3"

// This should be disallowed, since it is changing default num_tokens
err = k8sClient.Update(ctx, cluster)
require.Error(err)
}

func testReaperKeyspaceValidation(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/cassandra/datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,11 @@ func ValidateConfig(kc *api.K8ssandraCluster, desiredDc, actualDc *cassdcapi.Cas
if cassConfig := kc.Spec.Cassandra.CassandraConfig; cassConfig == nil {
if semver.MustParse(actualDc.Spec.ServerVersion).Major() == 3 && semver.MustParse(desiredDc.Spec.ServerVersion).Major() > 3 {
desiredCassYaml["num_tokens"] = actualCassYaml["num_tokens"]
// Assigning desiredCassYaml to desiredDc.Spec.Config
desiredConfig["cassandra-yaml"] = desiredCassYaml
newConfig, _ := json.Marshal(desiredConfig)
newConfig, err := json.Marshal(desiredConfig)
if err != nil {
return nil, err
}
desiredDc.Spec.Config = newConfig

Check warning on line 522 in pkg/cassandra/datacenter.go

View check run for this annotation

Codecov / codecov/patch

pkg/cassandra/datacenter.go#L514-L522

Added lines #L514 - L522 were not covered by tests
}
}
Expand Down

0 comments on commit dc08a94

Please sign in to comment.