github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/apis/meta/internalversion/validation/validation_test.go (about)

     1  /*
     2  Copyright 2020 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 validation
    18  
    19  import (
    20  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/meta/internalversion"
    21  	metav1 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/meta/v1"
    22  	"testing"
    23  )
    24  
    25  func TestValidateListOptions(t *testing.T) {
    26  	cases := []struct {
    27  		name        string
    28  		opts        internalversion.ListOptions
    29  		expectError string
    30  	}{
    31  		{
    32  			name: "valid-default",
    33  			opts: internalversion.ListOptions{},
    34  		},
    35  		{
    36  			name: "valid-resourceversionmatch-exact",
    37  			opts: internalversion.ListOptions{
    38  				ResourceVersion:      "1",
    39  				ResourceVersionMatch: metav1.ResourceVersionMatchExact,
    40  			},
    41  		},
    42  		{
    43  			name: "invalid-resourceversionmatch-exact",
    44  			opts: internalversion.ListOptions{
    45  				ResourceVersion:      "0",
    46  				ResourceVersionMatch: metav1.ResourceVersionMatchExact,
    47  			},
    48  			expectError: "resourceVersionMatch: Forbidden: resourceVersionMatch \"exact\" is forbidden for resourceVersion \"0\"",
    49  		},
    50  		{
    51  			name: "valid-resourceversionmatch-notolderthan",
    52  			opts: internalversion.ListOptions{
    53  				ResourceVersion:      "0",
    54  				ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
    55  			},
    56  		},
    57  		{
    58  			name: "invalid-resourceversionmatch",
    59  			opts: internalversion.ListOptions{
    60  				ResourceVersion:      "0",
    61  				ResourceVersionMatch: "foo",
    62  			},
    63  			expectError: "resourceVersionMatch: Unsupported value: \"foo\": supported values: \"Exact\", \"NotOlderThan\", \"\"",
    64  		},
    65  	}
    66  
    67  	for _, tc := range cases {
    68  		t.Run(tc.name, func(t *testing.T) {
    69  			errs := ValidateListOptions(&tc.opts)
    70  			if tc.expectError != "" {
    71  				if len(errs) != 1 {
    72  					t.Errorf("expected an error but got %d errors", len(errs))
    73  				} else if errs[0].Error() != tc.expectError {
    74  					t.Errorf("expected error '%s' but got '%s'", tc.expectError, errs[0].Error())
    75  				}
    76  				return
    77  			}
    78  			if len(errs) != 0 {
    79  				t.Errorf("expected no errors, but got: %v", errs)
    80  			}
    81  		})
    82  	}
    83  }