/
zhendosina
/
obagent
Обзор
Документация
Войти
/
zhendosina
/
obagent
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
lib/shell/exec_test.go
158 строк
4 KB
zhj-luo
Fix file header
14 авг 2023, 16:10
14 авг 2023, 16:10
8b4d999
Код
Авторство
О чём код?
/* * Copyright (c) 2023 OceanBase * OBAgent is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * http://license.coscl.org.cn/MulanPSL2 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ package shell import ( "bytes" "os/exec" "testing" "time" log "github.com/sirupsen/logrus" . "github.com/smartystreets/goconvey/convey" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) var ( sleepbin, _ = exec.LookPath("sleep") echobin, _ = exec.LookPath("echo") shell, _ = exec.LookPath("sh") ) var libShell Shell = ShellImpl{} func TestExecuteCommand(t *testing.T) { type args struct { cmd string } type want struct { successful bool } tests := []struct { name string args args want want }{ { name: "normal command", args: args{cmd: "echo a"}, want: want{successful: true}, }, { name: "command not exist", args: args{cmd: "command_not_exist"}, want: want{successful: false}, }, } for _, tt := range tests { Convey(tt.name, t, func() { executeResult, _ := libShell.NewCommand(tt.args.cmd).Execute() So(executeResult, ShouldNotBeNil) So(executeResult.IsSuccessful(), ShouldEqual, tt.want.successful) }) } } func TestRunTimeout(t *testing.T) { if testing.Short() { t.Skip("Skipping test due to random failures.") } if sleepbin == "" { t.Skip("'sleep' binary not available on OS, skipping.") } cmd := exec.Command(sleepbin, "10") start := time.Now() err := RunTimeout(cmd, time.Millisecond*20) elapsed := time.Since(start) assert.Equal(t, TimeoutErr, err) // Verify that command gets killed in 20ms, with some breathing room assert.True(t, elapsed < time.Millisecond*75) } // Verifies behavior of a command that doesn't get killed. func TestRunTimeoutFastExit(t *testing.T) { if testing.Short() { t.Skip("Skipping test due to random failures.") } if echobin == "" { t.Skip("'echo' binary not available on OS, skipping.") } cmd := exec.Command(echobin) start := time.Now() err := RunTimeout(cmd, time.Millisecond*20) buf := &bytes.Buffer{} log.SetOutput(buf) elapsed := time.Since(start) require.NoError(t, err) // Verify that command gets killed in 20ms, with some breathing room assert.True(t, elapsed < time.Millisecond*75) // Verify "process already finished" log doesn't occur. time.Sleep(time.Millisecond * 75) require.Equal(t, "", buf.String()) } func TestCombinedOutputTimeout(t *testing.T) { t.Skip("Test failing too often, skip for now and revisit later.") if sleepbin == "" { t.Skip("'sleep' binary not available on OS, skipping.") } cmd := exec.Command(sleepbin, "10") start := time.Now() _, err := CombinedOutputTimeout(cmd, time.Millisecond*20) elapsed := time.Since(start) assert.Equal(t, TimeoutErr, err) // Verify that command gets killed in 20ms, with some breathing room assert.True(t, elapsed < time.Millisecond*75) } func TestCombinedOutput(t *testing.T) { if echobin == "" { t.Skip("'echo' binary not available on OS, skipping.") } cmd := exec.Command(echobin, "foo") out, err := CombinedOutputTimeout(cmd, time.Second) assert.NoError(t, err) assert.Equal(t, "foo\n", string(out)) } // test that CombinedOutputTimeout and exec.Cmd.CombinedOutput return // the same output from a failed command. func TestCombinedOutputError(t *testing.T) { if shell == "" { t.Skip("'sh' binary not available on OS, skipping.") } cmd := exec.Command(shell, "-c", "false") expected, err := cmd.CombinedOutput() cmd2 := exec.Command(shell, "-c", "false") actual, err := CombinedOutputTimeout(cmd2, time.Second) assert.Error(t, err) assert.Equal(t, expected, actual) } func TestRunError(t *testing.T) { if shell == "" { t.Skip("'sh' binary not available on OS, skipping.") } cmd := exec.Command(shell, "-c", "false") err := RunTimeout(cmd, time.Second) assert.Error(t, err) }