Files
my-rook-config/pkg/daemon/ceph/client/pool_test.go
T

452 lines
14 KiB
Go
Raw Normal View History

2017-10-05 17:23:12 -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.
*/
package client
import (
"os/exec"
2019-09-24 15:05:42 +05:30
"reflect"
"strconv"
2017-10-05 17:23:12 -07:00
"testing"
2019-10-08 12:51:36 +02:00
"github.com/pkg/errors"
2020-03-24 22:15:11 -06:00
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
2017-10-05 17:23:12 -07:00
exectest "github.com/rook/rook/pkg/util/exec/test"
"github.com/stretchr/testify/assert"
)
func TestCreateECPoolWithOverwrites(t *testing.T) {
testCreateECPool(t, true, "")
}
func TestCreateECPoolWithoutOverwrites(t *testing.T) {
testCreateECPool(t, false, "")
}
func TestCreateECPoolWithCompression(t *testing.T) {
testCreateECPool(t, false, "aggressive")
testCreateECPool(t, true, "none")
}
func testCreateECPool(t *testing.T, overwrite bool, compressionMode string) {
2020-03-24 22:15:11 -06:00
poolName := "mypool"
compressionModeCreated := false
2020-03-24 22:15:11 -06:00
p := cephv1.PoolSpec{
FailureDomain: "host",
ErasureCoded: cephv1.ErasureCodedSpec{},
}
if compressionMode != "" {
p.CompressionMode = compressionMode
}
2017-10-05 17:23:12 -07:00
executor := &exectest.MockExecutor{}
context := &clusterd.Context{Executor: executor}
2021-07-21 11:25:52 +02:00
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
2017-10-05 17:23:12 -07:00
logger.Infof("Command: %s %v", command, args)
if args[1] == "pool" {
if args[2] == "create" {
assert.Equal(t, "mypool", args[3])
assert.Equal(t, "erasure", args[5])
2020-03-24 22:15:11 -06:00
assert.Equal(t, "mypoolprofile", args[6])
2017-10-05 17:23:12 -07:00
return "", nil
}
if args[2] == "set" {
assert.Equal(t, "mypool", args[3])
if args[4] == "allow_ec_overwrites" {
assert.Equal(t, true, overwrite)
assert.Equal(t, "true", args[5])
return "", nil
}
if args[4] == "compression_mode" {
assert.Equal(t, compressionMode, args[5])
compressionModeCreated = true
return "", nil
}
2017-10-05 17:23:12 -07:00
}
if args[2] == "application" {
assert.Equal(t, "enable", args[3])
assert.Equal(t, "mypool", args[4])
assert.Equal(t, "myapp", args[5])
return "", nil
}
}
2019-10-08 12:51:36 +02:00
return "", errors.Errorf("unexpected ceph command %q", args)
2017-10-05 17:23:12 -07:00
}
err := CreateECPoolForApp(context, AdminTestClusterInfo("mycluster"), poolName, "mypoolprofile", p, DefaultPGCount, "myapp", overwrite)
assert.Nil(t, err)
if compressionMode != "" {
assert.True(t, compressionModeCreated)
} else {
assert.False(t, compressionModeCreated)
2020-03-24 22:15:11 -06:00
}
2017-10-05 17:23:12 -07:00
}
func TestCreateReplicaPoolWithFailureDomain(t *testing.T) {
testCreateReplicaPool(t, "osd", "mycrushroot", "", "")
2017-10-05 17:23:12 -07:00
}
2019-05-09 13:05:06 +05:30
func TestCreateReplicaPoolWithDeviceClass(t *testing.T) {
testCreateReplicaPool(t, "osd", "mycrushroot", "hdd", "")
2019-05-09 13:05:06 +05:30
}
func TestCreateReplicaPoolWithCompression(t *testing.T) {
testCreateReplicaPool(t, "osd", "mycrushroot", "hdd", "passive")
testCreateReplicaPool(t, "osd", "mycrushroot", "hdd", "force")
}
func testCreateReplicaPool(t *testing.T, failureDomain, crushRoot, deviceClass, compressionMode string) {
2017-10-05 17:23:12 -07:00
crushRuleCreated := false
compressionModeCreated := false
2017-10-05 17:23:12 -07:00
executor := &exectest.MockExecutor{}
context := &clusterd.Context{Executor: executor}
2021-07-21 11:25:52 +02:00
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
2017-10-05 17:23:12 -07:00
logger.Infof("Command: %s %v", command, args)
if args[1] == "pool" {
if args[2] == "create" {
assert.Equal(t, "mypool", args[3])
assert.Equal(t, "replicated", args[5])
assert.Equal(t, "--size", args[7])
assert.Equal(t, "12345", args[8])
2017-10-05 17:23:12 -07:00
return "", nil
}
if args[2] == "set" {
assert.Equal(t, "mypool", args[3])
if args[4] == "size" {
assert.Equal(t, "12345", args[5])
}
if args[4] == "compression_mode" {
assert.Equal(t, compressionMode, args[5])
compressionModeCreated = true
}
2017-10-05 17:23:12 -07:00
return "", nil
}
if args[2] == "application" {
assert.Equal(t, "enable", args[3])
assert.Equal(t, "mypool", args[4])
assert.Equal(t, "myapp", args[5])
return "", nil
}
}
if args[1] == "crush" {
crushRuleCreated = true
assert.Equal(t, "rule", args[2])
2019-05-09 13:05:06 +05:30
assert.Equal(t, "create-replicated", args[3])
assert.Equal(t, "mypool", args[4])
2018-01-23 11:55:52 -08:00
if crushRoot == "" {
assert.Equal(t, "cluster-crush-root", args[5])
2018-01-23 11:55:52 -08:00
} else {
assert.Equal(t, crushRoot, args[5])
}
if failureDomain == "" {
assert.Equal(t, "host", args[6])
} else {
assert.Equal(t, failureDomain, args[6])
}
2019-05-09 13:05:06 +05:30
if deviceClass == "" {
assert.False(t, testIsStringInSlice("hdd", args))
} else {
assert.Equal(t, deviceClass, args[7])
}
2017-10-05 17:23:12 -07:00
return "", nil
}
2019-10-08 12:51:36 +02:00
return "", errors.Errorf("unexpected ceph command %q", args)
2017-10-05 17:23:12 -07:00
}
2020-03-24 22:15:11 -06:00
p := cephv1.PoolSpec{
FailureDomain: failureDomain, CrushRoot: crushRoot, DeviceClass: deviceClass,
Replicated: cephv1.ReplicatedSpec{Size: 12345},
}
if compressionMode != "" {
p.CompressionMode = compressionMode
}
clusterSpec := &cephv1.ClusterSpec{Storage: cephv1.StorageScopeSpec{Config: map[string]string{CrushRootConfigKey: "cluster-crush-root"}}}
err := CreateReplicatedPoolForApp(context, AdminTestClusterInfo("mycluster"), clusterSpec, "mypool", p, DefaultPGCount, "myapp")
2017-10-05 17:23:12 -07:00
assert.Nil(t, err)
2018-01-23 11:55:52 -08:00
assert.True(t, crushRuleCreated)
if compressionMode != "" {
assert.True(t, compressionModeCreated)
} else {
assert.False(t, compressionModeCreated)
}
2017-10-05 17:23:12 -07:00
}
2019-05-09 13:05:06 +05:30
func testIsStringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
2019-09-24 15:05:42 +05:30
func TestGetPoolStatistics(t *testing.T) {
p := PoolStatistics{}
p.Images.Count = 1
p.Images.ProvisionedBytes = 1024
p.Images.SnapCount = 1
p.Trash.Count = 1
p.Trash.ProvisionedBytes = 2048
p.Trash.SnapCount = 0
executor := &exectest.MockExecutor{}
context := &clusterd.Context{Executor: executor}
2020-03-18 11:53:33 -06:00
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
2019-09-24 15:05:42 +05:30
a := "{\"images\":{\"count\":1,\"provisioned_bytes\":1024,\"snap_count\":1},\"trash\":{\"count\":1,\"provisioned_bytes\":2048,\"snap_count\":0}}"
logger.Infof("Command: %s %v", command, args)
if args[0] == "pool" {
if args[1] == "stats" {
if args[2] == "replicapool" {
return a, nil
}
2019-10-08 12:51:36 +02:00
return "", errors.Errorf("rbd:error opening pool '%s': (2) No such file or directory", args[3])
2019-09-24 15:05:42 +05:30
}
}
2019-10-08 12:51:36 +02:00
return "", errors.Errorf("unexpected rbd command %q", args)
2019-09-24 15:05:42 +05:30
}
clusterInfo := AdminTestClusterInfo("mycluster")
stats, err := GetPoolStatistics(context, clusterInfo, "replicapool")
2019-09-24 15:05:42 +05:30
assert.Nil(t, err)
assert.True(t, reflect.DeepEqual(stats, &p))
stats, err = GetPoolStatistics(context, clusterInfo, "rbd")
2019-09-24 15:05:42 +05:30
assert.NotNil(t, err)
assert.Nil(t, stats)
}
2020-02-20 11:37:09 +01:00
func TestSetPoolReplicatedSizeProperty(t *testing.T) {
2020-03-24 22:15:11 -06:00
poolName := "mypool"
2020-02-20 11:37:09 +01:00
executor := &exectest.MockExecutor{}
context := &clusterd.Context{Executor: executor}
2021-07-21 11:25:52 +02:00
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
2020-02-20 11:37:09 +01:00
logger.Infof("Command: %s %v", command, args)
if args[2] == "set" {
2020-03-24 22:15:11 -06:00
assert.Equal(t, poolName, args[3])
2020-02-20 11:37:09 +01:00
assert.Equal(t, "size", args[4])
2020-03-24 22:15:11 -06:00
assert.Equal(t, "3", args[5])
2020-02-20 11:37:09 +01:00
return "", nil
}
return "", errors.Errorf("unexpected ceph command %q", args)
}
err := SetPoolReplicatedSizeProperty(context, AdminTestClusterInfo("mycluster"), poolName, "3")
2020-02-20 11:37:09 +01:00
assert.NoError(t, err)
// TEST POOL SIZE 1 AND RequireSafeReplicaSize True
2021-07-21 11:25:52 +02:00
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
2020-02-20 11:37:09 +01:00
logger.Infof("Command: %s %v", command, args)
if args[2] == "set" {
assert.Equal(t, "mypool", args[3])
assert.Equal(t, "size", args[4])
assert.Equal(t, "1", args[5])
assert.Equal(t, "--yes-i-really-mean-it", args[6])
return "", nil
}
return "", errors.Errorf("unexpected ceph command %q", args)
}
2020-03-24 22:15:11 -06:00
err = SetPoolReplicatedSizeProperty(context, AdminTestClusterInfo("mycluster"), poolName, "1")
2020-02-20 11:37:09 +01:00
assert.NoError(t, err)
}
2020-10-23 11:17:18 -06:00
func TestCreateStretchCrushRule(t *testing.T) {
testCreateStretchCrushRule(t, true)
testCreateStretchCrushRule(t, false)
}
func testCreateStretchCrushRule(t *testing.T, alreadyExists bool) {
executor := &exectest.MockExecutor{}
context := &clusterd.Context{Executor: executor}
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
logger.Infof("Command: %s %v", command, args)
if args[0] == "osd" {
if args[1] == "getcrushmap" {
return "", nil
}
if args[1] == "setcrushmap" {
if alreadyExists {
return "", errors.New("setcrushmap not expected for already existing crush rule")
}
return "", nil
}
}
if command == "crushtool" {
switch {
case args[0] == "--decompile" || args[0] == "--compile":
if alreadyExists {
return "", errors.New("--compile or --decompile not expected for already existing crush rule")
}
return "", nil
}
}
if args[0] == "osd" && args[1] == "crush" && args[2] == "dump" {
return testCrushMap, nil
}
return "", errors.Errorf("unexpected ceph command %q", args)
}
clusterInfo := AdminTestClusterInfo("mycluster")
2020-10-23 11:17:18 -06:00
clusterSpec := &cephv1.ClusterSpec{}
poolSpec := cephv1.PoolSpec{FailureDomain: "rack"}
2020-10-23 11:17:18 -06:00
ruleName := "testrule"
if alreadyExists {
ruleName = "replicated_ruleset"
}
2021-06-17 15:16:46 +05:30
err := createStretchCrushRule(context, clusterInfo, clusterSpec, ruleName, poolSpec)
2020-10-23 11:17:18 -06:00
assert.NoError(t, err)
}
func TestCreatePoolWithReplicasPerFailureDomain(t *testing.T) {
// This test goes via the path of explicit compile/decompile CRUSH map; ignored if 'crushtool' is not installed
// on local build machine
if hasCrushtool() {
testCreatePoolWithReplicasPerFailureDomain(t, "host", "mycrushroot", "hdd")
testCreatePoolWithReplicasPerFailureDomain(t, "rack", "mycrushroot", "ssd")
}
}
func testCreatePoolWithReplicasPerFailureDomain(t *testing.T, failureDomain, crushRoot, deviceClass string) {
poolName := "mypool-with-two-step-clush-rule"
poolRuleCreated := false
poolRuleSet := false
poolAppEnable := false
poolSpec := cephv1.PoolSpec{
FailureDomain: failureDomain,
CrushRoot: crushRoot,
DeviceClass: deviceClass,
Replicated: cephv1.ReplicatedSpec{
Size: 12345678,
ReplicasPerFailureDomain: 2,
},
}
executor := &exectest.MockExecutor{}
2021-07-21 11:25:52 +02:00
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
logger.Infof("Command: %s %v", command, args)
assert.Equal(t, command, "ceph")
assert.Equal(t, args[0], "osd")
if len(args) >= 3 && args[1] == "crush" && args[2] == "dump" {
return testCrushMap, nil
}
if len(args) >= 3 && args[1] == "pool" && args[2] == "create" {
// Currently, CRUSH-rule name equals pool's name
assert.GreaterOrEqual(t, len(args), 7)
assert.Equal(t, args[3], poolName)
assert.Equal(t, args[5], "replicated")
crushRuleName := args[6]
assert.Equal(t, crushRuleName, poolName)
poolRuleCreated = true
return "", nil
}
if len(args) >= 3 && args[1] == "pool" && args[2] == "set" {
crushRuleName := args[3]
assert.Equal(t, crushRuleName, poolName)
assert.Equal(t, args[4], "size")
poolSize, err := strconv.Atoi(args[5])
assert.NoError(t, err)
assert.Equal(t, uint(poolSize), poolSpec.Replicated.Size)
poolRuleSet = true
return "", nil
}
if len(args) >= 4 && args[1] == "pool" && args[2] == "application" && args[3] == "enable" {
crushRuleName := args[4]
assert.Equal(t, crushRuleName, poolName)
poolAppEnable = true
return "", nil
}
if len(args) >= 4 && args[1] == "crush" && args[2] == "rule" && args[3] == "create-replicated" {
crushRuleName := args[4]
assert.Equal(t, crushRuleName, poolName)
deviceClassName := args[7]
assert.Equal(t, deviceClassName, deviceClass)
poolRuleCreated = true
return "", nil
}
return "", errors.Errorf("unexpected ceph command %q", args)
}
context := &clusterd.Context{Executor: executor}
clusterSpec := &cephv1.ClusterSpec{Storage: cephv1.StorageScopeSpec{Config: map[string]string{CrushRootConfigKey: "cluster-crush-root"}}}
err := CreateReplicatedPoolForApp(context, AdminTestClusterInfo("mycluster"), clusterSpec, poolName, poolSpec, DefaultPGCount, "myapp")
assert.Nil(t, err)
assert.True(t, poolRuleCreated)
assert.True(t, poolRuleSet)
assert.True(t, poolAppEnable)
}
2021-06-17 15:16:46 +05:30
func TestCreateHybridCrushRule(t *testing.T) {
testCreateHybridCrushRule(t, true)
testCreateHybridCrushRule(t, false)
}
func testCreateHybridCrushRule(t *testing.T, alreadyExists bool) {
executor := &exectest.MockExecutor{}
context := &clusterd.Context{Executor: executor}
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
logger.Infof("Command: %s %v", command, args)
if args[0] == "osd" {
if args[1] == "getcrushmap" {
return "", nil
}
if args[1] == "setcrushmap" {
if alreadyExists {
return "", errors.New("setcrushmap not expected for already existing crush rule")
}
return "", nil
}
}
if command == "crushtool" {
switch {
case args[0] == "--decompile" || args[0] == "--compile":
if alreadyExists {
return "", errors.New("--compile or --decompile not expected for already existing crush rule")
}
return "", nil
}
}
if args[0] == "osd" && args[1] == "crush" && args[2] == "dump" {
return testCrushMap, nil
}
return "", errors.Errorf("unexpected ceph command %q", args)
}
clusterInfo := AdminTestClusterInfo("mycluster")
2021-06-17 15:16:46 +05:30
clusterSpec := &cephv1.ClusterSpec{}
poolSpec := cephv1.PoolSpec{
FailureDomain: "rack",
Replicated: cephv1.ReplicatedSpec{
HybridStorage: &cephv1.HybridStorageSpec{
PrimaryDeviceClass: "ssd",
SecondaryDeviceClass: "hdd",
},
},
}
ruleName := "testrule"
if alreadyExists {
ruleName = "hybrid_ruleset"
}
err := createHybridCrushRule(context, clusterInfo, clusterSpec, ruleName, poolSpec)
assert.NoError(t, err)
}
func hasCrushtool() bool {
_, err := exec.LookPath("crushtool")
return err == nil
}