...

Source file src/k8s.io/cli-runtime/pkg/genericclioptions/name_flags_test.go

Documentation: k8s.io/cli-runtime/pkg/genericclioptions

     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 genericclioptions
    18  
    19  import (
    20  	"bytes"
    21  	"strings"
    22  	"testing"
    23  
    24  	"k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  )
    27  
    28  func TestNamePrinterSupportsExpectedFormats(t *testing.T) {
    29  	testObject := &v1.Pod{
    30  		TypeMeta:   metav1.TypeMeta{APIVersion: "v1", Kind: "Pod"},
    31  		ObjectMeta: metav1.ObjectMeta{Name: "foo"},
    32  	}
    33  
    34  	testCases := []struct {
    35  		name           string
    36  		outputFormat   string
    37  		operation      string
    38  		dryRun         bool
    39  		expectedError  string
    40  		expectedOutput string
    41  		expectNoMatch  bool
    42  	}{
    43  		{
    44  			name:           "valid \"name\" output format with no operation prints resource name",
    45  			outputFormat:   "name",
    46  			expectedOutput: "pod/foo",
    47  		},
    48  		{
    49  			name:           "valid \"name\" output format and an operation results in a short-output (non success printer) message",
    50  			outputFormat:   "name",
    51  			operation:      "patched",
    52  			expectedOutput: "pod/foo",
    53  		},
    54  		{
    55  			name:          "operation and no valid \"name\" output does not match a printer",
    56  			operation:     "patched",
    57  			outputFormat:  "invalid",
    58  			dryRun:        true,
    59  			expectNoMatch: true,
    60  		},
    61  		{
    62  			name:           "operation and empty output still matches name printer",
    63  			expectedOutput: "pod/foo patched",
    64  			operation:      "patched",
    65  		},
    66  		{
    67  			name:          "no printer is matched on an invalid outputFormat",
    68  			outputFormat:  "invalid",
    69  			expectNoMatch: true,
    70  		},
    71  		{
    72  			name:          "printer should not match on any other format supported by another printer",
    73  			outputFormat:  "go-template",
    74  			expectNoMatch: true,
    75  		},
    76  	}
    77  
    78  	for _, tc := range testCases {
    79  		t.Run(tc.name, func(t *testing.T) {
    80  			printFlags := NamePrintFlags{
    81  				Operation: tc.operation,
    82  			}
    83  
    84  			p, err := printFlags.ToPrinter(tc.outputFormat)
    85  			if tc.expectNoMatch {
    86  				if !IsNoCompatiblePrinterError(err) {
    87  					t.Fatalf("expected no printer matches for output format %q", tc.outputFormat)
    88  				}
    89  				return
    90  			}
    91  			if IsNoCompatiblePrinterError(err) {
    92  				t.Fatalf("expected to match name printer for output format %q", tc.outputFormat)
    93  			}
    94  
    95  			if len(tc.expectedError) > 0 {
    96  				if err == nil || !strings.Contains(err.Error(), tc.expectedError) {
    97  					t.Errorf("expecting error %q, got %v", tc.expectedError, err)
    98  				}
    99  				return
   100  			}
   101  			if err != nil {
   102  				t.Fatalf("unexpected error: %v", err)
   103  			}
   104  
   105  			out := bytes.NewBuffer([]byte{})
   106  			err = p.PrintObj(testObject, out)
   107  			if err != nil {
   108  				t.Errorf("unexpected error: %v", err)
   109  			}
   110  
   111  			if !strings.Contains(out.String(), tc.expectedOutput) {
   112  				t.Errorf("unexpected output: expecting %q, got %q", tc.expectedOutput, out.String())
   113  			}
   114  		})
   115  	}
   116  }
   117  

View as plain text