Files
my-rook-config/pkg/operator/ceph/csi/ceph_connection_test.go
T
Travis Nielsen 76e9c04b36 csi: disable read affinity for ceph v20.2.0
Read affinity in Ceph v20.2.0 can cause corruption.
Disable this feature for this version of Ceph to
avoid hitting any corruption in this case. When the next
patch release is out for tentacle, this issue will be
resolved and when upgrading to it, read affinity would
automatically be re-enabled if needed.

Signed-off-by: Travis Nielsen <tnielsen@redhat.com>
2026-01-12 13:46:43 -07:00

200 lines
5.9 KiB
Go

/*
Copyright 2024 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package csi
import (
"context"
"strings"
"testing"
csiopv1 "github.com/ceph/ceph-csi-operator/api/v1"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/client/clientset/versioned/scheme"
clienttest "github.com/rook/rook/pkg/daemon/ceph/client/test"
"github.com/rook/rook/pkg/operator/ceph/cluster/osd/topology"
cephver "github.com/rook/rook/pkg/operator/ceph/version"
"github.com/rook/rook/pkg/operator/k8sutil"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
func TestCreateUpdateCephConnection(t *testing.T) {
c := clienttest.CreateTestClusterInfo(3)
ns := "test"
c.Namespace = ns
c.SetName("testcluster")
c.NamespacedName()
t.Setenv(k8sutil.PodNamespaceEnvVar, ns)
cluster := &cephv1.CephCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "testCluster",
Namespace: ns,
},
Spec: cephv1.ClusterSpec{
CSI: cephv1.CSIDriverSpec{
ReadAffinity: cephv1.ReadAffinitySpec{
Enabled: true,
CrushLocationLabels: []string{"kubernetes.io/hostname"},
},
},
},
}
csiCephConnection := &csiopv1.CephConnection{}
// Register operator types with the runtime scheme.
s := scheme.Scheme
s.AddKnownTypes(cephv1.SchemeGroupVersion, csiCephConnection, &cephv1.CephCluster{}, &cephv1.CephRBDMirrorList{})
object := []runtime.Object{
cluster,
}
// Create a fake client to mock API calls.
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build()
err := CreateUpdateCephConnection(cl, c, cluster.Spec)
assert.NoError(t, err)
// When no RBDMirror is created
err = cl.Get(context.TODO(), types.NamespacedName{Name: ns, Namespace: ns}, csiCephConnection)
assert.NoError(t, err)
assert.Equal(t, csiCephConnection.Spec.RbdMirrorDaemonCount, 0)
rbdMirror := &cephv1.CephRBDMirror{
ObjectMeta: metav1.ObjectMeta{
Name: "test-mirror",
Namespace: ns,
},
Spec: cephv1.RBDMirroringSpec{
Count: 1,
},
}
object = []runtime.Object{
rbdMirror,
cluster,
}
err = cl.Create(context.TODO(), rbdMirror)
assert.NoError(t, err)
// Create a fake client to mock API calls.
cl = fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build()
err = CreateUpdateCephConnection(cl, c, cluster.Spec)
assert.NoError(t, err)
// When RBDMirror is created
err = cl.Get(context.TODO(), types.NamespacedName{Name: ns, Namespace: ns}, csiCephConnection)
assert.NoError(t, err)
assert.Equal(t, 1, csiCephConnection.Spec.RbdMirrorDaemonCount)
}
func TestCephConnectionDefaultTopology(t *testing.T) {
c := clienttest.CreateTestClusterInfo(3)
ns := "test"
c.Namespace = ns
c.SetName("testcluster")
c.NamespacedName()
t.Setenv(k8sutil.PodNamespaceEnvVar, ns)
cluster := &cephv1.CephCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "testCluster",
Namespace: ns,
},
Spec: cephv1.ClusterSpec{
CSI: cephv1.CSIDriverSpec{
ReadAffinity: cephv1.ReadAffinitySpec{
Enabled: true,
},
},
},
}
csiCephConnection := &csiopv1.CephConnection{}
// Register operator types with the runtime scheme.
s := scheme.Scheme
s.AddKnownTypes(cephv1.SchemeGroupVersion, csiCephConnection, &cephv1.CephCluster{}, &cephv1.CephRBDMirrorList{})
object := []runtime.Object{
cluster,
}
// Create a fake client to mock API calls.
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build()
err := CreateUpdateCephConnection(cl, c, cluster.Spec)
assert.NoError(t, err)
err = cl.Get(context.TODO(), types.NamespacedName{Name: ns, Namespace: ns}, csiCephConnection)
assert.NoError(t, err)
assert.Equal(t, 10, len(csiCephConnection.Spec.ReadAffinity.CrushLocationLabels))
labels := map[string]bool{}
for _, label := range csiCephConnection.Spec.ReadAffinity.CrushLocationLabels {
labels[label] = true
}
for defaultLabel := range strings.SplitSeq(topology.GetDefaultTopologyLabels(), ",") {
assert.True(t, labels[defaultLabel], "expected label %q not found", defaultLabel)
}
}
func TestReadAffinityEnabled(t *testing.T) {
tests := []struct {
name string
enabledField bool
cephVersion cephver.CephVersion
expected bool
}{
{
name: "enabled field false, ceph version < 20.2.0",
enabledField: false,
cephVersion: cephver.CephVersion{Major: 19, Minor: 2, Extra: 0},
expected: false,
},
{
name: "enabled field true, ceph version < 20.2.0",
enabledField: true,
cephVersion: cephver.CephVersion{Major: 19, Minor: 2, Extra: 0},
expected: true,
},
{
name: "enabled field true, ceph version == 20.2.0",
enabledField: true,
cephVersion: cephver.CephVersion{Major: 20, Minor: 2, Extra: 0},
expected: false,
},
{
name: "enabled field true, ceph version > 20.2.0",
enabledField: true,
cephVersion: cephver.CephVersion{Major: 20, Minor: 2, Extra: 1},
expected: true,
},
{
name: "enabled field false, ceph version > 20.2.0",
enabledField: false,
cephVersion: cephver.CephVersion{Major: 20, Minor: 2, Extra: 1},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ReadAffinityEnabled(tt.enabledField, tt.cephVersion)
assert.Equal(t, tt.expected, result)
})
}
}