Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test command termination error #378

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/utils/commandexecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ func (e Executor) ExecContext(ctx context.Context, name string, arg ...string) (
cmd := exec.CommandContext(ctx, name, arg...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Cancel = func() error {
<-ctx.Done()
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
return fmt.Errorf("error killing process group: %w", err)
if err != nil {
balanza marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("error killing process group: %w", err)
}
return nil
}
return cmd.Output()
}
81 changes: 81 additions & 0 deletions pkg/utils/commandexecutor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package utils_test

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
commandexecutor "github.com/trento-project/agent/pkg/utils"
)

type CommandExecutorTestSuite struct {
suite.Suite
}

func TestCommandExecutorTestSuite(t *testing.T) {
suite.Run(t, new(CommandExecutorTestSuite))
}

func TestExec(t *testing.T) {
balanza marked this conversation as resolved.
Show resolved Hide resolved
executor := commandexecutor.Executor{}

result, err := executor.Exec("echo", "trento is not trieste")

assert.NoError(t, err)
assert.Equal(t, "trento is not trieste\n", string(result))
}

func TestExecWithError(t *testing.T) {
executor := commandexecutor.Executor{}

_, err := executor.Exec("nonexistentcommand")

assert.Error(t, err)
}

func TestExecContext(t *testing.T) {
executor := commandexecutor.Executor{}

ctx := context.Background()

result, err := executor.ExecContext(ctx, "echo", "trento is not trieste")

assert.NoError(t, err)
assert.Equal(t, "trento is not trieste\n", string(result))
}

func TestExecContextWithError(t *testing.T) {
executor := commandexecutor.Executor{}

ctx := context.Background()

_, err := executor.ExecContext(ctx, "nonexistentcommand")

assert.Error(t, err)
}

func TestExecContextWithCancel(t *testing.T) {
executor := commandexecutor.Executor{}

ctx, cancel := context.WithCancel(context.Background())

cancel()

_, err := executor.ExecContext(ctx, "echo", "trento is not trieste")
assert.Error(t, err)
}

func TestExecContextWithCancelLongRunning(t *testing.T) {
executor := commandexecutor.Executor{}

ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
timeoutCtx, cancelTimeout := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
defer cancelTimeout()

_, err := executor.ExecContext(ctx, "sleep", "3s")
balanza marked this conversation as resolved.
Show resolved Hide resolved
assert.Error(t, err)
assert.NoError(t, timeoutCtx.Err())
}
Loading