...

Source file src/k8s.io/kubectl/pkg/cmd/get/customcolumn_flags_test.go

Documentation: k8s.io/kubectl/pkg/cmd/get

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package get
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"os"
    23  	"strings"
    24  	"testing"
    25  
    26  	corev1 "k8s.io/api/core/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/cli-runtime/pkg/genericclioptions"
    29  )
    30  
    31  func TestPrinterSupportsExpectedCustomColumnFormats(t *testing.T) {
    32  	testObject := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
    33  
    34  	customColumnsFile, err := os.CreateTemp(os.TempDir(), "printers_jsonpath_flags")
    35  	if err != nil {
    36  		t.Fatalf("unexpected error: %v", err)
    37  	}
    38  	defer func(tempFile *os.File) {
    39  		tempFile.Close()
    40  		os.Remove(tempFile.Name())
    41  	}(customColumnsFile)
    42  
    43  	fmt.Fprintf(customColumnsFile, "NAME\n.metadata.name")
    44  
    45  	testCases := []struct {
    46  		name               string
    47  		outputFormat       string
    48  		templateArg        string
    49  		expectedError      string
    50  		expectedParseError string
    51  		expectedOutput     string
    52  		expectNoMatch      bool
    53  	}{
    54  		{
    55  			name:           "valid output format also containing the custom-columns argument succeeds",
    56  			outputFormat:   "custom-columns=NAME:.metadata.name",
    57  			expectedOutput: "foo",
    58  		},
    59  		{
    60  			name:          "valid output format and no --template argument results in an error",
    61  			outputFormat:  "custom-columns",
    62  			expectedError: "custom-columns format specified but no custom columns given",
    63  		},
    64  		{
    65  			name:           "valid output format and --template argument succeeds",
    66  			outputFormat:   "custom-columns",
    67  			templateArg:    "NAME:.metadata.name",
    68  			expectedOutput: "foo",
    69  		},
    70  		{
    71  			name:           "custom-columns template file should match, and successfully return correct value",
    72  			outputFormat:   "custom-columns-file",
    73  			templateArg:    customColumnsFile.Name(),
    74  			expectedOutput: "foo",
    75  		},
    76  		{
    77  			name:          "valid output format and invalid --template argument results in a parsing error from the printer",
    78  			outputFormat:  "custom-columns",
    79  			templateArg:   "invalid",
    80  			expectedError: "unexpected custom-columns spec: invalid, expected <header>:<json-path-expr>",
    81  		},
    82  		{
    83  			name:          "no printer is matched on an invalid outputFormat",
    84  			outputFormat:  "invalid",
    85  			expectNoMatch: true,
    86  		},
    87  		{
    88  			name:          "custom-columns printer should not match on any other format supported by another printer",
    89  			outputFormat:  "go-template",
    90  			expectNoMatch: true,
    91  		},
    92  	}
    93  
    94  	for _, tc := range testCases {
    95  		t.Run(tc.name, func(t *testing.T) {
    96  			printFlags := CustomColumnsPrintFlags{
    97  				TemplateArgument: tc.templateArg,
    98  			}
    99  
   100  			p, err := printFlags.ToPrinter(tc.outputFormat)
   101  			if tc.expectNoMatch {
   102  				if !genericclioptions.IsNoCompatiblePrinterError(err) {
   103  					t.Fatalf("expected no printer matches for output format %q", tc.outputFormat)
   104  				}
   105  				return
   106  			}
   107  			if genericclioptions.IsNoCompatiblePrinterError(err) {
   108  				t.Fatalf("expected to match template printer for output format %q", tc.outputFormat)
   109  			}
   110  
   111  			if len(tc.expectedError) > 0 {
   112  				if err == nil || !strings.Contains(err.Error(), tc.expectedError) {
   113  					t.Errorf("expecting error %q, got %v", tc.expectedError, err)
   114  				}
   115  				return
   116  			}
   117  			if err != nil {
   118  				t.Fatalf("unexpected error: %v", err)
   119  			}
   120  
   121  			out := bytes.NewBuffer([]byte{})
   122  			err = p.PrintObj(testObject, out)
   123  			if len(tc.expectedParseError) > 0 {
   124  				if err == nil || !strings.Contains(err.Error(), tc.expectedParseError) {
   125  					t.Errorf("expecting error %q, got %v", tc.expectedError, err)
   126  				}
   127  				return
   128  			}
   129  			if err != nil {
   130  				t.Errorf("unexpected error: %v", err)
   131  			}
   132  
   133  			if !strings.Contains(out.String(), tc.expectedOutput) {
   134  				t.Errorf("unexpected output: expecting %q, got %q", tc.expectedOutput, out.String())
   135  			}
   136  		})
   137  	}
   138  }
   139  

View as plain text