...

Source file src/k8s.io/apiextensions-apiserver/pkg/controller/apiapproval/apiapproval_controller_test.go

Documentation: k8s.io/apiextensions-apiserver/pkg/controller/apiapproval

     1  /*
     2  Copyright 2019 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 apiapproval
    18  
    19  import (
    20  	"testing"
    21  
    22  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  func TestCalculateCondition(t *testing.T) {
    27  	noConditionFn := func(t *testing.T, condition *apiextensionsv1.CustomResourceDefinitionCondition) {
    28  		t.Helper()
    29  		if condition != nil {
    30  			t.Fatal(condition)
    31  		}
    32  	}
    33  
    34  	verifyCondition := func(status apiextensionsv1.ConditionStatus, message string) func(t *testing.T, condition *apiextensionsv1.CustomResourceDefinitionCondition) {
    35  		return func(t *testing.T, condition *apiextensionsv1.CustomResourceDefinitionCondition) {
    36  			t.Helper()
    37  			if condition == nil {
    38  				t.Fatal("missing condition")
    39  			}
    40  			if e, a := status, condition.Status; e != a {
    41  				t.Errorf("expected %v, got %v", e, a)
    42  			}
    43  			if e, a := message, condition.Message; e != a {
    44  				t.Errorf("expected %v, got %v", e, a)
    45  			}
    46  		}
    47  	}
    48  
    49  	tests := []struct {
    50  		name string
    51  
    52  		group             string
    53  		annotationValue   string
    54  		validateCondition func(t *testing.T, condition *apiextensionsv1.CustomResourceDefinitionCondition)
    55  	}{
    56  		{
    57  			name:              "for other group",
    58  			group:             "other.io",
    59  			annotationValue:   "",
    60  			validateCondition: noConditionFn,
    61  		},
    62  		{
    63  			name:              "missing annotation",
    64  			group:             "sigs.k8s.io",
    65  			annotationValue:   "",
    66  			validateCondition: verifyCondition(apiextensionsv1.ConditionFalse, `protected groups must have approval annotation "api-approved.kubernetes.io", see https://github.com/kubernetes/enhancements/pull/1111`),
    67  		},
    68  		{
    69  			name:              "invalid annotation",
    70  			group:             "sigs.k8s.io",
    71  			annotationValue:   "bad value",
    72  			validateCondition: verifyCondition(apiextensionsv1.ConditionFalse, `protected groups must have approval annotation "api-approved.kubernetes.io" with either a URL or a reason starting with "unapproved", see https://github.com/kubernetes/enhancements/pull/1111`),
    73  		},
    74  		{
    75  			name:              "approved",
    76  			group:             "sigs.k8s.io",
    77  			annotationValue:   "https://github.com/kubernetes/kubernetes/pull/79724",
    78  			validateCondition: verifyCondition(apiextensionsv1.ConditionTrue, `approved in https://github.com/kubernetes/kubernetes/pull/79724`),
    79  		},
    80  		{
    81  			name:              "unapproved",
    82  			group:             "sigs.k8s.io",
    83  			annotationValue:   "unapproved for reasons",
    84  			validateCondition: verifyCondition(apiextensionsv1.ConditionFalse, `not approved: "unapproved for reasons"`),
    85  		},
    86  	}
    87  
    88  	for _, test := range tests {
    89  		t.Run(test.name, func(t *testing.T) {
    90  			crd := &apiextensionsv1.CustomResourceDefinition{
    91  				ObjectMeta: metav1.ObjectMeta{Name: "foo", Annotations: map[string]string{apiextensionsv1.KubeAPIApprovedAnnotation: test.annotationValue}},
    92  				Spec: apiextensionsv1.CustomResourceDefinitionSpec{
    93  					Group: test.group,
    94  				},
    95  			}
    96  
    97  			actual := calculateCondition(crd)
    98  			test.validateCondition(t, actual)
    99  
   100  		})
   101  	}
   102  
   103  }
   104  

View as plain text