...

Source file src/k8s.io/cli-runtime/pkg/genericclioptions/json_yaml_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 TestPrinterSupportsExpectedJSONYamlFormats(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  		expectedOutput string
    38  		expectNoMatch  bool
    39  	}{
    40  		{
    41  			name:           "json output format matches a json printer",
    42  			outputFormat:   "json",
    43  			expectedOutput: "\"name\": \"foo\"",
    44  		},
    45  		{
    46  			name:           "yaml output format matches a yaml printer",
    47  			outputFormat:   "yaml",
    48  			expectedOutput: "name: foo",
    49  		},
    50  		{
    51  			name:          "output format for another printer does not match a json/yaml printer",
    52  			outputFormat:  "jsonpath",
    53  			expectNoMatch: true,
    54  		},
    55  		{
    56  			name:          "invalid output format results in no match",
    57  			outputFormat:  "invalid",
    58  			expectNoMatch: true,
    59  		},
    60  	}
    61  
    62  	for _, tc := range testCases {
    63  		t.Run(tc.name, func(t *testing.T) {
    64  			printFlags := JSONYamlPrintFlags{}
    65  
    66  			p, err := printFlags.ToPrinter(tc.outputFormat)
    67  			if tc.expectNoMatch {
    68  				if !IsNoCompatiblePrinterError(err) {
    69  					t.Fatalf("expected no printer matches for output format %q", tc.outputFormat)
    70  				}
    71  				return
    72  			}
    73  			if IsNoCompatiblePrinterError(err) {
    74  				t.Fatalf("expected to match template printer for output format %q", tc.outputFormat)
    75  			}
    76  			if err != nil {
    77  				t.Fatalf("unexpected error: %v", err)
    78  			}
    79  
    80  			out := bytes.NewBuffer([]byte{})
    81  			err = p.PrintObj(testObject, out)
    82  			if err != nil {
    83  				t.Errorf("unexpected error: %v", err)
    84  			}
    85  
    86  			if !strings.Contains(out.String(), tc.expectedOutput) {
    87  				t.Errorf("unexpected output: expecting %q, got %q", tc.expectedOutput, out.String())
    88  			}
    89  		})
    90  	}
    91  }
    92  

View as plain text