Skip to content

Commit b88dbbb

Browse files
committed
Added unit tests documenting current behavior of tableConverter
1 parent cab89c7 commit b88dbbb

File tree

2 files changed

+227
-0
lines changed

2 files changed

+227
-0
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/tableconvertor/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ go_test(
3737
embed = [":go_default_library"],
3838
deps = [
3939
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
40+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
4041
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1:go_default_library",
4142
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
4243
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",

staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/tableconvertor/tableconvertor_test.go

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2728
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
2829
"k8s.io/apimachinery/pkg/runtime"
2930
"k8s.io/apimachinery/pkg/util/diff"
@@ -201,6 +202,225 @@ func Test_convertor_ConvertToTable(t *testing.T) {
201202
},
202203
},
203204
},
205+
{
206+
name: "Return table with additional column containing multiple string values",
207+
fields: fields{
208+
headers: []metav1.TableColumnDefinition{
209+
{Name: "name", Type: "string"},
210+
{Name: "valueOnly", Type: "string"},
211+
{Name: "single1", Type: "string"},
212+
{Name: "single2", Type: "string"},
213+
{Name: "multi", Type: "string"},
214+
},
215+
additionalColumns: []*jsonpath.JSONPath{
216+
newJSONPath("valueOnly", "{.spec.servers[0].hosts[0]}"),
217+
newJSONPath("single1", "{.spec.servers[0].hosts}"),
218+
newJSONPath("single2", "{.spec.servers[1].hosts}"),
219+
newJSONPath("multi", "{.spec.servers[*].hosts}"),
220+
},
221+
},
222+
args: args{
223+
obj: &unstructured.Unstructured{
224+
Object: map[string]interface{}{
225+
"apiVersion": "example.istio.io/v1alpha1",
226+
"kind": "Blah",
227+
"metadata": map[string]interface{}{
228+
"name": "blah",
229+
},
230+
"spec": map[string]interface{}{
231+
"servers": []map[string]interface{}{
232+
{"hosts": []string{"foo"}},
233+
{"hosts": []string{"bar", "baz"}},
234+
},
235+
},
236+
},
237+
},
238+
tableOptions: nil,
239+
},
240+
want: &metav1.Table{
241+
ColumnDefinitions: []metav1.TableColumnDefinition{
242+
{Name: "name", Type: "string"},
243+
{Name: "valueOnly", Type: "string"},
244+
{Name: "single1", Type: "string"},
245+
{Name: "single2", Type: "string"},
246+
{Name: "multi", Type: "string"},
247+
},
248+
Rows: []metav1.TableRow{
249+
{
250+
Cells: []interface{}{
251+
"blah",
252+
"foo",
253+
`["foo"]`,
254+
`["bar","baz"]`,
255+
`["foo"]`, // TODO: TableConverter should be changed so that the response is this: `["foo"] ["bar","baz"]`,
256+
},
257+
Object: runtime.RawExtension{
258+
Object: &unstructured.Unstructured{
259+
Object: map[string]interface{}{
260+
"apiVersion": "example.istio.io/v1alpha1",
261+
"kind": "Blah",
262+
"metadata": map[string]interface{}{
263+
"name": "blah",
264+
},
265+
"spec": map[string]interface{}{
266+
"servers": []map[string]interface{}{
267+
{"hosts": []string{"foo"}},
268+
{"hosts": []string{"bar", "baz"}},
269+
},
270+
},
271+
},
272+
},
273+
},
274+
},
275+
},
276+
},
277+
},
278+
{
279+
name: "Return table with additional column containing multiple integer values as string",
280+
fields: fields{
281+
headers: []metav1.TableColumnDefinition{
282+
{Name: "name", Type: "string"},
283+
{Name: "valueOnly", Type: "string"},
284+
{Name: "single1", Type: "string"},
285+
{Name: "single2", Type: "string"},
286+
{Name: "multi", Type: "string"},
287+
},
288+
additionalColumns: []*jsonpath.JSONPath{
289+
newJSONPath("valueOnly", "{.spec.foo[0].bar[0]}"),
290+
newJSONPath("single1", "{.spec.foo[0].bar}"),
291+
newJSONPath("single2", "{.spec.foo[1].bar}"),
292+
newJSONPath("multi", "{.spec.foo[*].bar}"),
293+
},
294+
},
295+
args: args{
296+
obj: &unstructured.Unstructured{
297+
Object: map[string]interface{}{
298+
"apiVersion": "example.istio.io/v1alpha1",
299+
"kind": "Blah",
300+
"metadata": map[string]interface{}{
301+
"name": "blah",
302+
},
303+
"spec": map[string]interface{}{
304+
"foo": []map[string]interface{}{
305+
{"bar": []int64{1}},
306+
{"bar": []int64{2, 3}},
307+
},
308+
},
309+
},
310+
},
311+
tableOptions: nil,
312+
},
313+
want: &metav1.Table{
314+
ColumnDefinitions: []metav1.TableColumnDefinition{
315+
{Name: "name", Type: "string"},
316+
{Name: "valueOnly", Type: "string"},
317+
{Name: "single1", Type: "string"},
318+
{Name: "single2", Type: "string"},
319+
{Name: "multi", Type: "string"},
320+
},
321+
Rows: []metav1.TableRow{
322+
{
323+
Cells: []interface{}{
324+
"blah",
325+
"1",
326+
"[1]",
327+
"[2,3]",
328+
"[1]", // TODO: TableConverter should be changed so that the response is this: `[1] [2,3]`,
329+
},
330+
Object: runtime.RawExtension{
331+
Object: &unstructured.Unstructured{
332+
Object: map[string]interface{}{
333+
"apiVersion": "example.istio.io/v1alpha1",
334+
"kind": "Blah",
335+
"metadata": map[string]interface{}{
336+
"name": "blah",
337+
},
338+
"spec": map[string]interface{}{
339+
"foo": []map[string]interface{}{
340+
{"bar": []int64{1}},
341+
{"bar": []int64{2, 3}},
342+
},
343+
},
344+
},
345+
},
346+
},
347+
},
348+
},
349+
},
350+
},
351+
{
352+
name: "Return table with additional column containing multiple integer values",
353+
fields: fields{
354+
headers: []metav1.TableColumnDefinition{
355+
{Name: "name", Type: "string"},
356+
{Name: "valueOnly", Type: "integer"},
357+
{Name: "single1", Type: "integer"},
358+
{Name: "single2", Type: "integer"},
359+
{Name: "multi", Type: "integer"},
360+
},
361+
additionalColumns: []*jsonpath.JSONPath{
362+
newJSONPath("valueOnly", "{.spec.foo[0].bar[0]}"),
363+
newJSONPath("single1", "{.spec.foo[0].bar}"),
364+
newJSONPath("single2", "{.spec.foo[1].bar}"),
365+
newJSONPath("multi", "{.spec.foo[*].bar}"),
366+
},
367+
},
368+
args: args{
369+
obj: &unstructured.Unstructured{
370+
Object: map[string]interface{}{
371+
"apiVersion": "example.istio.io/v1alpha1",
372+
"kind": "Blah",
373+
"metadata": map[string]interface{}{
374+
"name": "blah",
375+
},
376+
"spec": map[string]interface{}{
377+
"foo": []map[string]interface{}{
378+
{"bar": []int64{1}},
379+
{"bar": []int64{2, 3}},
380+
},
381+
},
382+
},
383+
},
384+
tableOptions: nil,
385+
},
386+
want: &metav1.Table{
387+
ColumnDefinitions: []metav1.TableColumnDefinition{
388+
{Name: "name", Type: "string"},
389+
{Name: "valueOnly", Type: "integer"},
390+
{Name: "single1", Type: "integer"},
391+
{Name: "single2", Type: "integer"},
392+
{Name: "multi", Type: "integer"},
393+
},
394+
Rows: []metav1.TableRow{
395+
{
396+
Cells: []interface{}{
397+
"blah",
398+
int64(1),
399+
nil, // TODO: Seems like this should either return some data or return an error, not just be nil
400+
nil, // TODO: Seems like this should either return some data or return an error, not just be nil
401+
nil, // TODO: Seems like this should either return some data or return an error, not just be nil
402+
},
403+
Object: runtime.RawExtension{
404+
Object: &unstructured.Unstructured{
405+
Object: map[string]interface{}{
406+
"apiVersion": "example.istio.io/v1alpha1",
407+
"kind": "Blah",
408+
"metadata": map[string]interface{}{
409+
"name": "blah",
410+
},
411+
"spec": map[string]interface{}{
412+
"foo": []map[string]interface{}{
413+
{"bar": []int64{1}},
414+
{"bar": []int64{2, 3}},
415+
},
416+
},
417+
},
418+
},
419+
},
420+
},
421+
},
422+
},
423+
},
204424
}
205425
for _, tt := range tests {
206426
t.Run(tt.name, func(t *testing.T) {
@@ -219,3 +439,9 @@ func Test_convertor_ConvertToTable(t *testing.T) {
219439
})
220440
}
221441
}
442+
443+
func newJSONPath(name string, jsonPathExpression string) *jsonpath.JSONPath {
444+
jp := jsonpath.New(name)
445+
_ = jp.Parse(jsonPathExpression)
446+
return jp
447+
}

0 commit comments

Comments
 (0)