cleanup misleading errors in openAPI tesing (#91321)

* cleanup misleading errors in openAPI tesing

* make sure test case would fail

Kubernetes-commit: 0459c2afab4f1913993e437a781e95457ecefb42
This commit is contained in:
Hongcai Ren 2020-05-27 04:37:42 +08:00 committed by Kubernetes Publisher
parent 7ab8430bef
commit f099a72e14
4 changed files with 51 additions and 25 deletions

4
Godeps/Godeps.json generated
View File

@ -436,11 +436,11 @@
}, },
{ {
"ImportPath": "k8s.io/api", "ImportPath": "k8s.io/api",
"Rev": "dccc90724807" "Rev": "6f652b6ce59c"
}, },
{ {
"ImportPath": "k8s.io/apimachinery", "ImportPath": "k8s.io/apimachinery",
"Rev": "bdcc9f4ab675" "Rev": "e0747e0da69d"
}, },
{ {
"ImportPath": "k8s.io/gengo", "ImportPath": "k8s.io/gengo",

View File

@ -454,8 +454,7 @@ func returnedOpenAPI() *openapi_v2.Document {
} }
} }
func openapiSchemaDeprecatedFakeServer(status int) (*httptest.Server, error) { func openapiSchemaDeprecatedFakeServer(status int, t *testing.T) (*httptest.Server, error) {
var sErr error
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/openapi/v2" { if req.URL.Path == "/openapi/v2" {
// write the error status for the new endpoint request // write the error status for the new endpoint request
@ -463,54 +462,81 @@ func openapiSchemaDeprecatedFakeServer(status int) (*httptest.Server, error) {
return return
} }
if req.URL.Path != "/swagger-2.0.0.pb-v1" { if req.URL.Path != "/swagger-2.0.0.pb-v1" {
sErr = fmt.Errorf("Unexpected url %v", req.URL) errMsg := fmt.Sprintf("Unexpected url %v", req.URL)
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(errMsg))
t.Errorf("testing should fail as %s", errMsg)
return
} }
if req.Method != "GET" { if req.Method != "GET" {
sErr = fmt.Errorf("Unexpected method %v", req.Method) errMsg := fmt.Sprintf("Unexpected method %v", req.Method)
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte(errMsg))
t.Errorf("testing should fail as %s", errMsg)
return
} }
mime.AddExtensionType(".pb-v1", "application/com.github.googleapis.gnostic.OpenAPIv2@68f4ded+protobuf") mime.AddExtensionType(".pb-v1", "application/com.github.googleapis.gnostic.OpenAPIv2@68f4ded+protobuf")
output, err := proto.Marshal(returnedOpenAPI()) output, err := proto.Marshal(returnedOpenAPI())
if err != nil { if err != nil {
sErr = err errMsg := fmt.Sprintf("Unexpected marshal error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errMsg))
t.Errorf("testing should fail as %s", errMsg)
return return
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write(output) w.Write(output)
})) }))
return server, sErr
return server, nil
} }
func openapiSchemaFakeServer() (*httptest.Server, error) { func openapiSchemaFakeServer(t *testing.T) (*httptest.Server, error) {
var sErr error
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/openapi/v2" { if req.URL.Path != "/openapi/v2" {
sErr = fmt.Errorf("Unexpected url %v", req.URL) errMsg := fmt.Sprintf("Unexpected url %v", req.URL)
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(errMsg))
t.Errorf("testing should fail as %s", errMsg)
return
} }
if req.Method != "GET" { if req.Method != "GET" {
sErr = fmt.Errorf("Unexpected method %v", req.Method) errMsg := fmt.Sprintf("Unexpected method %v", req.Method)
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte(errMsg))
t.Errorf("testing should fail as %s", errMsg)
return
} }
decipherableFormat := req.Header.Get("Accept") decipherableFormat := req.Header.Get("Accept")
if decipherableFormat != "application/com.github.proto-openapi.spec.v2@v1.0+protobuf" { if decipherableFormat != "application/com.github.proto-openapi.spec.v2@v1.0+protobuf" {
sErr = fmt.Errorf("Unexpected accept mime type %v", decipherableFormat) errMsg := fmt.Sprintf("Unexpected accept mime type %v", decipherableFormat)
w.WriteHeader(http.StatusUnsupportedMediaType)
w.Write([]byte(errMsg))
t.Errorf("testing should fail as %s", errMsg)
return
} }
mime.AddExtensionType(".pb-v1", "application/com.github.googleapis.gnostic.OpenAPIv2@68f4ded+protobuf") mime.AddExtensionType(".pb-v1", "application/com.github.googleapis.gnostic.OpenAPIv2@68f4ded+protobuf")
output, err := proto.Marshal(returnedOpenAPI()) output, err := proto.Marshal(returnedOpenAPI())
if err != nil { if err != nil {
sErr = err errMsg := fmt.Sprintf("Unexpected marshal error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errMsg))
t.Errorf("testing should fail as %s", errMsg)
return return
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write(output) w.Write(output)
})) }))
return server, sErr
return server, nil
} }
func TestGetOpenAPISchema(t *testing.T) { func TestGetOpenAPISchema(t *testing.T) {
server, err := openapiSchemaFakeServer() server, err := openapiSchemaFakeServer(t)
if err != nil { if err != nil {
t.Errorf("unexpected error starting fake server: %v", err) t.Errorf("unexpected error starting fake server: %v", err)
} }
@ -527,7 +553,7 @@ func TestGetOpenAPISchema(t *testing.T) {
} }
func TestGetOpenAPISchemaForbiddenFallback(t *testing.T) { func TestGetOpenAPISchemaForbiddenFallback(t *testing.T) {
server, err := openapiSchemaDeprecatedFakeServer(http.StatusForbidden) server, err := openapiSchemaDeprecatedFakeServer(http.StatusForbidden, t)
if err != nil { if err != nil {
t.Errorf("unexpected error starting fake server: %v", err) t.Errorf("unexpected error starting fake server: %v", err)
} }
@ -544,7 +570,7 @@ func TestGetOpenAPISchemaForbiddenFallback(t *testing.T) {
} }
func TestGetOpenAPISchemaNotFoundFallback(t *testing.T) { func TestGetOpenAPISchemaNotFoundFallback(t *testing.T) {
server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotFound) server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotFound, t)
if err != nil { if err != nil {
t.Errorf("unexpected error starting fake server: %v", err) t.Errorf("unexpected error starting fake server: %v", err)
} }
@ -561,7 +587,7 @@ func TestGetOpenAPISchemaNotFoundFallback(t *testing.T) {
} }
func TestGetOpenAPISchemaNotAcceptableFallback(t *testing.T) { func TestGetOpenAPISchemaNotAcceptableFallback(t *testing.T) {
server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotAcceptable) server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotAcceptable, t)
if err != nil { if err != nil {
t.Errorf("unexpected error starting fake server: %v", err) t.Errorf("unexpected error starting fake server: %v", err)
} }

8
go.mod
View File

@ -26,8 +26,8 @@ require (
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 golang.org/x/time v0.0.0-20191024005414-555d28b269f0
k8s.io/api v0.0.0-20200519082057-dccc90724807 k8s.io/api v0.0.0-20200526202119-6f652b6ce59c
k8s.io/apimachinery v0.0.0-20200519081849-bdcc9f4ab675 k8s.io/apimachinery v0.0.0-20200525041908-e0747e0da69d
k8s.io/klog/v2 v2.0.0 k8s.io/klog/v2 v2.0.0
k8s.io/utils v0.0.0-20200414100711-2df71ebbae66 k8s.io/utils v0.0.0-20200414100711-2df71ebbae66
sigs.k8s.io/yaml v1.2.0 sigs.k8s.io/yaml v1.2.0
@ -36,6 +36,6 @@ require (
replace ( replace (
golang.org/x/sys => golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // pinned to release-branch.go1.13 golang.org/x/sys => golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // pinned to release-branch.go1.13
golang.org/x/tools => golang.org/x/tools v0.0.0-20190821162956-65e3620a7ae7 // pinned to release-branch.go1.13 golang.org/x/tools => golang.org/x/tools v0.0.0-20190821162956-65e3620a7ae7 // pinned to release-branch.go1.13
k8s.io/api => k8s.io/api v0.0.0-20200519082057-dccc90724807 k8s.io/api => k8s.io/api v0.0.0-20200526202119-6f652b6ce59c
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20200519081849-bdcc9f4ab675 k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20200525041908-e0747e0da69d
) )

4
go.sum
View File

@ -284,8 +284,8 @@ honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
k8s.io/api v0.0.0-20200519082057-dccc90724807/go.mod h1:Q6M7crZfx9Kr6X36KdNV/vN9ZDm0zhlCbCjafx7a6LY= k8s.io/api v0.0.0-20200526202119-6f652b6ce59c/go.mod h1:SrtLvWo/N06luJcHPmYFDOnMrJk8U2Cr6Ir1liACC+0=
k8s.io/apimachinery v0.0.0-20200519081849-bdcc9f4ab675/go.mod h1:x4z2+k1N0YTBvV8PmaVs4/hSmKVVENZmTqI8gBygpLA= k8s.io/apimachinery v0.0.0-20200525041908-e0747e0da69d/go.mod h1:x4z2+k1N0YTBvV8PmaVs4/hSmKVVENZmTqI8gBygpLA=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/klog/v2 v2.0.0 h1:Foj74zO6RbjjP4hBEKjnYtjjAhGg4jNynUdYF6fJrok= k8s.io/klog/v2 v2.0.0 h1:Foj74zO6RbjjP4hBEKjnYtjjAhGg4jNynUdYF6fJrok=
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE= k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=