Merge pull request #85595 from zhouya0/add_test_coverage_with_kubectl_get_components

add test coverage with kubectl get components
This commit is contained in:
Kubernetes Prow Robot 2019-11-27 06:03:21 -08:00 committed by GitHub
commit ab0bfb304c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1176,7 +1176,7 @@ func TestGetListComponentStatus(t *testing.T) {
tf.UnstructuredClient = &fake.RESTClient{ tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer, NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
Resp: &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, statuses)}, Resp: &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: componentStatusTableObjBody(codec, (*statuses).Items...)},
} }
streams, _, buf, _ := genericclioptions.NewTestIOStreams() streams, _, buf, _ := genericclioptions.NewTestIOStreams()
@ -1184,10 +1184,10 @@ func TestGetListComponentStatus(t *testing.T) {
cmd.SetOutput(buf) cmd.SetOutput(buf)
cmd.Run(cmd, []string{"componentstatuses"}) cmd.Run(cmd, []string{"componentstatuses"})
expected := `NAME AGE expected := `NAME STATUS MESSAGE ERROR
servergood <unknown> servergood Healthy ok
serverbad <unknown> serverbad Unhealthy bad status: 500
serverunknown <unknown> serverunknown Unhealthy fizzbuzz error
` `
if e, a := expected, buf.String(); e != a { if e, a := expected, buf.String(); e != a {
t.Errorf("expected\n%v\ngot\n%v", e, a) t.Errorf("expected\n%v\ngot\n%v", e, a)
@ -2788,6 +2788,33 @@ func nodeTableObjBody(codec runtime.Codec, nodes ...corev1.Node) io.ReadCloser {
return cmdtesting.ObjBody(codec, table) return cmdtesting.ObjBody(codec, table)
} }
// build a meta table response from a componentStatus list
func componentStatusTableObjBody(codec runtime.Codec, componentStatuses ...corev1.ComponentStatus) io.ReadCloser {
table := &metav1.Table{
ColumnDefinitions: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name"},
{Name: "Status", Type: "string", Format: ""},
{Name: "Message", Type: "string", Format: ""},
{Name: "Error", Type: "string", Format: ""},
},
}
for _, v := range componentStatuses {
b := bytes.NewBuffer(nil)
codec.Encode(&v, b)
var status string
if v.Conditions[0].Status == corev1.ConditionTrue {
status = "Healthy"
} else {
status = "Unhealthy"
}
table.Rows = append(table.Rows, metav1.TableRow{
Object: runtime.RawExtension{Raw: b.Bytes()},
Cells: []interface{}{v.Name, status, v.Conditions[0].Message, v.Conditions[0].Error},
})
}
return cmdtesting.ObjBody(codec, table)
}
// build an empty table response // build an empty table response
func emptyTableObjBody(codec runtime.Codec) io.ReadCloser { func emptyTableObjBody(codec runtime.Codec) io.ReadCloser {
table := &metav1.Table{ table := &metav1.Table{