...

Source file src/k8s.io/apiextensions-apiserver/test/integration/deprecation_test.go

Documentation: k8s.io/apiextensions-apiserver/test/integration

     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 integration
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"testing"
    23  
    24  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    25  	"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
    26  	"k8s.io/apiextensions-apiserver/test/integration/fixtures"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/runtime/schema"
    29  	"k8s.io/client-go/dynamic"
    30  	"k8s.io/utils/pointer"
    31  )
    32  
    33  var deprecationFixture = &apiextensionsv1.CustomResourceDefinition{
    34  	ObjectMeta: metav1.ObjectMeta{Name: "deps.tests.example.com"},
    35  	Spec: apiextensionsv1.CustomResourceDefinitionSpec{
    36  		Group: "tests.example.com",
    37  		Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
    38  			{
    39  				Name:       "v1alpha1",
    40  				Served:     true,
    41  				Deprecated: true,
    42  				Schema:     &apiextensionsv1.CustomResourceValidation{OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{Type: "object"}},
    43  			},
    44  			{
    45  				Name:               "v1alpha2",
    46  				Served:             true,
    47  				Deprecated:         true,
    48  				DeprecationWarning: pointer.StringPtr("custom deprecation warning"),
    49  				Schema:             &apiextensionsv1.CustomResourceValidation{OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{Type: "object"}},
    50  			},
    51  			{
    52  				Name:   "v1beta3",
    53  				Served: true,
    54  				Schema: &apiextensionsv1.CustomResourceValidation{OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{Type: "object"}},
    55  			},
    56  			{
    57  				Name:   "v1beta2",
    58  				Served: true,
    59  				Schema: &apiextensionsv1.CustomResourceValidation{OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{Type: "object"}},
    60  			},
    61  			{
    62  				Name:    "v1",
    63  				Served:  false,
    64  				Storage: true,
    65  				Schema:  &apiextensionsv1.CustomResourceValidation{OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{Type: "object"}},
    66  			},
    67  		},
    68  		Names: apiextensionsv1.CustomResourceDefinitionNames{
    69  			Plural:   "deps",
    70  			Singular: "dep",
    71  			Kind:     "Dep",
    72  			ListKind: "DepList",
    73  		},
    74  		Scope: apiextensionsv1.ClusterScoped,
    75  	},
    76  }
    77  
    78  func TestCustomResourceDeprecation(t *testing.T) {
    79  	tearDown, config, _, err := fixtures.StartDefaultServer(t)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	defer tearDown()
    84  
    85  	apiExtensionsClient, err := clientset.NewForConfig(config)
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	handler := &warningHandler{}
    91  	config.WarningHandler = handler
    92  	dynamicClient, err := dynamic.NewForConfig(config)
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	crd := deprecationFixture.DeepCopy()
    98  	if _, err := fixtures.CreateNewV1CustomResourceDefinition(crd, apiExtensionsClient, dynamicClient); err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	testcases := []struct {
   103  		name    string
   104  		version string
   105  		want    []string
   106  	}{
   107  		{
   108  			name:    "default",
   109  			version: "v1alpha1",
   110  			want:    []string{"tests.example.com/v1alpha1 Dep is deprecated; use tests.example.com/v1beta3 Dep"},
   111  		},
   112  		{
   113  			name:    "custom",
   114  			version: "v1alpha2",
   115  			want:    []string{"custom deprecation warning"},
   116  		},
   117  		{
   118  			name:    "older non-deprecated",
   119  			version: "v1beta2",
   120  			want:    nil,
   121  		},
   122  		{
   123  			name:    "newest non-deprecated",
   124  			version: "v1beta3",
   125  			want:    nil,
   126  		},
   127  	}
   128  	for _, tc := range testcases {
   129  		t.Run(tc.name, func(t *testing.T) {
   130  			handler.warnings = nil
   131  			resource := schema.GroupVersionResource{Group: "tests.example.com", Version: tc.version, Resource: "deps"}
   132  			_, err := dynamicClient.Resource(resource).List(context.TODO(), metav1.ListOptions{})
   133  			if err != nil {
   134  				t.Fatal(err)
   135  			}
   136  			if !reflect.DeepEqual(handler.warnings, tc.want) {
   137  				t.Errorf("expected %v, got %v", tc.want, handler.warnings)
   138  			}
   139  		})
   140  	}
   141  }
   142  
   143  type warningHandler struct {
   144  	warnings []string
   145  }
   146  
   147  func (w *warningHandler) HandleWarningHeader(code int, agent string, text string) {
   148  	w.warnings = append(w.warnings, text)
   149  }
   150  

View as plain text