...

Source file src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder/merge_test.go

Documentation: k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder

     1  /*
     2  Copyright 2022 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 builder
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"k8s.io/kube-openapi/pkg/spec3"
    24  	"k8s.io/kube-openapi/pkg/validation/spec"
    25  )
    26  
    27  func TestMergeSpecV3(t *testing.T) {
    28  	tests := []struct {
    29  		name     string
    30  		specs    []*spec3.OpenAPI
    31  		expected *spec3.OpenAPI
    32  	}{
    33  		{
    34  			name: "oneCRD",
    35  			specs: []*spec3.OpenAPI{{
    36  				Paths: &spec3.Paths{
    37  					Paths: map[string]*spec3.Path{
    38  						"/apis/stable.example.com/v1/crd1": {},
    39  					},
    40  				},
    41  				Components: &spec3.Components{
    42  					Schemas: map[string]*spec.Schema{
    43  						"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
    44  					},
    45  				},
    46  			},
    47  			},
    48  			expected: &spec3.OpenAPI{
    49  				Paths: &spec3.Paths{
    50  					Paths: map[string]*spec3.Path{
    51  						"/apis/stable.example.com/v1/crd1": {},
    52  					},
    53  				},
    54  				Components: &spec3.Components{
    55  					Schemas: map[string]*spec.Schema{
    56  						"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
    57  					},
    58  				},
    59  			},
    60  		},
    61  		{
    62  			name: "two CRDs same gv",
    63  			specs: []*spec3.OpenAPI{{
    64  				Paths: &spec3.Paths{
    65  					Paths: map[string]*spec3.Path{
    66  						"/apis/stable.example.com/v1/crd1": {},
    67  					},
    68  				},
    69  				Components: &spec3.Components{
    70  					Schemas: map[string]*spec.Schema{
    71  						"com.example.stable.v1.CRD1": {},
    72  
    73  						"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
    74  					},
    75  				},
    76  			},
    77  				{
    78  					Paths: &spec3.Paths{
    79  						Paths: map[string]*spec3.Path{
    80  							"/apis/stable.example.com/v1/crd2": {},
    81  						},
    82  					},
    83  					Components: &spec3.Components{
    84  						Schemas: map[string]*spec.Schema{
    85  							"com.example.stable.v1.CRD2":                      {},
    86  							"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
    87  						},
    88  					},
    89  				},
    90  			},
    91  			expected: &spec3.OpenAPI{
    92  				Paths: &spec3.Paths{
    93  					Paths: map[string]*spec3.Path{
    94  						"/apis/stable.example.com/v1/crd1": {},
    95  						"/apis/stable.example.com/v1/crd2": {},
    96  					},
    97  				},
    98  				Components: &spec3.Components{
    99  					Schemas: map[string]*spec.Schema{
   100  						"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
   101  						"com.example.stable.v1.CRD1":                      {},
   102  						"com.example.stable.v1.CRD2":                      {},
   103  					},
   104  				},
   105  			},
   106  		},
   107  		{
   108  			name: "two CRDs with scale",
   109  			specs: []*spec3.OpenAPI{{
   110  				Paths: &spec3.Paths{
   111  					Paths: map[string]*spec3.Path{
   112  						"/apis/stable.example.com/v1/crd1": {},
   113  					},
   114  				},
   115  				Components: &spec3.Components{
   116  					Schemas: map[string]*spec.Schema{
   117  						"com.example.stable.v1.CRD1": {},
   118  
   119  						"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
   120  						"io.k8s.api.autoscaling.v1.Scale":                 {},
   121  					},
   122  				},
   123  			},
   124  				{
   125  					Paths: &spec3.Paths{
   126  						Paths: map[string]*spec3.Path{
   127  							"/apis/stable.example.com/v1/crd2": {},
   128  						},
   129  					},
   130  					Components: &spec3.Components{
   131  						Schemas: map[string]*spec.Schema{
   132  							"com.example.stable.v1.CRD2":                      {},
   133  							"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
   134  							"io.k8s.api.autoscaling.v1.Scale":                 {},
   135  						},
   136  					},
   137  				},
   138  			},
   139  			expected: &spec3.OpenAPI{
   140  				Paths: &spec3.Paths{
   141  					Paths: map[string]*spec3.Path{
   142  						"/apis/stable.example.com/v1/crd1": {},
   143  						"/apis/stable.example.com/v1/crd2": {},
   144  					},
   145  				},
   146  				Components: &spec3.Components{
   147  					Schemas: map[string]*spec.Schema{
   148  						"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {},
   149  						"com.example.stable.v1.CRD1":                      {},
   150  						"com.example.stable.v1.CRD2":                      {},
   151  						"io.k8s.api.autoscaling.v1.Scale":                 {},
   152  					},
   153  				},
   154  			},
   155  		},
   156  	}
   157  
   158  	for _, tt := range tests {
   159  		t.Run(tt.name, func(t *testing.T) {
   160  			merged, err := MergeSpecsV3(tt.specs...)
   161  			if err != nil {
   162  				t.Error(err)
   163  			}
   164  			if !reflect.DeepEqual(merged, tt.expected) {
   165  				t.Error("Merged spec is different from expected spec")
   166  			}
   167  
   168  		})
   169  	}
   170  }
   171  

View as plain text