You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
scrutiny/collector/pkg/common/exec_test.go

66 lines
1.2 KiB

package common_test
import (
"github.com/analogj/scrutiny/collector/pkg/common"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"os/exec"
"testing"
)
func TestExecCmd(t *testing.T) {
t.Parallel()
//setup
//test
result, err := common.ExecCmd(logrus.WithField("exec", "test"), "echo", []string{"hello world"}, "", nil)
//assert
require.NoError(t, err)
require.Equal(t, "hello world\n", result)
}
func TestExecCmd_Date(t *testing.T) {
t.Parallel()
//setup
//test
_, err := common.ExecCmd(logrus.WithField("exec", "test"), "date", []string{}, "", nil)
//assert
require.NoError(t, err)
}
//
//func TestExecCmd_Error(t *testing.T) {
// t.Parallel()
//
// //setup
// bc := collector.BaseCollector {}
//
// //test
// _, err := bc.ExecCmd("smartctl", []string{"-a", "/dev/doesnotexist"}, "", nil)
//
// //assert
// exitError, castOk := err.(*exec.ExitError);
// require.True(t, castOk)
// require.Equal(t, 1, exitError.ExitCode())
//
//}
//
func TestExecCmd_InvalidCommand(t *testing.T) {
t.Parallel()
//setup
//test
_, err := common.ExecCmd(logrus.WithField("exec", "test"), "invalid_binary", []string{}, "", nil)
//assert
_, castOk := err.(*exec.ExitError)
require.False(t, castOk)
}