2019-06-17 16:00:27 -06:00
|
|
|
/*
|
|
|
|
|
Copyright 2019 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 k8sutil
|
|
|
|
|
|
|
|
|
|
import (
|
2020-11-16 17:29:56 +05:30
|
|
|
"context"
|
2020-02-12 14:07:03 +05:30
|
|
|
"os"
|
2019-06-17 16:00:27 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-02-12 14:07:03 +05:30
|
|
|
v1 "k8s.io/api/core/v1"
|
2019-06-17 16:00:27 -06:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
"k8s.io/client-go/kubernetes/fake"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDeleteConfigMap(t *testing.T) {
|
2026-01-05 12:02:18 +00:00
|
|
|
k8s := fake.NewClientset()
|
2020-11-16 17:29:56 +05:30
|
|
|
ctx := context.TODO()
|
2019-06-17 16:00:27 -06:00
|
|
|
|
|
|
|
|
cm := &v1.ConfigMap{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: "test-configmap",
|
|
|
|
|
Namespace: "test-namespace",
|
|
|
|
|
},
|
|
|
|
|
Data: map[string]string{
|
|
|
|
|
"test": "data",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 17:29:56 +05:30
|
|
|
_, err := k8s.CoreV1().ConfigMaps("test-namespace").Create(ctx, cm, metav1.CreateOptions{})
|
2019-06-17 16:00:27 -06:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// There is no need to test all permutations, as the `DeleteResource` function is already
|
|
|
|
|
// tested. Setting Wait=true and ErrorOnTimeout=true will cause both the delete and verify
|
|
|
|
|
// functions to be exercised, and it will return error if either fail with an unexpected error.
|
|
|
|
|
opts := &DeleteOptions{}
|
|
|
|
|
opts.Wait = true
|
|
|
|
|
opts.ErrorOnTimeout = true
|
2021-12-11 11:21:55 +09:00
|
|
|
err = DeleteConfigMap(ctx, k8s, "test-configmap", "test-namespace", opts)
|
2019-06-17 16:00:27 -06:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
2020-11-16 17:29:56 +05:30
|
|
|
_, err = k8s.CoreV1().ConfigMaps("test-namespace").Get(ctx, "test-configmap", metav1.GetOptions{})
|
2019-06-17 16:00:27 -06:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
assert.True(t, errors.IsNotFound(err))
|
|
|
|
|
}
|
2020-02-12 14:07:03 +05:30
|
|
|
|
|
|
|
|
func TestGetOperatorSetting(t *testing.T) {
|
2026-01-05 12:02:18 +00:00
|
|
|
k8s := fake.NewClientset()
|
2020-11-16 17:29:56 +05:30
|
|
|
ctx := context.TODO()
|
2020-02-12 14:07:03 +05:30
|
|
|
|
2025-02-21 18:02:00 -07:00
|
|
|
operatorSettingConfigMapName := "rook-ceph-operator-config"
|
2020-02-24 18:13:36 +05:30
|
|
|
testNamespace := "test-namespace"
|
2020-02-12 14:07:03 +05:30
|
|
|
cm := &v1.ConfigMap{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2020-03-05 11:50:22 +05:30
|
|
|
Name: operatorSettingConfigMapName,
|
2020-02-24 18:13:36 +05:30
|
|
|
Namespace: testNamespace,
|
2020-02-12 14:07:03 +05:30
|
|
|
},
|
|
|
|
|
Data: map[string]string{
|
2020-02-24 18:13:36 +05:30
|
|
|
"NODE_AFFINITY": "storage=rook, worker",
|
2020-02-12 14:07:03 +05:30
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nodeAffinity := "NODE_AFFINITY"
|
|
|
|
|
podAffinity := "POD_AFFINITY"
|
|
|
|
|
envSettingValue := "role=storage-node"
|
2020-02-24 18:13:36 +05:30
|
|
|
cmSettingValue := "storage=rook, worker"
|
2020-02-12 14:07:03 +05:30
|
|
|
defaultValue := ""
|
|
|
|
|
|
2025-02-21 18:02:00 -07:00
|
|
|
// ConfigMap is not yet applied so we expect the default value
|
|
|
|
|
setting := GetOperatorSetting(nodeAffinity, defaultValue)
|
2020-02-12 14:07:03 +05:30
|
|
|
assert.Equal(t, defaultValue, setting)
|
2025-02-21 18:02:00 -07:00
|
|
|
|
2020-02-12 14:07:03 +05:30
|
|
|
// Env Var exists
|
2022-07-15 22:44:09 +08:00
|
|
|
t.Setenv(nodeAffinity, envSettingValue)
|
2025-02-21 18:02:00 -07:00
|
|
|
setting = GetOperatorSetting(nodeAffinity, defaultValue)
|
2020-02-12 14:07:03 +05:30
|
|
|
assert.Equal(t, envSettingValue, setting)
|
|
|
|
|
|
|
|
|
|
// ConfigMap is found
|
2022-07-15 22:44:09 +08:00
|
|
|
t.Setenv("POD_NAMESPACE", testNamespace)
|
2025-02-21 18:02:00 -07:00
|
|
|
_, err := k8s.CoreV1().ConfigMaps(testNamespace).Create(ctx, cm, metav1.CreateOptions{})
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
err = ApplyOperatorSettingsConfigmap(ctx, k8s)
|
2020-02-12 14:07:03 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Setting exists in ConfigMap
|
2025-02-21 18:02:00 -07:00
|
|
|
setting = GetOperatorSetting(nodeAffinity, defaultValue)
|
2020-02-12 14:07:03 +05:30
|
|
|
assert.Equal(t, cmSettingValue, setting)
|
2025-02-21 18:02:00 -07:00
|
|
|
|
2020-02-12 14:07:03 +05:30
|
|
|
// Env Var doesn't exist
|
|
|
|
|
err = os.Unsetenv(nodeAffinity)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Equal(t, cmSettingValue, setting)
|
|
|
|
|
|
2025-02-21 18:02:00 -07:00
|
|
|
// Setting doesn't exist in ConfigMap, expect the default value
|
|
|
|
|
setting = GetOperatorSetting(podAffinity, defaultValue)
|
2020-02-12 14:07:03 +05:30
|
|
|
assert.Equal(t, defaultValue, setting)
|
|
|
|
|
// Env Var exists
|
2022-07-15 22:44:09 +08:00
|
|
|
t.Setenv(podAffinity, envSettingValue)
|
2025-02-21 18:02:00 -07:00
|
|
|
setting = GetOperatorSetting(podAffinity, defaultValue)
|
2020-02-12 14:07:03 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Equal(t, envSettingValue, setting)
|
|
|
|
|
}
|
2023-08-07 20:09:10 +05:30
|
|
|
|
2023-06-22 09:59:34 +05:30
|
|
|
func TestCreateOrUpdateConfigMap(t *testing.T) {
|
2026-01-05 12:02:18 +00:00
|
|
|
k8s := fake.NewClientset()
|
2023-06-22 09:59:34 +05:30
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
|
|
cm := &v1.ConfigMap{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: "test-configmap",
|
|
|
|
|
Namespace: "test-namespace",
|
|
|
|
|
},
|
|
|
|
|
Data: map[string]string{
|
|
|
|
|
"test": "data",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := CreateOrUpdateConfigMap(ctx, k8s, cm)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
actualCM, err := k8s.CoreV1().ConfigMaps("test-namespace").Get(ctx, "test-configmap", metav1.GetOptions{})
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.NotNil(t, actualCM)
|
|
|
|
|
assert.Equal(t, "data", actualCM.Data["test"])
|
|
|
|
|
|
|
|
|
|
// update config map
|
|
|
|
|
cm.Data["test"] = "updatedData"
|
|
|
|
|
_, err = CreateOrUpdateConfigMap(ctx, k8s, cm)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
actualCM, err = k8s.CoreV1().ConfigMaps("test-namespace").Get(ctx, "test-configmap", metav1.GetOptions{})
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.NotNil(t, actualCM)
|
|
|
|
|
assert.Equal(t, "updatedData", actualCM.Data["test"])
|
|
|
|
|
}
|