From f2ecaa2bda0159af77fd08a0387c49402bf7e687 Mon Sep 17 00:00:00 2001 From: Travis Nielsen Date: Tue, 17 Mar 2020 18:23:16 -0600 Subject: [PATCH] exec: remove the unused actionName param The helpers for executing a process have long required an actionName param which is not being used. Now we remove the old param while also cleaning up various other usages of the exec package. Signed-off-by: Travis Nielsen --- cmd/rook/edgefs/start.go | 3 +- cmd/rook/rook/rook.go | 1 - pkg/clusterd/disk_test.go | 8 +-- .../flexvolume/manager/ceph/manager_test.go | 20 +++--- pkg/daemon/ceph/client/command.go | 14 ++-- pkg/daemon/ceph/client/crush_test.go | 2 +- .../ceph/client/erasure-code-profile_test.go | 2 +- pkg/daemon/ceph/client/filesystem_test.go | 4 +- pkg/daemon/ceph/client/image.go | 3 +- pkg/daemon/ceph/client/image_test.go | 10 +-- pkg/daemon/ceph/client/mgr_test.go | 8 +-- pkg/daemon/ceph/client/osd_test.go | 4 +- pkg/daemon/ceph/client/pool_test.go | 12 ++-- pkg/daemon/ceph/client/upgrade_test.go | 14 ++-- pkg/daemon/ceph/osd/daemon.go | 12 ++-- pkg/daemon/ceph/osd/daemon_test.go | 10 +-- pkg/daemon/ceph/osd/device_test.go | 2 +- pkg/daemon/ceph/osd/volume.go | 16 ++--- pkg/daemon/ceph/osd/volume_test.go | 16 ++--- pkg/daemon/discover/discover_test.go | 30 ++------- pkg/operator/ceph/client/controller_test.go | 6 +- pkg/operator/ceph/cluster/mgr/dashboard.go | 11 ---- .../ceph/cluster/mgr/dashboard_test.go | 6 +- pkg/operator/ceph/cluster/mgr/mgr.go | 3 +- pkg/operator/ceph/cluster/mgr/mgr_test.go | 4 +- .../ceph/cluster/mgr/orchestrator_test.go | 6 +- pkg/operator/ceph/cluster/mon/config.go | 2 +- pkg/operator/ceph/cluster/mon/config_test.go | 2 +- pkg/operator/ceph/cluster/mon/health_test.go | 6 +- pkg/operator/ceph/cluster/mon/mon_test.go | 4 +- pkg/operator/ceph/cluster/osd/health_test.go | 6 +- pkg/operator/ceph/cluster/osd/osd_test.go | 4 +- pkg/operator/ceph/cluster/rbd/mirror_test.go | 2 +- .../ceph/config/keyring/store_test.go | 2 +- pkg/operator/ceph/config/monstore_test.go | 4 +- pkg/operator/ceph/file/filesystem_test.go | 12 ++-- pkg/operator/ceph/nfs/nfs.go | 8 +-- pkg/operator/ceph/object/admin.go | 2 +- pkg/operator/ceph/object/controller_test.go | 4 +- pkg/operator/ceph/object/objectstore_test.go | 6 +- pkg/operator/ceph/object/rgw_test.go | 8 +-- .../ceph/object/user/controller_test.go | 4 +- pkg/operator/ceph/pool/controller_test.go | 10 +-- .../ceph/provisioner/provisioner_test.go | 4 +- pkg/operator/cockroachdb/controller.go | 2 +- pkg/operator/cockroachdb/controller_test.go | 2 +- pkg/operator/edgefs/cluster/mgr/mgr_test.go | 2 +- pkg/util/exec/error.go | 29 ++------ pkg/util/exec/exec.go | 66 +++++++++---------- pkg/util/exec/test/mockexec.go | 48 ++++++-------- pkg/util/exec/translate_exec.go | 45 ++++++------- pkg/util/sys/device.go | 31 +++------ pkg/util/sys/device_test.go | 8 +-- pkg/util/sys/kmod.go | 6 +- tests/framework/utils/exec_utils.go | 20 +++--- tests/framework/utils/helm_helper.go | 2 +- tests/framework/utils/k8s_helper.go | 2 +- 57 files changed, 245 insertions(+), 335 deletions(-) diff --git a/cmd/rook/edgefs/start.go b/cmd/rook/edgefs/start.go index 151dae87f..6ff2af64d 100644 --- a/cmd/rook/edgefs/start.go +++ b/cmd/rook/edgefs/start.go @@ -25,7 +25,6 @@ import ( const ( edgefStartCmdPath = "/opt/nedge/sbin/edgefs-start.sh" - logSectionName = "edgefs-internal" ) var startCmd = &cobra.Command{ @@ -44,7 +43,7 @@ func start(cmd *cobra.Command, args []string) error { rook.SetLogLevel() rook.LogStartupInfo(operatorCmd.Flags()) executor := &exec.CommandExecutor{} - err := executor.ExecuteCommand(true, logSectionName, edgefStartCmdPath, args...) + err := executor.ExecuteCommand(true, edgefStartCmdPath, args...) logger.Infof("Start script %s exited: %+v", edgefStartCmdPath, err) return nil diff --git a/cmd/rook/rook/rook.go b/cmd/rook/rook/rook.go index acf47cd46..d0c7e027b 100644 --- a/cmd/rook/rook/rook.go +++ b/cmd/rook/rook/rook.go @@ -126,7 +126,6 @@ func NewContext() *clusterd.Context { Executor: context.Executor, Translator: func( debug bool, - actionName string, command string, arg ...string, ) (string, []string) { diff --git a/pkg/clusterd/disk_test.go b/pkg/clusterd/disk_test.go index d9e07c042..2b55ad809 100644 --- a/pkg/clusterd/disk_test.go +++ b/pkg/clusterd/disk_test.go @@ -24,12 +24,12 @@ import ( func TestDiscoverDevices(t *testing.T) { executor := &exectest.MockExecutor{ - MockExecuteCommand: func(debug bool, actionName string, command string, arg ...string) error { - logger.Infof("mock execute. %s. %s", actionName, command) + MockExecuteCommand: func(debug bool, command string, arg ...string) error { + logger.Infof("mock execute. %s", command) return nil }, - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, arg ...string) (string, error) { - logger.Infof("mock execute with output. %s. %s", actionName, command) + MockExecuteCommandWithOutput: func(debug bool, command string, arg ...string) (string, error) { + logger.Infof("mock execute with outputP. %s", command) return "", nil }, } diff --git a/pkg/daemon/ceph/agent/flexvolume/manager/ceph/manager_test.go b/pkg/daemon/ceph/agent/flexvolume/manager/ceph/manager_test.go index 085e922e2..f22d5e4f5 100644 --- a/pkg/daemon/ceph/agent/flexvolume/manager/ceph/manager_test.go +++ b/pkg/daemon/ceph/agent/flexvolume/manager/ceph/manager_test.go @@ -50,13 +50,13 @@ func TestInitLoadRBDModSingleMajor(t *testing.T) { modprobeCalled := false executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { assert.Equal(t, "modinfo", command) assert.Equal(t, "rbd", args[2]) modInfoCalled = true return "single_major:Use a single major number for all rbd devices (default: false) (bool)", nil }, - MockExecuteCommand: func(debug bool, actionName string, command string, args ...string) error { + MockExecuteCommand: func(debug bool, command string, args ...string) error { assert.Equal(t, "modprobe", command) assert.Equal(t, "rbd", args[0]) assert.Equal(t, "single_major=Y", args[1]) @@ -78,13 +78,13 @@ func TestInitLoadRBDModNoSingleMajor(t *testing.T) { modprobeCalled := false executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { assert.Equal(t, "modinfo", command) assert.Equal(t, "rbd", args[2]) modInfoCalled = true return "", nil }, - MockExecuteCommand: func(debug bool, actionName string, command string, args ...string) error { + MockExecuteCommand: func(debug bool, command string, args ...string) error { assert.Equal(t, "modprobe", command) assert.Equal(t, 1, len(args)) assert.Equal(t, "rbd", args[0]) @@ -117,14 +117,14 @@ func TestAttach(t *testing.T) { runCount := 1 executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { if strings.Contains(command, "ceph-authtool") { err := cephtest.CreateConfigDir(path.Join(configDir, clusterNamespace)) assert.Nil(t, err) } return "", nil }, - MockExecuteCommandWithTimeout: func(debug bool, timeout time.Duration, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithTimeout: func(debug bool, timeout time.Duration, command string, args ...string) (string, error) { assert.Equal(t, "rbd", command) assert.Equal(t, "map", args[0]) assert.Equal(t, fmt.Sprintf("testpool/image%d", runCount), args[1]) @@ -201,14 +201,14 @@ func TestDetach(t *testing.T) { clientset.CoreV1().ConfigMaps(clusterNamespace).Create(cm) executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { if strings.Contains(command, "ceph-authtool") { err := cephtest.CreateConfigDir(path.Join(configDir, clusterNamespace)) assert.Nil(t, err) } return "", nil }, - MockExecuteCommandWithTimeout: func(debug bool, timeout time.Duration, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithTimeout: func(debug bool, timeout time.Duration, command string, args ...string) (string, error) { assert.Equal(t, "rbd", command) assert.Equal(t, "unmap", args[0]) assert.Equal(t, "testpool/image1", args[1]) @@ -253,14 +253,14 @@ func TestDetachCustomKeyring(t *testing.T) { clientset.CoreV1().ConfigMaps(clusterNamespace).Create(cm) executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { if strings.Contains(command, "ceph-authtool") { err := cephtest.CreateConfigDir(path.Join(configDir, clusterNamespace)) assert.Nil(t, err) } return "", nil }, - MockExecuteCommandWithTimeout: func(debug bool, timeout time.Duration, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithTimeout: func(debug bool, timeout time.Duration, command string, args ...string) (string, error) { assert.Equal(t, "rbd", command) assert.Equal(t, "unmap", args[0]) assert.Equal(t, "testpool/image1", args[1]) diff --git a/pkg/daemon/ceph/client/command.go b/pkg/daemon/ceph/client/command.go index 446146c08..895f9d935 100644 --- a/pkg/daemon/ceph/client/command.go +++ b/pkg/daemon/ceph/client/command.go @@ -138,22 +138,22 @@ func (c *CephToolCommand) run() ([]byte, error) { // file in the wrong place, so we will instead capture the output // from stdout for the tests if c.timeout == 0 { - output, err = c.context.Executor.ExecuteCommandWithOutput(c.Debug, "", command, args...) + output, err = c.context.Executor.ExecuteCommandWithOutput(c.Debug, command, args...) } else { - output, err = c.context.Executor.ExecuteCommandWithTimeout(c.Debug, c.timeout, "", command, args...) + output, err = c.context.Executor.ExecuteCommandWithTimeout(c.Debug, c.timeout, command, args...) } } else { if c.timeout == 0 { - output, err = c.context.Executor.ExecuteCommandWithOutputFile(c.Debug, "", command, "--out-file", args...) + output, err = c.context.Executor.ExecuteCommandWithOutputFile(c.Debug, command, "--out-file", args...) } else { - output, err = c.context.Executor.ExecuteCommandWithOutputFileTimeout(c.Debug, c.timeout, "", command, "--out-file", args...) + output, err = c.context.Executor.ExecuteCommandWithOutputFileTimeout(c.Debug, c.timeout, command, "--out-file", args...) } } } else { if c.timeout == 0 { - output, err = c.context.Executor.ExecuteCommandWithOutput(c.Debug, "", command, args...) + output, err = c.context.Executor.ExecuteCommandWithOutput(c.Debug, command, args...) } else { - output, err = c.context.Executor.ExecuteCommandWithTimeout(c.Debug, c.timeout, "", command, args...) + output, err = c.context.Executor.ExecuteCommandWithTimeout(c.Debug, c.timeout, command, args...) } } @@ -175,7 +175,7 @@ func (c *CephToolCommand) RunWithTimeout(timeout time.Duration) ([]byte, error) // configured its arguments. It is future work to integrate this case into the // generalization. func ExecuteRBDCommandWithTimeout(context *clusterd.Context, clusterName string, args []string) (string, error) { - output, err := context.Executor.ExecuteCommandWithTimeout(false, CmdExecuteTimeout, "", RBDTool, args...) + output, err := context.Executor.ExecuteCommandWithTimeout(false, CmdExecuteTimeout, RBDTool, args...) return output, err } diff --git a/pkg/daemon/ceph/client/crush_test.go b/pkg/daemon/ceph/client/crush_test.go index 3843f12e9..8af94e131 100644 --- a/pkg/daemon/ceph/client/crush_test.go +++ b/pkg/daemon/ceph/client/crush_test.go @@ -229,7 +229,7 @@ const testCrushMap = `{ func TestGetCrushMap(t *testing.T) { executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[1] == "crush" && args[2] == "dump" { return testCrushMap, nil diff --git a/pkg/daemon/ceph/client/erasure-code-profile_test.go b/pkg/daemon/ceph/client/erasure-code-profile_test.go index 901902247..b06e3e3ea 100644 --- a/pkg/daemon/ceph/client/erasure-code-profile_test.go +++ b/pkg/daemon/ceph/client/erasure-code-profile_test.go @@ -44,7 +44,7 @@ func testCreateProfile(t *testing.T, failureDomain, crushRoot, deviceClass strin executor := &exectest.MockExecutor{} context := &clusterd.Context{Executor: executor} - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[1] == "erasure-code-profile" { if args[2] == "get" { diff --git a/pkg/daemon/ceph/client/filesystem_test.go b/pkg/daemon/ceph/client/filesystem_test.go index 6f22ea871..5ab651919 100644 --- a/pkg/daemon/ceph/client/filesystem_test.go +++ b/pkg/daemon/ceph/client/filesystem_test.go @@ -103,7 +103,7 @@ func TestFilesystemRemove(t *testing.T) { DataPools: []int{1}, }, } - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[0] == "fs" { if args[1] == "get" { @@ -149,7 +149,7 @@ func TestFilesystemRemove(t *testing.T) { } return "", errors.Errorf("unexpected ceph command %q", args) } - executor.MockExecuteCommandWithOutput = func(debug bool, actionName string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { emptyPool := "{\"images\":{\"count\":0,\"provisioned_bytes\":0,\"snap_count\":0},\"trash\":{\"count\":1,\"provisioned_bytes\":2048,\"snap_count\":0}}" if args[0] == "pool" { diff --git a/pkg/daemon/ceph/client/image.go b/pkg/daemon/ceph/client/image.go index bcc506d8b..81f0d24a7 100644 --- a/pkg/daemon/ceph/client/image.go +++ b/pkg/daemon/ceph/client/image.go @@ -96,8 +96,7 @@ func CreateImage(context *clusterd.Context, clusterName, name, poolName, dataPoo buf, err := NewRBDCommand(context, clusterName, args).Run() if err != nil { - cmdErr, ok := err.(*exec.CommandError) - if ok && cmdErr.ExitStatus() == int(syscall.EEXIST) { + if code, ok := exec.ExitStatus(err); ok && code == int(syscall.EEXIST) { // Image with the same name already exists in the given rbd pool. Continuing with the link to PV. logger.Warningf("Requested image %s exists in pool %s. Continuing", name, poolName) } else { diff --git a/pkg/daemon/ceph/client/image_test.go b/pkg/daemon/ceph/client/image_test.go index 13ad06750..25174568e 100644 --- a/pkg/daemon/ceph/client/image_test.go +++ b/pkg/daemon/ceph/client/image_test.go @@ -38,7 +38,7 @@ func TestCreateImage(t *testing.T) { // mock an error during the create image call. rbd tool returns error information to the output stream, // separate from the error object, so verify that information also makes it back to us (because it is useful). - executor.MockExecuteCommandWithOutput = func(debug bool, actionName string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { switch { case command == "rbd" && args[0] == "create": return "mocked detailed ceph error output stream", errors.New("some mocked error") @@ -53,7 +53,7 @@ func TestCreateImage(t *testing.T) { // (except for 0, that's OK) createCalled := false expectedSizeArg := "" - executor.MockExecuteCommandWithOutput = func(debug bool, actionName string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { switch { case command == "rbd" && args[0] == "create": createCalled = true @@ -146,7 +146,7 @@ func TestCreateImage(t *testing.T) { func TestExpandImage(t *testing.T) { executor := &exectest.MockExecutor{} context := &clusterd.Context{Executor: executor} - executor.MockExecuteCommandWithTimeout = func(debug bool, timeout time.Duration, actionName string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithTimeout = func(debug bool, timeout time.Duration, command string, args ...string) (string, error) { switch { case args[1] != "kube/some-image": return "", errors.Errorf("no image %s", args[1]) @@ -171,7 +171,7 @@ func TestListImageLogLevelInfo(t *testing.T) { var err error listCalled := false emptyListResult := false - executor.MockExecuteCommandWithOutput = func(debug bool, actionName string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { switch { case command == "rbd" && args[0] == "ls" && args[1] == "-l": listCalled = true @@ -235,7 +235,7 @@ func TestListImageLogLevelDebug(t *testing.T) { listCalled := false emptyListResult := false - executor.MockExecuteCommandWithOutput = func(debug bool, actionName string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { switch { case command == "rbd" && args[0] == "ls" && args[1] == "-l": listCalled = true diff --git a/pkg/daemon/ceph/client/mgr_test.go b/pkg/daemon/ceph/client/mgr_test.go index 0a1560b60..199469fae 100644 --- a/pkg/daemon/ceph/client/mgr_test.go +++ b/pkg/daemon/ceph/client/mgr_test.go @@ -29,7 +29,7 @@ func TestEnableModuleRetries(t *testing.T) { moduleEnableRetries := 0 moduleEnableWaitTime = 0 executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) switch { case args[0] == "balancer" && args[1] == "on": @@ -66,7 +66,7 @@ func TestEnableModuleRetries(t *testing.T) { func TestEnableModule(t *testing.T) { executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) switch { case args[0] == "mgr" && args[1] == "module" && args[2] == "enable": @@ -98,7 +98,7 @@ func TestEnableModule(t *testing.T) { func TestEnableDisableBalancerModule(t *testing.T) { executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) switch { case args[0] == "balancer" && args[1] == "on": @@ -121,7 +121,7 @@ func TestEnableDisableBalancerModule(t *testing.T) { func TestSetBalancerMode(t *testing.T) { executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[0] == "balancer" && args[1] == "mode" && args[2] == "upmap" { return "", nil diff --git a/pkg/daemon/ceph/client/osd_test.go b/pkg/daemon/ceph/client/osd_test.go index 4bf99069a..75a7dc0b7 100644 --- a/pkg/daemon/ceph/client/osd_test.go +++ b/pkg/daemon/ceph/client/osd_test.go @@ -61,7 +61,7 @@ var ( func TestHostTree(t *testing.T) { executor := &exectest.MockExecutor{} emptyTreeResult := false - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) switch { case args[0] == "osd" && args[1] == "tree": @@ -89,7 +89,7 @@ func TestHostTree(t *testing.T) { func TestOsdListNum(t *testing.T) { executor := &exectest.MockExecutor{} emptyOsdListNumResult := false - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) switch { case args[0] == "osd" && args[1] == "ls": diff --git a/pkg/daemon/ceph/client/pool_test.go b/pkg/daemon/ceph/client/pool_test.go index e714b0c10..198947baf 100644 --- a/pkg/daemon/ceph/client/pool_test.go +++ b/pkg/daemon/ceph/client/pool_test.go @@ -32,7 +32,7 @@ func TestCreateECPoolWithOverwrites(t *testing.T) { p := CephStoragePoolDetails{Name: "mypool", Size: 12345, ErasureCodeProfile: "myecprofile", FailureDomain: "host"} executor := &exectest.MockExecutor{} context := &clusterd.Context{Executor: executor} - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[1] == "pool" { if args[2] == "create" { @@ -66,7 +66,7 @@ func TestCreateECPoolWithoutOverwrites(t *testing.T) { p := CephStoragePoolDetails{Name: "mypool", Size: 12345, ErasureCodeProfile: "myecprofile", FailureDomain: "host"} executor := &exectest.MockExecutor{} context := &clusterd.Context{Executor: executor} - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[1] == "pool" { if args[2] == "create" { @@ -109,7 +109,7 @@ func testCreateReplicaPool(t *testing.T, failureDomain, crushRoot, deviceClass s crushRuleCreated := false executor := &exectest.MockExecutor{} context := &clusterd.Context{Executor: executor} - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[1] == "pool" { if args[2] == "create" { @@ -180,7 +180,7 @@ func TestGetPoolStatistics(t *testing.T) { p.Trash.SnapCount = 0 executor := &exectest.MockExecutor{} context := &clusterd.Context{Executor: executor} - executor.MockExecuteCommandWithOutput = func(debug bool, actionName string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { 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) @@ -210,7 +210,7 @@ func TestSetPoolReplicatedSizeProperty(t *testing.T) { p := CephStoragePoolDetails{Name: "mypool", Size: 12345} executor := &exectest.MockExecutor{} context := &clusterd.Context{Executor: executor} - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[2] == "set" { @@ -228,7 +228,7 @@ func TestSetPoolReplicatedSizeProperty(t *testing.T) { assert.NoError(t, err) // TEST POOL SIZE 1 AND RequireSafeReplicaSize True - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[2] == "set" { diff --git a/pkg/daemon/ceph/client/upgrade_test.go b/pkg/daemon/ceph/client/upgrade_test.go index bcf114c67..739c4dc23 100644 --- a/pkg/daemon/ceph/client/upgrade_test.go +++ b/pkg/daemon/ceph/client/upgrade_test.go @@ -28,7 +28,7 @@ import ( func TestGetCephMonVersionString(t *testing.T) { executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { assert.Equal(t, "version", args[0]) return "", nil } @@ -40,7 +40,7 @@ func TestGetCephMonVersionString(t *testing.T) { func TestGetCephMonVersionsString(t *testing.T) { executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { assert.Equal(t, "versions", args[0]) return "", nil } @@ -52,7 +52,7 @@ func TestGetCephMonVersionsString(t *testing.T) { func TestEnableMessenger2(t *testing.T) { executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { assert.Equal(t, "mon", args[0]) assert.Equal(t, "enable-msgr2", args[1]) return "", nil @@ -65,7 +65,7 @@ func TestEnableMessenger2(t *testing.T) { func TestEnableReleaseOSDFunctionality(t *testing.T) { executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { assert.Equal(t, "osd", args[0]) assert.Equal(t, "require-osd-release", args[1]) assert.Equal(t, 3, len(args)) @@ -80,7 +80,7 @@ func TestEnableReleaseOSDFunctionality(t *testing.T) { func TestOkToStopDaemon(t *testing.T) { // First test executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { switch { case args[0] == "mon" && args[1] == "ok-to-stop" && args[2] == "a": return "", nil @@ -94,7 +94,7 @@ func TestOkToStopDaemon(t *testing.T) { assert.NoError(t, err) // Second test - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { assert.Equal(t, "mgr", args[0]) assert.Equal(t, "ok-to-stop", args[1]) assert.Equal(t, "a", args[2]) @@ -107,7 +107,7 @@ func TestOkToStopDaemon(t *testing.T) { assert.NoError(t, err) // Third test - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { assert.Equal(t, "dummy", args[0]) assert.Equal(t, "ok-to-stop", args[1]) assert.Equal(t, "a", args[2]) diff --git a/pkg/daemon/ceph/osd/daemon.go b/pkg/daemon/ceph/osd/daemon.go index 184e516ed..a0726051f 100644 --- a/pkg/daemon/ceph/osd/daemon.go +++ b/pkg/daemon/ceph/osd/daemon.go @@ -68,23 +68,23 @@ func StartOSD(context *clusterd.Context, osdType, osdID, osdUUID, lvPath string, go handleTerminate(context, lvPath, volumeGroupName) - if err := context.Executor.ExecuteCommand(false, "", "vgchange", "-an", volumeGroupName); err != nil { + if err := context.Executor.ExecuteCommand(false, "vgchange", "-an", volumeGroupName); err != nil { return errors.Wrapf(err, "failed to deactivate volume group for lv %q", lvPath) } - if err := context.Executor.ExecuteCommand(false, "", "vgchange", "-ay", volumeGroupName); err != nil { + if err := context.Executor.ExecuteCommand(false, "vgchange", "-ay", volumeGroupName); err != nil { return errors.Wrapf(err, "failed to activate volume group for lv %q", lvPath) } } // activate the osd with ceph-volume storeFlag := "--" + osdType - if err := context.Executor.ExecuteCommand(false, "", "stdbuf", "-oL", "ceph-volume", "lvm", "activate", "--no-systemd", storeFlag, osdID, osdUUID); err != nil { + if err := context.Executor.ExecuteCommand(false, "stdbuf", "-oL", "ceph-volume", "lvm", "activate", "--no-systemd", storeFlag, osdID, osdUUID); err != nil { return errors.Wrapf(err, "failed to activate osd") } // run the ceph-osd daemon - if err := context.Executor.ExecuteCommand(false, "", "ceph-osd", cephArgs...); err != nil { + if err := context.Executor.ExecuteCommand(false, "ceph-osd", cephArgs...); err != nil { // Instead of returning, we want to allow the lvm release to happen below, so we just log the err logger.Errorf("failed to start osd or shutting down. %v", err) } @@ -131,7 +131,7 @@ func killCephOSDProcess(context *clusterd.Context, lvPath string) error { // been testing with kill -9 so this is expected to be safe. There is a fix upstream Ceph that will // improve the shutdown time of the OSD. For cleanliness we should consider removing the -9 // once it is backported to Nautilus: https://github.com/ceph/ceph/pull/31677. - if err := context.Executor.ExecuteCommand(false, "", "kill", "-9", pid); err != nil { + if err := context.Executor.ExecuteCommand(false, "kill", "-9", pid); err != nil { return errors.Wrapf(err, "failed to kill ceph-osd process") } } @@ -429,7 +429,7 @@ func getAvailableDevices(context *clusterd.Context, desiredDevices []DesiredDevi // releaseLVMDevice deactivates the LV to release the device. func releaseLVMDevice(context *clusterd.Context, volumeGroupName string) error { - if err := context.Executor.ExecuteCommand(false, "", "lvchange", "-an", volumeGroupName); err != nil { + if err := context.Executor.ExecuteCommand(false, "lvchange", "-an", volumeGroupName); err != nil { return errors.Wrapf(err, "failed to deactivate LVM %s", volumeGroupName) } logger.Info("Successfully released device from lvm") diff --git a/pkg/daemon/ceph/osd/daemon_test.go b/pkg/daemon/ceph/osd/daemon_test.go index 41a09d01d..d00942b26 100644 --- a/pkg/daemon/ceph/osd/daemon_test.go +++ b/pkg/daemon/ceph/osd/daemon_test.go @@ -137,23 +137,23 @@ USEC_INITIALIZED=15981915740802 func TestAvailableDevices(t *testing.T) { executor := &exectest.MockExecutor{} // set up a mock function to return "rook owned" partitions on the device and it does not have a filesystem - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { - logger.Infof("OUTPUT for %s. %s %+v", name, command, args) + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { + logger.Infof("OUTPUT for %s %v", command, args) if command == "lsblk" { - if strings.Index(name, "sdb") != -1 { + if strings.Index(args[3], "sdb") != -1 { // /dev/sdb has a partition return `NAME="sdb" SIZE="65" TYPE="disk" PKNAME="" NAME="sdb1" SIZE="30" TYPE="part" PKNAME="sdb"`, nil } return "", nil } else if command == "blkid" { - if strings.Index(name, "sdb1") != -1 { + if strings.Index(args[3], "sdb1") != -1 { // partition sdb1 has a label MY-PART return "MY-PART", nil } } else if command == "udevadm" { - if strings.Index(name, "sdc") != -1 { + if strings.Index(args[3], "sdc") != -1 { // /dev/sdc has a file system return udevFSOutput, nil } diff --git a/pkg/daemon/ceph/osd/device_test.go b/pkg/daemon/ceph/osd/device_test.go index 173b20114..42b2e8c8b 100644 --- a/pkg/daemon/ceph/osd/device_test.go +++ b/pkg/daemon/ceph/osd/device_test.go @@ -33,7 +33,7 @@ func TestOSDBootstrap(t *testing.T) { defer os.RemoveAll(configDir) executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { return "{\"key\":\"mysecurekey\"}", nil }, } diff --git a/pkg/daemon/ceph/osd/volume.go b/pkg/daemon/ceph/osd/volume.go index f9d69f307..529a98d1a 100644 --- a/pkg/daemon/ceph/osd/volume.go +++ b/pkg/daemon/ceph/osd/volume.go @@ -294,7 +294,7 @@ func (a *OsdAgent) initializeBlockPVC(context *clusterd.Context, devices *Device } // execute ceph-volume with the device - op, err := context.Executor.ExecuteCommandWithCombinedOutput(false, "", baseCommand, immediateExecuteArgs...) + op, err := context.Executor.ExecuteCommandWithCombinedOutput(false, baseCommand, immediateExecuteArgs...) if err != nil { cvLogFilePath := path.Join(cvLogDir, "ceph-volume.log") @@ -487,12 +487,12 @@ func (a *OsdAgent) initializeDevices(context *clusterd.Context, devices *DeviceO logger.Infof("Base command - %+v", baseCommand) logger.Infof("immediateReportArgs - %+v", baseCommand) logger.Infof("immediateExecuteArgs - %+v", immediateExecuteArgs) - if err := context.Executor.ExecuteCommand(false, "", baseCommand, immediateReportArgs...); err != nil { + if err := context.Executor.ExecuteCommand(false, baseCommand, immediateReportArgs...); err != nil { return errors.Wrapf(err, "failed ceph-volume report") // fail return here as validation provided by ceph-volume } // execute ceph-volume immediately with the device-specific setting instead of batching up multiple devices together - if err := context.Executor.ExecuteCommand(false, "", baseCommand, immediateExecuteArgs...); err != nil { + if err := context.Executor.ExecuteCommand(false, baseCommand, immediateExecuteArgs...); err != nil { return errors.Wrapf(err, "failed ceph-volume") } @@ -535,7 +535,7 @@ func (a *OsdAgent) initializeDevices(context *clusterd.Context, devices *DeviceO "--report", }...) - if err := context.Executor.ExecuteCommand(false, "", baseCommand, reportArgs...); err != nil { + if err := context.Executor.ExecuteCommand(false, baseCommand, reportArgs...); err != nil { return errors.Wrapf(err, "failed ceph-volume report") // fail return here as validation provided by ceph-volume } @@ -544,7 +544,7 @@ func (a *OsdAgent) initializeDevices(context *clusterd.Context, devices *DeviceO "json", }...) - cvOut, err := context.Executor.ExecuteCommandWithCombinedOutput(false, "", baseCommand, reportArgs...) + cvOut, err := context.Executor.ExecuteCommandWithCombinedOutput(false, baseCommand, reportArgs...) if err != nil { return errors.Wrapf(err, "failed ceph-volume json report: %s", cvOut) // fail return here as validation provided by ceph-volume } @@ -561,7 +561,7 @@ func (a *OsdAgent) initializeDevices(context *clusterd.Context, devices *DeviceO } // execute ceph-volume batching up multiple devices - if err := context.Executor.ExecuteCommand(false, "", baseCommand, mdArgs...); err != nil { + if err := context.Executor.ExecuteCommand(false, baseCommand, mdArgs...); err != nil { return errors.Wrapf(err, "failed ceph-volume") // fail return here as validation provided by ceph-volume } } @@ -587,7 +587,7 @@ func getCephVolumeLVMOSDs(context *clusterd.Context, clusterName string, cephfsi // lv can be a block device if raw mode is used cvMode := "lvm" - result, err := context.Executor.ExecuteCommandWithOutput(false, "", cephVolumeCmd, cvMode, "list", lv, "--format", "json") + result, err := context.Executor.ExecuteCommandWithOutput(false, cephVolumeCmd, cvMode, "list", lv, "--format", "json") if err != nil { return nil, errors.Wrapf(err, "failed to retrieve ceph-volume %s list results", cvMode) } @@ -664,7 +664,7 @@ func getCephVolumeRawOSDs(context *clusterd.Context, clusterName string, cephfsi // lv can be a block device if raw mode is used cvMode := "raw" - result, err := context.Executor.ExecuteCommandWithOutput(false, "", cephVolumeCmd, cvMode, "list", block, "--format", "json") + result, err := context.Executor.ExecuteCommandWithOutput(false, cephVolumeCmd, cvMode, "list", block, "--format", "json") if err != nil { return nil, errors.Wrapf(err, "failed to retrieve ceph-volume %s list results", cvMode) } diff --git a/pkg/daemon/ceph/osd/volume_test.go b/pkg/daemon/ceph/osd/volume_test.go index 47758ca75..37261605c 100644 --- a/pkg/daemon/ceph/osd/volume_test.go +++ b/pkg/daemon/ceph/osd/volume_test.go @@ -262,8 +262,8 @@ var cephVolumeRAWTestResult = `{ func TestParseCephVolumeLVMResult(t *testing.T) { executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { - logger.Infof("%s %+v", command, args) + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { + logger.Infof("%s %v", command, args) if command == "ceph-volume" { return cephVolumeLVMTestResult, nil @@ -281,8 +281,8 @@ func TestParseCephVolumeLVMResult(t *testing.T) { func TestParseCephVolumeRawResult(t *testing.T) { executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { - logger.Infof("%s %+v", command, args) + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { + logger.Infof("%s %v", command, args) if command == "ceph-volume" { return cephVolumeRAWTestResult, nil @@ -301,8 +301,8 @@ func TestParseCephVolumeRawResult(t *testing.T) { func TestCephVolumeResultMultiClusterSingleOSD(t *testing.T) { executor := &exectest.MockExecutor{} // set up a mock function to return "rook owned" partitions on the device and it does not have a filesystem - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { - logger.Infof("%s %+v", command, args) + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { + logger.Infof("%s %v", command, args) if command == "ceph-volume" { return cephVolumeTestResultMultiCluster, nil @@ -322,8 +322,8 @@ func TestCephVolumeResultMultiClusterSingleOSD(t *testing.T) { func TestCephVolumeResultMultiClusterMultiOSD(t *testing.T) { executor := &exectest.MockExecutor{} // set up a mock function to return "rook owned" partitions on the device and it does not have a filesystem - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { - logger.Infof("%s %+v", command, args) + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { + logger.Infof("%s %v", command, args) if command == "ceph-volume" { return cephVolumeTestResultMultiClusterMultiOSD, nil diff --git a/pkg/daemon/discover/discover_test.go b/pkg/daemon/discover/discover_test.go index 55f636994..d32b83c93 100644 --- a/pkg/daemon/discover/discover_test.go +++ b/pkg/daemon/discover/discover_test.go @@ -71,31 +71,16 @@ Partition table holds up to 128 entries func TestProbeDevices(t *testing.T) { // set up mock execute so we can verify the partitioning happens on sda executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutput = func(debug bool, name string, command string, args ...string) (string, error) { - logger.Infof("RUN Command for '%s'. %s arg %+v", name, command, args) + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { + logger.Infof("RUN Command %s %v", command, args) output := "" - switch name { - case "lsblk all": + if args[0] == "--all" { output = "testa" - case "lsblk /dev/testa": + } else if args[0] == "/dev/testa" { output = `SIZE="249510756352" ROTA="1" RO="0" TYPE="disk" PKNAME=""` - case "get filesystem type for /dev/testa": - output = udevOutput - case "get parent for device testa": - output = ` testa -testa testa1 -testa testa2 -testa2 centos_host13-root -testa2 centos_host13-swap -testa2 centos_host13-home -` - case "get disk /dev/testa uuid": - output = sgdiskOutput - - case "get disk testa fs serial": + } else if args[0] == "info" && args[1] == "--query=property" { output = udevOutput } - return output, nil } @@ -105,7 +90,6 @@ testa2 centos_host13-home assert.Nil(t, err) assert.Equal(t, 1, len(devices)) assert.Equal(t, "ext2", devices[0].Filesystem) - } func TestMatchUdevMonitorFiltering(t *testing.T) { @@ -354,9 +338,9 @@ func TestDeviceListsEqual(t *testing.T) { func TestGetCephVolumeInventory(t *testing.T) { run := 0 executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, arg ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, arg ...string) (string, error) { run++ - logger.Infof("run %d action %s command %s", run, actionName, command) + logger.Infof("run %d command %s", run, command) switch { case run == 1: return `[{"available": true, "rejected_reasons": [], "sys_api": {"scheduler_mode": "noop", diff --git a/pkg/operator/ceph/client/controller_test.go b/pkg/operator/ceph/client/controller_test.go index 57641a296..a56755e1d 100644 --- a/pkg/operator/ceph/client/controller_test.go +++ b/pkg/operator/ceph/client/controller_test.go @@ -102,7 +102,7 @@ func TestGenerateClient(t *testing.T) { func TestCreateClient(t *testing.T) { clientset := testop.New(t, 1) executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command, outfileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command, outfileArg string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if command == "ceph" && args[1] == "get-or-create-key" { return `{"key":"AQC7ilJdAPijOBAABp+YAzg2QupRAWdnIh7w/Q=="}`, nil @@ -146,7 +146,7 @@ func TestCreateClient(t *testing.T) { func TestUpdateClient(t *testing.T) { clientset := testop.New(t, 1) executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command, outfileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command, outfileArg string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) return "", nil }, @@ -180,7 +180,7 @@ func TestUpdateClient(t *testing.T) { func TestDeleteClient(t *testing.T) { clientset := testop.New(t, 2) executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command, outfileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command, outfileArg string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if command == "ceph" && args[1] == "get-key" && args[2] == "client1" { return `{"key":"AQC7ilJdAPijOBAABp+YAzg2QupRAWdnIh7w/Q=="}`, nil diff --git a/pkg/operator/ceph/cluster/mgr/dashboard.go b/pkg/operator/ceph/cluster/mgr/dashboard.go index ca402352d..2ccbabda6 100755 --- a/pkg/operator/ceph/cluster/mgr/dashboard.go +++ b/pkg/operator/ceph/cluster/mgr/dashboard.go @@ -20,7 +20,6 @@ package mgr import ( "context" "crypto/rand" - "os/exec" "strconv" "syscall" "time" @@ -210,16 +209,6 @@ func (c *Cluster) createSelfSignedCert() (bool, error) { return false, nil } -// Get the return code from the process -func getExitCode(err error) (int, bool) { - if exiterr, ok := err.(*exec.ExitError); ok { - if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { - return status.ExitStatus(), true - } - } - return 0, false -} - func (c *Cluster) setLoginCredentials(password string) error { // Set the login credentials. Write the command/args to the debug log so we don't write the password by default to the log. logger.Infof("setting ceph dashboard %q login creds", dashboardUsername) diff --git a/pkg/operator/ceph/cluster/mgr/dashboard_test.go b/pkg/operator/ceph/cluster/mgr/dashboard_test.go index f80f351c7..2485b7196 100644 --- a/pkg/operator/ceph/cluster/mgr/dashboard_test.go +++ b/pkg/operator/ceph/cluster/mgr/dashboard_test.go @@ -78,7 +78,7 @@ func TestStartSecureDashboard(t *testing.T) { exitCodeResponse := 0 clientset := test.New(t, 3) executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { logger.Infof("command: %s %v", command, args) exitCodeResponse = 0 if args[1] == "module" { @@ -99,8 +99,8 @@ func TestStartSecureDashboard(t *testing.T) { return "", nil }, } - executor.MockExecuteCommandWithOutputFileTimeout = func(debug bool, timeout time.Duration, actionName string, command, outfileArg string, arg ...string) (string, error) { - return executor.MockExecuteCommandWithOutputFile(debug, actionName, command, outfileArg, arg...) + executor.MockExecuteCommandWithOutputFileTimeout = func(debug bool, timeout time.Duration, command, outfileArg string, arg ...string) (string, error) { + return executor.MockExecuteCommandWithOutputFile(debug, command, outfileArg, arg...) } clusterInfo := &cephconfig.ClusterInfo{ diff --git a/pkg/operator/ceph/cluster/mgr/mgr.go b/pkg/operator/ceph/cluster/mgr/mgr.go index c9ad23876..828982bcd 100644 --- a/pkg/operator/ceph/cluster/mgr/mgr.go +++ b/pkg/operator/ceph/cluster/mgr/mgr.go @@ -37,6 +37,7 @@ import ( "github.com/rook/rook/pkg/operator/ceph/controller" cephver "github.com/rook/rook/pkg/operator/ceph/version" "github.com/rook/rook/pkg/operator/k8sutil" + "github.com/rook/rook/pkg/util/exec" v1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -121,7 +122,7 @@ func New( resources: resources, priorityClassName: priorityClassName, ownerRef: ownerRef, - exitCode: getExitCode, + exitCode: exec.ExitStatus, dataDirHostPath: dataDirHostPath, skipUpgradeChecks: skipUpgradeChecks, } diff --git a/pkg/operator/ceph/cluster/mgr/mgr_test.go b/pkg/operator/ceph/cluster/mgr/mgr_test.go index 54d711da0..3d85949d8 100755 --- a/pkg/operator/ceph/cluster/mgr/mgr_test.go +++ b/pkg/operator/ceph/cluster/mgr/mgr_test.go @@ -43,7 +43,7 @@ func TestStartMGR(t *testing.T) { updateDeploymentAndWait, deploymentsUpdated = testopk8s.UpdateDeploymentAndWaitStub() executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { return "{\"key\":\"mysecurekey\"}", nil }, } @@ -139,7 +139,7 @@ func TestConfigureModules(t *testing.T) { configSettings := map[string]string{} lastModuleConfigured := "" executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if command == "ceph" && len(args) > 3 { if args[0] == "mgr" && args[1] == "module" { diff --git a/pkg/operator/ceph/cluster/mgr/orchestrator_test.go b/pkg/operator/ceph/cluster/mgr/orchestrator_test.go index 1cd66387e..866653ebb 100644 --- a/pkg/operator/ceph/cluster/mgr/orchestrator_test.go +++ b/pkg/operator/ceph/cluster/mgr/orchestrator_test.go @@ -34,7 +34,7 @@ func TestOrchestratorModules(t *testing.T) { rookModuleEnabled := false rookBackendSet := false backendErrorCount := 0 - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[0] == "mgr" && args[1] == "module" && args[2] == "enable" { if args[3] == "orchestrator_cli" { @@ -48,7 +48,7 @@ func TestOrchestratorModules(t *testing.T) { } return "", errors.Errorf("unexpected ceph command '%v'", args) } - executor.MockExecuteCommandWithOutputFileTimeout = func(debug bool, timeout time.Duration, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFileTimeout = func(debug bool, timeout time.Duration, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[0] == "orchestrator" && args[1] == "set" && args[2] == "backend" && args[3] == "rook" { if backendErrorCount < 5 { @@ -93,7 +93,7 @@ func TestOrchestratorModules(t *testing.T) { c.clusterInfo.CephVersion = cephver.Octopus err = c.setRookOrchestratorBackend() assert.Error(t, err) - executor.MockExecuteCommandWithOutputFileTimeout = func(debug bool, timeout time.Duration, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFileTimeout = func(debug bool, timeout time.Duration, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[0] == "orch" && args[1] == "set" && args[2] == "backend" && args[3] == "rook" { if backendErrorCount < 5 { diff --git a/pkg/operator/ceph/cluster/mon/config.go b/pkg/operator/ceph/cluster/mon/config.go index 20fec4782..54cc2c352 100644 --- a/pkg/operator/ceph/cluster/mon/config.go +++ b/pkg/operator/ceph/cluster/mon/config.go @@ -249,7 +249,7 @@ func genSecret(executor exec.Executor, configDir, name string, args []string) (s "-n", name, } args = append(base, args...) - _, err := executor.ExecuteCommandWithOutput(true, "gen secret", "ceph-authtool", args...) + _, err := executor.ExecuteCommandWithOutput(true, "ceph-authtool", args...) if err != nil { return "", errors.Wrapf(err, "failed to gen secret") } diff --git a/pkg/operator/ceph/cluster/mon/config_test.go b/pkg/operator/ceph/cluster/mon/config_test.go index 4c73e9916..2076131f5 100644 --- a/pkg/operator/ceph/cluster/mon/config_test.go +++ b/pkg/operator/ceph/cluster/mon/config_test.go @@ -38,7 +38,7 @@ func TestCreateClusterSecrets(t *testing.T) { defer os.RemoveAll(configDir) adminSecret := "AQDkLIBd9vLGJxAAnXsIKPrwvUXAmY+D1g0X1Q==" executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { logger.Infof("COMMAND: %s %v", command, args) if command == "ceph-authtool" && args[0] == "--create-keyring" { filename := args[1] diff --git a/pkg/operator/ceph/cluster/mon/health_test.go b/pkg/operator/ceph/cluster/mon/health_test.go index cbc18552e..52e094af3 100644 --- a/pkg/operator/ceph/cluster/mon/health_test.go +++ b/pkg/operator/ceph/cluster/mon/health_test.go @@ -42,7 +42,7 @@ func TestCheckHealth(t *testing.T) { updateDeploymentAndWait, deploymentsUpdated = testopk8s.UpdateDeploymentAndWaitStub() executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { return clienttest.MonInQuorumResponse(), nil }, } @@ -97,7 +97,7 @@ func TestCheckHealthNotFound(t *testing.T) { updateDeploymentAndWait, deploymentsUpdated = testopk8s.UpdateDeploymentAndWaitStub() executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { return clienttest.MonInQuorumResponse(), nil }, } @@ -153,7 +153,7 @@ func TestAddRemoveMons(t *testing.T) { monQuorumResponse := clienttest.MonInQuorumResponse() executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { return monQuorumResponse, nil }, } diff --git a/pkg/operator/ceph/cluster/mon/mon_test.go b/pkg/operator/ceph/cluster/mon/mon_test.go index c78ddaf3b..f984cff6a 100644 --- a/pkg/operator/ceph/cluster/mon/mon_test.go +++ b/pkg/operator/ceph/cluster/mon/mon_test.go @@ -84,14 +84,14 @@ func newTestStartClusterWithQuorumResponse(t *testing.T, namespace string, monRe configDir, _ := ioutil.TempDir("", "") defer os.RemoveAll(configDir) executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { if strings.Contains(command, "ceph-authtool") { err := cephtest.CreateConfigDir(path.Join(configDir, namespace)) return "", errors.Wrapf(err, "failed testing of start cluster without quorum response") } return "", nil }, - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { // mock quorum health check because a second `Start()` triggers a health check return monResponse() }, diff --git a/pkg/operator/ceph/cluster/osd/health_test.go b/pkg/operator/ceph/cluster/osd/health_test.go index 369ded981..554cae0b8 100644 --- a/pkg/operator/ceph/cluster/osd/health_test.go +++ b/pkg/operator/ceph/cluster/osd/health_test.go @@ -36,11 +36,11 @@ func TestOSDStatus(t *testing.T) { var execCount = 0 executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { return "{\"key\":\"mysecurekey\", \"osdid\":3.0}", nil }, } - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command string, outFileArg string, args ...string) (string, error) { logger.Infof("ExecuteCommandWithOutputFile: %s %v", command, args) execCount++ if args[1] == "dump" { @@ -53,7 +53,7 @@ func TestOSDStatus(t *testing.T) { return "", nil } - executor.MockExecuteCommandWithOutput = func(debug bool, actionName string, command string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutput = func(debug bool, command string, args ...string) (string, error) { logger.Infof("ExecuteCommandWithOutput: %s %v", command, args) return "", nil } diff --git a/pkg/operator/ceph/cluster/osd/osd_test.go b/pkg/operator/ceph/cluster/osd/osd_test.go index b0ad1ba0b..62265a251 100644 --- a/pkg/operator/ceph/cluster/osd/osd_test.go +++ b/pkg/operator/ceph/cluster/osd/osd_test.go @@ -124,7 +124,7 @@ func TestAddRemoveNode(t *testing.T) { } generateKey := "expected key" executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { return "{\"key\": \"" + generateKey + "\"}", nil }, } @@ -158,7 +158,7 @@ func TestAddRemoveNode(t *testing.T) { // mock the ceph calls that will be called during remove node mockExec := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[0] == "status" { return `{"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil diff --git a/pkg/operator/ceph/cluster/rbd/mirror_test.go b/pkg/operator/ceph/cluster/rbd/mirror_test.go index 9680a5614..7e2973f13 100644 --- a/pkg/operator/ceph/cluster/rbd/mirror_test.go +++ b/pkg/operator/ceph/cluster/rbd/mirror_test.go @@ -34,7 +34,7 @@ func TestRBDMirror(t *testing.T) { clientset := testop.New(t, 1) keysCreated := map[string]bool{} executor := &exectest.MockExecutor{} - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command string, outFileArg string, args ...string) (string, error) { logger.Infof("%s %+v", command, args) if args[0] == "auth" && args[1] == "get-or-create-key" { daemonName := args[2] diff --git a/pkg/operator/ceph/config/keyring/store_test.go b/pkg/operator/ceph/config/keyring/store_test.go index 4575c8cac..f5a024137 100644 --- a/pkg/operator/ceph/config/keyring/store_test.go +++ b/pkg/operator/ceph/config/keyring/store_test.go @@ -35,7 +35,7 @@ func TestGenerateKey(t *testing.T) { var generateKey = "" var failGenerateKey = false executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { if failGenerateKey { return "", errors.New("test error") } diff --git a/pkg/operator/ceph/config/monstore_test.go b/pkg/operator/ceph/config/monstore_test.go index 8117dcdb7..f5905e5d7 100644 --- a/pkg/operator/ceph/config/monstore_test.go +++ b/pkg/operator/ceph/config/monstore_test.go @@ -40,7 +40,7 @@ func TestMonStore_Set(t *testing.T) { execedCmd := "" execInjectErr := false executor.MockExecuteCommandWithOutputFile = - func(debug bool, actionName string, command string, outfile string, args ...string) (string, error) { + func(debug bool, command string, outfile string, args ...string) (string, error) { execedCmd = command + " " + strings.Join(args, " ") if execInjectErr { return "output from cmd with error", errors.New("mocked error") @@ -85,7 +85,7 @@ func TestMonStore_SetAll(t *testing.T) { execedCmds := []string{} execInjectErrOnKeyword := "donotinjectanerror" executor.MockExecuteCommandWithOutputFile = - func(debug bool, actionName string, command string, outfile string, args ...string) (string, error) { + func(debug bool, command string, outfile string, args ...string) (string, error) { execedCmd := command + " " + strings.Join(args, " ") execedCmds = append(execedCmds, execedCmd) k := execInjectErrOnKeyword diff --git a/pkg/operator/ceph/file/filesystem_test.go b/pkg/operator/ceph/file/filesystem_test.go index 92f3a5ce9..74d0eb4ed 100644 --- a/pkg/operator/ceph/file/filesystem_test.go +++ b/pkg/operator/ceph/file/filesystem_test.go @@ -77,10 +77,10 @@ func TestCreateFilesystem(t *testing.T) { fses := `[{"name":"myfs","metadata_pool":"myfs-metadata","metadata_pool_id":1,"data_pool_ids":[2],"data_pools":["myfs-data0"]}]` executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { return "{\"key\":\"mysecurekey\"}", nil }, - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { if strings.Contains(command, "ceph-authtool") { err := cephtest.CreateConfigDir(path.Join(configDir, "ns")) assert.Nil(t, err) @@ -131,7 +131,7 @@ func TestCreateFilesystem(t *testing.T) { // Test multiple filesystem creation executor = &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { if contains(args, "ls") { return fses, nil } @@ -155,10 +155,10 @@ func TestCreateNopoolFilesystem(t *testing.T) { fses := `[{"name":"myfs"}]` executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { return "{\"key\":\"mysecurekey\"}", nil }, - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { if strings.Contains(command, "ceph-authtool") { err := cephtest.CreateConfigDir(path.Join(configDir, "ns")) assert.Nil(t, err) @@ -194,7 +194,7 @@ func TestCreateNopoolFilesystem(t *testing.T) { // Test multiple filesystem creation executor = &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { if contains(args, "ls") { return fses, nil } diff --git a/pkg/operator/ceph/nfs/nfs.go b/pkg/operator/ceph/nfs/nfs.go index 8447f6ee2..e177b7b0e 100644 --- a/pkg/operator/ceph/nfs/nfs.go +++ b/pkg/operator/ceph/nfs/nfs.go @@ -127,15 +127,13 @@ func (c *CephNFSController) addRADOSConfigFile(n cephv1.CephNFS, name string) er "--namespace", n.Spec.RADOS.Namespace, "--conf", cephclient.CephConfFilePath(c.context.ConfigDir, c.namespace), } - moniker := "rados stat " + config - err := c.context.Executor.ExecuteCommand(false, moniker, cmd, append(args, "stat", config)...) + err := c.context.Executor.ExecuteCommand(false, cmd, append(args, "stat", config)...) if err == nil { // If stat works then we assume it's present already return nil } // try to create it - moniker = "rados create " + config - return c.context.Executor.ExecuteCommand(false, moniker, cmd, append(args, "create", config)...) + return c.context.Executor.ExecuteCommand(false, cmd, append(args, "create", config)...) } func (c *CephNFSController) addServerToDatabase(nfs cephv1.CephNFS, name string) { @@ -173,7 +171,7 @@ stderr: %s`, moniker, x.Stdout, x.Stderr) } return nil // The below can be used when/if `ganesha-rados-grace` supports a `--cephconf` or similar option - // return c.context.Executor.ExecuteCommand(false, moniker, cmd, args...) + // return c.context.Executor.ExecuteCommand(false, cmd, args...) } func (c *CephNFSController) generateConfig(n cephv1.CephNFS, name string) (string, error) { diff --git a/pkg/operator/ceph/object/admin.go b/pkg/operator/ceph/object/admin.go index c21038867..4b61d4fa5 100644 --- a/pkg/operator/ceph/object/admin.go +++ b/pkg/operator/ceph/object/admin.go @@ -40,7 +40,7 @@ func runAdminCommandNoRealm(c *Context, args ...string) (string, error) { command, args := client.FinalizeCephCommandArgs("radosgw-admin", args, c.Context.ConfigDir, c.ClusterName) // start the rgw admin command - output, err := c.Context.Executor.ExecuteCommandWithOutput(true, "", command, args...) + output, err := c.Context.Executor.ExecuteCommandWithOutput(true, command, args...) if err != nil { return "", errors.Wrapf(err, "failed to run radosgw-admin") } diff --git a/pkg/operator/ceph/object/controller_test.go b/pkg/operator/ceph/object/controller_test.go index 15534b631..10310ec10 100644 --- a/pkg/operator/ceph/object/controller_test.go +++ b/pkg/operator/ceph/object/controller_test.go @@ -166,7 +166,7 @@ func TestCephObjectStoreController(t *testing.T) { } executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName, command, outfile string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command, outfile string, args ...string) (string, error) { if args[0] == "status" { return `{"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil } @@ -175,7 +175,7 @@ func TestCephObjectStoreController(t *testing.T) { } return "", nil }, - MockExecuteCommandWithOutput: func(debug bool, actionName, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { if args[0] == "realm" && args[1] == "list" { return realmListJSON, nil } diff --git a/pkg/operator/ceph/object/objectstore_test.go b/pkg/operator/ceph/object/objectstore_test.go index c2535955a..50ccc0846 100644 --- a/pkg/operator/ceph/object/objectstore_test.go +++ b/pkg/operator/ceph/object/objectstore_test.go @@ -28,7 +28,7 @@ import ( func TestReconcileRealm(t *testing.T) { defaultStore := true - executorFunc := func(debug bool, actionName string, command string, args ...string) (string, error) { + executorFunc := func(debug bool, command string, args ...string) (string, error) { idResponse := `{"id":"test-id"}` logger.Infof("Execute: %s %v", command, args) if args[1] == "get" { @@ -81,7 +81,7 @@ func deleteStore(t *testing.T, name string, existingStores string, expectedDelet executor := &exectest.MockExecutor{} deletedRootPool := false deletedErasureCodeProfile := false - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { //logger.Infof("command: %s %v", command, args) if args[0] == "osd" { if args[1] == "pool" { @@ -118,7 +118,7 @@ func deleteStore(t *testing.T, name string, existingStores string, expectedDelet } return "", errors.Errorf("unexpected ceph command %q", args) } - executorFunc := func(debug bool, actionName, command string, args ...string) (string, error) { + executorFunc := func(debug bool, command string, args ...string) (string, error) { //logger.Infof("Command: %s %v", command, args) if args[0] == "realm" { if args[1] == "delete" { diff --git a/pkg/operator/ceph/object/rgw_test.go b/pkg/operator/ceph/object/rgw_test.go index f029cc48e..f5579c89e 100644 --- a/pkg/operator/ceph/object/rgw_test.go +++ b/pkg/operator/ceph/object/rgw_test.go @@ -38,10 +38,10 @@ import ( func TestStartRGW(t *testing.T) { clientset := testop.New(t, 3) executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { return `{"key":"mysecurekey"}`, nil }, - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { return `{"id":"test-id"}`, nil }, } @@ -76,13 +76,13 @@ func validateStart(t *testing.T, c *clusterConfig, clientset *fclient.Clientset) } func TestCreateObjectStore(t *testing.T) { - commandWithOutputFunc := func(debug bool, actionName, command string, args ...string) (string, error) { + commandWithOutputFunc := func(debug bool, command string, args ...string) (string, error) { return `{"realms": []}`, nil } executor := &exectest.MockExecutor{ MockExecuteCommandWithCombinedOutput: commandWithOutputFunc, MockExecuteCommandWithOutput: commandWithOutputFunc, - MockExecuteCommandWithOutputFile: func(debug bool, actionName, command, outfile string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command, outfile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if command == "ceph" { if args[1] == "erasure-code-profile" { diff --git a/pkg/operator/ceph/object/user/controller_test.go b/pkg/operator/ceph/object/user/controller_test.go index 739359db1..bf63f8dc0 100644 --- a/pkg/operator/ceph/object/user/controller_test.go +++ b/pkg/operator/ceph/object/user/controller_test.go @@ -116,13 +116,13 @@ func TestCephObjectStoreUserController(t *testing.T) { } executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName, command, outfile string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command, outfile string, args ...string) (string, error) { if args[0] == "status" { return `{"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil } return "", nil }, - MockExecuteCommandWithOutput: func(debug bool, actionName, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { if args[0] == "user" { return userCreateJSON, nil } diff --git a/pkg/operator/ceph/pool/controller_test.go b/pkg/operator/ceph/pool/controller_test.go index cabb1e710..c490a725a 100644 --- a/pkg/operator/ceph/pool/controller_test.go +++ b/pkg/operator/ceph/pool/controller_test.go @@ -89,7 +89,7 @@ func TestValidatePool(t *testing.T) { func TestValidateCrushProperties(t *testing.T) { executor := &exectest.MockExecutor{} context := &clusterd.Context{Executor: executor} - executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) { + executor.MockExecuteCommandWithOutputFile = func(debug bool, command, outputFile string, args ...string) (string, error) { logger.Infof("Command: %s %v", command, args) if args[1] == "crush" && args[2] == "dump" { return `{"types":[{"type_id": 0,"name": "osd"}],"buckets":[{"id": -1,"name":"default"},{"id": -2,"name":"good"}]}`, nil @@ -127,7 +127,7 @@ func TestValidateCrushProperties(t *testing.T) { func TestCreatePool(t *testing.T) { executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName, command, outfile string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command, outfile string, args ...string) (string, error) { if command == "ceph" && args[1] == "erasure-code-profile" { return `{"k":"2","m":"1","plugin":"jerasure","technique":"reed_sol_van"}`, nil } @@ -152,7 +152,7 @@ func TestCreatePool(t *testing.T) { func TestDeletePool(t *testing.T) { failOnDelete := false executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName, command, outfile string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command, outfile string, args ...string) (string, error) { if command == "ceph" && args[1] == "lspools" { return `[{"poolnum":1,"poolname":"mypool"}]`, nil } else if command == "ceph" && args[1] == "pool" && args[2] == "get" { @@ -161,7 +161,7 @@ func TestDeletePool(t *testing.T) { return "", nil }, - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { emptyPool := "{\"images\":{\"count\":0,\"provisioned_bytes\":0,\"snap_count\":0},\"trash\":{\"count\":1,\"provisioned_bytes\":2048,\"snap_count\":0}}" p := "{\"images\":{\"count\":1,\"provisioned_bytes\":1024,\"snap_count\":0},\"trash\":{\"count\":1,\"provisioned_bytes\":2048,\"snap_count\":0}}" logger.Infof("Command: %s %v", command, args) @@ -236,7 +236,7 @@ func TestCephBlockPoolController(t *testing.T) { } executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName, command, outfile string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command, outfile string, args ...string) (string, error) { if args[0] == "status" { return `{"pgmap":{"num_pgs":100,"pgs_by_state":[{"state_name":"active+clean","count":100}]}}`, nil } diff --git a/pkg/operator/ceph/provisioner/provisioner_test.go b/pkg/operator/ceph/provisioner/provisioner_test.go index ff91dada2..f9b88b76e 100644 --- a/pkg/operator/ceph/provisioner/provisioner_test.go +++ b/pkg/operator/ceph/provisioner/provisioner_test.go @@ -46,7 +46,7 @@ func TestProvisionImage(t *testing.T) { defer os.Setenv("POD_NAMESPACE", "") defer os.RemoveAll(configDir) executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { if strings.Contains(command, "ceph-authtool") { err := cephtest.CreateConfigDir(path.Join(configDir, namespace)) assert.Nil(t, err) @@ -113,7 +113,7 @@ func TestReclaimPolicyForProvisionedImages(t *testing.T) { defer os.Setenv("POD_NAMESPACE", "") defer os.RemoveAll(configDir) executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, args ...string) (string, error) { if strings.Contains(command, "ceph-authtool") { err := cephtest.CreateConfigDir(path.Join(configDir, namespace)) assert.Nil(t, err) diff --git a/pkg/operator/cockroachdb/controller.go b/pkg/operator/cockroachdb/controller.go index afdc5f5aa..9b2136749 100644 --- a/pkg/operator/cockroachdb/controller.go +++ b/pkg/operator/cockroachdb/controller.go @@ -486,7 +486,7 @@ func (c *ClusterController) initCluster(cluster *cluster) error { } hostFlag := fmt.Sprintf("--host=%s", createQualifiedReplicaServiceName(0, cluster.namespace)) - out, err := c.context.Executor.ExecuteCommandWithCombinedOutput(false, "cockroachdb init", + out, err := c.context.Executor.ExecuteCommandWithCombinedOutput(false, "/cockroach/cockroach", "init", "--insecure", hostFlag) if err != nil { return fmt.Errorf("cluster init failed for namespace %s: %+v. %s", cluster.namespace, err, out) diff --git a/pkg/operator/cockroachdb/controller_test.go b/pkg/operator/cockroachdb/controller_test.go index 599cf9cc8..c02e620e5 100644 --- a/pkg/operator/cockroachdb/controller_test.go +++ b/pkg/operator/cockroachdb/controller_test.go @@ -151,7 +151,7 @@ func TestOnAdd(t *testing.T) { // keep track of if the cockroachdb init command was called initCalled := false executor := &exectest.MockExecutor{ - MockExecuteCommandWithCombinedOutput: func(debug bool, actionName string, command string, arg ...string) (string, error) { + MockExecuteCommandWithCombinedOutput: func(debug bool, command string, arg ...string) (string, error) { if strings.Contains(command, "cockroach") && arg[0] == "init" { initCalled = true } diff --git a/pkg/operator/edgefs/cluster/mgr/mgr_test.go b/pkg/operator/edgefs/cluster/mgr/mgr_test.go index 310a4b379..7056be700 100644 --- a/pkg/operator/edgefs/cluster/mgr/mgr_test.go +++ b/pkg/operator/edgefs/cluster/mgr/mgr_test.go @@ -34,7 +34,7 @@ import ( func TestStartMGR(t *testing.T) { executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + MockExecuteCommandWithOutputFile: func(debug bool, command string, outFileArg string, args ...string) (string, error) { return "{\"key\":\"mysecurekey\"}", nil }, } diff --git a/pkg/util/exec/error.go b/pkg/util/exec/error.go index 69a7abea0..19cf2155b 100644 --- a/pkg/util/exec/error.go +++ b/pkg/util/exec/error.go @@ -16,38 +16,17 @@ limitations under the License. package exec import ( - "fmt" "os/exec" "syscall" ) -type CommandError struct { - ActionName string - Err error -} - -func (e *CommandError) Error() string { - var exitErrMsg string - if exitErr, ok := e.Err.(*exec.ExitError); ok { - // the error is an ExitError, include the stderr output in the final error output - exitErrMsg = string(exitErr.Stderr) - } - - return fmt.Sprintf("Failed to complete '%s': %+v. %s", e.ActionName, e.Err, exitErrMsg) -} - -func (e *CommandError) ExitStatus() int { - exitStatus := -1 - exitErr, ok := e.Err.(*exec.ExitError) +func ExitStatus(err error) (int, bool) { + exitErr, ok := err.(*exec.ExitError) if ok { waitStatus, ok := exitErr.ProcessState.Sys().(syscall.WaitStatus) if ok { - exitStatus = waitStatus.ExitStatus() + return waitStatus.ExitStatus(), true } } - return exitStatus -} - -func createCommandError(err error, actionName string) error { - return &CommandError{ActionName: actionName, Err: err} + return 0, false } diff --git a/pkg/util/exec/exec.go b/pkg/util/exec/exec.go index f64548a96..87ed0b3f4 100644 --- a/pkg/util/exec/exec.go +++ b/pkg/util/exec/exec.go @@ -31,36 +31,35 @@ import ( ) type Executor interface { - ExecuteCommand(suppressLogOutput bool, actionName string, command string, arg ...string) error - ExecuteCommandWithOutput(suppressLogCommand bool, actionName string, command string, arg ...string) (string, error) - ExecuteCommandWithCombinedOutput(suppressLogOutput bool, actionName string, command string, arg ...string) (string, error) - ExecuteCommandWithOutputFile(suppressLogOutput bool, actionName, command, outfileArg string, arg ...string) (string, error) - ExecuteCommandWithOutputFileTimeout(suppressLogOutput bool, timeout time.Duration, actionName, command, outfileArg string, arg ...string) (string, error) - ExecuteCommandWithTimeout(suppressLogOutput bool, timeout time.Duration, actionName string, command string, arg ...string) (string, error) - ExecuteStat(name string) (os.FileInfo, error) + ExecuteCommand(suppressLogOutput bool, command string, arg ...string) error + ExecuteCommandWithOutput(suppressLogCommand bool, command string, arg ...string) (string, error) + ExecuteCommandWithCombinedOutput(suppressLogOutput bool, command string, arg ...string) (string, error) + ExecuteCommandWithOutputFile(suppressLogOutput bool, command, outfileArg string, arg ...string) (string, error) + ExecuteCommandWithOutputFileTimeout(suppressLogOutput bool, timeout time.Duration, command, outfileArg string, arg ...string) (string, error) + ExecuteCommandWithTimeout(suppressLogOutput bool, timeout time.Duration, command string, arg ...string) (string, error) } type CommandExecutor struct { } // Start a process and wait for its completion -func (*CommandExecutor) ExecuteCommand(suppressLogOutput bool, actionName string, command string, arg ...string) error { +func (*CommandExecutor) ExecuteCommand(suppressLogOutput bool, command string, arg ...string) error { cmd, stdout, stderr, err := startCommand(suppressLogOutput, command, arg...) if err != nil { - return createCommandError(err, actionName) + return err } - logOutput(suppressLogOutput, actionName, stdout, stderr) + logOutput(suppressLogOutput, stdout, stderr) if err := cmd.Wait(); err != nil { - return createCommandError(err, actionName) + return err } return nil } // ExecuteCommandWithTimeout starts a process and wait for its completion with timeout. -func (*CommandExecutor) ExecuteCommandWithTimeout(suppressLogOutput bool, timeout time.Duration, actionName string, command string, arg ...string) (string, error) { +func (*CommandExecutor) ExecuteCommandWithTimeout(suppressLogOutput bool, timeout time.Duration, command string, arg ...string) (string, error) { logCommand(suppressLogOutput, command, arg...) cmd := exec.Command(command, arg...) @@ -69,7 +68,7 @@ func (*CommandExecutor) ExecuteCommandWithTimeout(suppressLogOutput bool, timeou cmd.Stderr = &b if err := cmd.Start(); err != nil { - return "", createCommandError(err, actionName) + return "", err } done := make(chan error, 1) @@ -82,18 +81,18 @@ func (*CommandExecutor) ExecuteCommandWithTimeout(suppressLogOutput bool, timeou select { case <-time.After(timeout): if interruptSent { - logger.Infof("Timeout waiting for process %s to return after interrupt signal was sent. Sending kill signal to the process", command) + logger.Infof("timeout waiting for process %s to return after interrupt signal was sent. Sending kill signal to the process", command) var e error if err := cmd.Process.Kill(); err != nil { logger.Errorf("Failed to kill process %s: %+v", command, err) - e = fmt.Errorf("Timeout waiting for the command %s to return after interrupt signal was sent. Tried to kill the process but that failed: %+v", command, err) + e = fmt.Errorf("timeout waiting for the command %s to return after interrupt signal was sent. Tried to kill the process but that failed: %+v", command, err) } else { - e = fmt.Errorf("Timeout waiting for the command %s to return", command) + e = fmt.Errorf("timeout waiting for the command %s to return", command) } - return strings.TrimSpace(b.String()), createCommandError(e, command) + return strings.TrimSpace(b.String()), e } - logger.Infof("Timeout waiting for process %s to return. Sending interrupt signal to the process", command) + logger.Infof("timeout waiting for process %s to return. Sending interrupt signal to the process", command) if err := cmd.Process.Signal(os.Interrupt); err != nil { logger.Errorf("Failed to send interrupt signal to process %s: %+v", command, err) // kill signal will be sent next loop @@ -101,31 +100,30 @@ func (*CommandExecutor) ExecuteCommandWithTimeout(suppressLogOutput bool, timeou interruptSent = true case err := <-done: if err != nil { - return strings.TrimSpace(b.String()), createCommandError(err, command) + return strings.TrimSpace(b.String()), err } if interruptSent { - e := fmt.Errorf("Timeout waiting for the command %s to return", command) - return strings.TrimSpace(b.String()), createCommandError(e, command) + return strings.TrimSpace(b.String()), fmt.Errorf("timeout waiting for the command %s to return", command) } return strings.TrimSpace(b.String()), nil } } } -func (*CommandExecutor) ExecuteCommandWithOutput(suppressLogOutput bool, actionName string, command string, arg ...string) (string, error) { +func (*CommandExecutor) ExecuteCommandWithOutput(suppressLogOutput bool, command string, arg ...string) (string, error) { logCommand(suppressLogOutput, command, arg...) cmd := exec.Command(command, arg...) - return runCommandWithOutput(actionName, cmd, false) + return runCommandWithOutput(cmd, false) } -func (*CommandExecutor) ExecuteCommandWithCombinedOutput(suppressLogOutput bool, actionName string, command string, arg ...string) (string, error) { +func (*CommandExecutor) ExecuteCommandWithCombinedOutput(suppressLogOutput bool, command string, arg ...string) (string, error) { logCommand(suppressLogOutput, command, arg...) cmd := exec.Command(command, arg...) - return runCommandWithOutput(actionName, cmd, true) + return runCommandWithOutput(cmd, true) } // Same as ExecuteCommandWithOutputFile but with a timeout limit. -func (*CommandExecutor) ExecuteCommandWithOutputFileTimeout(suppressLogOutput bool, timeout time.Duration, actionName string, +func (*CommandExecutor) ExecuteCommandWithOutputFileTimeout(suppressLogOutput bool, timeout time.Duration, command, outfileArg string, arg ...string) (string, error) { outFile, err := ioutil.TempFile("", "") @@ -162,7 +160,7 @@ func (*CommandExecutor) ExecuteCommandWithOutputFileTimeout(suppressLogOutput bo return string(fileOut), err } -func (*CommandExecutor) ExecuteCommandWithOutputFile(suppressLogOutput bool, actionName string, command, outfileArg string, arg ...string) (string, error) { +func (*CommandExecutor) ExecuteCommandWithOutputFile(suppressLogOutput bool, command, outfileArg string, arg ...string) (string, error) { // create a temporary file to serve as the output file for the command to be run and ensure // it is cleaned up after this function is done @@ -210,10 +208,6 @@ func startCommand(suppressLogOutput bool, command string, arg ...string) (*exec. return cmd, stdout, stderr, err } -func (*CommandExecutor) ExecuteStat(name string) (os.FileInfo, error) { - return os.Stat(name) -} - // read from reader line by line and write it to the log func logFromReader(suppressLogOutput bool, logger *capnslog.PackageLogger, reader io.ReadCloser) { in := bufio.NewScanner(reader) @@ -224,7 +218,7 @@ func logFromReader(suppressLogOutput bool, logger *capnslog.PackageLogger, reade } } -func logOutput(suppressLogOutput bool, name string, stdout, stderr io.ReadCloser) { +func logOutput(suppressLogOutput bool, stdout, stderr io.ReadCloser) { if stdout == nil || stderr == nil { logger.Warningf("failed to collect stdout and stderr") return @@ -233,11 +227,11 @@ func logOutput(suppressLogOutput bool, name string, stdout, stderr io.ReadCloser // The child processes should appropriately be outputting at the desired global level. Therefore, // we always log at INFO level here, so that log statements from child procs at higher levels // (e.g., WARNING) will still be displayed. We are relying on the child procs to output appropriately. - childLogger := capnslog.NewPackageLogger("github.com/rook/rook", name) + childLogger := capnslog.NewPackageLogger("github.com/rook/rook", "exec") if !childLogger.LevelAt(capnslog.INFO) { rl, err := capnslog.GetRepoLogger("github.com/rook/rook") if err == nil { - rl.SetLogLevel(map[string]capnslog.LogLevel{name: capnslog.INFO}) + rl.SetLogLevel(map[string]capnslog.LogLevel{"exec": capnslog.INFO}) } } @@ -245,7 +239,7 @@ func logOutput(suppressLogOutput bool, name string, stdout, stderr io.ReadCloser logFromReader(suppressLogOutput, childLogger, stdout) } -func runCommandWithOutput(actionName string, cmd *exec.Cmd, combinedOutput bool) (string, error) { +func runCommandWithOutput(cmd *exec.Cmd, combinedOutput bool) (string, error) { var output []byte var err error @@ -258,7 +252,7 @@ func runCommandWithOutput(actionName string, cmd *exec.Cmd, combinedOutput bool) out := strings.TrimSpace(string(output)) if err != nil { - return out, createCommandError(err, actionName) + return out, err } return out, nil diff --git a/pkg/util/exec/test/mockexec.go b/pkg/util/exec/test/mockexec.go index 652b525a4..3988d3ecc 100644 --- a/pkg/util/exec/test/mockexec.go +++ b/pkg/util/exec/test/mockexec.go @@ -16,76 +16,66 @@ limitations under the License. package test import ( - "os" "os/exec" "time" ) // ******************** MockExecutor ******************** type MockExecutor struct { - MockExecuteCommand func(debug bool, actionName string, command string, arg ...string) error - MockStartExecuteCommand func(debug bool, actionName string, command string, arg ...string) (*exec.Cmd, error) - MockExecuteCommandWithOutput func(debug bool, actionName string, command string, arg ...string) (string, error) - MockExecuteCommandWithCombinedOutput func(debug bool, actionName string, command string, arg ...string) (string, error) - MockExecuteCommandWithOutputFile func(debug bool, actionName string, command, outfileArg string, arg ...string) (string, error) - MockExecuteCommandWithOutputFileTimeout func(debug bool, timeout time.Duration, actionName string, command, outfileArg string, arg ...string) (string, error) - MockExecuteCommandWithTimeout func(debug bool, timeout time.Duration, actionName string, command string, arg ...string) (string, error) - MockExecuteStat func(name string) (os.FileInfo, error) + MockExecuteCommand func(suppressLogOutput bool, command string, arg ...string) error + MockStartExecuteCommand func(suppressLogOutput bool, command string, arg ...string) (*exec.Cmd, error) + MockExecuteCommandWithOutput func(suppressLogOutput bool, command string, arg ...string) (string, error) + MockExecuteCommandWithCombinedOutput func(suppressLogOutput bool, command string, arg ...string) (string, error) + MockExecuteCommandWithOutputFile func(suppressLogOutput bool, command, outfileArg string, arg ...string) (string, error) + MockExecuteCommandWithOutputFileTimeout func(suppressLogOutput bool, timeout time.Duration, command, outfileArg string, arg ...string) (string, error) + MockExecuteCommandWithTimeout func(suppressLogOutput bool, timeout time.Duration, command string, arg ...string) (string, error) } -func (e *MockExecutor) ExecuteCommand(debug bool, actionName string, command string, arg ...string) error { +func (e *MockExecutor) ExecuteCommand(suppressLogOutput bool, command string, arg ...string) error { if e.MockExecuteCommand != nil { - return e.MockExecuteCommand(debug, actionName, command, arg...) + return e.MockExecuteCommand(suppressLogOutput, command, arg...) } return nil } -func (e *MockExecutor) ExecuteCommandWithOutput(debug bool, actionName string, command string, arg ...string) (string, error) { +func (e *MockExecutor) ExecuteCommandWithOutput(suppressLogOutput bool, command string, arg ...string) (string, error) { if e.MockExecuteCommandWithOutput != nil { - return e.MockExecuteCommandWithOutput(debug, actionName, command, arg...) + return e.MockExecuteCommandWithOutput(suppressLogOutput, command, arg...) } return "", nil } -func (e *MockExecutor) ExecuteCommandWithTimeout(debug bool, timeout time.Duration, actionName string, command string, arg ...string) (string, error) { +func (e *MockExecutor) ExecuteCommandWithTimeout(suppressLogOutput bool, timeout time.Duration, command string, arg ...string) (string, error) { if e.MockExecuteCommandWithTimeout != nil { - return e.MockExecuteCommandWithTimeout(debug, time.Second, actionName, command, arg...) + return e.MockExecuteCommandWithTimeout(suppressLogOutput, time.Second, command, arg...) } return "", nil } -func (e *MockExecutor) ExecuteCommandWithCombinedOutput(debug bool, actionName string, command string, arg ...string) (string, error) { +func (e *MockExecutor) ExecuteCommandWithCombinedOutput(suppressLogOutput bool, command string, arg ...string) (string, error) { if e.MockExecuteCommandWithCombinedOutput != nil { - return e.MockExecuteCommandWithCombinedOutput(debug, actionName, command, arg...) + return e.MockExecuteCommandWithCombinedOutput(suppressLogOutput, command, arg...) } return "", nil } -func (e *MockExecutor) ExecuteCommandWithOutputFile(debug bool, actionName string, command, outfileArg string, arg ...string) (string, error) { +func (e *MockExecutor) ExecuteCommandWithOutputFile(suppressLogOutput bool, command, outfileArg string, arg ...string) (string, error) { if e.MockExecuteCommandWithOutputFile != nil { - return e.MockExecuteCommandWithOutputFile(debug, actionName, command, outfileArg, arg...) + return e.MockExecuteCommandWithOutputFile(suppressLogOutput, command, outfileArg, arg...) } return "", nil } -func (e *MockExecutor) ExecuteCommandWithOutputFileTimeout(debug bool, timeout time.Duration, actionName string, command, outfileArg string, arg ...string) (string, error) { +func (e *MockExecutor) ExecuteCommandWithOutputFileTimeout(suppressLogOutput bool, timeout time.Duration, command, outfileArg string, arg ...string) (string, error) { if e.MockExecuteCommandWithOutputFileTimeout != nil { - return e.MockExecuteCommandWithOutputFileTimeout(debug, timeout, actionName, command, outfileArg, arg...) + return e.MockExecuteCommandWithOutputFileTimeout(suppressLogOutput, timeout, command, outfileArg, arg...) } return "", nil } - -func (e *MockExecutor) ExecuteStat(name string) (os.FileInfo, error) { - if e.MockExecuteStat != nil { - return e.MockExecuteStat(name) - } - - return nil, nil -} diff --git a/pkg/util/exec/translate_exec.go b/pkg/util/exec/translate_exec.go index 68450d367..0b0439674 100644 --- a/pkg/util/exec/translate_exec.go +++ b/pkg/util/exec/translate_exec.go @@ -16,8 +16,6 @@ limitations under the License. package exec import ( - "fmt" - "os" "time" ) @@ -30,48 +28,43 @@ type TranslateCommandExecutor struct { Executor Executor // Translator translates every command before running it - Translator func(debug bool, actionName string, command string, arg ...string) (string, []string) + Translator func(suppressLogOutput bool, command string, arg ...string) (string, []string) } // ExecuteCommand starts a process and wait for its completion -func (e *TranslateCommandExecutor) ExecuteCommand(debug bool, actionName string, command string, arg ...string) error { - transCommand, transArgs := e.Translator(debug, actionName, command, arg...) - return e.Executor.ExecuteCommand(debug, actionName, transCommand, transArgs...) +func (e *TranslateCommandExecutor) ExecuteCommand(suppressLogOutput bool, command string, arg ...string) error { + transCommand, transArgs := e.Translator(suppressLogOutput, command, arg...) + return e.Executor.ExecuteCommand(suppressLogOutput, transCommand, transArgs...) } // ExecuteCommandWithOutput starts a process and wait for its completion -func (e *TranslateCommandExecutor) ExecuteCommandWithOutput(debug bool, actionName string, command string, arg ...string) (string, error) { - transCommand, transArgs := e.Translator(debug, actionName, command, arg...) - return e.Executor.ExecuteCommandWithOutput(debug, actionName, transCommand, transArgs...) +func (e *TranslateCommandExecutor) ExecuteCommandWithOutput(suppressLogOutput bool, command string, arg ...string) (string, error) { + transCommand, transArgs := e.Translator(suppressLogOutput, command, arg...) + return e.Executor.ExecuteCommandWithOutput(suppressLogOutput, transCommand, transArgs...) } // ExecuteCommandWithCombinedOutput starts a process and returns its stdout and stderr combined. -func (e *TranslateCommandExecutor) ExecuteCommandWithCombinedOutput(debug bool, actionName string, command string, arg ...string) (string, error) { - transCommand, transArgs := e.Translator(debug, actionName, command, arg...) - return e.Executor.ExecuteCommandWithCombinedOutput(debug, actionName, transCommand, transArgs...) +func (e *TranslateCommandExecutor) ExecuteCommandWithCombinedOutput(suppressLogOutput bool, command string, arg ...string) (string, error) { + transCommand, transArgs := e.Translator(suppressLogOutput, command, arg...) + return e.Executor.ExecuteCommandWithCombinedOutput(suppressLogOutput, transCommand, transArgs...) } // ExecuteCommandWithOutputFile starts a process and saves output to file -func (e *TranslateCommandExecutor) ExecuteCommandWithOutputFile(debug bool, actionName string, command, outfileArg string, arg ...string) (string, error) { - transCommand, transArgs := e.Translator(debug, actionName, command, arg...) - return e.Executor.ExecuteCommandWithOutputFile(debug, actionName, transCommand, outfileArg, transArgs...) +func (e *TranslateCommandExecutor) ExecuteCommandWithOutputFile(suppressLogOutput bool, command, outfileArg string, arg ...string) (string, error) { + transCommand, transArgs := e.Translator(suppressLogOutput, command, arg...) + return e.Executor.ExecuteCommandWithOutputFile(suppressLogOutput, transCommand, outfileArg, transArgs...) } // ExecuteCommandWithOutputFileTimeout is the same as ExecuteCommandWithOutputFile but with a timeout limit. func (e *TranslateCommandExecutor) ExecuteCommandWithOutputFileTimeout( - debug bool, timeout time.Duration, actionName string, + suppressLogOutput bool, timeout time.Duration, command, outfileArg string, arg ...string) (string, error) { - transCommand, transArgs := e.Translator(debug, actionName, command, arg...) - return e.Executor.ExecuteCommandWithOutputFileTimeout(debug, timeout, actionName, transCommand, outfileArg, transArgs...) + transCommand, transArgs := e.Translator(suppressLogOutput, command, arg...) + return e.Executor.ExecuteCommandWithOutputFileTimeout(suppressLogOutput, timeout, transCommand, outfileArg, transArgs...) } // ExecuteCommandWithTimeout starts a process and wait for its completion with timeout. -func (e *TranslateCommandExecutor) ExecuteCommandWithTimeout(debug bool, timeout time.Duration, actionName string, command string, arg ...string) (string, error) { - transCommand, transArgs := e.Translator(debug, actionName, command, arg...) - return e.Executor.ExecuteCommandWithTimeout(debug, timeout, actionName, transCommand, transArgs...) -} - -// ExecuteStat returns a file stat -func (e *TranslateCommandExecutor) ExecuteStat(name string) (os.FileInfo, error) { - return nil, fmt.Errorf("TODO: TranslateCommandExecutor.ExecuteStat() not implemented ... is it needed?") +func (e *TranslateCommandExecutor) ExecuteCommandWithTimeout(suppressLogOutput bool, timeout time.Duration, command string, arg ...string) (string, error) { + transCommand, transArgs := e.Translator(suppressLogOutput, command, arg...) + return e.Executor.ExecuteCommandWithTimeout(suppressLogOutput, timeout, transCommand, transArgs...) } diff --git a/pkg/util/sys/device.go b/pkg/util/sys/device.go index b238dd8ee..a7026efbf 100644 --- a/pkg/util/sys/device.go +++ b/pkg/util/sys/device.go @@ -110,8 +110,7 @@ type LocalDisk struct { // ListDevices list all devices available on a machine func ListDevices(executor exec.Executor) ([]string, error) { - cmd := "lsblk all" - devices, err := executor.ExecuteCommandWithOutput(false, cmd, "lsblk", "--all", "--noheadings", "--list", "--output", "KNAME") + devices, err := executor.ExecuteCommandWithOutput(false, "lsblk", "--all", "--noheadings", "--list", "--output", "KNAME") if err != nil { return nil, fmt.Errorf("failed to list all devices: %+v", err) } @@ -130,8 +129,7 @@ func GetDevicePartitions(device string, executor exec.Executor) (partitions []Pa devicePath = device //use the exact device path (like /mnt/) in case of PVC block device } - cmd := fmt.Sprintf("lsblk %s", devicePath) - output, err := executor.ExecuteCommandWithOutput(false, cmd, "lsblk", devicePath, + output, err := executor.ExecuteCommandWithOutput(false, "lsblk", devicePath, "--bytes", "--pairs", "--output", "NAME,SIZE,TYPE,PKNAME") logger.Infof("Output: %+v", output) if err != nil { @@ -200,8 +198,7 @@ func GetDeviceProperties(device string, executor exec.Executor) (map[string]stri // GetDevicePropertiesFromPath gets a device property from a path func GetDevicePropertiesFromPath(devicePath string, executor exec.Executor) (map[string]string, error) { - cmd := fmt.Sprintf("lsblk %s", devicePath) - output, err := executor.ExecuteCommandWithOutput(false, cmd, "lsblk", devicePath, + output, err := executor.ExecuteCommandWithOutput(false, "lsblk", devicePath, "--bytes", "--nodeps", "--pairs", "--output", "SIZE,ROTA,RO,TYPE,PKNAME,NAME") if err != nil { // The "not a block device" error also returns code 32 so the ExitStatus() check hides this error @@ -210,8 +207,7 @@ func GetDevicePropertiesFromPath(devicePath string, executor exec.Executor) (map } // try to get more information about the command error - cmdErr, ok := err.(*exec.CommandError) - if ok && cmdErr.ExitStatus() == 32 { + if code, ok := exec.ExitStatus(err); ok && code == 32 { // certain device types (such as loop) return exit status 32 when probed further, // ignore and continue without logging return map[string]string{}, nil @@ -238,8 +234,7 @@ func IsLV(devicePath string, executor exec.Executor) (bool, error) { // GetUdevInfo gets udev information func GetUdevInfo(device string, executor exec.Executor) (map[string]string, error) { - cmd := fmt.Sprintf("udevadm info %s", device) - output, err := executor.ExecuteCommandWithOutput(false, cmd, "udevadm", "info", "--query=property", fmt.Sprintf("/dev/%s", device)) + output, err := executor.ExecuteCommandWithOutput(false, "udevadm", "info", "--query=property", fmt.Sprintf("/dev/%s", device)) if err != nil { return nil, err } @@ -253,10 +248,9 @@ func GetDeviceFilesystems(device string, executor exec.Executor) (string, error) if len(devicePath) == 1 { device = fmt.Sprintf("/dev/%s", device) } - cmd := fmt.Sprintf("get filesystem type for %s", device) - output, err := executor.ExecuteCommandWithOutput(false, cmd, "udevadm", "info", "--query=property", device) + output, err := executor.ExecuteCommandWithOutput(false, "udevadm", "info", "--query=property", device) if err != nil { - return "", fmt.Errorf("command %s failed: %+v", cmd, err) + return "", err } return parseFS(output), nil @@ -274,10 +268,7 @@ func GetDiskUUID(device string, executor exec.Executor) (string, error) { device = fmt.Sprintf("/dev/%s", device) } - cmd := fmt.Sprintf("get disk %s uuid", device) - - output, err := executor.ExecuteCommandWithOutput(false, cmd, - sgdisk, "--print", device) + output, err := executor.ExecuteCommandWithOutput(false, sgdisk, "--print", device) if err != nil { return "", err } @@ -378,7 +369,7 @@ func inventoryDevice(executor exec.Executor, dev string) (CephVolumeInventory, e device := path.Join("/dev", dev) args := []string{"inventory", "--format", "json", device} - inventory, err := executor.ExecuteCommandWithOutput(false, "", "ceph-volume", args...) + inventory, err := executor.ExecuteCommandWithOutput(false, "ceph-volume", args...) if err != nil { return CVInventory, fmt.Errorf("failed to execute ceph-volume inventory on disk %q. %v", err, device) } @@ -394,9 +385,7 @@ func inventoryDevice(executor exec.Executor, dev string) (CephVolumeInventory, e // ListDevicesChild list all child available on a device func ListDevicesChild(executor exec.Executor, device string) ([]string, error) { - cmd := "lsblk for child" - - childListRaw, err := executor.ExecuteCommandWithOutput(false, cmd, "lsblk", "--noheadings", "--pairs", path.Join("/dev", device)) + childListRaw, err := executor.ExecuteCommandWithOutput(false, "lsblk", "--noheadings", "--pairs", path.Join("/dev", device)) if err != nil { return []string{}, fmt.Errorf("failed to list child devices of %q. %v", device, err) } diff --git a/pkg/util/sys/device_test.go b/pkg/util/sys/device_test.go index 02ef3e229..3b9c20065 100644 --- a/pkg/util/sys/device_test.go +++ b/pkg/util/sys/device_test.go @@ -116,9 +116,9 @@ func TestParseFileSystem(t *testing.T) { func TestGetPartitions(t *testing.T) { run := 0 executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, arg ...string) (string, error) { + MockExecuteCommandWithOutput: func(debug bool, command string, arg ...string) (string, error) { run++ - logger.Infof("run %d action %s command %s", run, actionName, command) + logger.Infof("run %d command %s", run, command) switch { case run == 1: return `NAME="sdc" SIZE="100000" TYPE="disk" PKNAME=""`, nil @@ -185,8 +185,8 @@ func TestParseUdevInfo(t *testing.T) { func TestListDevicesChildListDevicesChild(t *testing.T) { executor := &exectest.MockExecutor{ - MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, arg ...string) (string, error) { - logger.Infof("action %s command %s", actionName, command) + MockExecuteCommandWithOutput: func(debug bool, command string, arg ...string) (string, error) { + logger.Infof("command %s", command) return lsblkChildOutput, nil }, } diff --git a/pkg/util/sys/kmod.go b/pkg/util/sys/kmod.go index ac6e70c13..4eb6bce2f 100644 --- a/pkg/util/sys/kmod.go +++ b/pkg/util/sys/kmod.go @@ -42,7 +42,7 @@ func IsBuiltinKernelModule(name string, executor pkgexec.Executor) (bool, error) } kv = fmt.Sprintf("/lib/modules/%s/modules.builtin", kv) - out, err := executor.ExecuteCommandWithCombinedOutput(false, "check builtin kmod", "cat", kv) + out, err := executor.ExecuteCommandWithCombinedOutput(false, "cat", kv) if err != nil { return false, fmt.Errorf("failed to cat %s: %+v", kv, err) } @@ -58,7 +58,7 @@ func LoadKernelModule(name string, options []string, executor pkgexec.Executor) args := append([]string{name}, options...) - if err := executor.ExecuteCommand(false, fmt.Sprintf("modprobe %s", name), "modprobe", args[:]...); err != nil { + if err := executor.ExecuteCommand(false, "modprobe", args[:]...); err != nil { return fmt.Errorf("failed to load kernel module %s: %+v", name, err) } @@ -66,7 +66,7 @@ func LoadKernelModule(name string, options []string, executor pkgexec.Executor) } func CheckKernelModuleParam(name, param string, executor pkgexec.Executor) (bool, error) { - out, err := executor.ExecuteCommandWithOutput(false, "check kmod param", "modinfo", "-F", "parm", name) + out, err := executor.ExecuteCommandWithOutput(false, "modinfo", "-F", "parm", name) if err != nil { return false, fmt.Errorf("failed to check for %s module %s param: %+v", name, param, err) } diff --git a/tests/framework/utils/exec_utils.go b/tests/framework/utils/exec_utils.go index 7ac02784d..ef40ec1c1 100644 --- a/tests/framework/utils/exec_utils.go +++ b/tests/framework/utils/exec_utils.go @@ -20,12 +20,11 @@ import ( "bufio" "bytes" "fmt" - "log" "os/exec" "strings" - "syscall" "github.com/coreos/pkg/capnslog" + utilexec "github.com/rook/rook/pkg/util/exec" ) var logger = capnslog.NewPackageLogger("github.com/rook/rook", "testutil") @@ -111,17 +110,14 @@ func ExecuteCommand(cmdStruct CommandArgs) CommandOut { stdin.Close() } - if err := cmd.Wait(); err != nil { - if exiterr, ok := err.(*exec.ExitError); ok { - // The program has exited with an exit code != 0 - if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { - log.Printf("Exit Status: %d", status.ExitStatus()) - return CommandOut{StdErr: errBuffer.String(), StdOut: outBuffer.String(), ExitCode: status.ExitStatus(), Err: exiterr} - } - } else { - return CommandOut{StdErr: errBuffer.String(), StdOut: outBuffer.String(), Err: nil} + err = cmd.Wait() + out := CommandOut{StdErr: errBuffer.String(), StdOut: outBuffer.String()} + if err != nil { + out.Err = err + if code, ok := utilexec.ExitStatus(err); ok { + out.ExitCode = code } } - return CommandOut{StdErr: errBuffer.String(), StdOut: outBuffer.String(), Err: err} + return out } diff --git a/tests/framework/utils/helm_helper.go b/tests/framework/utils/helm_helper.go index c3fb21228..a3fa1edcc 100644 --- a/tests/framework/utils/helm_helper.go +++ b/tests/framework/utils/helm_helper.go @@ -39,7 +39,7 @@ func NewHelmHelper(helmPath string) *HelmHelper { // Execute is wrapper for executing helm commands func (h *HelmHelper) Execute(args ...string) (string, error) { - result, err := h.executor.ExecuteCommandWithOutput(false, "", h.HelmPath, args...) + result, err := h.executor.ExecuteCommandWithOutput(false, h.HelmPath, args...) if err != nil { logger.Errorf("Errors Encountered while executing helm command %v: %v", result, err) return result, fmt.Errorf("Failed to run helm commands on args %v : %v , err -> %v", args, result, err) diff --git a/tests/framework/utils/k8s_helper.go b/tests/framework/utils/k8s_helper.go index ee1df1722..ab8f921cb 100644 --- a/tests/framework/utils/k8s_helper.go +++ b/tests/framework/utils/k8s_helper.go @@ -128,7 +128,7 @@ func (k8sh *K8sHelper) SetDeploymentVersion(namespace, deploymentName, container // Kubectl is wrapper for executing kubectl commands func (k8sh *K8sHelper) Kubectl(args ...string) (string, error) { - result, err := k8sh.executor.ExecuteCommandWithTimeout(false, 15*time.Second, "kubectl", "kubectl", args...) + result, err := k8sh.executor.ExecuteCommandWithTimeout(false, 15*time.Second, "kubectl", args...) if err != nil { k8slogger.Errorf("Failed to execute: kubectl %+v : %+v. %s", args, err, result) if args[0] == "delete" {