2016-11-05 17:09:05 -07:00
|
|
|
/*
|
|
|
|
|
Copyright 2016 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.
|
|
|
|
|
*/
|
2018-08-31 13:39:02 -06:00
|
|
|
|
2020-07-01 22:46:39 -06:00
|
|
|
package client
|
2016-10-26 09:48:25 -07:00
|
|
|
|
|
|
|
|
import (
|
2020-11-16 17:29:56 +05:30
|
|
|
"context"
|
2017-01-04 09:01:47 -08:00
|
|
|
"path/filepath"
|
2016-10-26 09:48:25 -07:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2020-09-24 20:33:20 +05:30
|
|
|
"github.com/rook/rook/pkg/operator/k8sutil"
|
|
|
|
|
"github.com/rook/rook/pkg/operator/test"
|
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
|
2017-01-04 09:01:47 -08:00
|
|
|
"github.com/go-ini/ini"
|
|
|
|
|
"github.com/rook/rook/pkg/clusterd"
|
2019-03-15 14:22:09 -07:00
|
|
|
cephver "github.com/rook/rook/pkg/operator/ceph/version"
|
2016-10-26 09:48:25 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCreateDefaultCephConfig(t *testing.T) {
|
|
|
|
|
clusterInfo := &ClusterInfo{
|
|
|
|
|
FSID: "id",
|
|
|
|
|
MonitorSecret: "monsecret",
|
2020-07-10 16:40:16 -06:00
|
|
|
Namespace: "foo-cluster",
|
2018-08-31 13:39:02 -06:00
|
|
|
Monitors: map[string]*MonInfo{
|
2019-01-17 14:37:58 -07:00
|
|
|
"node0": {Name: "mon0", Endpoint: "10.0.0.1:6789"},
|
|
|
|
|
"node1": {Name: "mon1", Endpoint: "10.0.0.2:6789"},
|
2016-10-26 09:48:25 -07:00
|
|
|
},
|
2022-07-01 10:38:15 -06:00
|
|
|
CephVersion: cephver.Quincy,
|
2016-10-26 09:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
2017-01-30 21:13:05 -08:00
|
|
|
// start with INFO level logging
|
2021-09-23 10:56:42 -06:00
|
|
|
context := &clusterd.Context{}
|
2017-01-30 21:13:05 -08:00
|
|
|
|
2019-09-24 10:21:05 +02:00
|
|
|
cephConfig, err := CreateDefaultCephConfig(context, clusterInfo)
|
2019-04-09 17:40:28 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to create default ceph config. %+v", err)
|
|
|
|
|
}
|
2019-01-24 14:58:46 +01:00
|
|
|
verifyConfig(t, cephConfig, clusterInfo, 0)
|
2016-10-26 09:48:25 -07:00
|
|
|
|
2019-09-24 10:21:05 +02:00
|
|
|
cephConfig, err = CreateDefaultCephConfig(context, clusterInfo)
|
2019-04-09 17:40:28 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to create default ceph config. %+v", err)
|
|
|
|
|
}
|
2019-01-24 14:58:46 +01:00
|
|
|
verifyConfig(t, cephConfig, clusterInfo, 10)
|
2016-10-26 09:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
2017-01-04 09:01:47 -08:00
|
|
|
func TestGenerateConfigFile(t *testing.T) {
|
2020-11-16 17:29:56 +05:30
|
|
|
ctx := context.TODO()
|
2017-01-04 09:01:47 -08:00
|
|
|
// set up a temporary config directory that will be cleaned up after test
|
2022-01-24 23:31:04 +08:00
|
|
|
configDir := t.TempDir()
|
2017-01-04 09:01:47 -08:00
|
|
|
|
|
|
|
|
// create mocked cluster context and info
|
2020-09-14 18:42:26 +02:00
|
|
|
clientset := test.New(t, 3)
|
|
|
|
|
|
2017-01-04 09:01:47 -08:00
|
|
|
context := &clusterd.Context{
|
2019-08-05 15:06:47 -06:00
|
|
|
ConfigDir: configDir,
|
2020-09-14 18:42:26 +02:00
|
|
|
Clientset: clientset,
|
2017-01-04 09:01:47 -08:00
|
|
|
}
|
2020-09-14 18:42:26 +02:00
|
|
|
|
|
|
|
|
ns := "foo-cluster"
|
|
|
|
|
data := make(map[string]string, 1)
|
|
|
|
|
data["config"] = "[global]\n bluestore_min_alloc_size_hdd = 4096"
|
|
|
|
|
cm := &v1.ConfigMap{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: k8sutil.ConfigOverrideName,
|
|
|
|
|
Namespace: ns,
|
|
|
|
|
},
|
|
|
|
|
Data: data,
|
|
|
|
|
}
|
2022-01-24 23:31:04 +08:00
|
|
|
_, err := clientset.CoreV1().ConfigMaps(ns).Create(ctx, cm, metav1.CreateOptions{})
|
2020-09-24 20:33:20 +05:30
|
|
|
assert.NoError(t, err)
|
2020-09-14 18:42:26 +02:00
|
|
|
|
2017-01-04 09:01:47 -08:00
|
|
|
clusterInfo := &ClusterInfo{
|
|
|
|
|
FSID: "myfsid",
|
|
|
|
|
MonitorSecret: "monsecret",
|
2020-09-14 18:42:26 +02:00
|
|
|
Namespace: ns,
|
2018-08-31 13:39:02 -06:00
|
|
|
Monitors: map[string]*MonInfo{
|
2019-01-17 14:37:58 -07:00
|
|
|
"node0": {Name: "mon0", Endpoint: "10.0.0.1:6789"},
|
2017-01-04 09:01:47 -08:00
|
|
|
},
|
2022-07-01 10:38:15 -06:00
|
|
|
CephVersion: cephver.Quincy,
|
2020-06-24 22:42:38 -06:00
|
|
|
CephCred: CephCred{Username: "admin", Secret: "mysecret"},
|
2021-08-06 17:46:01 +02:00
|
|
|
Context: ctx,
|
2017-01-04 09:01:47 -08:00
|
|
|
}
|
|
|
|
|
|
2022-06-28 14:22:10 +02:00
|
|
|
isInitialized := clusterInfo.IsInitialized()
|
|
|
|
|
assert.NoError(t, isInitialized)
|
2020-04-09 17:03:45 +02:00
|
|
|
|
2017-01-04 09:01:47 -08:00
|
|
|
// generate the config file to disk now
|
2020-07-01 22:46:39 -06:00
|
|
|
configFilePath, err := generateConfigFile(context, clusterInfo, configDir, filepath.Join(configDir, "mykeyring"), nil, nil)
|
2017-01-04 09:01:47 -08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, filepath.Join(configDir, "foo-cluster.config"), configFilePath)
|
|
|
|
|
|
|
|
|
|
// verify some of the contents of written config file by loading it from disk
|
|
|
|
|
actualConf, err := ini.Load(configFilePath)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
verifyConfigValue(t, actualConf, "global", "fsid", clusterInfo.FSID)
|
2020-09-14 18:42:26 +02:00
|
|
|
verifyConfigValue(t, actualConf, "global", "bluestore_min_alloc_size_hdd", "4096")
|
2017-01-04 09:01:47 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 14:58:46 +01:00
|
|
|
func verifyConfig(t *testing.T, cephConfig *CephConfig, cluster *ClusterInfo, loggingLevel int) {
|
|
|
|
|
monMembers := make([]string, len(cluster.Monitors))
|
|
|
|
|
i := 0
|
|
|
|
|
for _, expectedMon := range cluster.Monitors {
|
2016-10-26 09:48:25 -07:00
|
|
|
contained := false
|
2019-01-24 14:58:46 +01:00
|
|
|
monMembers[i] = expectedMon.Name
|
2016-10-26 09:48:25 -07:00
|
|
|
for _, actualMon := range strings.Split(cephConfig.MonMembers, " ") {
|
2019-01-24 14:58:46 +01:00
|
|
|
if expectedMon.Name == actualMon {
|
|
|
|
|
contained = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.True(t, contained)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Testing mon_host
|
|
|
|
|
|
2020-02-11 17:20:06 +01:00
|
|
|
expectedMons := "[v2:10.0.0.1:3300,v1:10.0.0.1:6789],[v2:10.0.0.2:3300,v1:10.0.0.2:6789]"
|
2019-01-24 14:58:46 +01:00
|
|
|
|
|
|
|
|
for _, expectedMon := range strings.Split(expectedMons, ",") {
|
|
|
|
|
contained := false
|
|
|
|
|
for _, actualMon := range strings.Split(cephConfig.MonHost, ",") {
|
2016-10-26 09:48:25 -07:00
|
|
|
if expectedMon == actualMon {
|
|
|
|
|
contained = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-24 14:58:46 +01:00
|
|
|
assert.True(t, contained, "expectedMons: %+v, actualMons: %+v", expectedMons, cephConfig.MonHost)
|
2016-10-26 09:48:25 -07:00
|
|
|
}
|
|
|
|
|
}
|
2017-01-04 09:01:47 -08:00
|
|
|
|
|
|
|
|
func verifyConfigValue(t *testing.T, actualConf *ini.File, section, key, expectedVal string) {
|
|
|
|
|
s, err := actualConf.GetSection(section)
|
|
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
k := s.Key(key)
|
|
|
|
|
if !assert.NotNil(t, k) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
actualVal := k.Value()
|
|
|
|
|
assert.Equal(t, expectedVal, actualVal)
|
|
|
|
|
}
|