| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package testutil |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"github.com/emicklei/go-restful/v3" |
| 24 |
"k8s.io/kube-openapi/pkg/common" |
| 25 |
"k8s.io/kube-openapi/pkg/spec3" |
| 26 |
"k8s.io/kube-openapi/pkg/util" |
| 27 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
func CreateOpenAPIBuilderConfig() *common.Config { |
| 33 |
return &common.Config{ |
| 34 |
ProtocolList: []string{"https"}, |
| 35 |
IgnorePrefixes: []string{"/swaggerapi"}, |
| 36 |
Info: &spec.Info{ |
| 37 |
InfoProps: spec.InfoProps{ |
| 38 |
Title: "Integration Test", |
| 39 |
Version: "1.0", |
| 40 |
}, |
| 41 |
}, |
| 42 |
ResponseDefinitions: map[string]spec.Response{ |
| 43 |
"NotFound": { |
| 44 |
ResponseProps: spec.ResponseProps{ |
| 45 |
Description: "Entity not found.", |
| 46 |
}, |
| 47 |
}, |
| 48 |
}, |
| 49 |
CommonResponses: map[int]spec.Response{ |
| 50 |
404: *spec.ResponseRef("#/responses/NotFound"), |
| 51 |
}, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func CreateOpenAPIV3BuilderConfig() *common.OpenAPIV3Config { |
| 56 |
return &common.OpenAPIV3Config{ |
| 57 |
IgnorePrefixes: []string{"/swaggerapi"}, |
| 58 |
Info: &spec.Info{ |
| 59 |
InfoProps: spec.InfoProps{ |
| 60 |
Title: "Integration Test", |
| 61 |
Version: "1.0", |
| 62 |
}, |
| 63 |
}, |
| 64 |
ResponseDefinitions: map[string]*spec3.Response{ |
| 65 |
"NotFound": { |
| 66 |
ResponseProps: spec3.ResponseProps{ |
| 67 |
Description: "Entity not found.", |
| 68 |
}, |
| 69 |
}, |
| 70 |
}, |
| 71 |
CommonResponses: map[int]*spec3.Response{ |
| 72 |
404: { |
| 73 |
Refable: spec.Refable{Ref: spec.MustCreateRef("#/components/responses/NotFound")}, |
| 74 |
}, |
| 75 |
}, |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
func CreateWebServices(includeV2SchemaAnnotation bool) []*restful.WebService { |
| 83 |
w := new(restful.WebService) |
| 84 |
addRoutes(w, buildRouteForType(w, "dummytype", "Foo")...) |
| 85 |
addRoutes(w, buildRouteForType(w, "dummytype", "Bar")...) |
| 86 |
addRoutes(w, buildRouteForType(w, "dummytype", "Baz")...) |
| 87 |
addRoutes(w, buildRouteForType(w, "dummytype", "Waldo")...) |
| 88 |
addRoutes(w, buildRouteForType(w, "listtype", "AtomicList")...) |
| 89 |
addRoutes(w, buildRouteForType(w, "listtype", "MapList")...) |
| 90 |
addRoutes(w, buildRouteForType(w, "listtype", "SetList")...) |
| 91 |
addRoutes(w, buildRouteForType(w, "uniontype", "TopLevelUnion")...) |
| 92 |
addRoutes(w, buildRouteForType(w, "uniontype", "InlinedUnion")...) |
| 93 |
addRoutes(w, buildRouteForType(w, "custom", "Bal")...) |
| 94 |
addRoutes(w, buildRouteForType(w, "custom", "Bak")...) |
| 95 |
if includeV2SchemaAnnotation { |
| 96 |
addRoutes(w, buildRouteForType(w, "custom", "Bac")...) |
| 97 |
addRoutes(w, buildRouteForType(w, "custom", "Bah")...) |
| 98 |
addRoutes(w, buildRouteForType(w, "valuevalidation", "Foo2")...) |
| 99 |
addRoutes(w, buildRouteForType(w, "valuevalidation", "Foo3")...) |
| 100 |
addRoutes(w, buildRouteForType(w, "valuevalidation", "Foo4")...) |
| 101 |
addRoutes(w, buildRouteForType(w, "valuevalidation", "Foo5")...) |
| 102 |
} |
| 103 |
addRoutes(w, buildRouteForType(w, "maptype", "GranularMap")...) |
| 104 |
addRoutes(w, buildRouteForType(w, "maptype", "AtomicMap")...) |
| 105 |
addRoutes(w, buildRouteForType(w, "structtype", "GranularStruct")...) |
| 106 |
addRoutes(w, buildRouteForType(w, "structtype", "FieldLevelOverrideStruct")...) |
| 107 |
addRoutes(w, buildRouteForType(w, "structtype", "AtomicStruct")...) |
| 108 |
addRoutes(w, buildRouteForType(w, "structtype", "DeclaredAtomicStruct")...) |
| 109 |
addRoutes(w, buildRouteForType(w, "defaults", "Defaulted")...) |
| 110 |
addRoutes(w, buildRouteForType(w, "valuevalidation", "Foo")...) |
| 111 |
return []*restful.WebService{w} |
| 112 |
} |
| 113 |
|
| 114 |
func addRoutes(ws *restful.WebService, routes ...*restful.RouteBuilder) { |
| 115 |
for _, r := range routes { |
| 116 |
ws.Route(r) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
var _ = util.OpenAPICanonicalTypeNamer(&typeNamer{}) |
| 122 |
|
| 123 |
type typeNamer struct { |
| 124 |
pkg string |
| 125 |
name string |
| 126 |
} |
| 127 |
|
| 128 |
func (t *typeNamer) OpenAPICanonicalTypeName() string { |
| 129 |
return fmt.Sprintf("k8s.io/kube-openapi/test/integration/testdata/%s.%s", t.pkg, t.name) |
| 130 |
} |
| 131 |
|
| 132 |
func buildRouteForType(ws *restful.WebService, pkg, name string) []*restful.RouteBuilder { |
| 133 |
namer := typeNamer{ |
| 134 |
pkg: pkg, |
| 135 |
name: name, |
| 136 |
} |
| 137 |
|
| 138 |
routes := []*restful.RouteBuilder{ |
| 139 |
ws.GET(fmt.Sprintf("/test/%s/%s", pkg, strings.ToLower(name))). |
| 140 |
Operation(fmt.Sprintf("get-%s.%s", pkg, name)). |
| 141 |
Produces("application/json"). |
| 142 |
To(func(*restful.Request, *restful.Response) {}). |
| 143 |
Writes(&namer), |
| 144 |
ws.POST(fmt.Sprintf("/test/%s", pkg)). |
| 145 |
Operation(fmt.Sprintf("create-%s.%s", pkg, name)). |
| 146 |
Produces("application/json"). |
| 147 |
To(func(*restful.Request, *restful.Response) {}). |
| 148 |
Returns(201, "Created", &namer). |
| 149 |
Writes(&namer), |
| 150 |
} |
| 151 |
|
| 152 |
if pkg == "dummytype" { |
| 153 |
statusErrType := typeNamer{ |
| 154 |
pkg: "dummytype", |
| 155 |
name: "StatusError", |
| 156 |
} |
| 157 |
|
| 158 |
for _, route := range routes { |
| 159 |
route.Returns(500, "Internal Service Error", &statusErrType) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return routes |
| 164 |
} |