...

Source file src/k8s.io/kubernetes/pkg/kubelet/cm/admission/errors_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/cm/admission

     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 admission
    18  
    19  import (
    20  	"errors"
    21  	"testing"
    22  )
    23  
    24  type TestAdmissionError struct {
    25  	message string
    26  	reason  string
    27  }
    28  
    29  func (e *TestAdmissionError) Error() string {
    30  	return e.message
    31  }
    32  
    33  func (e *TestAdmissionError) Type() string {
    34  	return e.reason
    35  }
    36  
    37  func TestAdmissionErrors(t *testing.T) {
    38  	testCases := []struct {
    39  		Error                  error
    40  		expectedAdmissionError bool
    41  	}{
    42  		{
    43  			nil,
    44  			false,
    45  		},
    46  		{
    47  			errors.New("Not an AdmissionError error"),
    48  			false,
    49  		},
    50  		{
    51  			&TestAdmissionError{
    52  				"Is an AdmissionError error",
    53  				"TestAdmissionError",
    54  			},
    55  			true,
    56  		},
    57  	}
    58  
    59  	for _, tc := range testCases {
    60  		h := GetPodAdmitResult(tc.Error)
    61  		if tc.Error == nil {
    62  			if !h.Admit {
    63  				t.Errorf("expected PodAdmitResult.Admit = true")
    64  			}
    65  			continue
    66  		}
    67  
    68  		if h.Admit {
    69  			t.Errorf("expected PodAdmitResult.Admit = false")
    70  		}
    71  
    72  		if tc.expectedAdmissionError {
    73  			err, ok := tc.Error.(*TestAdmissionError)
    74  			if !ok {
    75  				t.Errorf("expected TestAdmissionError")
    76  			}
    77  			if h.Reason != err.reason {
    78  				t.Errorf("expected PodAdmitResult.Reason = %v, got %v", err.reason, h.Reason)
    79  			}
    80  			continue
    81  		}
    82  
    83  		if h.Reason != ErrorReasonUnexpected {
    84  			t.Errorf("expected PodAdmitResult.Reason = %v, got %v", ErrorReasonUnexpected, h.Reason)
    85  		}
    86  	}
    87  }
    88  

View as plain text