...

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

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

     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 printers
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestIllegalPackageSourceChecker(t *testing.T) {
    24  	disallowedPrefixes := []string{
    25  		"foo/bar",
    26  		"k8s.io/foo/bar/vendor/k8s.io/baz/buz",
    27  		"bar/foo/baz",
    28  	}
    29  
    30  	testCases := []struct {
    31  		name            string
    32  		pkgPath         string
    33  		shouldBeAllowed bool
    34  	}{
    35  		{
    36  			name:            "package path beginning with forbidden prefix is rejected",
    37  			pkgPath:         "foo/bar/baz/buz",
    38  			shouldBeAllowed: false,
    39  		},
    40  		{
    41  			name:            "package path not fully matching forbidden prefix is allowed",
    42  			pkgPath:         "bar/foo",
    43  			shouldBeAllowed: true,
    44  		},
    45  		{
    46  			name:            "package path containing forbidden prefix (not as prefix) is allowed",
    47  			pkgPath:         "k8s.io/bar/foo/baz/etc",
    48  			shouldBeAllowed: true,
    49  		},
    50  	}
    51  
    52  	checker := &illegalPackageSourceChecker{disallowedPrefixes}
    53  
    54  	for _, tc := range testCases {
    55  		if checker.IsForbidden(tc.pkgPath) {
    56  			if tc.shouldBeAllowed {
    57  				t.Fatalf("expected package path %q to have been allowed", tc.pkgPath)
    58  			}
    59  			continue
    60  		}
    61  
    62  		if !tc.shouldBeAllowed {
    63  			t.Fatalf("expected package path %q to have been rejected", tc.pkgPath)
    64  		}
    65  	}
    66  }
    67  

View as plain text