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

Commit f2f9988

Browse files
committed
add other resultwriter implementations
1 parent 5a26ffc commit f2f9988

File tree

13 files changed

+262
-24
lines changed

13 files changed

+262
-24
lines changed

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
112112
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
113113
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
114114
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
115+
github.com/evanphx/json-patch v4.9.0+incompatible h1:kLcOMZeuLAJvL2BPWLMIj5oaZQobrkAqrL+WFZwQses=
115116
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
116117
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
117118
github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=
@@ -230,6 +231,7 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
230231
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
231232
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
232233
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
234+
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
233235
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
234236
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
235237
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
@@ -281,13 +283,16 @@ github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8m
281283
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
282284
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
283285
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
286+
github.com/onsi/ginkgo v1.11.0 h1:JAKSXpt1YjtLA7YpPiqO9ss6sNXEsPfSGdwN0UHqzrw=
284287
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
285288
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
289+
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
286290
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
287291
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
288292
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
289293
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
290294
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
295+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
291296
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
292297
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
293298
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -686,10 +691,12 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
686691
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
687692
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
688693
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
694+
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
689695
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
690696
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
691697
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
692698
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
699+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
693700
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
694701
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
695702
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
@@ -718,6 +725,7 @@ k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
718725
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
719726
k8s.io/klog/v2 v2.10.0 h1:R2HDMDJsHVTHA2n4RjwbeYXdOcBymXdX/JRb1v0VGhE=
720727
k8s.io/klog/v2 v2.10.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec=
728+
k8s.io/kube-openapi v0.0.0-20200805222855-6aeccd4b50c6 h1:+WnxoVtG8TMiudHBSEtrVL1egv36TkkJm+bA8AxicmQ=
721729
k8s.io/kube-openapi v0.0.0-20200805222855-6aeccd4b50c6/go.mod h1:UuqjUnNftUyPE5H64/qeyjQoUZhGpeFDVdxjTeEVN2o=
722730
k8s.io/utils v0.0.0-20200729134348-d5654de09c73/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
723731
k8s.io/utils v0.0.0-20210305010621-2afb4311ab10 h1:u5rPykqiCpL+LBfjRkXvnK71gOgIdmq3eHUEkPrbeTI=

internal/api/types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ func (s CheckState) String() string {
114114
}
115115

116116
type CheckResult struct {
117-
Name string
118-
State CheckState
119-
Summary string
120-
Details map[string]interface{}
117+
Name string `json:"name"`
118+
State CheckState `json:"state"`
119+
Summary string `json:"summary"`
120+
Details map[string]interface{} `json:"details,omitempty"`
121121
}

internal/api/writer.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,32 @@ type DiscardWriter struct {
1414
func (*DiscardWriter) WriteResult(_ *CheckResult) error {
1515
return nil
1616
}
17+
18+
var _ = ResultWriter(&CaptureWriter{})
19+
20+
// CaptureWriter is a writer that stores all results in an
21+
// internal buffer.
22+
type CaptureWriter struct {
23+
results []*CheckResult
24+
}
25+
26+
func (w *CaptureWriter) WriteResult(result *CheckResult) error {
27+
w.results = append(w.results, result)
28+
return nil
29+
}
30+
31+
func (w *CaptureWriter) Clear() {
32+
w.results = nil
33+
}
34+
35+
func (w *CaptureWriter) Get() []*CheckResult {
36+
return w.results
37+
}
38+
39+
func (w *CaptureWriter) Empty() bool {
40+
return w.Len() == 0
41+
}
42+
43+
func (w *CaptureWriter) Len() int {
44+
return len(w.results)
45+
}

internal/api/writer_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,24 @@ func TestDiscardWriter(t *testing.T) {
2222
})
2323
assert.Success(t, "discard with success result", err)
2424
}
25+
26+
func TestCaptureWriter(t *testing.T) {
27+
t.Parallel()
28+
29+
w := &api.CaptureWriter{}
30+
31+
assert.True(t, "initially empty", w.Empty())
32+
33+
result := &api.CheckResult{
34+
Name: "test",
35+
State: api.StatePassed,
36+
Summary: "test result",
37+
}
38+
err := w.WriteResult(result)
39+
assert.Success(t, "captured result", err)
40+
assert.True(t, "result length 1", w.Len() == 1)
41+
assert.Equal(t, "get back result", result, w.Get()[0])
42+
43+
w.Clear()
44+
assert.True(t, "empty buffer", w.Empty())
45+
}

internal/checks/kube/kubernetes.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ func WithWriter(writer api.ResultWriter) Option {
4646
}
4747
}
4848

49-
func WithClient(client kubernetes.Interface) Option {
50-
return func(k *KubernetesChecker) {
51-
k.client = client
52-
}
53-
}
54-
5549
func WithCoderVersion(version *semver.Version) Option {
5650
return func(k *KubernetesChecker) {
5751
k.coderVersion = version
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
11
package kube
2+
3+
import (
4+
"testing"
5+
6+
"k8s.io/client-go/kubernetes/fake"
7+
8+
"cdr.dev/slog/sloggers/slogtest/assert"
9+
)
10+
11+
func TestKubernetesOptions(t *testing.T) {
12+
t.Parallel()
13+
14+
clientset := fake.NewSimpleClientset()
15+
checker := NewKubernetesChecker(clientset)
16+
assert.Success(t, "validation successful", checker.Validate())
17+
18+
// var buf bytes.Buffer
19+
// log := slog.Make(sloghuman.Sink(&buf)).Leveled(slog.LevelDebug)
20+
// checker = NewKubernetesChecker(clientset,
21+
// WithCoderVersion(semver.MustParse("1.0")),
22+
// WithLogger(log))
23+
// assert.True(t, "log has output", buf.Len() > 0)
24+
}

internal/checks/kube/version.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ var versionRequirements = []CoderVersionRequirement{
2424
CoderVersion: semver.MustParse("1.21"),
2525
KubernetesVersionMin: semver.MustParse("1.19"),
2626
KubernetesVersionMax: semver.MustParse("1.22"),
27-
}, {
27+
},
28+
{
2829
CoderVersion: semver.MustParse("1.20"),
2930
KubernetesVersionMin: semver.MustParse("1.19"),
3031
KubernetesVersionMax: semver.MustParse("1.21"),

internal/checks/kube/version_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ func TestNearestVersion(t *testing.T) {
177177
Name: "exact-match",
178178
RequestedVersion: "1.20.0",
179179
NearestVersion: "1.20.0",
180-
}, {
180+
},
181+
{
181182
Name: "nearby-match",
182183
RequestedVersion: "1.20.1",
183184
NearestVersion: "1.20.0",
@@ -188,8 +189,10 @@ func TestNearestVersion(t *testing.T) {
188189
test := test
189190
t.Run(test.Name, func(t *testing.T) {
190191
t.Parallel()
192+
191193
requestedVersion := semver.MustParse(test.RequestedVersion)
192194
nearestVersion := semver.MustParse(test.NearestVersion)
195+
193196
found := findNearestVersion(requestedVersion)
194197
assert.Equal(t, "nearest version matches", nearestVersion, found.CoderVersion)
195198
})

internal/filterwriter/filter_test.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,27 @@ func TestFilter(t *testing.T) {
2727
{
2828
State: api.StateInfo,
2929
Summary: "filtered message",
30-
}, {
30+
},
31+
{
3132
State: api.StateSkipped,
3233
Summary: "skipped message",
33-
}, {
34+
},
35+
{
3436
State: api.StatePassed,
3537
Summary: "passed result",
36-
}, {
38+
},
39+
{
3740
State: api.StateFailed,
3841
Summary: "failed result",
39-
}, {
42+
},
43+
{
4044
State: api.StateInfo,
4145
Summary: "info message",
4246
},
4347
},
4448
ExpectedOutput: "PASS passed result\nFAIL failed result\n",
45-
}, {
49+
},
50+
{
4651
// Everything filtered out, write at each level and verify the
4752
// result is empty
4853
Name: "filter-everything",
@@ -60,7 +65,8 @@ func TestFilter(t *testing.T) {
6065
},
6166
},
6267
ExpectedOutput: "",
63-
}, {
68+
},
69+
{
6470
// Accept informational messages only
6571
Name: "accept-informational",
6672
Options: []filterwriter.Option{
@@ -75,21 +81,26 @@ func TestFilter(t *testing.T) {
7581
{
7682
State: api.StateInfo,
7783
Summary: "info message",
78-
}, {
84+
},
85+
{
7986
State: api.StateSkipped,
8087
Summary: "skipped message",
81-
}, {
88+
},
89+
{
8290
State: api.StatePassed,
8391
Summary: "passed result",
84-
}, {
92+
},
93+
{
8594
State: api.StateFailed,
8695
Summary: "failed result",
87-
}, {
96+
},
97+
{
8898
State: api.StateInfo,
8999
Summary: "info result",
90100
},
91101
},
92-
ExpectedOutput: "INFO info message\nINFO info result\n",
102+
ExpectedOutput: "INFO info message\n" +
103+
"INFO info result\n",
93104
},
94105
}
95106

internal/jsonwriter/json.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
11
package jsonwriter
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
7+
"github.com/cdr/coder-doctor/internal/api"
8+
)
9+
10+
var _ = api.ResultWriter(&JSONWriter{})
11+
12+
// JSONWriter is a writer that writes results to a stream in
13+
// JSON Lines format.
14+
type JSONWriter struct {
15+
writer io.Writer
16+
encoder *json.Encoder
17+
}
18+
19+
func New(writer io.Writer) *JSONWriter {
20+
return &JSONWriter{
21+
writer: writer,
22+
encoder: json.NewEncoder(writer),
23+
}
24+
}
25+
26+
func (w *JSONWriter) WriteResult(result *api.CheckResult) error {
27+
return w.encoder.Encode(result)
28+
}

0 commit comments

Comments
 (0)