...

Source file src/k8s.io/kubernetes/test/images/agnhost/crd-conversion-webhook/converter/converter_test.go

Documentation: k8s.io/kubernetes/test/images/agnhost/crd-conversion-webhook/converter

     1  /*
     2  Copyright 2018 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 converter
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"strings"
    25  	"testing"
    26  
    27  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    28  	"k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    30  	"k8s.io/apimachinery/pkg/runtime"
    31  	"k8s.io/apimachinery/pkg/runtime/serializer/json"
    32  )
    33  
    34  func TestConverterYAML(t *testing.T) {
    35  	cases := []struct {
    36  		apiVersion     string
    37  		contentType    string
    38  		expected400Err string
    39  	}{
    40  		{
    41  			apiVersion:     "apiextensions.k8s.io/v1beta1",
    42  			contentType:    "application/json",
    43  			expected400Err: "json parse error",
    44  		},
    45  		{
    46  			apiVersion:  "apiextensions.k8s.io/v1beta1",
    47  			contentType: "application/yaml",
    48  		},
    49  		{
    50  			apiVersion:     "apiextensions.k8s.io/v1",
    51  			contentType:    "application/json",
    52  			expected400Err: "json parse error",
    53  		},
    54  		{
    55  			apiVersion:  "apiextensions.k8s.io/v1",
    56  			contentType: "application/yaml",
    57  		},
    58  	}
    59  	sampleObjTemplate := `kind: ConversionReview
    60  apiVersion: %s
    61  request:
    62    uid: 0000-0000-0000-0000
    63    desiredAPIVersion: stable.example.com/v2
    64    objects:
    65      - apiVersion: stable.example.com/v1
    66        kind: CronTab
    67        metadata:
    68          name: my-new-cron-object
    69        spec:
    70          cronSpec: "* * * * */5"
    71          image: my-awesome-cron-image
    72        hostPort: "localhost:7070"
    73  `
    74  	for _, tc := range cases {
    75  		t.Run(tc.apiVersion+" "+tc.contentType, func(t *testing.T) {
    76  			sampleObj := fmt.Sprintf(sampleObjTemplate, tc.apiVersion)
    77  			// First try json, it should fail as the data is taml
    78  			response := httptest.NewRecorder()
    79  			request, err := http.NewRequest("POST", "/convert", strings.NewReader(sampleObj))
    80  			if err != nil {
    81  				t.Fatal(err)
    82  			}
    83  			request.Header.Add("Content-Type", tc.contentType)
    84  			ServeExampleConvert(response, request)
    85  			convertReview := apiextensionsv1.ConversionReview{}
    86  			scheme := runtime.NewScheme()
    87  			if len(tc.expected400Err) > 0 {
    88  				body := response.Body.Bytes()
    89  				if !bytes.Contains(body, []byte(tc.expected400Err)) {
    90  					t.Fatalf("expected to fail on '%s', but it failed with: %s", tc.expected400Err, string(body))
    91  				}
    92  				return
    93  			}
    94  
    95  			yamlSerializer := json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, json.SerializerOptions{Yaml: true})
    96  			if _, _, err := yamlSerializer.Decode(response.Body.Bytes(), nil, &convertReview); err != nil {
    97  				t.Fatalf("cannot decode data: \n %v\n Error: %v", response.Body, err)
    98  			}
    99  			if convertReview.Response.Result.Status != v1.StatusSuccess {
   100  				t.Fatalf("cr conversion failed: %v", convertReview.Response)
   101  			}
   102  			convertedObj := unstructured.Unstructured{}
   103  			if _, _, err := yamlSerializer.Decode(convertReview.Response.ConvertedObjects[0].Raw, nil, &convertedObj); err != nil {
   104  				t.Fatal(err)
   105  			}
   106  			if e, a := "stable.example.com/v2", convertedObj.GetAPIVersion(); e != a {
   107  				t.Errorf("expected= %v, actual= %v", e, a)
   108  			}
   109  			if e, a := "localhost", convertedObj.Object["host"]; e != a {
   110  				t.Errorf("expected= %v, actual= %v", e, a)
   111  			}
   112  			if e, a := "7070", convertedObj.Object["port"]; e != a {
   113  				t.Errorf("expected= %v, actual= %v", e, a)
   114  			}
   115  		})
   116  	}
   117  }
   118  

View as plain text