Skip to content

Commit

Permalink
Refactor Nomad Command Generation.
Browse files Browse the repository at this point in the history
- Abstracting from the exec form while generating.
- Removal of single quotes (usage of only double-quotes).
- Bash-nesting using escaping of special characters.
  • Loading branch information
mpass99 committed Mar 6, 2023
1 parent d93c4af commit 996a21b
Show file tree
Hide file tree
Showing 12 changed files with 1,137 additions and 144 deletions.
16 changes: 8 additions & 8 deletions internal/api/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func TestWebSocketProxyStopsReadingTheWebSocketAfterClosingIt(t *testing.T) {

r.StoreExecution(executionID, &executionRequestHead)
mockAPIExecute(apiMock, &executionRequestHead,
func(_ string, ctx context.Context, _ []string, _ bool, _ io.Reader, _, _ io.Writer) (int, error) {
func(_ string, ctx context.Context, _ string, _ bool, _ io.Reader, _, _ io.Writer) (int, error) {
return 0, nil
})
connection, _, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
Expand Down Expand Up @@ -376,7 +376,7 @@ var executionRequestLs = dto.ExecutionRequest{Command: "ls"}
// 'ls existing-file non-existing-file' was executed.
func mockAPIExecuteLs(api *nomad.ExecutorAPIMock) {
mockAPIExecute(api, &executionRequestLs,
func(_ string, _ context.Context, _ []string, _ bool, _ io.Reader, stdout, stderr io.Writer) (int, error) {
func(_ string, _ context.Context, _ string, _ bool, _ io.Reader, stdout, stderr io.Writer) (int, error) {
_, _ = stdout.Write([]byte("existing-file\n"))
_, _ = stderr.Write([]byte("ls: cannot access 'non-existing-file': No such file or directory\n"))
return 0, nil
Expand All @@ -388,7 +388,7 @@ var executionRequestHead = dto.ExecutionRequest{Command: "head -n 1"}
// mockAPIExecuteHead mocks the ExecuteCommand of an ExecutorApi to act as if 'head -n 1' was executed.
func mockAPIExecuteHead(api *nomad.ExecutorAPIMock) {
mockAPIExecute(api, &executionRequestHead,
func(_ string, _ context.Context, _ []string, _ bool,
func(_ string, _ context.Context, _ string, _ bool,
stdin io.Reader, stdout io.Writer, stderr io.Writer,
) (int, error) {
scanner := bufio.NewScanner(stdin)
Expand All @@ -408,7 +408,7 @@ func mockAPIExecuteSleep(api *nomad.ExecutorAPIMock) <-chan bool {
canceled := make(chan bool, 1)

mockAPIExecute(api, &executionRequestSleep,
func(_ string, ctx context.Context, _ []string, _ bool,
func(_ string, ctx context.Context, _ string, _ bool,
stdin io.Reader, stdout io.Writer, stderr io.Writer,
) (int, error) {
var err error
Expand All @@ -429,7 +429,7 @@ var executionRequestError = dto.ExecutionRequest{Command: "error"}
// mockAPIExecuteError mocks the ExecuteCommand method of an ExecutorApi to return an error.
func mockAPIExecuteError(api *nomad.ExecutorAPIMock) {
mockAPIExecute(api, &executionRequestError,
func(_ string, _ context.Context, _ []string, _ bool, _ io.Reader, _, _ io.Writer) (int, error) {
func(_ string, _ context.Context, _ string, _ bool, _ io.Reader, _, _ io.Writer) (int, error) {
return 0, tests.ErrDefault
})
}
Expand All @@ -439,15 +439,15 @@ var executionRequestExitNonZero = dto.ExecutionRequest{Command: "exit 42"}
// mockAPIExecuteExitNonZero mocks the ExecuteCommand method of an ExecutorApi to exit with exit status 42.
func mockAPIExecuteExitNonZero(api *nomad.ExecutorAPIMock) {
mockAPIExecute(api, &executionRequestExitNonZero,
func(_ string, _ context.Context, _ []string, _ bool, _ io.Reader, _, _ io.Writer) (int, error) {
func(_ string, _ context.Context, _ string, _ bool, _ io.Reader, _, _ io.Writer) (int, error) {
return 42, nil
})
}

// mockAPIExecute mocks the ExecuteCommand method of an ExecutorApi to call the given method run when the command
// corresponding to the given ExecutionRequest is called.
func mockAPIExecute(api *nomad.ExecutorAPIMock, request *dto.ExecutionRequest,
run func(runnerId string, ctx context.Context, command []string, tty bool,
run func(runnerId string, ctx context.Context, command string, tty bool,
stdin io.Reader, stdout, stderr io.Writer) (int, error)) {
call := api.On("ExecuteCommand",
mock.AnythingOfType("string"),
Expand All @@ -461,7 +461,7 @@ func mockAPIExecute(api *nomad.ExecutorAPIMock, request *dto.ExecutionRequest,
call.Run(func(args mock.Arguments) {
exit, err := run(args.Get(0).(string),
args.Get(1).(context.Context),
args.Get(2).([]string),
args.Get(2).(string),
args.Get(3).(bool),
args.Get(5).(io.Reader),
args.Get(6).(io.Writer),
Expand Down
10 changes: 5 additions & 5 deletions internal/nomad/api_querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type apiQuerier interface {
DeleteJob(jobID string) (err error)

// Execute runs a command in the passed job.
Execute(jobID string, ctx context.Context, command []string, tty bool,
Execute(jobID string, ctx context.Context, command string, tty bool,
stdin io.Reader, stdout, stderr io.Writer) (int, error)

// listJobs loads all jobs with the specified prefix.
Expand Down Expand Up @@ -87,11 +87,10 @@ func (nc *nomadAPIClient) DeleteJob(jobID string) (err error) {
}

func (nc *nomadAPIClient) Execute(runnerID string,
ctx context.Context, command []string, tty bool,
ctx context.Context, cmd string, tty bool,
stdin io.Reader, stdout, stderr io.Writer,
) (int, error) {
log.WithField("command", strings.ReplaceAll(strings.Join(command, ", "), "\n", "")).
Trace("Requesting Nomad Exec")
log.WithField("command", strings.ReplaceAll(cmd, "\n", "")).Trace("Requesting Nomad Exec")
var allocations []*nomadApi.AllocationListStub
var err error
logging.StartSpan("nomad.execute.list", "List Allocations for id", ctx, func(_ context.Context) {
Expand All @@ -115,8 +114,9 @@ func (nc *nomadAPIClient) Execute(runnerID string,
var exitCode int
logging.StartSpan("nomad.execute.exec", "Execute Command in Allocation", ctx, func(ctx context.Context) {
debugWriter := NewSentryDebugWriter(stdout, ctx)
commands := []string{"/bin/bash", "-c", cmd}
exitCode, err = nc.client.Allocations().
Exec(ctx, allocation, TaskName, tty, command, stdin, debugWriter, stderr, nil, nil)
Exec(ctx, allocation, TaskName, tty, commands, stdin, debugWriter, stderr, nil, nil)
debugWriter.Close(exitCode)
})
switch {
Expand Down
Loading

0 comments on commit 996a21b

Please sign in to comment.