...

Source file src/k8s.io/cli-runtime/pkg/printers/managedfields_test.go

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

     1  /*
     2  Copyright 2021 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 printers
    18  
    19  import (
    20  	"io"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/require"
    24  
    25  	v1 "k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  )
    29  
    30  type testResourcePrinter func(object runtime.Object, writer io.Writer) error
    31  
    32  func (p testResourcePrinter) PrintObj(o runtime.Object, w io.Writer) error {
    33  	return p(o, w)
    34  }
    35  
    36  func TestOmitManagedFieldsPrinter(t *testing.T) {
    37  	testCases := []struct {
    38  		name     string
    39  		object   runtime.Object
    40  		expected runtime.Object
    41  	}{
    42  		{
    43  			name: "pod without managedFields",
    44  			object: &v1.Pod{
    45  				ObjectMeta: metav1.ObjectMeta{Name: "pod1"},
    46  			},
    47  			expected: &v1.Pod{
    48  				ObjectMeta: metav1.ObjectMeta{Name: "pod1"},
    49  			},
    50  		},
    51  		{
    52  			name: "pod with managedFields",
    53  			object: &v1.Pod{
    54  				ObjectMeta: metav1.ObjectMeta{
    55  					Name: "pod1",
    56  					ManagedFields: []metav1.ManagedFieldsEntry{
    57  						{Manager: "kubectl", Operation: metav1.ManagedFieldsOperationApply},
    58  					},
    59  				},
    60  			},
    61  			expected: &v1.Pod{
    62  				ObjectMeta: metav1.ObjectMeta{Name: "pod1"},
    63  			},
    64  		},
    65  		{
    66  			name: "pod list",
    67  			object: &v1.PodList{
    68  				Items: []v1.Pod{
    69  					{
    70  						ObjectMeta: metav1.ObjectMeta{
    71  							Name:          "pod1",
    72  							ManagedFields: []metav1.ManagedFieldsEntry{},
    73  						},
    74  					},
    75  					{
    76  						ObjectMeta: metav1.ObjectMeta{
    77  							Name: "pod2",
    78  							ManagedFields: []metav1.ManagedFieldsEntry{
    79  								{Manager: "kubectl", Operation: metav1.ManagedFieldsOperationApply},
    80  							},
    81  						},
    82  					},
    83  					{ObjectMeta: metav1.ObjectMeta{Name: "pod3"}},
    84  				},
    85  			},
    86  			expected: &v1.PodList{
    87  				Items: []v1.Pod{
    88  					{ObjectMeta: metav1.ObjectMeta{Name: "pod1"}},
    89  					{ObjectMeta: metav1.ObjectMeta{Name: "pod2"}},
    90  					{ObjectMeta: metav1.ObjectMeta{Name: "pod3"}},
    91  				},
    92  			},
    93  		},
    94  	}
    95  	for _, tc := range testCases {
    96  		t.Run(tc.name, func(t *testing.T) {
    97  			r := require.New(t)
    98  			delegate := func(o runtime.Object, w io.Writer) error {
    99  				r.Equal(tc.expected, o)
   100  				return nil
   101  			}
   102  			p := OmitManagedFieldsPrinter{Delegate: testResourcePrinter(delegate)}
   103  			r.NoError(p.PrintObj(tc.object, nil))
   104  		})
   105  	}
   106  }
   107  

View as plain text