1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
|
/*
Copyright 2019 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"
"regexp"
"testing"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
var testPodName = "test-pod-name"
var testPodNamespace = "test-namespace"
var testPod = &corev1.Pod{
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Pod"},
ObjectMeta: metav1.ObjectMeta{
Name: testPodName,
Namespace: testPodNamespace,
Labels: map[string]string{
"first-label": "12",
"second-label": "label-value",
},
},
}
var testStatus = &metav1.Status{
TypeMeta: metav1.TypeMeta{APIVersion: "metav1", Kind: "Status"},
Status: "Failure",
Message: "test-status-message",
Reason: "test-status-reason",
}
func TestPrintTable_MissingColumnsRows(t *testing.T) {
tests := []struct {
columns []metav1.TableColumnDefinition
rows []metav1.TableRow
options PrintOptions
expected string
}{
// No columns and no rows means table string is empty.
{
columns: []metav1.TableColumnDefinition{},
rows: []metav1.TableRow{},
options: PrintOptions{},
expected: "",
},
// No rows, means table string is empty (columns aren't printed).
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer"},
{Name: "Age", Type: "string"},
},
rows: []metav1.TableRow{},
options: PrintOptions{},
expected: "",
},
}
for _, test := range tests {
// Create the table from the columns and rows.
table := &metav1.Table{
ColumnDefinitions: test.columns,
Rows: test.rows,
}
// Print the table
out := bytes.NewBuffer([]byte{})
printer := NewTablePrinter(test.options)
printer.PrintObj(table, out)
// Validate the printed table is empty.
if len(out.String()) > 0 {
t.Errorf("Error Printing Table. Should be empty; got (%s)", out.String())
}
}
}
func TestPrintTable_ColumnPriority(t *testing.T) {
tests := []struct {
columns []metav1.TableColumnDefinition
rows []metav1.TableRow
options PrintOptions
expected string
}{
// Test a basic single row table. Columns with priority > 0 are not printed.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer", Priority: 1}, // Priority > 0
{Name: "Age", Type: "string", Priority: 1}, // Priority > 0
},
rows: []metav1.TableRow{
{Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"}},
},
options: PrintOptions{},
expected: "NAME READY STATUS\ntest1 1/1 podPhase\n",
},
// Test a basic multi-row table row table. Columns with priority > 0 are not printed.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer", Priority: 1}, // Priority > 0
{Name: "Age", Type: "string", Priority: 1}, // Priority > 0
},
rows: []metav1.TableRow{
{Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"}},
{Cells: []interface{}{"test2", "1/2", "podPhase", int64(30), "21h"}},
{Cells: []interface{}{"test3", "4/4", "podPhase", int64(1), "22h"}},
},
options: PrintOptions{},
expected: `NAME READY STATUS
test1 1/1 podPhase
test2 1/2 podPhase
test3 4/4 podPhase
`,
},
// Test a single row table with "wide" printing option. Columns with priority > 0 are printed.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer", Priority: 1},
{Name: "Age", Type: "string", Priority: 1},
},
rows: []metav1.TableRow{
{Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"}},
},
// Print with no headers.
options: PrintOptions{Wide: true},
expected: "NAME READY STATUS RETRIES AGE\ntest1 1/1 podPhase 5 20h\n",
},
// Test a multi-row table row table with "wide" printing option. Columns with priority > 0 are printed.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer", Priority: 1},
{Name: "Age", Type: "string", Priority: 1},
},
rows: []metav1.TableRow{
{Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"}},
{Cells: []interface{}{"test2", "1/2", "podPhase", int64(30), "21h"}},
{Cells: []interface{}{"test3", "4/4", "podPhase", int64(1), "22h"}},
},
options: PrintOptions{Wide: true},
expected: `NAME READY STATUS RETRIES AGE
test1 1/1 podPhase 5 20h
test2 1/2 podPhase 30 21h
test3 4/4 podPhase 1 22h
`,
},
}
for _, test := range tests {
// Create the table from the columns and rows.
table := &metav1.Table{
ColumnDefinitions: test.columns,
Rows: test.rows,
}
// Print the table
out := bytes.NewBuffer([]byte{})
printer := NewTablePrinter(test.options)
printer.PrintObj(table, out)
// Validate the expected output matches the printed table.
if test.expected != out.String() {
t.Errorf("Table printing error: expected (%s), got (%s)", test.expected, out.String())
}
}
}
func TestPrintTable_ColumnHeaders(t *testing.T) {
tests := []struct {
columns []metav1.TableColumnDefinition
rows []metav1.TableRow
options PrintOptions
expected string
}{
// Test a basic single row table, shows columns.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer"},
{Name: "Age", Type: "string"},
},
rows: []metav1.TableRow{
{Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"}},
},
options: PrintOptions{},
expected: "NAME READY STATUS RETRIES AGE\ntest1 1/1 podPhase 5 20h\n",
},
// Test a basic multi-row table row table, shows columns.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer"},
{Name: "Age", Type: "string"},
},
rows: []metav1.TableRow{
{Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"}},
{Cells: []interface{}{"test2", "1/2", "podPhase", int64(30), "21h"}},
{Cells: []interface{}{"test3", "4/4", "podPhase", int64(1), "22h"}},
},
options: PrintOptions{},
expected: `NAME READY STATUS RETRIES AGE
test1 1/1 podPhase 5 20h
test2 1/2 podPhase 30 21h
test3 4/4 podPhase 1 22h
`,
},
// Test a single row table with "NoHeaders" option, doesn't print columns.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer"},
{Name: "Age", Type: "string"},
},
rows: []metav1.TableRow{
{Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"}},
},
// Print with no headers.
options: PrintOptions{NoHeaders: true},
expected: "test1 1/1 podPhase 5 20h\n",
},
// Test a multi-row table row table with "NoHeaders" option, doesn't print columns.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer"},
{Name: "Age", Type: "string"},
},
rows: []metav1.TableRow{
{Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"}},
{Cells: []interface{}{"test2", "1/2", "podPhase", int64(30), "21h"}},
{Cells: []interface{}{"test3", "4/4", "podPhase", int64(1), "22h"}},
},
options: PrintOptions{NoHeaders: true},
expected: `test1 1/1 podPhase 5 20h
test2 1/2 podPhase 30 21h
test3 4/4 podPhase 1 22h
`,
},
}
for _, test := range tests {
// Create the table from the columns and rows.
table := &metav1.Table{
ColumnDefinitions: test.columns,
Rows: test.rows,
}
// Print the table
out := bytes.NewBuffer([]byte{})
printer := NewTablePrinter(test.options)
printer.PrintObj(table, out)
// Validate the expected output matches the printed table.
if test.expected != out.String() {
t.Errorf("Table printing error: expected (%s), got (%s)", test.expected, out.String())
}
}
}
func TestPrintTable_WithNamespace(t *testing.T) {
tests := []struct {
columns []metav1.TableColumnDefinition
rows []metav1.TableRow
options PrintOptions
expected string
}{
// Test a single row table "WithNamespace" option, prepends NAMESPACE column.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer"},
{Name: "Age", Type: "string"},
},
rows: []metav1.TableRow{
{
Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"},
Object: runtime.RawExtension{Object: testPod},
},
},
// Print with namespace column prepended.
options: PrintOptions{WithNamespace: true},
expected: `NAMESPACE NAME READY STATUS RETRIES AGE
test-namespace test1 1/1 podPhase 5 20h
`,
},
}
for _, test := range tests {
// Create the table from the columns and rows.
table := &metav1.Table{
ColumnDefinitions: test.columns,
Rows: test.rows,
}
// Print the table
out := bytes.NewBuffer([]byte{})
printer := NewTablePrinter(test.options)
printer.PrintObj(table, out)
// Validate the expected output matches the printed table.
if test.expected != out.String() {
t.Errorf("Table printing error: expected (%s), got (%s)", test.expected, out.String())
}
}
}
func TestPrintTable_WithKind(t *testing.T) {
tests := []struct {
columns []metav1.TableColumnDefinition
rows []metav1.TableRow
options PrintOptions
expected string
}{
// Test a single row table "WithKind" option, prepends "pod" to name.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer"},
{Name: "Age", Type: "string"},
},
rows: []metav1.TableRow{
{
Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"},
},
},
// Print with Kind "pod" prepended to name.
options: PrintOptions{WithKind: true, Kind: schema.GroupKind{Kind: "Pod"}},
expected: `NAME READY STATUS RETRIES AGE
pod/test1 1/1 podPhase 5 20h
`,
},
}
for _, test := range tests {
// Create the table from the columns and rows.
table := &metav1.Table{
ColumnDefinitions: test.columns,
Rows: test.rows,
}
// Print the table
out := bytes.NewBuffer([]byte{})
printer := NewTablePrinter(test.options)
printer.PrintObj(table, out)
// Validate the expected output matches the printed table.
if test.expected != out.String() {
t.Errorf("Table printing error: expected (%s), got (%s)", test.expected, out.String())
}
}
}
func TestPrintTable_WithLabels(t *testing.T) {
tests := []struct {
columns []metav1.TableColumnDefinition
rows []metav1.TableRow
options PrintOptions
expected string
}{
// Test a table "ShowLabels" option, appends labels as columns.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer"},
{Name: "Age", Type: "string"},
},
rows: []metav1.TableRow{
{
Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"},
Object: runtime.RawExtension{Object: testPod},
},
},
options: PrintOptions{ShowLabels: true},
expected: `NAME READY STATUS RETRIES AGE LABELS
test1 1/1 podPhase 5 20h first-label=12,second-label=label-value
`,
},
// Test a table "ColumnLabels" option, appends labels as columns.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer"},
{Name: "Age", Type: "string"},
},
rows: []metav1.TableRow{
{
Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"},
Object: runtime.RawExtension{Object: testPod},
},
},
// Print "second-label" as column name, with label value.
options: PrintOptions{ColumnLabels: []string{"second-label"}},
expected: `NAME READY STATUS RETRIES AGE SECOND-LABEL
test1 1/1 podPhase 5 20h label-value
`,
},
// Test a table "ColumnLabels" option, appends labels as columns.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer"},
{Name: "Age", Type: "string"},
},
rows: []metav1.TableRow{
{
Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"},
Object: runtime.RawExtension{Object: testPod},
},
},
// Print multiple-labels as columns, with their values.
options: PrintOptions{ColumnLabels: []string{"first-label", "second-label"}},
expected: `NAME READY STATUS RETRIES AGE FIRST-LABEL SECOND-LABEL
test1 1/1 podPhase 5 20h 12 label-value
`,
},
}
for _, test := range tests {
// Create the table from the columns and rows.
table := &metav1.Table{
ColumnDefinitions: test.columns,
Rows: test.rows,
}
// Print the table
out := bytes.NewBuffer([]byte{})
printer := NewTablePrinter(test.options)
printer.PrintObj(table, out)
// Validate the expected output matches the printed table.
if test.expected != out.String() {
t.Errorf("Table printing error: expected (%s), got (%s)", test.expected, out.String())
}
}
}
func TestPrintTable_NonTable(t *testing.T) {
tests := []struct {
object runtime.Object
options PrintOptions
expected string
}{
// Test non-table default printing for a pod.
{
object: testPod,
options: PrintOptions{},
expected: "NAME AGE\ntest-pod-name <unknown>\n",
},
// Test non-table default printing for a pod with "NoHeaders" option.
{
object: testPod,
options: PrintOptions{NoHeaders: true},
expected: "test-pod-name <unknown>\n",
},
// Test non-table default printing for a pod with "WithNamespace" option.
{
object: testPod,
options: PrintOptions{WithNamespace: true},
expected: "NAMESPACE NAME AGE\ntest-namespace test-pod-name <unknown>\n",
},
// Test non-table default printing for a pod with "ShowLabels" option.
{
object: testPod,
options: PrintOptions{ShowLabels: true},
expected: `NAME AGE LABELS
test-pod-name <unknown> first-label=12,second-label=label-value
`,
},
// Test non-table default printing for a Status resource.
{
object: testStatus,
options: PrintOptions{},
expected: `STATUS REASON MESSAGE
Failure test-status-reason test-status-message
`,
},
// Test non-table default printing for a Status resource with NoHeaders options.
{
object: testStatus,
options: PrintOptions{NoHeaders: true},
expected: "Failure test-status-reason test-status-message\n",
},
}
for _, test := range tests {
// Print the table
out := bytes.NewBuffer([]byte{})
printer := NewTablePrinter(test.options)
printer.PrintObj(test.object, out)
// Validate the expected output matches the printed table.
if test.expected != out.String() {
t.Errorf("Table printing error: expected (%s), got (%s)", test.expected, out.String())
}
}
}
func TestPrintTable_WatchEvents(t *testing.T) {
tests := []struct {
columns []metav1.TableColumnDefinition
rows []metav1.TableRow
options PrintOptions
expected string
}{
// Print WatchEvent with Table resource.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer"},
{Name: "Age", Type: "string"},
},
rows: []metav1.TableRow{
{
Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"},
Object: runtime.RawExtension{Object: testPod},
},
},
options: PrintOptions{},
expected: `EVENT NAME READY STATUS RETRIES AGE
Added test1 1/1 podPhase 5 20h
`,
},
// Print WatchEvent with Table, and "NoHeaders", "WithNamespace", and "ShowLabels" options.
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Ready", Type: "string"},
{Name: "Status", Type: "string"},
{Name: "Retries", Type: "integer"},
{Name: "Age", Type: "string"},
},
rows: []metav1.TableRow{
{
Cells: []interface{}{"test1", "1/1", "podPhase", int64(5), "20h"},
Object: runtime.RawExtension{Object: testPod},
},
},
options: PrintOptions{NoHeaders: true, WithNamespace: true, ShowLabels: true},
expected: "Added test-namespace test1 1/1 podPhase 5 20h first-label=12,second-label=label-value\n",
},
}
for _, test := range tests {
// Create the table from the columns and rows.
table := &metav1.Table{
ColumnDefinitions: test.columns,
Rows: test.rows,
}
// Add the table to the WatchEvent.
event := &metav1.WatchEvent{
Type: "Added",
Object: runtime.RawExtension{Object: table},
}
// Print the event
out := bytes.NewBuffer([]byte{})
printer := NewTablePrinter(test.options)
printer.PrintObj(event, out)
// Validate the expected output matches the printed table.
if test.expected != out.String() {
t.Errorf("Event/Table printing error: expected (%s), got (%s)", test.expected, out.String())
}
}
}
func TestPrintUnstructuredObject(t *testing.T) {
obj := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Test",
"dummy1": "present",
"dummy2": "present",
"metadata": map[string]interface{}{
"name": "MyName",
"namespace": "MyNamespace",
"creationTimestamp": "2017-04-01T00:00:00Z",
"resourceVersion": 123,
"uid": "00000000-0000-0000-0000-000000000001",
"dummy3": "present",
"labels": map[string]interface{}{"test": "other"},
},
/*"items": []interface{}{
map[string]interface{}{
"itemBool": true,
"itemInt": 42,
},
},*/
"url": "http://localhost",
"status": "ok",
},
}
tests := []struct {
expected string
options PrintOptions
object runtime.Object
}{
{
expected: "NAME\\s+AGE\nMyName\\s+\\d+",
object: obj,
},
{
options: PrintOptions{
WithNamespace: true,
},
expected: "NAMESPACE\\s+NAME\\s+AGE\nMyNamespace\\s+MyName\\s+\\d+",
object: obj,
},
{
options: PrintOptions{
ShowLabels: true,
WithNamespace: true,
},
expected: "NAMESPACE\\s+NAME\\s+AGE\\s+LABELS\nMyNamespace\\s+MyName\\s+\\d+\\w+\\s+test\\=other",
object: obj,
},
{
expected: "NAME\\s+AGE\nMyName\\s+\\d+\\w+\nMyName2\\s+\\d+",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Test",
"dummy1": "present",
"dummy2": "present",
"items": []interface{}{
map[string]interface{}{
"metadata": map[string]interface{}{
"name": "MyName",
"namespace": "MyNamespace",
"creationTimestamp": "2017-04-01T00:00:00Z",
"resourceVersion": 123,
"uid": "00000000-0000-0000-0000-000000000001",
"dummy3": "present",
"labels": map[string]interface{}{"test": "other"},
},
},
map[string]interface{}{
"metadata": map[string]interface{}{
"name": "MyName2",
"namespace": "MyNamespace",
"creationTimestamp": "2017-04-01T00:00:00Z",
"resourceVersion": 123,
"uid": "00000000-0000-0000-0000-000000000001",
"dummy3": "present",
"labels": "badlabel",
},
},
},
"url": "http://localhost",
"status": "ok",
},
},
},
}
out := bytes.NewBuffer([]byte{})
for _, test := range tests {
out.Reset()
printer := NewTablePrinter(test.options)
printer.PrintObj(test.object, out)
matches, err := regexp.MatchString(test.expected, out.String())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !matches {
t.Errorf("wanted:\n%s\ngot:\n%s", test.expected, out)
}
}
}
type TestUnknownType struct{}
func (obj *TestUnknownType) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (obj *TestUnknownType) DeepCopyObject() runtime.Object {
if obj == nil {
return nil
}
clone := *obj
return &clone
}
func TestUnknownTypePrinting(t *testing.T) {
printer := NewTablePrinter(PrintOptions{})
buffer := &bytes.Buffer{}
err := printer.PrintObj(&TestUnknownType{}, buffer)
if err == nil {
t.Errorf("An error was expected from printing unknown type")
}
}
func TestStringPrinting(t *testing.T) {
tests := []struct {
columns []metav1.TableColumnDefinition
rows []metav1.TableRow
expected string
}{
// multiline string
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Age", Type: "string"},
{Name: "Description", Type: "string"},
},
rows: []metav1.TableRow{
{Cells: []interface{}{"test1", "20h", "This is first line\nThis is second line\nThis is third line\nand another one\n"}},
},
expected: `NAME AGE DESCRIPTION
test1 20h This is first line...
`,
},
// lengthy string
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Age", Type: "string"},
{Name: "Description", Type: "string"},
},
rows: []metav1.TableRow{
{Cells: []interface{}{"test1", "20h", "This is first line which is long and goes for on and on and on an on and on and on and on and on and on and on and on and on and on and on"}},
},
expected: `NAME AGE DESCRIPTION
test1 20h This is first line which is long and goes for on and on and on an on and on and on and on and on and on and on and on and on and on and on
`,
},
// lengthy string + newline
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
{Name: "Age", Type: "string"},
{Name: "Description", Type: "string"},
},
rows: []metav1.TableRow{
{Cells: []interface{}{"test1", "20h", "This is first\n line which is long and goes for on and on and on an on and on and on and on and on and on and on and on and on and on and on"}},
},
expected: `NAME AGE DESCRIPTION
test1 20h This is first...
`,
},
// terminal special character, should be escaped
{
columns: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
},
rows: []metav1.TableRow{
{Cells: []interface{}{"test1\x1b"}},
},
expected: `NAME
test1^[
`,
},
}
for _, test := range tests {
// Create the table from the columns and rows.
table := &metav1.Table{
ColumnDefinitions: test.columns,
Rows: test.rows,
}
// Print the table
out := bytes.NewBuffer([]byte{})
printer := NewTablePrinter(PrintOptions{})
printer.PrintObj(table, out)
if test.expected != out.String() {
t.Errorf("Table printing error: expected \n(%s), got \n(%s)", test.expected, out.String())
}
}
}
|