Server timeout returns an incorrect error

Not a valid Status object in JSON
pull/6/head
Clayton Coleman 2017-02-27 15:45:34 -05:00
parent 70797f51e1
commit 60d8365751
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3
3 changed files with 15 additions and 6 deletions

View File

@ -197,7 +197,7 @@ func (tw *baseTimeoutWriter) timeout(err *apierrors.StatusError) {
if !tw.wroteHeader && !tw.hijacked {
tw.w.WriteHeader(http.StatusGatewayTimeout)
enc := json.NewEncoder(tw.w)
enc.Encode(err)
enc.Encode(&err.ErrStatus)
} else {
// The timeout writer has been used by the inner handler. There is
// no way to timeout the HTTP request at the point. We have to shutdown

View File

@ -17,15 +17,18 @@ limitations under the License.
package filters
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"strings"
"k8s.io/apimachinery/pkg/util/diff"
)
func TestTimeout(t *testing.T) {
@ -50,7 +53,7 @@ func TestTimeout(t *testing.T) {
sendResponse <- struct{}{}
res, err := http.Get(ts.URL)
if err != nil {
t.Error(err)
t.Fatal(err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got res.StatusCode %d; expected %d", res.StatusCode, http.StatusOK)
@ -67,14 +70,18 @@ func TestTimeout(t *testing.T) {
timeout <- time.Time{}
res, err = http.Get(ts.URL)
if err != nil {
t.Error(err)
t.Fatal(err)
}
if res.StatusCode != http.StatusGatewayTimeout {
t.Errorf("got res.StatusCode %d; expected %d", res.StatusCode, http.StatusServiceUnavailable)
}
body, _ = ioutil.ReadAll(res.Body)
if !strings.Contains(string(body), timeoutErr.Error()) {
t.Errorf("got body %q; expected it to contain %q", string(body), timeoutErr.Error())
status := &metav1.Status{}
if err := json.Unmarshal(body, status); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(status, &timeoutErr.ErrStatus) {
t.Errorf("unexpected object: %s", diff.ObjectReflectDiff(&timeoutErr.ErrStatus, status))
}
// Now try to send a response

2
vendor/BUILD vendored
View File

@ -10399,7 +10399,9 @@ go_test(
tags = ["automanaged"],
deps = [
"//vendor:k8s.io/apimachinery/pkg/api/errors",
"//vendor:k8s.io/apimachinery/pkg/apis/meta/v1",
"//vendor:k8s.io/apimachinery/pkg/runtime/schema",
"//vendor:k8s.io/apimachinery/pkg/util/diff",
"//vendor:k8s.io/apimachinery/pkg/util/sets",
"//vendor:k8s.io/apiserver/pkg/endpoints/filters",
"//vendor:k8s.io/apiserver/pkg/endpoints/request",