Skip to content
Merged
Show file tree
Hide file tree
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
Next Next commit
fixup! refactor: extract WarnMatchedProvisioners to cliutil
  • Loading branch information
johnstcn committed Dec 1, 2024
commit e1423f5c5fadec565eb0b5a91032e23416fed95e
23 changes: 15 additions & 8 deletions cli/cliutil/provisionerwarn.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,30 @@ Details:
`
)

func WarnMatchedProvisioners(w io.Writer, tv codersdk.TemplateVersion) {
if tv.MatchedProvisioners == nil {
// WarnMatchedProvisioners warns the user if there are no provisioners that
// match the requested tags for a given provisioner job.
// If the job is not pending, it is ignored.
func WarnMatchedProvisioners(w io.Writer, mp *codersdk.MatchedProvisioners, job codersdk.ProvisionerJob) {
if mp == nil {
// Nothing in the response, nothing to do here!
return
}
if job.Status != codersdk.ProvisionerJobPending {
// Only warn if the job is pending.
return
}
var tagsJSON strings.Builder
if err := json.NewEncoder(&tagsJSON).Encode(tv.Job.Tags); err != nil {
if err := json.NewEncoder(&tagsJSON).Encode(job.Tags); err != nil {
// Fall back to the less-pretty string representation.
tagsJSON.Reset()
_, _ = tagsJSON.WriteString(fmt.Sprintf("%v", tv.Job.Tags))
_, _ = tagsJSON.WriteString(fmt.Sprintf("%v", job.Tags))
}
if tv.MatchedProvisioners.Count == 0 {
cliui.Warnf(w, warnNoMatchedProvisioners, tv.Job.ID, tagsJSON.String())
if mp.Count == 0 {
cliui.Warnf(w, warnNoMatchedProvisioners, job.ID, tagsJSON.String())
return
}
if tv.MatchedProvisioners.Available == 0 {
cliui.Warnf(w, warnNoAvailableProvisioners, tv.Job.ID, strings.TrimSpace(tagsJSON.String()), tv.MatchedProvisioners.MostRecentlySeen.Time)
if mp.Available == 0 {
cliui.Warnf(w, warnNoAvailableProvisioners, job.ID, strings.TrimSpace(tagsJSON.String()), mp.MostRecentlySeen.Time)
return
}
}
74 changes: 74 additions & 0 deletions cli/cliutil/provisionerwarn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cliutil_test

import (
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/cli/cliutil"
"github.com/coder/coder/v2/codersdk"
)

func TestWarnMatchedProvisioners(t *testing.T) {
t.Parallel()

for _, tt := range []struct {
name string
mp *codersdk.MatchedProvisioners
job codersdk.ProvisionerJob
expect string
}{
{
name: "no_match",
mp: &codersdk.MatchedProvisioners{
Count: 0,
Available: 0,
},
job: codersdk.ProvisionerJob{
Status: codersdk.ProvisionerJobPending,
},
expect: `there are no provisioners that accept the required tags`,
},
{
name: "no_available",
mp: &codersdk.MatchedProvisioners{
Count: 1,
Available: 0,
},
job: codersdk.ProvisionerJob{
Status: codersdk.ProvisionerJobPending,
},
expect: `Provisioners that accept the required tags have not responded for longer than expected`,
},
{
name: "match",
mp: &codersdk.MatchedProvisioners{
Count: 1,
Available: 1,
},
job: codersdk.ProvisionerJob{
Status: codersdk.ProvisionerJobPending,
},
},
{
name: "not_pending",
mp: &codersdk.MatchedProvisioners{},
job: codersdk.ProvisionerJob{
Status: codersdk.ProvisionerJobRunning,
},
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var w strings.Builder
cliutil.WarnMatchedProvisioners(&w, tt.mp, tt.job)
if tt.expect != "" {
require.Contains(t, w.String(), tt.expect)
} else {
require.Empty(t, w.String())
}
})
}
}