Skip to content

test(cli/ssh): fix ssh start conflict test by faking API response #16082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions cli/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,25 @@ func TestSSH(t *testing.T) {
// a start build of the workspace.
isFirstBuild := true
buildURL := regexp.MustCompile("/api/v2/workspaces/.*/builds")
buildReq := make(chan struct{})
buildResume := make(chan struct{})
buildPause := make(chan bool)
buildDone := make(chan struct{})
buildSyncMW := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost && buildURL.MatchString(r.URL.Path) {
if !isFirstBuild {
t.Log("buildSyncMW: blocking build")
buildReq <- struct{}{}
<-buildResume
t.Log("buildSyncMW: pausing build")
if shouldContinue := <-buildPause; !shouldContinue {
// We can't force the API to trigger a build conflict (racy) so we fake it.
t.Log("buildSyncMW: return conflict")
w.WriteHeader(http.StatusConflict)
return
}
t.Log("buildSyncMW: resuming build")
defer func() {
t.Log("buildSyncMW: sending build done")
buildDone <- struct{}{}
t.Log("buildSyncMW: done")
}()
} else {
isFirstBuild = false
}
Expand Down Expand Up @@ -211,10 +220,15 @@ func TestSSH(t *testing.T) {
for _, pty := range ptys {
pty.ExpectMatchContext(ctx, "Workspace was stopped, starting workspace to allow connecting to")
}
for range ptys {
testutil.RequireRecvCtx(ctx, t, buildReq)

// Allow one build to complete.
testutil.RequireSendCtx(ctx, t, buildPause, true)
testutil.RequireRecvCtx(ctx, t, buildDone)

// Allow the remaining builds to continue.
for i := 0; i < len(ptys)-1; i++ {
testutil.RequireSendCtx(ctx, t, buildPause, false)
}
close(buildResume)

var foundConflict int
for _, pty := range ptys {
Expand Down
Loading