Skip to content
This repository was archived by the owner on Nov 14, 2024. It is now read-only.

fix: validate kubernetes checker #7

Merged
merged 3 commits into from
Aug 25, 2021
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
fix: validate local helm requirements
  • Loading branch information
johnstcn committed Aug 24, 2021
commit 82e76be838ede63a175c5de4a03086e93198f51f
4 changes: 3 additions & 1 deletion internal/checks/local/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/Masterminds/semver/v3"
"golang.org/x/xerrors"

"cdr.dev/slog"
"github.com/cdr/coder-doctor/internal/api"
Expand Down Expand Up @@ -52,7 +53,8 @@ func (l *Checker) CheckLocalHelmVersion(ctx context.Context) *api.CheckResult {

selectedVersion := findNearestHelmVersion(l.coderVersion)
if selectedVersion == nil {
return api.ErrorResult(LocalHelmVersionCheck, fmt.Sprintf("checking coder version %s not supported", l.coderVersion.String()), nil)
// If this happens, Validate() was not called properly.
panic(xerrors.Errorf("helm version was nil - did you forget to call validate?"))
}
l.log.Debug(ctx, "selected coder version", slog.F("requested", l.coderVersion), slog.F("selected", selectedVersion.Coder))

Expand Down
17 changes: 8 additions & 9 deletions internal/checks/local/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,17 @@ func Test_CheckLocalHelmVersion(t *testing.T) {
}
})

run(t, "helm: coder version is unsupported", func(t *testing.T, p *params) {
run(t, "helm: someone did not call validate", func(t *testing.T, p *params) {
defer func() {
if r := recover(); r == nil {
t.Errorf("this code should have panicked")
t.FailNow()
}
}()
p.Opts = append(p.Opts, WithCoderVersion(semver.MustParse("v1.19")))
p.LP.Handle("helm", "/usr/local/bin/helm", nil)
p.EX.Handle("/usr/local/bin/helm version --short", []byte("v3.6.0+g7f2df64"), nil)
lc := NewChecker(p.Opts...)
err := lc.Run(p.Ctx)
assert.Success(t, "run local checker", err)
assert.False(t, "results should not be empty", p.W.Empty())
for _, res := range p.W.Get() {
if res.Name == LocalHelmVersionCheck {
assert.Equal(t, "should fail", api.StateFailed, res.State)
}
}
_ = lc.Run(p.Ctx)
})
}
6 changes: 5 additions & 1 deletion internal/checks/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ func WithLookPathF(f LookPathF) Option {
}
}

func (*Checker) Validate() error {
func (l *Checker) Validate() error {
// Ensure we know the Helm version requirement for our Coder version.
if findNearestHelmVersion(l.coderVersion) == nil {
return xerrors.Errorf("unsupported coder version %s: compatible helm version not specified", l.coderVersion.String())
}
return nil
}

Expand Down
13 changes: 13 additions & 0 deletions internal/checks/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@ import (
"context"
"strings"
"testing"

"cdr.dev/slog/sloggers/slogtest/assert"

"github.com/Masterminds/semver/v3"
)

func Test_LocalChecker_Validate(t *testing.T) {
t.Parallel()
lc := NewChecker()
assert.Success(t, "local checker with defaults should be successful", lc.Validate())

lc = NewChecker(WithCoderVersion(semver.MustParse("0.0.1")))
assert.ErrorContains(t, "local checker with defaults should be successful", lc.Validate(), "unsupported coder version")
}

type execResult struct {
Output []byte
Err error
Expand Down