Files
my-rook-config/pkg/operator/ceph/config/monstore_test.go
T
Sébastien Han b89730d895 ceph: refactor operator initialization sequence
This commit is a large refactor on how the operator starts, stops and
how it starts various sub-components such as the ceph-csi driver. It
also refines the way we cancel orchestrations. We don't use breakpoints
anymore but send our self a SIGUP to reload our controller runtime
manager.
The reload will happen under different circonstances like:

* a new adminission controller secret is created/deleted/changed
* a CephCluster CR is edited

As mentioned earlier, the csi driver now has its own controller, just
like flex. It reacts to change in the operator config map for particular
ROOK_CSI_ fields.

A second new controller for the operator's general config has been
created, it manages:

* the logging level
* the ceph CLI command timeout
* the discovery daemon

The operator reacts much more rapidly to cancellation events by stopping
the manager's context and reloading it.

Signed-off-by: Sébastien Han <seb@redhat.com>
2021-09-17 16:57:12 +02:00

244 lines
8.2 KiB
Go

/*
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 config
import (
"reflect"
"strings"
"testing"
"github.com/pkg/errors"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/daemon/ceph/client"
testop "github.com/rook/rook/pkg/operator/test"
exectest "github.com/rook/rook/pkg/util/exec/test"
"github.com/stretchr/testify/assert"
)
func TestMonStore_Set(t *testing.T) {
executor := &exectest.MockExecutor{}
clientset := testop.New(t, 1)
ctx := &clusterd.Context{
Clientset: clientset,
Executor: executor,
}
// create a mock command runner which creates a simple string of the command it ran, and allow
// us to cause it to return an error when it detects a keyword.
execedCmd := ""
execInjectErr := false
executor.MockExecuteCommandWithOutput =
func(command string, args ...string) (string, error) {
execedCmd = command + " " + strings.Join(args, " ")
if execInjectErr {
return "output from cmd with error", errors.New("mocked error")
}
return "", nil
}
monStore := GetMonStore(ctx, client.AdminClusterInfo("mycluster"))
// setting with spaces converts to underscores
e := monStore.Set("global", "debug ms", "10")
assert.NoError(t, e)
assert.Contains(t, execedCmd, "config set global debug_ms 10")
// setting with dashes converts to underscores
e = monStore.Set("osd.0", "debug-osd", "20")
assert.NoError(t, e)
assert.Contains(t, execedCmd, " config set osd.0 debug_osd 20 ")
// setting with underscores stays the same
e = monStore.Set("mds.*", "debug_mds", "15")
assert.NoError(t, e)
assert.Contains(t, execedCmd, " config set mds.* debug_mds 15 ")
// errors returned as expected
execInjectErr = true
e = monStore.Set("mon.*", "unknown_setting", "10")
assert.Error(t, e)
assert.Contains(t, execedCmd, " config set mon.* unknown_setting 10 ")
}
func TestMonStore_Delete(t *testing.T) {
executor := &exectest.MockExecutor{}
clientset := testop.New(t, 1)
ctx := &clusterd.Context{
Clientset: clientset,
Executor: executor,
}
// create a mock command runner which creates a simple string of the command it ran, and allow
// us to cause it to return an error when it detects a keyword.
execedCmd := ""
execInjectErr := false
executor.MockExecuteCommandWithOutput =
func(command string, args ...string) (string, error) {
execedCmd = command + " " + strings.Join(args, " ")
if execInjectErr {
return "output from cmd with error", errors.New("mocked error")
}
return "", nil
}
monStore := GetMonStore(ctx, client.AdminClusterInfo("mycluster"))
// ceph config rm called as expected
e := monStore.Delete("global", "debug ms")
assert.NoError(t, e)
assert.Contains(t, execedCmd, "config rm global debug_ms")
// errors returned as expected
execInjectErr = true
e = monStore.Delete("mon.*", "unknown_setting")
assert.Error(t, e)
assert.Contains(t, execedCmd, " config rm mon.* unknown_setting ")
}
func TestMonStore_GetDaemon(t *testing.T) {
executor := &exectest.MockExecutor{}
clientset := testop.New(t, 1)
ctx := &clusterd.Context{
Clientset: clientset,
Executor: executor,
}
// create a mock command runner which creates a simple string of the command it ran, and allow
// us to cause it to return an error when it detects a keyword and to return a specific string
execedCmd := ""
execReturn := "{\"rbd_default_features\":{\"value\":\"3\",\"section\":\"global\",\"mask\":{}," +
"\"can_update_at_runtime\":true}," +
"\"rgw_enable_usage_log\":{\"value\":\"true\",\"section\":\"client.rgw.test.a\",\"mask\":{}," +
"\"can_update_at_runtime\":true}}"
execInjectErr := false
executor.MockExecuteCommandWithOutput =
func(command string, args ...string) (string, error) {
execedCmd = command + " " + strings.Join(args, " ")
if execInjectErr {
return "output from cmd with error", errors.New("mocked error")
}
return execReturn, nil
}
monStore := GetMonStore(ctx, client.AdminClusterInfo("mycluster"))
// ceph config get called as expected
options, e := monStore.GetDaemon("client.rgw.test.a")
assert.NoError(t, e)
assert.Contains(t, execedCmd, "ceph config get client.rgw.test.a")
assert.True(t, reflect.DeepEqual(options, []Option{{"client.rgw.test.a", "rgw_enable_usage_log", "true"}}))
// json parse exception return as expected
execReturn = "bad json output"
_, e = monStore.GetDaemon("client.rgw.test.a")
assert.Error(t, e)
assert.Contains(t, e.Error(), "failed to parse json config for daemon \"client.rgw.test.a\". json: "+
"bad json output")
// errors returned as expected
execInjectErr = true
_, e = monStore.GetDaemon("mon.*")
assert.Error(t, e)
assert.Contains(t, execedCmd, " config get mon.* ")
}
func TestMonStore_DeleteDaemon(t *testing.T) {
executor := &exectest.MockExecutor{}
clientset := testop.New(t, 1)
ctx := &clusterd.Context{
Clientset: clientset,
Executor: executor,
}
// create a mock command runner which creates a simple string of the command it ran, and allow
// us to cause it to return an error when it detects a keyword and to return a specific string
execedCmd := ""
execReturn := "{\"rbd_default_features\":{\"value\":\"3\",\"section\":\"global\",\"mask\":{}," +
"\"can_update_at_runtime\":true}," +
"\"rgw_enable_usage_log\":{\"value\":\"true\",\"section\":\"client.rgw.test.a\",\"mask\":{}," +
"\"can_update_at_runtime\":true}}"
executor.MockExecuteCommandWithOutput =
func(command string, args ...string) (string, error) {
execedCmd = command + " " + strings.Join(args, " ")
return execReturn, nil
}
monStore := GetMonStore(ctx, client.AdminClusterInfo("mycluster"))
// ceph config rm rgw_enable_usage_log called as expected
e := monStore.DeleteDaemon("client.rgw.test.a")
assert.NoError(t, e)
assert.Contains(t, execedCmd, "ceph config rm client.rgw.test.a rgw_enable_usage_log")
}
func TestMonStore_SetAll(t *testing.T) {
clientset := testop.New(t, 1)
executor := &exectest.MockExecutor{}
ctx := &clusterd.Context{
Clientset: clientset,
Executor: executor,
}
// create a mock command runner which creates a simple string of the command it ran, and allow
// us to cause it to return an error when it detects a keyword.
execedCmds := []string{}
execInjectErrOnKeyword := "donotinjectanerror"
executor.MockExecuteCommandWithOutput =
func(command string, args ...string) (string, error) {
execedCmd := command + " " + strings.Join(args, " ")
execedCmds = append(execedCmds, execedCmd)
k := execInjectErrOnKeyword
if strings.Contains(execedCmd, k) {
return "output from cmd with error on keyword: " + k, errors.Errorf("mocked error on keyword: " + k)
}
return "", nil
}
monStore := GetMonStore(ctx, client.AdminClusterInfo("mycluster"))
cfgOverrides := []Option{
configOverride("global", "debug ms", "10"), // setting w/ spaces converts to underscores
configOverride("osd.0", "debug-osd", "20"), // setting w/ dashes converts to underscores
configOverride("mds.*", "debug_mds", "15"), // setting w/ underscores remains the same
}
// commands w/ no error
e := monStore.SetAll(cfgOverrides...)
assert.NoError(t, e)
assert.Len(t, execedCmds, 3)
assert.Contains(t, execedCmds[0], " global debug_ms 10 ")
assert.Contains(t, execedCmds[1], " osd.0 debug_osd 20 ")
assert.Contains(t, execedCmds[2], " mds.* debug_mds 15 ")
// commands w/ one error
// keep cfgOverrides from last test
execInjectErrOnKeyword = "debug_osd"
execedCmds = execedCmds[:0] // empty execedCmds slice
e = monStore.SetAll(cfgOverrides...)
assert.Error(t, e)
// Rook should not return error before trying to set all config overrides
assert.Len(t, execedCmds, 3)
// all commands return error
// keep cfgOverrides
execInjectErrOnKeyword = "debug"
execedCmds = execedCmds[:0]
e = monStore.SetAll(cfgOverrides...)
assert.Error(t, e)
assert.Len(t, execedCmds, 3)
}