From 6b211eacbadf98a87ca64686e686680a08a4b74b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Wed, 4 Nov 2020 10:51:41 +0100 Subject: [PATCH] ceph: add unit test for osd batch mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now have much better coverage on the c-v on non-pvc scenario. Signed-off-by: Sébastien Han --- pkg/daemon/ceph/osd/volume.go | 3 +- pkg/daemon/ceph/osd/volume_test.go | 145 +++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 2 deletions(-) diff --git a/pkg/daemon/ceph/osd/volume.go b/pkg/daemon/ceph/osd/volume.go index f24a537b4..9848bbc0a 100644 --- a/pkg/daemon/ceph/osd/volume.go +++ b/pkg/daemon/ceph/osd/volume.go @@ -55,8 +55,7 @@ var ( cvLogDir = "" // The "ceph-volume raw" command is available since Ceph 14.2.8 as well as partition support in ceph-volume cephVolumeRawModeMinCephVersion = cephver.CephVersion{Major: 14, Minor: 2, Extra: 8} - - isEncrypted = os.Getenv(oposd.EncryptedDeviceEnvVarName) == "true" + isEncrypted = os.Getenv(oposd.EncryptedDeviceEnvVarName) == "true" ) type osdInfoBlock struct { diff --git a/pkg/daemon/ceph/osd/volume_test.go b/pkg/daemon/ceph/osd/volume_test.go index f398409d9..9d2b02ab8 100644 --- a/pkg/daemon/ceph/osd/volume_test.go +++ b/pkg/daemon/ceph/osd/volume_test.go @@ -27,6 +27,7 @@ import ( "github.com/rook/rook/pkg/clusterd" cephclient "github.com/rook/rook/pkg/daemon/ceph/client" oposd "github.com/rook/rook/pkg/operator/ceph/cluster/osd" + "github.com/rook/rook/pkg/operator/ceph/cluster/osd/config" cephver "github.com/rook/rook/pkg/operator/ceph/version" "github.com/rook/rook/pkg/operator/test" exectest "github.com/rook/rook/pkg/util/exec/test" @@ -535,6 +536,150 @@ func TestConfigureCVDevices(t *testing.T) { } } +func testBaseArgs(args []string) error { + if args[1] == "ceph-volume" && args[2] == "--log-path" && args[3] == "/tmp/ceph-log" && args[4] == "lvm" && args[5] == "batch" && args[6] == "--prepare" && args[7] == "--bluestore" && args[8] == "--yes" { + return nil + } + + return errors.Errorf("unknown args %s ", args) +} + +func TestInitializeBlock(t *testing.T) { + // Common vars for all the tests + devices := &DeviceOsdMapping{ + Entries: map[string]*DeviceOsdIDEntry{ + "sda": {Data: -1, Metadata: nil, Config: DesiredDevice{Name: "/dev/sda"}}, + }, + } + + // Test default behavior + { + executor := &exectest.MockExecutor{} + executor.MockExecuteCommand = func(command string, args ...string) error { + logger.Infof("%s %v", command, args) + + // Validate base common args + err := testBaseArgs(args) + if err != nil { + return err + } + + // First command + if args[9] == "--osds-per-device" && args[10] == "1" && args[11] == "/dev/sda" { + return nil + } + + // Second command + if args[9] == "--osds-per-device" && args[10] == "1" && args[11] == "/dev/sda" && args[12] == "--report" { + return nil + } + + return errors.Errorf("unknown command %s %s", command, args) + } + a := &OsdAgent{clusterInfo: &cephclient.ClusterInfo{CephVersion: cephver.CephVersion{Major: 14, Minor: 2, Extra: 8}}, nodeName: "node1"} + context := &clusterd.Context{Executor: executor} + + err := a.initializeDevices(context, devices) + assert.NoError(t, err, "failed default behavior test") + logger.Info("success, go to next test") + } + + // Test encryption behavior + { + executor := &exectest.MockExecutor{} + executor.MockExecuteCommand = func(command string, args ...string) error { + logger.Infof("%s %v", command, args) + + // Validate base common args + err := testBaseArgs(args) + if err != nil { + return err + } + + // First command + if args[9] == "--dmcrypt" && args[10] == "--osds-per-device" && args[11] == "1" && args[12] == "/dev/sda" { + return nil + } + + // Second command + if args[9] == "--dmcrypt" && args[10] == "--osds-per-device" && args[11] == "1" && args[12] == "/dev/sda" && args[13] == "--report" { + return nil + } + + return errors.Errorf("unknown command %s %s", command, args) + } + a := &OsdAgent{clusterInfo: &cephclient.ClusterInfo{CephVersion: cephver.CephVersion{Major: 14, Minor: 2, Extra: 8}}, nodeName: "node1", storeConfig: config.StoreConfig{EncryptedDevice: true}} + context := &clusterd.Context{Executor: executor} + + err := a.initializeDevices(context, devices) + assert.NoError(t, err, "failed encryption test") + logger.Info("success, go to next test") + } + + // Test multiple OSD per device + { + executor := &exectest.MockExecutor{} + executor.MockExecuteCommand = func(command string, args ...string) error { + logger.Infof("%s %v", command, args) + + // Validate base common args + err := testBaseArgs(args) + if err != nil { + return err + } + + // First command + if args[9] == "--osds-per-device" && args[10] == "3" && args[11] == "/dev/sda" { + return nil + } + + // Second command + if args[9] == "--osds-per-device" && args[10] == "3" && args[11] == "/dev/sda" && args[12] == "--report" { + return nil + } + + return errors.Errorf("unknown command %s %s", command, args) + } + a := &OsdAgent{clusterInfo: &cephclient.ClusterInfo{CephVersion: cephver.CephVersion{Major: 14, Minor: 2, Extra: 8}}, nodeName: "node1", storeConfig: config.StoreConfig{OSDsPerDevice: 3}} + context := &clusterd.Context{Executor: executor} + + err := a.initializeDevices(context, devices) + assert.NoError(t, err, "failed multiple osd test") + logger.Info("success, go to next test") + } + + // Test crush device class + { + executor := &exectest.MockExecutor{} + executor.MockExecuteCommand = func(command string, args ...string) error { + logger.Infof("%s %v", command, args) + + // Validate base common args + err := testBaseArgs(args) + if err != nil { + return err + } + + // First command + if args[9] == "--osds-per-device" && args[10] == "1" && args[11] == "/dev/sda" && args[12] == "--crush-device-class" && args[13] == "hybrid" { + return nil + } + + // Second command + if args[9] == "--osds-per-device" && args[10] == "1" && args[11] == "/dev/sda" && args[12] == "--crush-device-class" && args[13] == "hybrid" && args[14] == "--report" { + return nil + } + + return errors.Errorf("unknown command %s %s", command, args) + } + a := &OsdAgent{clusterInfo: &cephclient.ClusterInfo{CephVersion: cephver.CephVersion{Major: 14, Minor: 2, Extra: 8}}, nodeName: "node1", storeConfig: config.StoreConfig{DeviceClass: "hybrid"}} + context := &clusterd.Context{Executor: executor} + err := a.initializeDevices(context, devices) + assert.NoError(t, err, "failed crush device class test") + logger.Info("success, go to next test") + } +} + func TestInitializeBlockPVC(t *testing.T) { executor := &exectest.MockExecutor{} executor.MockExecuteCommandWithCombinedOutput = func(command string, args ...string) (string, error) {