...

Source file src/k8s.io/kubectl/pkg/util/prune/prune_test.go

Documentation: k8s.io/kubectl/pkg/util/prune

     1  /*
     2  Copyright 2017 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 prune
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"k8s.io/apimachinery/pkg/api/meta"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  )
    27  
    28  type testRESTMapper struct {
    29  	meta.RESTMapper
    30  	scope meta.RESTScope
    31  }
    32  
    33  func (m *testRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
    34  	return &meta.RESTMapping{
    35  		Resource: schema.GroupVersionResource{
    36  			Group:    gk.Group,
    37  			Version:  "",
    38  			Resource: "",
    39  		},
    40  		GroupVersionKind: schema.GroupVersionKind{
    41  			Group:   gk.Group,
    42  			Version: "",
    43  			Kind:    gk.Kind,
    44  		},
    45  		Scope: m.scope,
    46  	}, nil
    47  }
    48  
    49  func TestGetRESTMappings(t *testing.T) {
    50  	tests := []struct {
    51  		mapper             *testRESTMapper
    52  		pr                 []Resource
    53  		namespaceSpecified bool
    54  		expectedns         int
    55  		expectednns        int
    56  		expectederr        error
    57  	}{
    58  		{
    59  			mapper:             &testRESTMapper{},
    60  			pr:                 []Resource{},
    61  			namespaceSpecified: false,
    62  			expectedns:         14,
    63  			expectednns:        2,
    64  			expectederr:        nil,
    65  		},
    66  		{
    67  			mapper:             &testRESTMapper{},
    68  			pr:                 []Resource{},
    69  			namespaceSpecified: true,
    70  			expectedns:         14,
    71  			// it will be 0 non-namespaced resources after the deprecation period has passed.
    72  			// for details, refer to https://github.com/kubernetes/kubernetes/pull/110907/.
    73  			expectednns: 2,
    74  			expectederr: nil,
    75  		},
    76  		{
    77  			mapper: &testRESTMapper{},
    78  			pr: []Resource{
    79  				{"apps", "v1", "DaemonSet", true},
    80  				{"core", "v1", "Pod", true},
    81  				{"", "v1", "Foo2", false},
    82  				{"foo", "v1", "Foo3", false},
    83  			},
    84  			namespaceSpecified: false,
    85  			expectedns:         2,
    86  			expectednns:        2,
    87  			expectederr:        nil,
    88  		},
    89  	}
    90  
    91  	for _, tc := range tests {
    92  		actualns, actualnns, actualerr := GetRESTMappings(tc.mapper, tc.pr, tc.namespaceSpecified)
    93  		if tc.expectederr != nil {
    94  			assert.NotEmptyf(t, actualerr, "getRESTMappings error expected but not fired")
    95  		}
    96  		assert.Equal(t, len(actualns), tc.expectedns, "getRESTMappings failed expected number namespaced %d actual %d", tc.expectedns, len(actualns))
    97  		assert.Equal(t, len(actualnns), tc.expectednns, "getRESTMappings failed expected number nonnamespaced %d actual %d", tc.expectednns, len(actualnns))
    98  	}
    99  }
   100  
   101  func TestParsePruneResources(t *testing.T) {
   102  	tests := []struct {
   103  		mapper   *testRESTMapper
   104  		gvks     []string
   105  		expected []Resource
   106  		err      bool
   107  	}{
   108  		{
   109  			mapper: &testRESTMapper{
   110  				scope: meta.RESTScopeNamespace,
   111  			},
   112  			gvks:     nil,
   113  			expected: []Resource{},
   114  			err:      false,
   115  		},
   116  		{
   117  			mapper: &testRESTMapper{
   118  				scope: meta.RESTScopeNamespace,
   119  			},
   120  			gvks:     []string{"group/kind/version/test"},
   121  			expected: []Resource{},
   122  			err:      true,
   123  		},
   124  		{
   125  			mapper: &testRESTMapper{
   126  				scope: meta.RESTScopeNamespace,
   127  			},
   128  			gvks:     []string{"group/kind/version"},
   129  			expected: []Resource{{group: "group", version: "kind", kind: "version", namespaced: true}},
   130  			err:      false,
   131  		},
   132  		{
   133  			mapper: &testRESTMapper{
   134  				scope: meta.RESTScopeRoot,
   135  			},
   136  			gvks:     []string{"group/kind/version"},
   137  			expected: []Resource{{group: "group", version: "kind", kind: "version", namespaced: false}},
   138  			err:      false,
   139  		},
   140  	}
   141  
   142  	for _, tc := range tests {
   143  		actual, err := ParseResources(tc.mapper, tc.gvks)
   144  		if tc.err {
   145  			assert.NotEmptyf(t, err, "parsePruneResources error expected but not fired")
   146  		} else {
   147  			assert.Equal(t, actual, tc.expected, "parsePruneResources failed expected %v actual %v", tc.expected, actual)
   148  		}
   149  	}
   150  }
   151  

View as plain text