From 91a5c86bbcd07d775c550483d576a3ac4ccd47b5 Mon Sep 17 00:00:00 2001 From: YaoZengzeng Date: Thu, 26 Jul 2018 17:08:45 +0800 Subject: [PATCH] test: unit test for cri/stream/errors.go Signed-off-by: YaoZengzeng --- cri/stream/errors_test.go | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 cri/stream/errors_test.go diff --git a/cri/stream/errors_test.go b/cri/stream/errors_test.go new file mode 100644 index 000000000..35d341c84 --- /dev/null +++ b/cri/stream/errors_test.go @@ -0,0 +1,47 @@ +package stream + +import ( + "fmt" + "net/http" + "net/http/httptest" + "reflect" + "strconv" + "testing" + + "google.golang.org/grpc" + "google.golang.org/grpc/codes" +) + +func TestWriteError(t *testing.T) { + for _, tt := range []struct { + err error + code int + }{ + { + fmt.Errorf("error that not from grpc package"), + http.StatusInternalServerError, + }, + { + ErrorStreamingDisabled("methodNotExist"), + http.StatusNotFound, + }, + { + ErrorTooManyInFlight(), + http.StatusTooManyRequests, + }, + } { + res := httptest.NewRecorder() + WriteError(tt.err, res) + if res.Code != tt.code { + t.Fatalf("unexpected code in http response") + } + if !reflect.DeepEqual([]byte(tt.err.Error()), res.Body.Bytes()) { + t.Fatalf("unexpected content in the body of http response") + } + if grpc.Code(tt.err) == codes.ResourceExhausted { + if res.Header().Get("Retry-After") != strconv.Itoa(int(CacheTTL.Seconds())) { + t.Fatalf("the Retry-After field of http header is not expected when the error is resource exhausted") + } + } + } +}