Merge pull request #42187 from smarterclayton/wrong_error_from_timeout

Automatic merge from submit-queue (batch tested with PRs 41306, 42187, 41666, 42275, 42266)

Server timeout returns an incorrect error

Not a valid Status object in JSON

Part of #42163
This commit is contained in:
Kubernetes Submit Queue 2017-03-03 10:54:40 -08:00 committed by GitHub
commit 4932b1422c
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

@ -10508,7 +10508,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",