...

Source file src/sigs.k8s.io/cli-utils/pkg/printers/table/printer_test.go

Documentation: sigs.k8s.io/cli-utils/pkg/printers/table

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package table
     5  
     6  import (
     7  	"bytes"
     8  	"testing"
     9  
    10  	"k8s.io/cli-runtime/pkg/genericclioptions"
    11  	"sigs.k8s.io/cli-utils/pkg/apply/event"
    12  	"sigs.k8s.io/cli-utils/pkg/print/table"
    13  	"sigs.k8s.io/cli-utils/pkg/printers/printer"
    14  	printertesting "sigs.k8s.io/cli-utils/pkg/printers/testutil"
    15  )
    16  
    17  func TestActionColumnDef(t *testing.T) {
    18  	testCases := map[string]struct {
    19  		resource       table.Resource
    20  		columnWidth    int
    21  		expectedOutput string
    22  	}{
    23  		"unexpected implementation of Resource interface": {
    24  			resource:       &subResourceInfo{},
    25  			columnWidth:    15,
    26  			expectedOutput: "",
    27  		},
    28  		"neither applied nor pruned": {
    29  			resource:       &resourceInfo{},
    30  			columnWidth:    15,
    31  			expectedOutput: "Pending",
    32  		},
    33  		"applied": {
    34  			resource: &resourceInfo{
    35  				ResourceAction: event.ApplyAction,
    36  				ApplyStatus:    event.ApplySuccessful,
    37  			},
    38  			columnWidth:    15,
    39  			expectedOutput: "Successful",
    40  		},
    41  		"pruned": {
    42  			resource: &resourceInfo{
    43  				ResourceAction: event.PruneAction,
    44  				PruneStatus:    event.PruneSuccessful,
    45  			},
    46  			columnWidth:    15,
    47  			expectedOutput: "Successful",
    48  		},
    49  		"trimmed output": {
    50  			resource: &resourceInfo{
    51  				ResourceAction: event.ApplyAction,
    52  				ApplyStatus:    event.ApplySuccessful,
    53  			},
    54  			columnWidth:    5,
    55  			expectedOutput: "Succe",
    56  		},
    57  	}
    58  
    59  	for tn, tc := range testCases {
    60  		t.Run(tn, func(t *testing.T) {
    61  			var buf bytes.Buffer
    62  			_, err := actionColumnDef.PrintResource(&buf, tc.columnWidth, tc.resource)
    63  			if err != nil {
    64  				t.Error(err)
    65  			}
    66  
    67  			if want, got := tc.expectedOutput, buf.String(); want != got {
    68  				t.Errorf("expected %q, but got %q", want, got)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func TestPrint(t *testing.T) {
    75  	printertesting.PrintResultErrorTest(t, func() printer.Printer {
    76  		ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
    77  		return &Printer{
    78  			IOStreams: ioStreams,
    79  		}
    80  	})
    81  }
    82  

View as plain text