| 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 internalscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal`/ |
| 18 | |
| 19 | import ( |
| 20 | "github.com/go-openapi/jsonreferencescip-go gomod github.com/go-openapi/jsonreference v0.20.1 `github.com/go-openapi/jsonreference`/" |
| 21 | "reflectscip-go gomod github.com/golang/go/src go1.21 reflect/" |
| 22 | "testingscip-go gomod github.com/golang/go/src go1.21 testing/" |
| 23 | ) |
| 24 | |
| 25 | func TestJSONRefFromMapscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal`/TestJSONRefFromMap().(t *testingscip-go gomod github.com/golang/go/src go1.21 testing/.Tscip-go gomod github.com/golang/go/src go1.21 testing/T#) { |
| 26 | testcases := []struct { |
| 27 | name string |
| 28 | fromMap map[string]interface{} |
| 29 | expectNil bool |
| 30 | expectRefString string |
| 31 | }{ |
| 32 | { |
| 33 | name: "nil map", |
| 34 | expectRefString: "", |
| 35 | }, { |
| 36 | name: "map with no $ref", |
| 37 | fromMap: map[string]interface{}{"a": "b"}, |
| 38 | expectRefString: "", |
| 39 | }, { |
| 40 | name: "ref path", |
| 41 | fromMap: map[string]interface{}{"$ref": "#/path"}, |
| 42 | expectRefString: "#/path", |
| 43 | }, |
| 44 | } |
| 45 | |
| 46 | for _, tc := range testcases { |
| 47 | var tmp jsonreferencescip-go gomod github.com/go-openapi/jsonreference v0.20.1 `github.com/go-openapi/jsonreference`/.Refscip-go gomod github.com/go-openapi/jsonreference v0.20.1 `github.com/go-openapi/jsonreference`/Ref# |
| 48 | err := JSONRefFromMapscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal`/JSONRefFromMap().(&tmp, tc.fromMap) |
| 49 | if err != nil { |
| 50 | t.Errorfscip-go gomod github.com/golang/go/src go1.21 testing/common#Errorf().("Expect no error from JSONRefMap %s, got error %v", tc.name, err) |
| 51 | } |
| 52 | |
| 53 | if tmp.Stringscip-go gomod github.com/go-openapi/jsonreference v0.20.1 `github.com/go-openapi/jsonreference`/Ref#String().() != tc.expectRefString { |
| 54 | t.Errorfscip-go gomod github.com/golang/go/src go1.21 testing/common#Errorf().("Expect jsonRef to be %s, got %s for %s", tc.expectRefString, tmp.Stringscip-go gomod github.com/go-openapi/jsonreference v0.20.1 `github.com/go-openapi/jsonreference`/Ref#String().(), tc.name) |
| 55 | } |
| 56 | |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestSanitizeExtensionsscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal`/TestSanitizeExtensions().(t *testingscip-go gomod github.com/golang/go/src go1.21 testing/.Tscip-go gomod github.com/golang/go/src go1.21 testing/T#) { |
| 61 | testcases := []struct { |
| 62 | in map[string]interface{} |
| 63 | expected map[string]interface{} |
| 64 | }{ |
| 65 | { |
| 66 | in: map[string]interface{}{"a": "b", "x-extension": "foo"}, |
| 67 | expected: map[string]interface{}{"x-extension": "foo"}, |
| 68 | }, |
| 69 | { |
| 70 | in: map[string]interface{}{"a": "b"}, |
| 71 | expected: nil, |
| 72 | }, |
| 73 | { |
| 74 | in: map[string]interface{}{}, |
| 75 | expected: nil, |
| 76 | }, |
| 77 | } |
| 78 | |
| 79 | for _, tc := range testcases { |
| 80 | e := SanitizeExtensionsscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal`/SanitizeExtensions().(tc.in) |
| 81 | if !reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.21 reflect/DeepEqual().(tc.expected, e) { |
| 82 | t.Errorfscip-go gomod github.com/golang/go/src go1.21 testing/common#Errorf().("Error: sanitize extensions does not match expected") |
| 83 | } |
| 84 | } |
| 85 | } |