Files
my-rook-config/pkg/daemon/ceph/client/osd_test.go
T
Blaine Gardner 795124b7a8 ceph: update osds in parallel
Update OSDs in parallel per the design in
design/ceph/update-osds-in-parallel.md

The max number of OSDs updated in parallel is currently fixed at 20.

Signed-off-by: Blaine Gardner <blaine.gardner@redhat.com>
2021-03-29 10:55:28 -06:00

202 lines
5.7 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 client
import (
"fmt"
"testing"
"github.com/pkg/errors"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/daemon/ceph/client/fake"
"github.com/rook/rook/pkg/operator/ceph/version"
exectest "github.com/rook/rook/pkg/util/exec/test"
"github.com/stretchr/testify/assert"
)
var (
fakeOsdTree = `{
"nodes": [
{
"id": -3,
"name": "minikube",
"type": "host",
"type_id": 1,
"pool_weights": {},
"children": [
2,
1,
0
]
},
{
"id": -2,
"name": "minikube-2",
"type": "host",
"type_id": 1,
"pool_weights": {},
"children": [
3,
4,
5
]
}
]
}`
fakeOSdList = `[0,1,2]`
)
func TestHostTree(t *testing.T) {
executor := &exectest.MockExecutor{}
emptyTreeResult := false
executor.MockExecuteCommandWithOutputFile = func(command, outputFile string, args ...string) (string, error) {
logger.Infof("Command: %s %v", command, args)
switch {
case args[0] == "osd" && args[1] == "tree":
if emptyTreeResult {
return `not a json`, nil
}
return fakeOsdTree, nil
}
return "", errors.Errorf("unexpected ceph command %q", args)
}
tree, err := HostTree(&clusterd.Context{Executor: executor}, AdminClusterInfo("mycluster"))
assert.NoError(t, err)
assert.Equal(t, 2, len(tree.Nodes))
assert.Equal(t, "minikube", tree.Nodes[0].Name)
assert.Equal(t, 3, len(tree.Nodes[0].Children))
emptyTreeResult = true
tree, err = HostTree(&clusterd.Context{Executor: executor}, AdminClusterInfo("mycluster"))
assert.Error(t, err)
assert.Equal(t, 0, len(tree.Nodes))
}
func TestOsdListNum(t *testing.T) {
executor := &exectest.MockExecutor{}
emptyOsdListNumResult := false
executor.MockExecuteCommandWithOutputFile = func(command, outputFile string, args ...string) (string, error) {
logger.Infof("Command: %s %v", command, args)
switch {
case args[0] == "osd" && args[1] == "ls":
if emptyOsdListNumResult {
return `not a json`, nil
}
return fakeOSdList, nil
}
return "", errors.Errorf("unexpected ceph command %q", args)
}
list, err := OsdListNum(&clusterd.Context{Executor: executor}, AdminClusterInfo("mycluster"))
assert.NoError(t, err)
assert.Equal(t, 3, len(list))
emptyOsdListNumResult = true
list, err = OsdListNum(&clusterd.Context{Executor: executor}, AdminClusterInfo("mycluster"))
assert.Error(t, err)
assert.Equal(t, 0, len(list))
}
func TestOSDOkToStop(t *testing.T) {
returnString := ""
returnOkResult := true
seenArgs := []string{}
executor := &exectest.MockExecutor{}
executor.MockExecuteCommandWithOutputFile = func(command, outputFile string, args ...string) (string, error) {
logger.Infof("Command: %s %v", command, args)
switch {
case args[0] == "osd" && args[1] == "ok-to-stop":
seenArgs = args
if returnOkResult {
return returnString, nil
}
return returnString, errors.Errorf("Error EBUSY: unsafe to stop osd(s) at this time (50 PGs are or would become offline)")
}
panic(fmt.Sprintf("unexpected ceph command %q", args))
}
context := &clusterd.Context{Executor: executor}
clusterInfo := AdminClusterInfo("mycluster")
doSetup := func() {
seenArgs = []string{}
}
t.Run("pacific output ok to stop", func(t *testing.T) {
doSetup()
clusterInfo.CephVersion = version.Pacific
returnString = fake.OsdOkToStopOutput(1, []int{1, 2}, true)
returnOkResult = true
osds, err := OSDOkToStop(context, clusterInfo, 1, 2)
assert.NoError(t, err)
assert.ElementsMatch(t, osds, []int{1, 2})
assert.Equal(t, "1", seenArgs[2])
assert.Equal(t, "--max=2", seenArgs[3])
})
t.Run("pacific output not ok to stop", func(t *testing.T) {
doSetup()
clusterInfo.CephVersion = version.Pacific
returnString = fake.OsdOkToStopOutput(3, []int{}, true)
returnOkResult = false
_, err := OSDOkToStop(context, clusterInfo, 3, 5)
assert.Error(t, err)
assert.Equal(t, "3", seenArgs[2])
assert.Equal(t, "--max=5", seenArgs[3])
})
t.Run("pacific handles maxReturned=0", func(t *testing.T) {
doSetup()
clusterInfo.CephVersion = version.Pacific
returnString = fake.OsdOkToStopOutput(4, []int{4, 8}, true)
returnOkResult = true
osds, err := OSDOkToStop(context, clusterInfo, 4, 0)
assert.NoError(t, err)
assert.ElementsMatch(t, osds, []int{4, 8})
assert.Equal(t, "4", seenArgs[2])
// should just pass through as --max=0; don't do any special processing
assert.Equal(t, "--max=0", seenArgs[3])
})
t.Run("octopus output not ok to stop", func(t *testing.T) {
doSetup()
clusterInfo.CephVersion = version.Octopus
returnString = fake.OsdOkToStopOutput(3, []int{}, false)
returnOkResult = false
_, err := OSDOkToStop(context, clusterInfo, 3, 5)
assert.Error(t, err)
assert.Equal(t, "3", seenArgs[2])
assert.NotContains(t, seenArgs[3], "--max") // do not issue the "--max" flag below pacific
})
t.Run("octopus output ok to stop", func(t *testing.T) {
doSetup()
clusterInfo.CephVersion = version.Octopus
returnString = fake.OsdOkToStopOutput(50, []int{50}, false)
returnOkResult = true
osds, err := OSDOkToStop(context, clusterInfo, 50, 2)
assert.NoError(t, err)
assert.ElementsMatch(t, osds, []int{50})
assert.Equal(t, "50", seenArgs[2])
assert.NotContains(t, seenArgs[3], "--max") // do not issue the "--max" flag below pacific
})
}