|
| 1 | +/* |
| 2 | +Copyright 2023 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 openapi |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "net/http/httptest" |
| 23 | + "testing" |
| 24 | + |
| 25 | + appsv1 "k8s.io/api/apps/v1" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + "k8s.io/client-go/kubernetes/scheme" |
| 28 | + "k8s.io/client-go/rest" |
| 29 | +) |
| 30 | + |
| 31 | +func TestGroupVersion(t *testing.T) { |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + prefix string |
| 35 | + serverReturnsPrefix bool |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "no prefix", |
| 39 | + prefix: "", |
| 40 | + serverReturnsPrefix: false, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "prefix not in discovery", |
| 44 | + prefix: "/test-endpoint", |
| 45 | + serverReturnsPrefix: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "prefix in discovery", |
| 49 | + prefix: "/test-endpoint", |
| 50 | + serverReturnsPrefix: true, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + for _, test := range tests { |
| 55 | + t.Run(test.name, func(t *testing.T) { |
| 56 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 57 | + switch { |
| 58 | + case r.URL.Path == test.prefix+"/openapi/v3/apis/apps/v1" && r.URL.RawQuery == "hash=014fbff9a07c": |
| 59 | + w.Header().Set("Content-Type", "application/json") |
| 60 | + w.WriteHeader(http.StatusOK) |
| 61 | + w.Write([]byte(`{"openapi":"3.0.0","info":{"title":"Kubernetes","version":"unversioned"}}`)) |
| 62 | + case r.URL.Path == test.prefix+"/openapi/v3": |
| 63 | + // return root content |
| 64 | + w.Header().Set("Content-Type", "application/json") |
| 65 | + w.WriteHeader(http.StatusOK) |
| 66 | + if test.serverReturnsPrefix { |
| 67 | + w.Write([]byte(fmt.Sprintf(`{"paths":{"apis/apps/v1":{"serverRelativeURL":"%s/openapi/v3/apis/apps/v1?hash=014fbff9a07c"}}}`, test.prefix))) |
| 68 | + } else { |
| 69 | + w.Write([]byte(`{"paths":{"apis/apps/v1":{"serverRelativeURL":"/openapi/v3/apis/apps/v1?hash=014fbff9a07c"}}}`)) |
| 70 | + } |
| 71 | + default: |
| 72 | + t.Errorf("unexpected request: %s", r.URL.String()) |
| 73 | + w.WriteHeader(http.StatusNotFound) |
| 74 | + return |
| 75 | + } |
| 76 | + })) |
| 77 | + defer server.Close() |
| 78 | + |
| 79 | + c, err := rest.RESTClientFor(&rest.Config{ |
| 80 | + Host: server.URL + test.prefix, |
| 81 | + ContentConfig: rest.ContentConfig{ |
| 82 | + NegotiatedSerializer: scheme.Codecs, |
| 83 | + GroupVersion: &appsv1.SchemeGroupVersion, |
| 84 | + }, |
| 85 | + }) |
| 86 | + |
| 87 | + if err != nil { |
| 88 | + t.Fatalf("unexpected error occurred: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + openapiClient := NewClient(c) |
| 92 | + paths, err := openapiClient.Paths() |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("unexpected error occurred: %v", err) |
| 95 | + } |
| 96 | + schema, err := paths["apis/apps/v1"].Schema(runtime.ContentTypeJSON) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("unexpected error occurred: %v", err) |
| 99 | + } |
| 100 | + expectedResult := `{"openapi":"3.0.0","info":{"title":"Kubernetes","version":"unversioned"}}` |
| 101 | + if string(schema) != expectedResult { |
| 102 | + t.Fatalf("unexpected result actual: %s expected: %s", string(schema), expectedResult) |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
0 commit comments