Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test coverage with kubectl get components #85595

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 32 additions & 5 deletions staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go
Expand Up @@ -1176,18 +1176,18 @@ func TestGetListComponentStatus(t *testing.T) {

tf.UnstructuredClient = &fake.RESTClient{
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...)},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can just be statuses.Items...

}

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

expected := `NAME AGE
servergood <unknown>
serverbad <unknown>
serverunknown <unknown>
expected := `NAME STATUS MESSAGE ERROR
servergood Healthy ok
serverbad Unhealthy bad status: 500
serverunknown Unhealthy fizzbuzz error
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected\n%v\ngot\n%v", e, a)
Expand Down Expand Up @@ -2788,6 +2788,33 @@ func nodeTableObjBody(codec runtime.Codec, nodes ...corev1.Node) io.ReadCloser {
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
func emptyTableObjBody(codec runtime.Codec) io.ReadCloser {
table := &metav1.Table{
Expand Down