1
1
package agentscripts_test
2
2
3
3
import (
4
- "cmp"
5
4
"context"
6
5
"path/filepath"
7
6
"runtime"
8
- "slices"
9
7
"testing"
10
8
"time"
11
9
@@ -20,7 +18,6 @@ import (
20
18
"github.com/coder/coder/v2/agent/agentscripts"
21
19
"github.com/coder/coder/v2/agent/agentssh"
22
20
"github.com/coder/coder/v2/agent/agenttest"
23
- "github.com/coder/coder/v2/agent/proto"
24
21
"github.com/coder/coder/v2/codersdk"
25
22
"github.com/coder/coder/v2/codersdk/agentsdk"
26
23
"github.com/coder/coder/v2/testutil"
@@ -121,66 +118,30 @@ func TestTimeout(t *testing.T) {
121
118
122
119
func TestScriptReportsTiming (t * testing.T ) {
123
120
t .Parallel ()
124
-
125
- type test struct {
126
- displayName string
127
- script string
128
- exitCode int32
129
- }
130
-
131
- tests := []test {
132
- {
133
- displayName : "exit-0" ,
134
- script : "exit 0" ,
135
- exitCode : 0 ,
136
- },
137
- {
138
- displayName : "say-hello" ,
139
- script : "echo 'Hello, World!'" ,
140
- exitCode : 0 ,
141
- },
142
- }
143
-
144
121
ctx := testutil .Context (t , testutil .WaitShort )
145
122
fLogger := newFakeScriptLogger ()
146
123
runner := setup (t , func (uuid2 uuid.UUID ) agentscripts.ScriptLogger {
147
124
return fLogger
148
125
})
149
126
defer runner .Close ()
150
127
aAPI := agenttest .NewFakeAgentAPI (t , slogtest .Make (t , nil ), nil , nil )
151
-
152
- scripts := []codersdk.WorkspaceAgentScript {}
153
- for _ , tt := range tests {
154
- scripts = append (scripts , codersdk.WorkspaceAgentScript {
155
- DisplayName : tt .displayName ,
156
- LogSourceID : uuid .New (),
157
- Script : tt .script ,
158
- })
159
- }
160
-
161
- err := runner .Init (scripts , aAPI .ScriptCompleted )
128
+ err := runner .Init ([]codersdk.WorkspaceAgentScript {{
129
+ DisplayName : "say-hello" ,
130
+ LogSourceID : uuid .New (),
131
+ Script : "echo hello" ,
132
+ }}, aAPI .ScriptCompleted )
162
133
require .NoError (t , err )
163
134
require .NoError (t , runner .Execute (context .Background (), func (script codersdk.WorkspaceAgentScript ) bool {
164
135
return true
165
136
}))
166
- _ = testutil .RequireRecvCtx (ctx , t , fLogger .logs )
167
-
168
- timing := aAPI .GetTiming ()
169
- require .Equal (t , len (timing ), len (tests ))
170
-
171
- // Sort the tests and timing by display name to ensure
172
- // consistent order.
173
- slices .SortFunc (timing , func (a , b * proto.Timing ) int {
174
- return cmp .Compare (a .DisplayName , b .DisplayName )
175
- })
176
- slices .SortFunc (tests , func (a , b test ) int {
177
- return cmp .Compare (a .displayName , b .displayName )
178
- })
179
-
180
- for i , tt := range tests {
181
- require .Equal (t , timing [i ].DisplayName , tt .displayName )
182
- require .Equal (t , timing [i ].ExitCode , tt .exitCode )
183
- }
137
+ log := testutil .RequireRecvCtx (ctx , t , fLogger .logs )
138
+ require .Equal (t , "hello" , log .Output )
139
+ timings := aAPI .GetTimings ()
140
+ require .Equal (t , len (timings ), 1 )
141
+ timing := timings [0 ]
142
+ require .Equal (t , timing .DisplayName , "say-hello" )
143
+ require .Equal (t , timing .ExitCode , int32 (0 ))
144
+ require .GreaterOrEqual (t , timing .End .AsTime (), timing .Start .AsTime ())
184
145
}
185
146
186
147
// TestCronClose exists because cron.Run() can happen after cron.Close().
0 commit comments