diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2024-01-19 18:10:53 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2024-01-19 18:10:53 +0300 |
commit | 8f3a9dbb94b5a9ae4570a22bbc2a75e7572407c8 (patch) | |
tree | 0d7e5d6d5747b57a93df1181bd86a7a127c16934 /workhorse/internal/helper/command/command_test.go | |
parent | 7344cec8c24f1599086498ba19096cf9918ee168 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'workhorse/internal/helper/command/command_test.go')
-rw-r--r-- | workhorse/internal/helper/command/command_test.go | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/workhorse/internal/helper/command/command_test.go b/workhorse/internal/helper/command/command_test.go new file mode 100644 index 00000000000..2f25f09373c --- /dev/null +++ b/workhorse/internal/helper/command/command_test.go @@ -0,0 +1,97 @@ +package command + +import ( + "errors" + "os/exec" + "testing" + + "github.com/stretchr/testify/require" +) + +type ErrorWithExitCode struct { + exitCode int +} + +func (e ErrorWithExitCode) Error() string { + return "Error that responds to ExitCode()" +} + +func (e ErrorWithExitCode) ExitCode() int { + return e.exitCode +} + +func TestExitStatus(t *testing.T) { + tests := []struct { + name string + err error + exitCode int + ok bool + }{ + { + name: "error responds to ExitCode()", + err: ErrorWithExitCode{exitCode: 0}, + exitCode: 0, + ok: true, + }, + { + name: "error is not nil", + err: errors.New("some generic error"), + exitCode: -1, + ok: false, + }, + { + name: "else", + err: nil, + exitCode: 0, + ok: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + exitCode, ok := ExitStatus(tt.err) + + require.Equal(t, tt.exitCode, exitCode) + require.Equal(t, tt.ok, ok) + }) + } +} + +func TestKillProcessGroup(t *testing.T) { + tests := []struct { + name string + cmd *exec.Cmd + start bool + err error + }{ + { + name: "command is nil", + cmd: nil, + start: false, + err: nil, + }, + { + name: "command not started", + cmd: exec.Command("sleep"), + start: false, + err: errors.New(""), + }, + { + name: "command started", + cmd: exec.Command("sleep"), + start: true, + err: &exec.ExitError{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.start == true { + tt.cmd.Start() + } + + err := KillProcessGroup(tt.cmd) + require.IsType(t, tt.err, err) + }) + } +} |