2022-04-21 20:00:50 +03:00
|
|
|
package platform
|
|
|
|
|
|
|
|
import (
|
2024-08-07 00:36:55 +03:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2022-04-21 20:00:50 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-08-07 00:36:55 +03:00
|
|
|
// Command execution time is more than timeout, so ExecuteRawCommand should return error
|
|
|
|
func TestExecuteRawCommandTimeout(t *testing.T) {
|
2022-04-21 20:00:50 +03:00
|
|
|
const timeout = 2 * time.Second
|
|
|
|
client := NewExecClientTimeout(timeout)
|
|
|
|
|
2024-08-07 00:36:55 +03:00
|
|
|
_, err := client.ExecuteRawCommand("sleep 3")
|
2022-04-21 20:00:50 +03:00
|
|
|
if err == nil {
|
2024-08-07 00:36:55 +03:00
|
|
|
t.Errorf("TestExecuteRawCommandTimeout should have returned timeout error")
|
2022-04-21 20:00:50 +03:00
|
|
|
}
|
|
|
|
t.Logf("%s", err.Error())
|
|
|
|
}
|
|
|
|
|
2024-08-07 00:36:55 +03:00
|
|
|
// Command execution time is less than timeout, so ExecuteRawCommand should work without error
|
|
|
|
func TestExecuteRawCommandNoTimeout(t *testing.T) {
|
2022-04-21 20:00:50 +03:00
|
|
|
const timeout = 2 * time.Second
|
|
|
|
client := NewExecClientTimeout(timeout)
|
|
|
|
|
2024-08-07 00:36:55 +03:00
|
|
|
_, err := client.ExecuteRawCommand("sleep 1")
|
2022-04-21 20:00:50 +03:00
|
|
|
if err != nil {
|
2024-08-07 00:36:55 +03:00
|
|
|
t.Errorf("TestExecuteRawCommandNoTimeout failed with error %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecuteCommand(t *testing.T) {
|
|
|
|
output, err := NewExecClient(nil).ExecuteCommand(context.Background(), "echo", "/B && echo two")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("TestExecuteCommand failed with error %v", err)
|
|
|
|
}
|
|
|
|
if strings.TrimRight(output, "\n\r") != "/B && echo two" {
|
|
|
|
t.Errorf("TestExecuteCommand failed with output %s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecuteCommandError(t *testing.T) {
|
|
|
|
_, err := NewExecClient(nil).ExecuteCommand(context.Background(), "donotaddtopath")
|
|
|
|
if !errors.Is(err, exec.ErrNotFound) {
|
|
|
|
t.Errorf("TestExecuteCommand failed with error %v", err)
|
2022-04-21 20:00:50 +03:00
|
|
|
}
|
|
|
|
}
|
2024-08-07 00:36:55 +03:00
|
|
|
|
|
|
|
// Command execution time is more than timeout, so ExecuteCommand should return error
|
|
|
|
func TestExecuteCommandTimeout(t *testing.T) {
|
|
|
|
const timeout = 2 * time.Second
|
|
|
|
client := NewExecClientTimeout(timeout)
|
|
|
|
|
|
|
|
_, err := client.ExecuteCommand(context.Background(), "sleep", "3")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("TestExecuteCommandTimeout should have returned timeout error")
|
|
|
|
}
|
|
|
|
t.Logf("%s", err.Error())
|
|
|
|
}
|