Skip to content
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
12 changes: 11 additions & 1 deletion .github/workflows/worker-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ on:

jobs:
worker_tests:
runs-on: ubuntu-latest
strategy:
matrix:
os: [Linux, Windows]
include:
- os: Linux
pool: ubuntu-latest
- os: Windows
pool: windows-latest
name: Run tests on ${{ matrix.os }}
runs-on: ${{ matrix.pool }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4

Expand All @@ -17,6 +26,7 @@ jobs:
go-version: "1.23.6"

- name: Build worker Docker image
if: ${{ matrix.os != 'Windows' }}
run: |
docker compose build

Expand Down
20 changes: 16 additions & 4 deletions tests/helper/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,30 @@ import (
)

var (
HostsFilePath string
NoFileErrorMessage string
HostsFilePath string
NoFileErrMessage string
SignalKilledErrMessage string
)

func init() {
switch runtime.GOOS {
case "windows":
HostsFilePath = "$env:windir/System32/drivers/etc/hosts"
NoFileErrorMessage = "Cannot find path (\\r\\n)*'.+%s' because (\\r\\n)*it (\\r\\n)*does (\\r\\n)*not (\\r\\n)*exist."
NoFileErrMessage = "Cannot find path (\\r\\n)*'.+%s' because (\\r\\n)*it (\\r\\n)*does (\\r\\n)*not (\\r\\n)*exist."
SignalKilledErrMessage = "exit status 1"
case "linux":
HostsFilePath = "/etc/hosts"
NoFileErrorMessage = "%s: No such file or directory"
NoFileErrMessage = "%s: No such file or directory"
SignalKilledErrMessage = "signal: killed"
}
}

func PingTask(name, host string) task.Task {
return task.Task{
ID: uuid.New(),
Name: name,
Executable: "ping",
Args: []string{host, "-n", "20"},
}
}

Expand Down
43 changes: 43 additions & 0 deletions tests/worker/stop_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -53,3 +55,44 @@ func TestStopTask__ShouldStopCompletedTask(t *testing.T) {
assert.Empty(t, responseRecorder.Body, "Response body should be empty")
assert.Empty(t, api.Worker.Tasks, "Tasks map should be empty")
}

func TestStopTask__ShouldStopRunningTask(t *testing.T) {
api := &worker.Api{
Worker: &worker.Worker{
Tasks: make(map[uuid.UUID]*task.Task),
},
}
testTask := helper.PingTask("ping-task", "127.0.0.1")

// 1 - Create a task
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()

createRequest := helper.NewTaskPostRequest(testTask)
createResponseRecorder := httptest.NewRecorder()

api.HandleCreateTask(createResponseRecorder, createRequest)

assert.Equal(t, http.StatusInternalServerError, createResponseRecorder.Code, "Response status code should be 500 Internal Server Error")
assert.Equal(t, fmt.Sprintf("Error when executing the task: %s", helper.SignalKilledErrMessage), createResponseRecorder.Body.String(), "Response body should contain error message")
assert.Empty(t, api.Worker.Tasks, "Tasks map should be empty")
}()

time.Sleep(1 * time.Second)

assert.Equal(t, 1, len(api.Worker.Tasks), "Tasks map should contain 1 task")

// 2 - Delete a task
deleteRequest := helper.NewTaskDeleteRequest(testTask.ID)
deleteResponseRecorder := httptest.NewRecorder()

api.HandleDeleteTask(deleteResponseRecorder, deleteRequest)

assert.Equal(t, http.StatusNoContent, deleteResponseRecorder.Code, "Response status code should be 204 No Content")
assert.Empty(t, deleteResponseRecorder.Body, "Response body should be empty")
assert.Empty(t, api.Worker.Tasks, "Tasks map should be empty")

wg.Wait()
}
2 changes: 1 addition & 1 deletion tests/worker/task_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestTaskLogs__PrintFile(t *testing.T) {
path: "non-existing-file.txt",
responseStatus: http.StatusInternalServerError,
responseBody: "Error when executing the task: exit status 1",
stdoutRegexp: fmt.Sprintf(helper.NoFileErrorMessage, "non-existing-file.txt"),
stdoutRegexp: fmt.Sprintf(helper.NoFileErrMessage, "non-existing-file.txt"),
},
}

Expand Down
3 changes: 1 addition & 2 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ func (w *Worker) GetTask(id uuid.UUID) *task.Task {

func (w *Worker) StartTask(t task.Task) error {
t.Cmd = exec.Command(t.Executable, t.Args...)
w.Tasks[t.ID] = &t

stdout, err := t.Cmd.CombinedOutput()
os.Stdout.Write(stdout)
if err != nil {
return err
}

w.Tasks[t.ID] = &t

return nil
}

Expand Down
Loading