Skip to content

fix(cli/clitest): race between Start/StartWithWaiter cleanup order #7232

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 2 commits into from
Apr 20, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
fixup! fix(cli/clitest): race between Start/StartWithWaiter clean…
…up order
  • Loading branch information
coadler committed Apr 20, 2023
commit 2612baeed97f07b6eab8ec3e49db8e91d1c01920
23 changes: 21 additions & 2 deletions cli/templatecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package cli_test

import (
"bytes"
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
Expand All @@ -13,6 +15,7 @@ import (
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)

var provisionCompleteWithAgent = []*proto.Provision_Response{
Expand Down Expand Up @@ -290,11 +293,23 @@ func TestTemplateCreate(t *testing.T) {
removeTmpDirUntilSuccessAfterTest(t, tempDir)
variablesFile, _ := os.CreateTemp(tempDir, "variables*.yaml")
_, _ = variablesFile.WriteString(`second_variable: foobar`)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()

inv, root := clitest.New(t, "templates", "create", "my-template", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--variables-file", variablesFile.Name())
clitest.SetupConfig(t, client, root)
inv = inv.WithContext(ctx)
pty := ptytest.New(t).Attach(inv)

clitest.Start(t, inv)
// We expect the cli to return an error, so we have to handle it
// ourselves.
go func() {
cancel()
err := inv.Run()
assert.Error(t, err)
}()

matches := []struct {
match string
write string
Expand All @@ -307,6 +322,8 @@ func TestTemplateCreate(t *testing.T) {
pty.WriteLine(m.write)
}
}

<-ctx.Done()
})

t.Run("WithVariablesFileWithTheRequiredValue", func(t *testing.T) {
Expand Down Expand Up @@ -390,7 +407,9 @@ func TestTemplateCreate(t *testing.T) {
}
for _, m := range matches {
pty.ExpectMatch(m.match)
pty.WriteLine(m.write)
if len(m.write) > 0 {
pty.WriteLine(m.write)
}
}
})
}
Expand Down