-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_events_test.go
More file actions
130 lines (110 loc) · 3.49 KB
/
Copy pathhttp_events_test.go
File metadata and controls
130 lines (110 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package tasks
import (
"context"
"io"
"log/slog"
"strings"
"testing"
"time"
"github.com/noderax/noderax-agent/internal/api"
)
type lifecycleClientStub struct {
acceptedReqs []api.TaskAcceptedRequest
startedReqs []api.TaskStartedRequest
logReqs []api.TaskLogRequest
completedReqs []api.TaskCompletedRequest
acceptedErrors []error
startedErrors []error
logErrors []error
completedErrors []error
}
func (s *lifecycleClientStub) ReportTaskAccepted(ctx context.Context, req api.TaskAcceptedRequest) error {
s.acceptedReqs = append(s.acceptedReqs, req)
return popLifecycleError(&s.acceptedErrors)
}
func (s *lifecycleClientStub) ReportTaskStarted(ctx context.Context, req api.TaskStartedRequest) error {
s.startedReqs = append(s.startedReqs, req)
return popLifecycleError(&s.startedErrors)
}
func (s *lifecycleClientStub) ReportTaskLog(ctx context.Context, req api.TaskLogRequest) error {
s.logReqs = append(s.logReqs, req)
return popLifecycleError(&s.logErrors)
}
func (s *lifecycleClientStub) ReportTaskCompleted(ctx context.Context, req api.TaskCompletedRequest) error {
s.completedReqs = append(s.completedReqs, req)
return popLifecycleError(&s.completedErrors)
}
func popLifecycleError(errorsSlice *[]error) error {
if len(*errorsSlice) == 0 {
return nil
}
err := (*errorsSlice)[0]
*errorsSlice = (*errorsSlice)[1:]
return err
}
func queuedConflictRequestError(path string) error {
return &api.RequestError{
Method: "post",
Path: path,
StatusCode: 409,
Message: "Task is in queued state and cannot accept lifecycle updates",
}
}
func TestHTTPTaskEventsTaskLogTruncatesLongLine(t *testing.T) {
t.Parallel()
stub := &lifecycleClientStub{}
events := NewHTTPTaskEvents(stub, slog.New(slog.NewTextHandler(io.Discard, nil)))
line := strings.Repeat("x", 5100)
err := events.TaskLog(context.Background(), "task-1", "stdout", line, time.Now().UTC())
if err != nil {
t.Fatalf("TaskLog returned error: %v", err)
}
if len(stub.logReqs) != 1 {
t.Fatalf("expected one log request, got %d", len(stub.logReqs))
}
sent := stub.logReqs[0].Line
runeCount := 0
for range sent {
runeCount++
}
if runeCount > taskLogMaxLineChars {
t.Fatalf("expected log line <= %d chars, got %d", taskLogMaxLineChars, runeCount)
}
if !strings.HasSuffix(sent, taskLogTruncatedSuffix) {
t.Fatalf("expected truncated suffix %q, got %q", taskLogTruncatedSuffix, sent)
}
}
func TestHTTPTaskEventsTaskStartedRetriesQueuedConflict(t *testing.T) {
t.Parallel()
stub := &lifecycleClientStub{
startedErrors: []error{
queuedConflictRequestError("/api/v1/agent/tasks/task-1/started"),
nil,
},
}
events := NewHTTPTaskEvents(stub, slog.New(slog.NewTextHandler(io.Discard, nil)))
err := events.TaskStarted(context.Background(), "task-1", time.Now().UTC())
if err != nil {
t.Fatalf("TaskStarted returned error: %v", err)
}
if len(stub.startedReqs) != 2 {
t.Fatalf("expected two started attempts, got %d", len(stub.startedReqs))
}
}
func TestHTTPTaskEventsTaskLogRetriesQueuedConflict(t *testing.T) {
t.Parallel()
stub := &lifecycleClientStub{
logErrors: []error{
queuedConflictRequestError("/api/v1/agent/tasks/task-1/logs"),
nil,
},
}
events := NewHTTPTaskEvents(stub, slog.New(slog.NewTextHandler(io.Discard, nil)))
err := events.TaskLog(context.Background(), "task-1", "stdout", "hello", time.Now().UTC())
if err != nil {
t.Fatalf("TaskLog returned error: %v", err)
}
if len(stub.logReqs) != 2 {
t.Fatalf("expected two log attempts, got %d", len(stub.logReqs))
}
}