Skip to content

Commit 3fa68d8

Browse files
authored
Merge pull request crossplane#6944 from intojhanurag/fix/top-table-writer-error
tests: add error handling test for printPodsTable
2 parents 009c6ef + 140f02a commit 3fa68d8

1 file changed

Lines changed: 39 additions & 9 deletions

File tree

cmd/crank/beta/top/top_test.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@ package top
22

33
import (
44
"bytes"
5+
"fmt"
6+
"io"
57
"strings"
68
"testing"
79

810
"github.com/google/go-cmp/cmp"
11+
"github.com/google/go-cmp/cmp/cmpopts"
912
corev1 "k8s.io/api/core/v1"
1013
"k8s.io/apimachinery/pkg/api/resource"
1114
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1215

13-
"github.com/crossplane/crossplane-runtime/v2/pkg/test"
14-
1516
v1 "github.com/crossplane/crossplane/v2/apis/pkg/v1"
1617
)
1718

19+
type errorWriter struct{}
20+
21+
func (w *errorWriter) Write(_ []byte) (n int, err error) {
22+
return 0, fmt.Errorf("write error")
23+
}
24+
1825
func TestGetCrossplanePods(t *testing.T) {
1926
type want struct {
2027
topMetrics []topMetrics
@@ -165,11 +172,13 @@ func TestPrintPodsTable(t *testing.T) {
165172
tests := map[string]struct {
166173
reason string
167174
crossplanePods []topMetrics
175+
writer io.Writer
168176
want want
169177
}{
170178
"NoPodsFound": {
171179
reason: "Should return header when no pods are found",
172180
crossplanePods: []topMetrics{},
181+
writer: &bytes.Buffer{},
173182
want: want{
174183
results: `
175184
TYPE NAMESPACE NAME CPU(cores) MEMORY
@@ -188,6 +197,7 @@ TYPE NAMESPACE NAME CPU(cores) MEMORY
188197
MemoryUsage: resource.MustParse("512Mi"),
189198
},
190199
},
200+
writer: &bytes.Buffer{},
191201
want: want{
192202
results: `
193203
TYPE NAMESPACE NAME CPU(cores) MEMORY
@@ -214,6 +224,7 @@ crossplane crossplane-system crossplane-123 100m 512Mi
214224
MemoryUsage: resource.MustParse("1024Mi"),
215225
},
216226
},
227+
writer: &bytes.Buffer{},
217228
want: want{
218229
results: `
219230
TYPE NAMESPACE NAME CPU(cores) MEMORY
@@ -223,18 +234,37 @@ function crossplane-system function-123 200m 1024Mi
223234
err: nil,
224235
},
225236
},
237+
"WriterError": {
238+
reason: "Should return error when writer fails",
239+
crossplanePods: []topMetrics{
240+
{
241+
PodType: "crossplane",
242+
PodName: "crossplane-123",
243+
PodNamespace: "crossplane-system",
244+
CPUUsage: resource.MustParse("100m"),
245+
MemoryUsage: resource.MustParse("512Mi"),
246+
},
247+
},
248+
writer: &errorWriter{},
249+
want: want{
250+
results: "",
251+
err: cmpopts.AnyError,
252+
},
253+
},
226254
}
227255
for name, tt := range tests {
228256
t.Run(name, func(t *testing.T) {
229-
b := &bytes.Buffer{}
230-
err := printPodsTable(b, tt.crossplanePods)
231-
// TODO:(piotr1215) add error test case
232-
if diff := cmp.Diff(tt.want.err, err, test.EquateErrors()); diff != "" {
233-
t.Errorf("%s\nprintPodsTable(): -want, +got:\n%s", tt.reason, diff)
257+
w := tt.writer
258+
259+
err := printPodsTable(w, tt.crossplanePods)
260+
if diff := cmp.Diff(tt.want.err, err, cmpopts.EquateErrors()); diff != "" {
261+
t.Errorf("%s\nprintPodsTable() error: -want,+got:\n%s", tt.reason, diff)
234262
}
235263

236-
if diff := cmp.Diff(strings.TrimSpace(tt.want.results), strings.TrimSpace(b.String())); diff != "" {
237-
t.Errorf("%s\nprintPodsTable(): -want, +got:\n%s", tt.reason, diff)
264+
if buf, ok := w.(*bytes.Buffer); ok {
265+
if diff := cmp.Diff(strings.TrimSpace(tt.want.results), strings.TrimSpace(buf.String())); diff != "" {
266+
t.Errorf("%s\nprintPodsTable(): -want, +got:\n%s", tt.reason, diff)
267+
}
238268
}
239269
})
240270
}

0 commit comments

Comments
 (0)