ceph-csi/vendor/k8s.io/kubernetes/pkg/printers/humanreadable_test.go

135 lines
3.9 KiB
Go
Raw Normal View History

2018-01-09 18:57:14 +00:00
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package printers
import (
"bytes"
"fmt"
"reflect"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2018-03-06 22:33:18 +00:00
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
2018-01-09 18:57:14 +00:00
"k8s.io/apimachinery/pkg/runtime"
api "k8s.io/kubernetes/pkg/apis/core"
)
2018-03-06 22:33:18 +00:00
var testNamespaceColumnDefinitions = []metav1beta1.TableColumnDefinition{
2018-01-09 18:57:14 +00:00
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
{Name: "Status", Type: "string", Description: "The status of the namespace"},
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
}
2018-03-06 22:33:18 +00:00
func testPrintNamespace(obj *api.Namespace, options PrintOptions) ([]metav1beta1.TableRow, error) {
2018-01-09 18:57:14 +00:00
if options.WithNamespace {
return nil, fmt.Errorf("namespace is not namespaced")
}
2018-03-06 22:33:18 +00:00
row := metav1beta1.TableRow{
2018-01-09 18:57:14 +00:00
Object: runtime.RawExtension{Object: obj},
}
row.Cells = append(row.Cells, obj.Name, obj.Status.Phase, "<unknow>")
2018-03-06 22:33:18 +00:00
return []metav1beta1.TableRow{row}, nil
2018-01-09 18:57:14 +00:00
}
func TestPrintRowsForHandlerEntry(t *testing.T) {
printFunc := reflect.ValueOf(testPrintNamespace)
2018-07-18 14:47:22 +00:00
testCase := []struct {
name string
2018-01-09 18:57:14 +00:00
h *handlerEntry
opt PrintOptions
obj runtime.Object
includeHeader bool
expectOut string
expectErr string
}{
2018-07-18 14:47:22 +00:00
{
name: "no tablecolumndefinition and includeheader flase",
2018-01-09 18:57:14 +00:00
h: &handlerEntry{
2018-03-06 22:33:18 +00:00
columnDefinitions: []metav1beta1.TableColumnDefinition{},
2018-01-09 18:57:14 +00:00
printRows: true,
printFunc: printFunc,
},
opt: PrintOptions{},
obj: &api.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: "test"},
},
includeHeader: false,
expectOut: "test\t\t<unknow>\n",
},
2018-07-18 14:47:22 +00:00
{
name: "no tablecolumndefinition and includeheader true",
2018-01-09 18:57:14 +00:00
h: &handlerEntry{
2018-03-06 22:33:18 +00:00
columnDefinitions: []metav1beta1.TableColumnDefinition{},
2018-01-09 18:57:14 +00:00
printRows: true,
printFunc: printFunc,
},
opt: PrintOptions{},
obj: &api.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: "test"},
},
includeHeader: true,
expectOut: "\ntest\t\t<unknow>\n",
},
2018-07-18 14:47:22 +00:00
{
name: "have tablecolumndefinition and includeheader true",
2018-01-09 18:57:14 +00:00
h: &handlerEntry{
columnDefinitions: testNamespaceColumnDefinitions,
printRows: true,
printFunc: printFunc,
},
opt: PrintOptions{},
obj: &api.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: "test"},
},
includeHeader: true,
expectOut: "NAME\tSTATUS\tAGE\ntest\t\t<unknow>\n",
},
2018-07-18 14:47:22 +00:00
{
name: "print namespace and withnamespace true, should not print header",
2018-01-09 18:57:14 +00:00
h: &handlerEntry{
columnDefinitions: testNamespaceColumnDefinitions,
printRows: true,
printFunc: printFunc,
},
opt: PrintOptions{
WithNamespace: true,
},
obj: &api.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: "test"},
},
includeHeader: true,
expectOut: "",
expectErr: "namespace is not namespaced",
},
}
2018-07-18 14:47:22 +00:00
for _, test := range testCase {
t.Run(test.name, func(t *testing.T) {
buffer := &bytes.Buffer{}
err := printRowsForHandlerEntry(buffer, test.h, test.obj, test.opt, test.includeHeader)
if err != nil {
if err.Error() != test.expectErr {
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectErr, err)
}
2018-01-09 18:57:14 +00:00
}
2018-07-18 14:47:22 +00:00
if test.expectOut != buffer.String() {
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectOut, buffer.String())
}
})
2018-01-09 18:57:14 +00:00
}
}