mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-10-22 20:39:41 +00:00
utils: Add waitLocalProcess function
Refactored some of the hypervisors to remove the duplicated code used to trigger a shutdown. Also added some unit tests. Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
This commit is contained in:
@@ -9,14 +9,19 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const waitLocalProcessTimeoutSecs = 3
|
||||
|
||||
func TestFileCopySuccessful(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
fileContent := "testContent"
|
||||
@@ -375,3 +380,99 @@ func TestToBytes(t *testing.T) {
|
||||
expected := uint64(1073741824)
|
||||
assert.Equal(expected, result)
|
||||
}
|
||||
|
||||
func TestWaitLocalProcessInvalidSignal(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
const invalidSignal = syscall.Signal(999)
|
||||
|
||||
cmd := exec.Command("sleep", "999")
|
||||
err := cmd.Start()
|
||||
assert.NoError(err)
|
||||
|
||||
pid := cmd.Process.Pid
|
||||
|
||||
logger := logrus.WithField("foo", "bar")
|
||||
|
||||
err = WaitLocalProcess(pid, waitLocalProcessTimeoutSecs, invalidSignal, logger)
|
||||
assert.Error(err)
|
||||
|
||||
err = syscall.Kill(pid, syscall.SIGTERM)
|
||||
assert.NoError(err)
|
||||
|
||||
err = cmd.Wait()
|
||||
|
||||
// This will error because we killed the process without the knowledge
|
||||
// of exec.Command.
|
||||
assert.Error(err)
|
||||
}
|
||||
|
||||
func TestWaitLocalProcessInvalidPid(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
invalidPids := []int{-999, -173, -3, -2, -1, 0}
|
||||
|
||||
logger := logrus.WithField("foo", "bar")
|
||||
|
||||
for i, pid := range invalidPids {
|
||||
msg := fmt.Sprintf("test[%d]: %v", i, pid)
|
||||
|
||||
err := WaitLocalProcess(pid, waitLocalProcessTimeoutSecs, syscall.Signal(0), logger)
|
||||
assert.Error(err, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWaitLocalProcessBrief(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cmd := exec.Command("true")
|
||||
err := cmd.Start()
|
||||
assert.NoError(err)
|
||||
|
||||
pid := cmd.Process.Pid
|
||||
|
||||
logger := logrus.WithField("foo", "bar")
|
||||
|
||||
err = WaitLocalProcess(pid, waitLocalProcessTimeoutSecs, syscall.SIGKILL, logger)
|
||||
assert.NoError(err)
|
||||
|
||||
_ = cmd.Wait()
|
||||
}
|
||||
|
||||
func TestWaitLocalProcessLongRunningPreKill(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cmd := exec.Command("sleep", "999")
|
||||
err := cmd.Start()
|
||||
assert.NoError(err)
|
||||
|
||||
pid := cmd.Process.Pid
|
||||
|
||||
logger := logrus.WithField("foo", "bar")
|
||||
|
||||
err = WaitLocalProcess(pid, waitLocalProcessTimeoutSecs, syscall.SIGKILL, logger)
|
||||
assert.NoError(err)
|
||||
|
||||
_ = cmd.Wait()
|
||||
}
|
||||
|
||||
func TestWaitLocalProcessLongRunning(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cmd := exec.Command("sleep", "999")
|
||||
err := cmd.Start()
|
||||
assert.NoError(err)
|
||||
|
||||
pid := cmd.Process.Pid
|
||||
|
||||
logger := logrus.WithField("foo", "bar")
|
||||
|
||||
// Don't wait for long as the process isn't actually trying to stop,
|
||||
// so it will have to timeout and then be killed.
|
||||
const timeoutSecs = 1
|
||||
|
||||
err = WaitLocalProcess(pid, timeoutSecs, syscall.Signal(0), logger)
|
||||
assert.NoError(err)
|
||||
|
||||
_ = cmd.Wait()
|
||||
}
|
||||
|
Reference in New Issue
Block a user