Skip to content

chore(coderd/coderdtest): wait for provisioner daemons to be connected #15936

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
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 32 additions & 0 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ func NewWithAPI(t testing.TB, options *Options) (*codersdk.Client, io.Closer, *c
var provisionerCloser io.Closer = nopcloser{}
if options.IncludeProvisionerDaemon {
provisionerCloser = NewTaggedProvisionerDaemon(t, coderAPI, "test", options.ProvisionerDaemonTags)
// Wait for the provisioner daemon to be ready before continuing.
AwaitProvisionerDaemonsConnected(t, coderAPI)
}
client := codersdk.New(serverURL)
t.Cleanup(func() {
Expand All @@ -570,6 +572,36 @@ func NewWithAPI(t testing.TB, options *Options) (*codersdk.Client, io.Closer, *c
return client, provisionerCloser, coderAPI
}

// AwaitProvisionerDaemonsConnected waits for the provisioner daemon to connect.
func AwaitProvisionerDaemonsConnected(t testing.TB, api *coderd.API) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()
for {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this polling loop with potentially noisy logs, what if we made a slight addition to the provisionerd.Server such that it closes or sends on a buffered channel when it connects for the first time. This is how the tailnet dialer works.

Then we could select on that channel (vs ctx.Done()) rather than polling.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this out, except at the provisionerd level itself. LMK if that suits, or I can circle back and do it in the provisionerd server as you suggested.

select {
case <-ctx.Done():
t.Fatal("provisioner daemon did not connect in time")
case <-time.After(testutil.IntervalFast):
// nolint:gocritic // used for testing only
daemons, err := api.Database.GetProvisionerDaemons(dbauthz.AsSystemReadProvisionerDaemons(ctx))
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
t.Logf("no provisioner daemons found yet")
continue
}
require.NoError(t, err)
}
if len(daemons) == 0 {
t.Logf("no provisioner daemons found yet")
continue
}
for _, daemon := range daemons {
t.Logf("found provisioner daemon %q", daemon.Name)
}
return
}
}
}

// ProvisionerdCloser wraps a provisioner daemon as an io.Closer that can be called multiple times
type ProvisionerdCloser struct {
mu sync.Mutex
Expand Down
4 changes: 2 additions & 2 deletions coderd/templateversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
require.Equal(t, provisionersdk.ScopeOrganization, version.Job.Tags[provisionersdk.TagScope])
if assert.Equal(t, version.Job.Status, codersdk.ProvisionerJobPending) {
assert.NotNil(t, version.MatchedProvisioners)
assert.Equal(t, version.MatchedProvisioners.Available, 1)
assert.Equal(t, version.MatchedProvisioners.Count, 1)
assert.Equal(t, 1, version.MatchedProvisioners.Available)
assert.Equal(t, 1, version.MatchedProvisioners.Count)
assert.True(t, version.MatchedProvisioners.MostRecentlySeen.Valid)
}

Expand Down
2 changes: 2 additions & 0 deletions enterprise/coderd/coderdenttest/coderdenttest.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ func NewWithAPI(t *testing.T, options *Options) (
var provisionerCloser io.Closer = nopcloser{}
if options.IncludeProvisionerDaemon {
provisionerCloser = coderdtest.NewProvisionerDaemon(t, coderAPI.AGPL)
// Wait for provisioner daemons to be connected before continuing
coderdtest.AwaitProvisionerDaemonsConnected(t, coderAPI.AGPL)
}

t.Cleanup(func() {
Expand Down
Loading