...

Source file src/k8s.io/kube-aggregator/pkg/controllers/openapi/aggregator/downloader_test.go

Documentation: k8s.io/kube-aggregator/pkg/controllers/openapi/aggregator

     1  /*
     2  Copyright 2017 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 aggregator
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  
    26  	"k8s.io/kube-openapi/pkg/validation/spec"
    27  )
    28  
    29  type handlerTest struct {
    30  	etag string
    31  	data []byte
    32  }
    33  
    34  var _ http.Handler = handlerTest{}
    35  
    36  func (h handlerTest) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    37  	if len(h.etag) > 0 {
    38  		w.Header().Add("Etag", h.etag)
    39  	}
    40  	ifNoneMatches := r.Header["If-None-Match"]
    41  	for _, match := range ifNoneMatches {
    42  		if match == h.etag {
    43  			w.WriteHeader(http.StatusNotModified)
    44  			return
    45  		}
    46  	}
    47  	w.Write(h.data)
    48  }
    49  
    50  type handlerDeprecatedTest struct {
    51  	etag string
    52  	data []byte
    53  }
    54  
    55  var _ http.Handler = handlerDeprecatedTest{}
    56  
    57  func (h handlerDeprecatedTest) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    58  	// old server returns 403 on new endpoint
    59  	if r.URL.Path == "/openapi/v2" {
    60  		w.WriteHeader(http.StatusForbidden)
    61  		return
    62  	}
    63  	if len(h.etag) > 0 {
    64  		w.Header().Add("Etag", h.etag)
    65  	}
    66  	ifNoneMatches := r.Header["If-None-Match"]
    67  	for _, match := range ifNoneMatches {
    68  		if match == h.etag {
    69  			w.WriteHeader(http.StatusNotModified)
    70  			return
    71  		}
    72  	}
    73  	w.Write(h.data)
    74  }
    75  
    76  func assertDownloadedSpec(actualSpec *spec.Swagger, actualEtag string, err error,
    77  	expectedSpecID string, expectedEtag string) error {
    78  	if err != nil {
    79  		return fmt.Errorf("downloadOpenAPISpec failed : %s", err)
    80  	}
    81  	if expectedSpecID == "" && actualSpec != nil {
    82  		return fmt.Errorf("expected Not Modified, actual ID %s", actualSpec.ID)
    83  	}
    84  	if actualSpec != nil && actualSpec.ID != expectedSpecID {
    85  		return fmt.Errorf("expected ID %s, actual ID %s", expectedSpecID, actualSpec.ID)
    86  	}
    87  	if actualEtag != expectedEtag {
    88  		return fmt.Errorf("expected ETag '%s', actual ETag '%s'", expectedEtag, actualEtag)
    89  	}
    90  	return nil
    91  }
    92  
    93  func TestDownloadOpenAPISpec(t *testing.T) {
    94  	s := Downloader{}
    95  
    96  	// Test with no eTag
    97  	actualSpec, actualEtag, _, err := s.Download(handlerTest{data: []byte("{\"id\": \"test\"}")}, "")
    98  	assert.NoError(t, assertDownloadedSpec(actualSpec, actualEtag, err, "test", "\"6E8F849B434D4B98A569B9D7718876E9-356ECAB19D7FBE1336BABB1E70F8F3025050DE218BE78256BE81620681CFC9A268508E542B8B55974E17B2184BBFC8FFFAA577E51BE195D32B3CA2547818ABE4\""))
    99  
   100  	// Test with eTag
   101  	actualSpec, actualEtag, _, err = s.Download(
   102  		handlerTest{data: []byte("{\"id\": \"test\"}"), etag: "etag_test"}, "")
   103  	assert.NoError(t, assertDownloadedSpec(actualSpec, actualEtag, err, "test", "etag_test"))
   104  
   105  	// Test not modified
   106  	actualSpec, actualEtag, _, err = s.Download(
   107  		handlerTest{data: []byte("{\"id\": \"test\"}"), etag: "etag_test"}, "etag_test")
   108  	assert.NoError(t, assertDownloadedSpec(actualSpec, actualEtag, err, "", "etag_test"))
   109  
   110  	// Test different eTags
   111  	actualSpec, actualEtag, _, err = s.Download(
   112  		handlerTest{data: []byte("{\"id\": \"test\"}"), etag: "etag_test1"}, "etag_test2")
   113  	assert.NoError(t, assertDownloadedSpec(actualSpec, actualEtag, err, "test", "etag_test1"))
   114  }
   115  

View as plain text