Skip to content

Commit 0459c2a

Browse files
authored
cleanup misleading errors in openAPI tesing (kubernetes#91321)
* cleanup misleading errors in openAPI tesing * make sure test case would fail
1 parent dee4a7c commit 0459c2a

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

staging/src/k8s.io/client-go/discovery/discovery_client_test.go

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -454,63 +454,89 @@ func returnedOpenAPI() *openapi_v2.Document {
454454
}
455455
}
456456

457-
func openapiSchemaDeprecatedFakeServer(status int) (*httptest.Server, error) {
458-
var sErr error
457+
func openapiSchemaDeprecatedFakeServer(status int, t *testing.T) (*httptest.Server, error) {
459458
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
460459
if req.URL.Path == "/openapi/v2" {
461460
// write the error status for the new endpoint request
462461
w.WriteHeader(status)
463462
return
464463
}
465464
if req.URL.Path != "/swagger-2.0.0.pb-v1" {
466-
sErr = fmt.Errorf("Unexpected url %v", req.URL)
465+
errMsg := fmt.Sprintf("Unexpected url %v", req.URL)
466+
w.WriteHeader(http.StatusNotFound)
467+
w.Write([]byte(errMsg))
468+
t.Errorf("testing should fail as %s", errMsg)
469+
return
467470
}
468471
if req.Method != "GET" {
469-
sErr = fmt.Errorf("Unexpected method %v", req.Method)
472+
errMsg := fmt.Sprintf("Unexpected method %v", req.Method)
473+
w.WriteHeader(http.StatusMethodNotAllowed)
474+
w.Write([]byte(errMsg))
475+
t.Errorf("testing should fail as %s", errMsg)
476+
return
470477
}
471478

472479
mime.AddExtensionType(".pb-v1", "application/com.github.googleapis.gnostic.OpenAPIv2@68f4ded+protobuf")
473480

474481
output, err := proto.Marshal(returnedOpenAPI())
475482
if err != nil {
476-
sErr = err
483+
errMsg := fmt.Sprintf("Unexpected marshal error: %v", err)
484+
w.WriteHeader(http.StatusInternalServerError)
485+
w.Write([]byte(errMsg))
486+
t.Errorf("testing should fail as %s", errMsg)
477487
return
478488
}
479489
w.WriteHeader(http.StatusOK)
480490
w.Write(output)
481491
}))
482-
return server, sErr
492+
493+
return server, nil
483494
}
484495

485-
func openapiSchemaFakeServer() (*httptest.Server, error) {
486-
var sErr error
496+
func openapiSchemaFakeServer(t *testing.T) (*httptest.Server, error) {
487497
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
488498
if req.URL.Path != "/openapi/v2" {
489-
sErr = fmt.Errorf("Unexpected url %v", req.URL)
499+
errMsg := fmt.Sprintf("Unexpected url %v", req.URL)
500+
w.WriteHeader(http.StatusNotFound)
501+
w.Write([]byte(errMsg))
502+
t.Errorf("testing should fail as %s", errMsg)
503+
return
490504
}
491505
if req.Method != "GET" {
492-
sErr = fmt.Errorf("Unexpected method %v", req.Method)
506+
errMsg := fmt.Sprintf("Unexpected method %v", req.Method)
507+
w.WriteHeader(http.StatusMethodNotAllowed)
508+
w.Write([]byte(errMsg))
509+
t.Errorf("testing should fail as %s", errMsg)
510+
return
493511
}
494512
decipherableFormat := req.Header.Get("Accept")
495513
if decipherableFormat != "application/[email protected]+protobuf" {
496-
sErr = fmt.Errorf("Unexpected accept mime type %v", decipherableFormat)
514+
errMsg := fmt.Sprintf("Unexpected accept mime type %v", decipherableFormat)
515+
w.WriteHeader(http.StatusUnsupportedMediaType)
516+
w.Write([]byte(errMsg))
517+
t.Errorf("testing should fail as %s", errMsg)
518+
return
497519
}
498520

499521
mime.AddExtensionType(".pb-v1", "application/com.github.googleapis.gnostic.OpenAPIv2@68f4ded+protobuf")
500522

501523
output, err := proto.Marshal(returnedOpenAPI())
502524
if err != nil {
503-
sErr = err
525+
errMsg := fmt.Sprintf("Unexpected marshal error: %v", err)
526+
w.WriteHeader(http.StatusInternalServerError)
527+
w.Write([]byte(errMsg))
528+
t.Errorf("testing should fail as %s", errMsg)
504529
return
505530
}
506531
w.WriteHeader(http.StatusOK)
507532
w.Write(output)
508533
}))
509-
return server, sErr
534+
535+
return server, nil
510536
}
511537

512538
func TestGetOpenAPISchema(t *testing.T) {
513-
server, err := openapiSchemaFakeServer()
539+
server, err := openapiSchemaFakeServer(t)
514540
if err != nil {
515541
t.Errorf("unexpected error starting fake server: %v", err)
516542
}
@@ -527,7 +553,7 @@ func TestGetOpenAPISchema(t *testing.T) {
527553
}
528554

529555
func TestGetOpenAPISchemaForbiddenFallback(t *testing.T) {
530-
server, err := openapiSchemaDeprecatedFakeServer(http.StatusForbidden)
556+
server, err := openapiSchemaDeprecatedFakeServer(http.StatusForbidden, t)
531557
if err != nil {
532558
t.Errorf("unexpected error starting fake server: %v", err)
533559
}
@@ -544,7 +570,7 @@ func TestGetOpenAPISchemaForbiddenFallback(t *testing.T) {
544570
}
545571

546572
func TestGetOpenAPISchemaNotFoundFallback(t *testing.T) {
547-
server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotFound)
573+
server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotFound, t)
548574
if err != nil {
549575
t.Errorf("unexpected error starting fake server: %v", err)
550576
}
@@ -561,7 +587,7 @@ func TestGetOpenAPISchemaNotFoundFallback(t *testing.T) {
561587
}
562588

563589
func TestGetOpenAPISchemaNotAcceptableFallback(t *testing.T) {
564-
server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotAcceptable)
590+
server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotAcceptable, t)
565591
if err != nil {
566592
t.Errorf("unexpected error starting fake server: %v", err)
567593
}

0 commit comments

Comments
 (0)