Skip to content

Commit ab0bfb3

Browse files
authored
Merge pull request kubernetes#85595 from zhouya0/add_test_coverage_with_kubectl_get_components
add test coverage with kubectl get components
2 parents 7ed5eb6 + 07806d4 commit ab0bfb3

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,18 +1176,18 @@ func TestGetListComponentStatus(t *testing.T) {
11761176

11771177
tf.UnstructuredClient = &fake.RESTClient{
11781178
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
1179-
Resp: &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, statuses)},
1179+
Resp: &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: componentStatusTableObjBody(codec, (*statuses).Items...)},
11801180
}
11811181

11821182
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
11831183
cmd := NewCmdGet("kubectl", tf, streams)
11841184
cmd.SetOutput(buf)
11851185
cmd.Run(cmd, []string{"componentstatuses"})
11861186

1187-
expected := `NAME AGE
1188-
servergood <unknown>
1189-
serverbad <unknown>
1190-
serverunknown <unknown>
1187+
expected := `NAME STATUS MESSAGE ERROR
1188+
servergood Healthy ok
1189+
serverbad Unhealthy bad status: 500
1190+
serverunknown Unhealthy fizzbuzz error
11911191
`
11921192
if e, a := expected, buf.String(); e != a {
11931193
t.Errorf("expected\n%v\ngot\n%v", e, a)
@@ -2788,6 +2788,33 @@ func nodeTableObjBody(codec runtime.Codec, nodes ...corev1.Node) io.ReadCloser {
27882788
return cmdtesting.ObjBody(codec, table)
27892789
}
27902790

2791+
// build a meta table response from a componentStatus list
2792+
func componentStatusTableObjBody(codec runtime.Codec, componentStatuses ...corev1.ComponentStatus) io.ReadCloser {
2793+
table := &metav1.Table{
2794+
ColumnDefinitions: []metav1.TableColumnDefinition{
2795+
{Name: "Name", Type: "string", Format: "name"},
2796+
{Name: "Status", Type: "string", Format: ""},
2797+
{Name: "Message", Type: "string", Format: ""},
2798+
{Name: "Error", Type: "string", Format: ""},
2799+
},
2800+
}
2801+
for _, v := range componentStatuses {
2802+
b := bytes.NewBuffer(nil)
2803+
codec.Encode(&v, b)
2804+
var status string
2805+
if v.Conditions[0].Status == corev1.ConditionTrue {
2806+
status = "Healthy"
2807+
} else {
2808+
status = "Unhealthy"
2809+
}
2810+
table.Rows = append(table.Rows, metav1.TableRow{
2811+
Object: runtime.RawExtension{Raw: b.Bytes()},
2812+
Cells: []interface{}{v.Name, status, v.Conditions[0].Message, v.Conditions[0].Error},
2813+
})
2814+
}
2815+
return cmdtesting.ObjBody(codec, table)
2816+
}
2817+
27912818
// build an empty table response
27922819
func emptyTableObjBody(codec runtime.Codec) io.ReadCloser {
27932820
table := &metav1.Table{

0 commit comments

Comments
 (0)