|
| 1 | +package local |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/Masterminds/semver/v3" |
| 9 | + |
| 10 | + "cdr.dev/slog/sloggers/slogtest/assert" |
| 11 | + "github.com/cdr/coder-doctor/internal/api" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_CheckLocalHelmVersion(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + type params struct { |
| 18 | + W *api.CaptureWriter |
| 19 | + EX *fakeExecer |
| 20 | + LP *fakeLookPather |
| 21 | + Opts []Option |
| 22 | + Ctx context.Context |
| 23 | + } |
| 24 | + |
| 25 | + run := func(t *testing.T, name string, fn func(t *testing.T, p *params)) { |
| 26 | + t.Run(name, func(t *testing.T) { |
| 27 | + ctx := context.Background() |
| 28 | + cw := &api.CaptureWriter{} |
| 29 | + ex := newFakeExecer(t) |
| 30 | + lp := newFakeLookPather(t) |
| 31 | + opts := []Option{ |
| 32 | + WithWriter(cw), |
| 33 | + WithExecF(ex.ExecContext), |
| 34 | + WithLookPathF(lp.LookPath), |
| 35 | + WithTarget(api.CheckTargetKubernetes), // default |
| 36 | + } |
| 37 | + p := ¶ms{ |
| 38 | + W: cw, |
| 39 | + EX: ex, |
| 40 | + LP: lp, |
| 41 | + Opts: opts, |
| 42 | + Ctx: ctx, |
| 43 | + } |
| 44 | + fn(t, p) |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + run(t, "helm: when not running against kubernetes", func(t *testing.T, p *params) { |
| 49 | + p.Opts = append(p.Opts, WithTarget(api.CheckTargetUndefined)) |
| 50 | + lc := NewChecker(p.Opts...) |
| 51 | + err := lc.Run(p.Ctx) |
| 52 | + assert.Success(t, "run local checker", err) |
| 53 | + assert.False(t, "results should not be empty", p.W.Empty()) |
| 54 | + for _, res := range p.W.Get() { |
| 55 | + if res.Name == LocalHelmVersionCheck { |
| 56 | + assert.Equal(t, "should skip helm check if not running against kubernetes", api.StateSkipped, res.State) |
| 57 | + } |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + run(t, "helm: with version 3.6", func(t *testing.T, p *params) { |
| 62 | + p.LP.Handle("helm", "/usr/local/bin/helm", nil) |
| 63 | + p.EX.Handle("/usr/local/bin/helm version --short", []byte("v3.6.0+g7f2df64"), nil) |
| 64 | + lc := NewChecker(p.Opts...) |
| 65 | + err := lc.Run(p.Ctx) |
| 66 | + assert.Success(t, "run local checker", err) |
| 67 | + assert.False(t, "results should not be empty", p.W.Empty()) |
| 68 | + for _, res := range p.W.Get() { |
| 69 | + if res.Name == LocalHelmVersionCheck { |
| 70 | + assert.Equal(t, "should pass", api.StatePassed, res.State) |
| 71 | + } |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + run(t, "helm: with version 2", func(t *testing.T, p *params) { |
| 76 | + p.LP.Handle("helm", "/usr/local/bin/helm", nil) |
| 77 | + p.EX.Handle("/usr/local/bin/helm version --short", []byte("v2.0.0"), nil) |
| 78 | + lc := NewChecker(p.Opts...) |
| 79 | + err := lc.Run(p.Ctx) |
| 80 | + assert.Success(t, "run local checker", err) |
| 81 | + assert.False(t, "results should not be empty", p.W.Empty()) |
| 82 | + for _, res := range p.W.Get() { |
| 83 | + if res.Name == LocalHelmVersionCheck { |
| 84 | + assert.Equal(t, "should fail", api.StateFailed, res.State) |
| 85 | + } |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + run(t, "helm: not in path", func(t *testing.T, p *params) { |
| 90 | + p.LP.Handle("helm", "", os.ErrNotExist) |
| 91 | + lc := NewChecker(p.Opts...) |
| 92 | + err := lc.Run(p.Ctx) |
| 93 | + assert.Success(t, "run local checker", err) |
| 94 | + assert.False(t, "results should not be empty", p.W.Empty()) |
| 95 | + for _, res := range p.W.Get() { |
| 96 | + if res.Name == LocalHelmVersionCheck { |
| 97 | + assert.Equal(t, "should fail", api.StateFailed, res.State) |
| 98 | + } |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + run(t, "helm: cannot be executed", func(t *testing.T, p *params) { |
| 103 | + p.LP.Handle("helm", "/usr/local/bin/helm", nil) |
| 104 | + p.EX.Handle("/usr/local/bin/helm version --short", []byte(""), os.ErrPermission) |
| 105 | + lc := NewChecker(p.Opts...) |
| 106 | + err := lc.Run(p.Ctx) |
| 107 | + assert.Success(t, "run local checker", err) |
| 108 | + assert.False(t, "results should not be empty", p.W.Empty()) |
| 109 | + for _, res := range p.W.Get() { |
| 110 | + if res.Name == LocalHelmVersionCheck { |
| 111 | + assert.Equal(t, "should fail", api.StateFailed, res.State) |
| 112 | + } |
| 113 | + } |
| 114 | + }) |
| 115 | + |
| 116 | + run(t, "helm: returns garbage version", func(t *testing.T, p *params) { |
| 117 | + p.LP.Handle("helm", "/usr/local/bin/helm", nil) |
| 118 | + p.EX.Handle("/usr/local/bin/helm version --short", []byte(""), nil) |
| 119 | + lc := NewChecker(p.Opts...) |
| 120 | + err := lc.Run(p.Ctx) |
| 121 | + assert.Success(t, "run local checker", err) |
| 122 | + assert.False(t, "results should not be empty", p.W.Empty()) |
| 123 | + for _, res := range p.W.Get() { |
| 124 | + if res.Name == LocalHelmVersionCheck { |
| 125 | + assert.Equal(t, "should fail", api.StateFailed, res.State) |
| 126 | + } |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + run(t, "helm: coder version is unsupported", func(t *testing.T, p *params) { |
| 131 | + p.Opts = append(p.Opts, WithCoderVersion(semver.MustParse("v1.19"))) |
| 132 | + p.LP.Handle("helm", "/usr/local/bin/helm", nil) |
| 133 | + p.EX.Handle("/usr/local/bin/helm version --short", []byte("v3.6.0+g7f2df64"), nil) |
| 134 | + lc := NewChecker(p.Opts...) |
| 135 | + err := lc.Run(p.Ctx) |
| 136 | + assert.Success(t, "run local checker", err) |
| 137 | + assert.False(t, "results should not be empty", p.W.Empty()) |
| 138 | + for _, res := range p.W.Get() { |
| 139 | + if res.Name == LocalHelmVersionCheck { |
| 140 | + assert.Equal(t, "should fail", api.StateFailed, res.State) |
| 141 | + } |
| 142 | + } |
| 143 | + }) |
| 144 | +} |
0 commit comments