2021-08-06 17:46:01 +02:00
|
|
|
/*
|
|
|
|
|
Copyright 2021 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"
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2025-08-28 12:27:32 -06:00
|
|
|
csiopv1 "github.com/ceph/ceph-csi-operator/api/v1"
|
2021-08-06 17:46:01 +02:00
|
|
|
"github.com/coreos/pkg/capnslog"
|
|
|
|
|
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
|
|
|
|
|
rookclient "github.com/rook/rook/pkg/client/clientset/versioned/fake"
|
|
|
|
|
"github.com/rook/rook/pkg/client/clientset/versioned/scheme"
|
|
|
|
|
"github.com/rook/rook/pkg/clusterd"
|
|
|
|
|
"github.com/rook/rook/pkg/operator/ceph/controller"
|
|
|
|
|
"github.com/rook/rook/pkg/operator/k8sutil"
|
|
|
|
|
"github.com/rook/rook/pkg/operator/test"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
|
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"
|
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCephCSIController(t *testing.T) {
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
var (
|
|
|
|
|
name = "rook-ceph"
|
|
|
|
|
namespace = "rook-ceph"
|
|
|
|
|
)
|
|
|
|
|
capnslog.SetGlobalLogLevel(capnslog.DEBUG)
|
|
|
|
|
os.Setenv("ROOK_LOG_LEVEL", "DEBUG")
|
2022-07-15 22:44:09 +08:00
|
|
|
t.Setenv(k8sutil.PodNameEnvVar, "rook-ceph-operator")
|
|
|
|
|
t.Setenv(k8sutil.PodNamespaceEnvVar, namespace)
|
2021-08-06 17:46:01 +02:00
|
|
|
|
|
|
|
|
req := reconcile.Request{
|
|
|
|
|
NamespacedName: types.NamespacedName{
|
|
|
|
|
Name: name,
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Run("failure because no CephCluster", func(t *testing.T) {
|
|
|
|
|
fakeClientSet := test.New(t, 1)
|
|
|
|
|
test.SetFakeKubernetesVersion(fakeClientSet, "v1.21.0")
|
|
|
|
|
c := &clusterd.Context{
|
|
|
|
|
Clientset: fakeClientSet,
|
|
|
|
|
RookClientset: rookclient.NewSimpleClientset(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := c.Clientset.CoreV1().Pods(namespace).Create(ctx, test.FakeOperatorPod(namespace), metav1.CreateOptions{})
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
_, err = c.Clientset.AppsV1().ReplicaSets(namespace).Create(context.TODO(), test.FakeReplicaSet(namespace), metav1.CreateOptions{})
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
s := scheme.Scheme
|
2025-08-28 12:27:32 -06:00
|
|
|
s.AddKnownTypes(cephv1.SchemeGroupVersion, &v1.ConfigMap{}, &v1.ConfigMapList{}, &cephv1.CephClusterList{},
|
|
|
|
|
&csiopv1.CephConnection{}, &csiopv1.ClientProfile{}, &csiopv1.OperatorConfig{}, &csiopv1.Driver{})
|
2021-08-06 17:46:01 +02:00
|
|
|
|
|
|
|
|
cl := fake.NewClientBuilder().WithScheme(s).Build()
|
|
|
|
|
c.Client = cl
|
|
|
|
|
|
|
|
|
|
r := &ReconcileCSI{
|
|
|
|
|
client: cl,
|
|
|
|
|
context: c,
|
|
|
|
|
opConfig: controller.OperatorConfig{
|
|
|
|
|
OperatorNamespace: namespace,
|
|
|
|
|
Image: "rook",
|
|
|
|
|
ServiceAccount: "foo",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := r.Reconcile(ctx, req)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.False(t, res.Requeue)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("success ceph csi deployment", func(t *testing.T) {
|
|
|
|
|
fakeClientSet := test.New(t, 1)
|
|
|
|
|
test.SetFakeKubernetesVersion(fakeClientSet, "v1.21.0")
|
|
|
|
|
c := &clusterd.Context{
|
2026-03-10 15:41:57 +05:30
|
|
|
Clientset: fakeClientSet,
|
|
|
|
|
RookClientset: rookclient.NewSimpleClientset(),
|
2021-08-06 17:46:01 +02:00
|
|
|
}
|
|
|
|
|
_, err := c.Clientset.CoreV1().Pods(namespace).Create(ctx, test.FakeOperatorPod(namespace), metav1.CreateOptions{})
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
_, err = c.Clientset.AppsV1().ReplicaSets(namespace).Create(context.TODO(), test.FakeReplicaSet(namespace), metav1.CreateOptions{})
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
cephCluster := &cephv1.CephCluster{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: namespace,
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
},
|
|
|
|
|
Status: cephv1.ClusterStatus{
|
|
|
|
|
Phase: "",
|
|
|
|
|
CephVersion: &cephv1.ClusterVersion{
|
|
|
|
|
Version: "14.2.9-0",
|
|
|
|
|
},
|
|
|
|
|
CephStatus: &cephv1.CephStatus{
|
|
|
|
|
Health: "",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2024-03-04 11:08:11 -07:00
|
|
|
secrets := map[string][]byte{
|
|
|
|
|
"fsid": []byte(name),
|
|
|
|
|
"mon-secret": []byte("monsecret"),
|
|
|
|
|
"admin-secret": []byte("adminsecret"),
|
|
|
|
|
}
|
|
|
|
|
secret := &v1.Secret{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: "rook-ceph-mon",
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
},
|
|
|
|
|
Data: secrets,
|
|
|
|
|
Type: k8sutil.RookType,
|
|
|
|
|
}
|
|
|
|
|
_, err = c.Clientset.CoreV1().Secrets(namespace).Create(ctx, secret, metav1.CreateOptions{})
|
|
|
|
|
assert.NoError(t, err)
|
2021-08-06 17:46:01 +02:00
|
|
|
s := scheme.Scheme
|
2023-07-07 22:38:20 +05:30
|
|
|
s.AddKnownTypes(cephv1.SchemeGroupVersion, &cephv1.CephCluster{}, &cephv1.CephClusterList{}, &v1.ConfigMap{})
|
2021-08-06 17:46:01 +02:00
|
|
|
object := []runtime.Object{
|
|
|
|
|
cephCluster,
|
|
|
|
|
}
|
|
|
|
|
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build()
|
|
|
|
|
c.Client = cl
|
|
|
|
|
r := &ReconcileCSI{
|
|
|
|
|
client: cl,
|
|
|
|
|
context: c,
|
|
|
|
|
opConfig: controller.OperatorConfig{
|
|
|
|
|
OperatorNamespace: namespace,
|
|
|
|
|
Image: "rook",
|
|
|
|
|
ServiceAccount: "foo",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := r.Reconcile(ctx, req)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.False(t, res.Requeue)
|
2022-04-14 09:50:32 +02:00
|
|
|
})
|
2021-08-06 17:46:01 +02:00
|
|
|
}
|