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 1 commit
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
5 changes: 4 additions & 1 deletion pkg/utils/commandexecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func (e Executor) ExecContext(ctx context.Context, name string, arg ...string) (
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 ctx.Err()
balanza marked this conversation as resolved.
Show resolved Hide resolved
}
return cmd.Output()
}
89 changes: 89 additions & 0 deletions pkg/utils/commandexecutor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package utils_test

import (
"context"
"testing"
"time"

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

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")
if err != nil {
balanza marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("Error executing command: %v", err)
}

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

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

_, err := executor.Exec("nonexistentcommand")
if err == nil {
balanza marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("Expected error executing command")
}
}

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

ctx := context.Background()

result, err := executor.ExecContext(ctx, "echo", "trento is not trieste")
if err != nil {
t.Errorf("Error executing command: %v", 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")
if err == nil {
t.Errorf("Expected error executing command")
}
}

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

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

cancel()

_, err := executor.ExecContext(ctx, "echo", "trento is not trieste")
if err == nil {
balanza marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("Expected error executing command")
}
}

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

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

go func() {
cancel()
}()

_, err := executor.ExecContext(ctx, "sleep", "3s")
balanza marked this conversation as resolved.
Show resolved Hide resolved

switch {
case timeoutCtx.Err() != nil:
t.Errorf("Expected process to be killed before timeout")
case err == nil:
t.Errorf("Expected error when cancelling")
}

}
Loading