| 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 aggregated |
| 18 |
|
| 19 |
import ( |
| 20 |
"net/http" |
| 21 |
"net/http/httptest" |
| 22 |
|
| 23 |
"io" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"github.com/stretchr/testify/assert" |
| 27 |
genericfeatures "k8s.io/apiserver/pkg/features" |
| 28 |
utilfeature "k8s.io/apiserver/pkg/util/feature" |
| 29 |
featuregatetesting "k8s.io/component-base/featuregate/testing" |
| 30 |
) |
| 31 |
|
| 32 |
const discoveryPath = "/apis" |
| 33 |
const jsonAccept = "application/json" |
| 34 |
const protobufAccept = "application/vnd.kubernetes.protobuf" |
| 35 |
const aggregatedV2Beta1AcceptSuffix = ";g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList" |
| 36 |
const aggregatedAcceptSuffix = ";g=apidiscovery.k8s.io;v=v2;as=APIGroupDiscoveryList" |
| 37 |
|
| 38 |
const aggregatedV2Beta1JSONAccept = jsonAccept + aggregatedV2Beta1AcceptSuffix |
| 39 |
const aggregatedV2Beta1ProtoAccept = protobufAccept + aggregatedV2Beta1AcceptSuffix |
| 40 |
const aggregatedJSONAccept = jsonAccept + aggregatedAcceptSuffix |
| 41 |
const aggregatedProtoAccept = protobufAccept + aggregatedAcceptSuffix |
| 42 |
|
| 43 |
func fetchPath(handler http.Handler, path, accept string) string { |
| 44 |
w := httptest.NewRecorder() |
| 45 |
req := httptest.NewRequest("GET", discoveryPath, nil) |
| 46 |
|
| 47 |
// Ask for JSON response |
| 48 |
req.Header.Set("Accept", accept) |
| 49 |
|
| 50 |
handler.ServeHTTP(w, req) |
| 51 |
return string(w.Body.Bytes()) |
| 52 |
} |
| 53 |
|
| 54 |
type fakeHTTPHandler struct { |
| 55 |
data string |
| 56 |
} |
| 57 |
|
| 58 |
func (f fakeHTTPHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { |
| 59 |
io.WriteString(resp, f.data) |
| 60 |
} |
| 61 |
|
| 62 |
func TestAggregationEnabled(t *testing.T) { |
| 63 |
unaggregated := fakeHTTPHandler{data: "unaggregated"} |
| 64 |
aggregated := fakeHTTPHandler{data: "aggregated"} |
| 65 |
wrapped := WrapAggregatedDiscoveryToHandler(unaggregated, aggregated) |
| 66 |
|
| 67 |
testCases := []struct { |
| 68 |
accept string |
| 69 |
expected string |
| 70 |
}{ |
| 71 |
{ |
| 72 |
// Misconstructed/incorrect accept headers should be passed to the unaggregated handler to return an error |
| 73 |
accept: "application/json;foo=bar", |
| 74 |
expected: "unaggregated", |
| 75 |
}, { |
| 76 |
// Empty accept headers are valid and should be handled by the unaggregated handler |
| 77 |
accept: "", |
| 78 |
expected: "unaggregated", |
| 79 |
}, { |
| 80 |
accept: aggregatedV2Beta1JSONAccept, |
| 81 |
expected: "aggregated", |
| 82 |
}, { |
| 83 |
accept: aggregatedV2Beta1ProtoAccept, |
| 84 |
expected: "aggregated", |
| 85 |
}, { |
| 86 |
accept: aggregatedJSONAccept, |
| 87 |
expected: "aggregated", |
| 88 |
}, { |
| 89 |
accept: aggregatedProtoAccept, |
| 90 |
expected: "aggregated", |
| 91 |
}, { |
| 92 |
accept: jsonAccept, |
| 93 |
expected: "unaggregated", |
| 94 |
}, { |
| 95 |
accept: protobufAccept, |
| 96 |
expected: "unaggregated", |
| 97 |
}, { |
| 98 |
// Server should return the first accepted type |
| 99 |
accept: aggregatedJSONAccept + "," + jsonAccept, |
| 100 |
expected: "aggregated", |
| 101 |
}, { |
| 102 |
// Server should return the first accepted type |
| 103 |
accept: aggregatedProtoAccept + "," + protobufAccept, |
| 104 |
expected: "aggregated", |
| 105 |
}, |
| 106 |
} |
| 107 |
|
| 108 |
for _, tc := range testCases { |
| 109 |
if tc.accept == aggregatedV2Beta1JSONAccept || tc.accept == aggregatedV2Beta1ProtoAccept { |
| 110 |
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, genericfeatures.AggregatedDiscoveryRemoveBetaType, false) |
| 111 |
} |
| 112 |
body := fetchPath(wrapped, discoveryPath, tc.accept) |
| 113 |
assert.Equal(t, tc.expected, body) |
| 114 |
} |
| 115 |
} |