@@ -4,16 +4,21 @@ import (
4
4
"context"
5
5
"net/http"
6
6
"net/http/httptest"
7
+ "strings"
7
8
"sync"
8
9
"sync/atomic"
9
10
"testing"
10
11
"time"
11
12
13
+ "github.com/google/uuid"
14
+ "github.com/stretchr/testify/assert"
12
15
"github.com/stretchr/testify/require"
13
16
14
17
"cdr.dev/slog"
15
18
"cdr.dev/slog/sloggers/slogtest"
16
19
"github.com/coder/coder/v2/agent"
20
+ "github.com/coder/coder/v2/agent/agenttest"
21
+ "github.com/coder/coder/v2/agent/proto"
17
22
"github.com/coder/coder/v2/coderd/httpapi"
18
23
"github.com/coder/coder/v2/codersdk"
19
24
"github.com/coder/coder/v2/codersdk/agentsdk"
@@ -40,12 +45,23 @@ func TestAppHealth_Healthy(t *testing.T) {
40
45
},
41
46
Health : codersdk .WorkspaceAppHealthInitializing ,
42
47
},
48
+ {
49
+ Slug : "app3" ,
50
+ Healthcheck : codersdk.Healthcheck {
51
+ Interval : 2 ,
52
+ Threshold : 1 ,
53
+ },
54
+ Health : codersdk .WorkspaceAppHealthInitializing ,
55
+ },
43
56
}
44
57
handlers := []http.Handler {
45
58
nil ,
46
59
http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
47
60
httpapi .Write (r .Context (), w , http .StatusOK , nil )
48
61
}),
62
+ http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
63
+ httpapi .Write (r .Context (), w , http .StatusOK , nil )
64
+ }),
49
65
}
50
66
getApps , closeFn := setupAppReporter (ctx , t , apps , handlers )
51
67
defer closeFn ()
@@ -58,7 +74,7 @@ func TestAppHealth_Healthy(t *testing.T) {
58
74
return false
59
75
}
60
76
61
- return apps [1 ].Health == codersdk .WorkspaceAppHealthHealthy
77
+ return apps [1 ].Health == codersdk .WorkspaceAppHealthHealthy && apps [ 2 ]. Health == codersdk . WorkspaceAppHealthHealthy
62
78
}, testutil .WaitLong , testutil .IntervalSlow )
63
79
}
64
80
@@ -163,6 +179,12 @@ func TestAppHealth_NotSpamming(t *testing.T) {
163
179
164
180
func setupAppReporter (ctx context.Context , t * testing.T , apps []codersdk.WorkspaceApp , handlers []http.Handler ) (agent.WorkspaceAgentApps , func ()) {
165
181
closers := []func (){}
182
+ for i , app := range apps {
183
+ if app .ID == uuid .Nil {
184
+ app .ID = uuid .New ()
185
+ apps [i ] = app
186
+ }
187
+ }
166
188
for i , handler := range handlers {
167
189
if handler == nil {
168
190
continue
@@ -181,23 +203,43 @@ func setupAppReporter(ctx context.Context, t *testing.T, apps []codersdk.Workspa
181
203
var newApps []codersdk.WorkspaceApp
182
204
return append (newApps , apps ... ), nil
183
205
}
184
- postWorkspaceAgentAppHealth := func (_ context.Context , req agentsdk.PostAppHealthsRequest ) error {
185
- mu .Lock ()
186
- for id , health := range req .Healths {
187
- for i , app := range apps {
188
- if app .ID != id {
189
- continue
206
+
207
+ // We don't care about manifest or stats in this test since it's not using
208
+ // a full agent and these RPCs won't get called.
209
+ //
210
+ // We use a proper fake agent API so we can test the conversion code and the
211
+ // request code as well. Before we were bypassing these by using a custom
212
+ // post function.
213
+ fakeAAPI := agenttest .NewFakeAgentAPI (t , slogtest .Make (t , nil ), nil , nil )
214
+
215
+ // Process events from the channel and update the health of the apps.
216
+ go func () {
217
+ appHealthCh := fakeAAPI .AppHealthCh ()
218
+ for {
219
+ select {
220
+ case <- ctx .Done ():
221
+ return
222
+ case req := <- appHealthCh :
223
+ mu .Lock ()
224
+ for _ , update := range req .Updates {
225
+ updateID , err := uuid .FromBytes (update .Id )
226
+ assert .NoError (t , err )
227
+ updateHealth := codersdk .WorkspaceAppHealth (strings .ToLower (proto .AppHealth_name [int32 (update .Health )]))
228
+
229
+ for i , app := range apps {
230
+ if app .ID != updateID {
231
+ continue
232
+ }
233
+ app .Health = updateHealth
234
+ apps [i ] = app
235
+ }
190
236
}
191
- app .Health = health
192
- apps [i ] = app
237
+ mu .Unlock ()
193
238
}
194
239
}
195
- mu .Unlock ()
196
-
197
- return nil
198
- }
240
+ }()
199
241
200
- go agent .NewWorkspaceAppHealthReporter (slogtest .Make (t , nil ).Leveled (slog .LevelDebug ), apps , postWorkspaceAgentAppHealth )(ctx )
242
+ go agent .NewWorkspaceAppHealthReporter (slogtest .Make (t , nil ).Leveled (slog .LevelDebug ), apps , agentsdk . AppHealthPoster ( fakeAAPI ) )(ctx )
201
243
202
244
return workspaceAgentApps , func () {
203
245
for _ , closeFn := range closers {
0 commit comments