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.
67 lines
1.3 KiB
67 lines
1.3 KiB
3 years ago
|
package shell
|
||
4 years ago
|
|
||
|
import (
|
||
4 years ago
|
"github.com/sirupsen/logrus"
|
||
4 years ago
|
"github.com/stretchr/testify/require"
|
||
|
"os/exec"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
3 years ago
|
func TestLocalShellCommand(t *testing.T) {
|
||
4 years ago
|
t.Parallel()
|
||
|
|
||
|
//setup
|
||
3 years ago
|
testShell := localShell{}
|
||
4 years ago
|
//test
|
||
3 years ago
|
result, err := testShell.Command(logrus.WithField("exec", "test"), "echo", []string{"hello world"}, "", nil)
|
||
4 years ago
|
|
||
|
//assert
|
||
|
require.NoError(t, err)
|
||
|
require.Equal(t, "hello world\n", result)
|
||
|
}
|
||
|
|
||
3 years ago
|
func TestLocalShellCommand_Date(t *testing.T) {
|
||
4 years ago
|
t.Parallel()
|
||
|
|
||
|
//setup
|
||
3 years ago
|
testShell := localShell{}
|
||
4 years ago
|
|
||
|
//test
|
||
3 years ago
|
_, err := testShell.Command(logrus.WithField("exec", "test"), "date", []string{}, "", nil)
|
||
4 years ago
|
|
||
|
//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())
|
||
|
//
|
||
|
//}
|
||
|
//
|
||
|
|
||
3 years ago
|
func TestLocalShellCommand_InvalidCommand(t *testing.T) {
|
||
4 years ago
|
t.Parallel()
|
||
|
|
||
|
//setup
|
||
3 years ago
|
testShell := localShell{}
|
||
4 years ago
|
|
||
|
//test
|
||
3 years ago
|
_, err := testShell.Command(logrus.WithField("exec", "test"), "invalid_binary", []string{}, "", nil)
|
||
4 years ago
|
|
||
|
//assert
|
||
|
_, castOk := err.(*exec.ExitError)
|
||
|
require.False(t, castOk)
|
||
|
}
|