Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitffc2981

Browse files
committed
add agentscripts test for execute option
1 parent9aca1c8 commitffc2981

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

‎agent/agentscripts/agentscripts_test.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"path/filepath"
66
"runtime"
7+
"slices"
8+
"sync"
79
"testing"
810
"time"
911

@@ -151,6 +153,155 @@ func TestCronClose(t *testing.T) {
151153
require.NoError(t,runner.Close(),"close runner")
152154
}
153155

156+
funcTestExecuteOptions(t*testing.T) {
157+
t.Parallel()
158+
159+
startScript:= codersdk.WorkspaceAgentScript{
160+
ID:uuid.New(),
161+
LogSourceID:uuid.New(),
162+
Script:"echo start",
163+
RunOnStart:true,
164+
}
165+
stopScript:= codersdk.WorkspaceAgentScript{
166+
ID:uuid.New(),
167+
LogSourceID:uuid.New(),
168+
Script:"echo stop",
169+
RunOnStop:true,
170+
}
171+
postStartScript:= codersdk.WorkspaceAgentScript{
172+
ID:uuid.New(),
173+
LogSourceID:uuid.New(),
174+
Script:"echo poststart",
175+
}
176+
regularScript:= codersdk.WorkspaceAgentScript{
177+
ID:uuid.New(),
178+
LogSourceID:uuid.New(),
179+
Script:"echo regular",
180+
}
181+
182+
scripts:= []codersdk.WorkspaceAgentScript{
183+
startScript,
184+
stopScript,
185+
regularScript,
186+
}
187+
allScripts:=append(slices.Clone(scripts),postStartScript)
188+
189+
scriptByID:=func(t*testing.T,id uuid.UUID) codersdk.WorkspaceAgentScript {
190+
for_,script:=rangeallScripts {
191+
ifscript.ID==id {
192+
returnscript
193+
}
194+
}
195+
t.Fatal("script not found")
196+
return codersdk.WorkspaceAgentScript{}
197+
}
198+
199+
wantOutput:=map[uuid.UUID]string{
200+
startScript.ID:"start",
201+
stopScript.ID:"stop",
202+
postStartScript.ID:"poststart",
203+
regularScript.ID:"regular",
204+
}
205+
206+
testCases:= []struct {
207+
namestring
208+
option agentscripts.ExecuteOption
209+
wantRun []uuid.UUID
210+
}{
211+
{
212+
name:"ExecuteAllScripts",
213+
option:agentscripts.ExecuteAllScripts,
214+
wantRun: []uuid.UUID{startScript.ID,stopScript.ID,regularScript.ID,postStartScript.ID},
215+
},
216+
{
217+
name:"ExecuteStartScripts",
218+
option:agentscripts.ExecuteStartScripts,
219+
wantRun: []uuid.UUID{startScript.ID},
220+
},
221+
{
222+
name:"ExecutePostStartScripts",
223+
option:agentscripts.ExecutePostStartScripts,
224+
wantRun: []uuid.UUID{postStartScript.ID},
225+
},
226+
{
227+
name:"ExecuteStopScripts",
228+
option:agentscripts.ExecuteStopScripts,
229+
wantRun: []uuid.UUID{stopScript.ID},
230+
},
231+
}
232+
233+
for_,tc:=rangetestCases {
234+
t.Run(tc.name,func(t*testing.T) {
235+
t.Parallel()
236+
237+
ctx:=testutil.Context(t,testutil.WaitMedium)
238+
executedScripts:=make(map[uuid.UUID]bool)
239+
fLogger:=&filterTestLogger{
240+
tb:t,
241+
executedScripts:executedScripts,
242+
wantOutput:wantOutput,
243+
}
244+
245+
runner:=setup(t,func(_ uuid.UUID) agentscripts.ScriptLogger {
246+
returnfLogger
247+
})
248+
deferrunner.Close()
249+
250+
aAPI:=agenttest.NewFakeAgentAPI(t,testutil.Logger(t),nil,nil)
251+
err:=runner.Init(
252+
scripts,
253+
aAPI.ScriptCompleted,
254+
agentscripts.WithPostStartScripts(postStartScript),
255+
)
256+
require.NoError(t,err)
257+
258+
err=runner.Execute(ctx,tc.option)
259+
require.NoError(t,err)
260+
261+
gotRun:=map[uuid.UUID]bool{}
262+
for_,id:=rangetc.wantRun {
263+
gotRun[id]=true
264+
require.True(t,executedScripts[id],
265+
"script %s should have run when using filter %s",scriptByID(t,id).Script,tc.name)
266+
}
267+
268+
for_,script:=rangeallScripts {
269+
if_,ok:=gotRun[script.ID];ok {
270+
continue
271+
}
272+
require.False(t,executedScripts[script.ID],
273+
"script %s should not have run when using filter %s",script.Script,tc.name)
274+
}
275+
})
276+
}
277+
}
278+
279+
typefilterTestLoggerstruct {
280+
tb testing.TB
281+
executedScriptsmap[uuid.UUID]bool
282+
wantOutputmap[uuid.UUID]string
283+
mu sync.Mutex
284+
}
285+
286+
func (l*filterTestLogger)Send(ctx context.Context,logs...agentsdk.Log)error {
287+
l.mu.Lock()
288+
deferl.mu.Unlock()
289+
for_,log:=rangelogs {
290+
l.tb.Log(log.Output)
291+
forid,output:=rangel.wantOutput {
292+
iflog.Output==output {
293+
l.executedScripts[id]=true
294+
break
295+
}
296+
}
297+
}
298+
returnnil
299+
}
300+
301+
func (l*filterTestLogger)Flush(context.Context)error {
302+
returnnil
303+
}
304+
154305
funcsetup(t*testing.T,getScriptLoggerfunc(logSourceID uuid.UUID) agentscripts.ScriptLogger)*agentscripts.Runner {
155306
t.Helper()
156307
ifgetScriptLogger==nil {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp