Skip to content

Commit b22cb57

Browse files
test: rewrite test again
1 parent 14b68a1 commit b22cb57

File tree

2 files changed

+17
-56
lines changed

2 files changed

+17
-56
lines changed

agent/agentscripts/agentscripts_test.go

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package agentscripts_test
22

33
import (
4-
"cmp"
54
"context"
65
"path/filepath"
76
"runtime"
8-
"slices"
97
"testing"
108
"time"
119

@@ -20,7 +18,6 @@ import (
2018
"github.com/coder/coder/v2/agent/agentscripts"
2119
"github.com/coder/coder/v2/agent/agentssh"
2220
"github.com/coder/coder/v2/agent/agenttest"
23-
"github.com/coder/coder/v2/agent/proto"
2421
"github.com/coder/coder/v2/codersdk"
2522
"github.com/coder/coder/v2/codersdk/agentsdk"
2623
"github.com/coder/coder/v2/testutil"
@@ -121,66 +118,30 @@ func TestTimeout(t *testing.T) {
121118

122119
func TestScriptReportsTiming(t *testing.T) {
123120
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-
144121
ctx := testutil.Context(t, testutil.WaitShort)
145122
fLogger := newFakeScriptLogger()
146123
runner := setup(t, func(uuid2 uuid.UUID) agentscripts.ScriptLogger {
147124
return fLogger
148125
})
149126
defer runner.Close()
150127
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)
162133
require.NoError(t, err)
163134
require.NoError(t, runner.Execute(context.Background(), func(script codersdk.WorkspaceAgentScript) bool {
164135
return true
165136
}))
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())
184145
}
185146

186147
// TestCronClose exists because cron.Run() can happen after cron.Close().

agent/agenttest/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ type FakeAgentAPI struct {
170170
logsCh chan<- *agentproto.BatchCreateLogsRequest
171171
lifecycleStates []codersdk.WorkspaceAgentLifecycle
172172
metadata map[string]agentsdk.Metadata
173-
timing []*agentproto.Timing
173+
timings []*agentproto.Timing
174174

175175
getAnnouncementBannersFunc func() ([]codersdk.BannerConfig, error)
176176
}
@@ -183,8 +183,8 @@ func (*FakeAgentAPI) GetServiceBanner(context.Context, *agentproto.GetServiceBan
183183
return &agentproto.ServiceBanner{}, nil
184184
}
185185

186-
func (f *FakeAgentAPI) GetTiming() []*agentproto.Timing {
187-
return f.timing
186+
func (f *FakeAgentAPI) GetTimings() []*agentproto.Timing {
187+
return f.timings
188188
}
189189

190190
func (f *FakeAgentAPI) SetAnnouncementBannersFunc(fn func() ([]codersdk.BannerConfig, error)) {
@@ -308,7 +308,7 @@ func (f *FakeAgentAPI) BatchCreateLogs(ctx context.Context, req *agentproto.Batc
308308

309309
func (f *FakeAgentAPI) ScriptCompleted(_ context.Context, req *agentproto.WorkspaceAgentScriptCompletedRequest) (*agentproto.WorkspaceAgentScriptCompletedResponse, error) {
310310
f.Lock()
311-
f.timing = append(f.timing, req.Timing)
311+
f.timings = append(f.timings, req.Timing)
312312
f.Unlock()
313313

314314
return &agentproto.WorkspaceAgentScriptCompletedResponse{}, nil

0 commit comments

Comments
 (0)