2014-09-03 21:16:00 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-09-03 21:16:00 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-12-10 18:32:29 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-09-09 21:59:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2015-11-06 23:30:52 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/validation/field"
|
2014-09-03 21:16:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestErrorNew(t *testing.T) {
|
2015-12-10 18:32:29 +00:00
|
|
|
err := NewAlreadyExists(api.Resource("tests"), "1")
|
2014-09-03 21:16:00 +00:00
|
|
|
if !IsAlreadyExists(err) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to be %s", unversioned.StatusReasonAlreadyExists)
|
2014-09-03 21:16:00 +00:00
|
|
|
}
|
|
|
|
if IsConflict(err) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to not be %s", unversioned.StatusReasonConflict)
|
2014-09-03 21:16:00 +00:00
|
|
|
}
|
|
|
|
if IsNotFound(err) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf(fmt.Sprintf("expected to not be %s", unversioned.StatusReasonNotFound))
|
2014-09-03 21:16:00 +00:00
|
|
|
}
|
|
|
|
if IsInvalid(err) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to not be %s", unversioned.StatusReasonInvalid)
|
2014-09-03 21:16:00 +00:00
|
|
|
}
|
2015-01-12 05:12:49 +00:00
|
|
|
if IsBadRequest(err) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to not be %s", unversioned.StatusReasonBadRequest)
|
2015-01-12 05:12:49 +00:00
|
|
|
}
|
2015-01-29 04:11:29 +00:00
|
|
|
if IsForbidden(err) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to not be %s", unversioned.StatusReasonForbidden)
|
2015-01-29 04:11:29 +00:00
|
|
|
}
|
2015-02-12 17:24:34 +00:00
|
|
|
if IsServerTimeout(err) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to not be %s", unversioned.StatusReasonServerTimeout)
|
2015-01-29 04:11:29 +00:00
|
|
|
}
|
2015-01-12 05:12:49 +00:00
|
|
|
if IsMethodNotSupported(err) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to not be %s", unversioned.StatusReasonMethodNotAllowed)
|
2015-01-12 05:12:49 +00:00
|
|
|
}
|
2014-09-03 21:16:00 +00:00
|
|
|
|
2015-12-10 18:32:29 +00:00
|
|
|
if !IsConflict(NewConflict(api.Resource("tests"), "2", errors.New("message"))) {
|
2014-09-03 21:16:00 +00:00
|
|
|
t.Errorf("expected to be conflict")
|
|
|
|
}
|
2015-12-10 18:32:29 +00:00
|
|
|
if !IsNotFound(NewNotFound(api.Resource("tests"), "3")) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to be %s", unversioned.StatusReasonNotFound)
|
2014-09-03 21:16:00 +00:00
|
|
|
}
|
2015-12-10 18:32:29 +00:00
|
|
|
if !IsInvalid(NewInvalid(api.Kind("Test"), "2", nil)) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to be %s", unversioned.StatusReasonInvalid)
|
2014-09-03 21:16:00 +00:00
|
|
|
}
|
2015-01-12 05:12:49 +00:00
|
|
|
if !IsBadRequest(NewBadRequest("reason")) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to be %s", unversioned.StatusReasonBadRequest)
|
2015-01-12 05:12:49 +00:00
|
|
|
}
|
2015-12-10 18:32:29 +00:00
|
|
|
if !IsForbidden(NewForbidden(api.Resource("tests"), "2", errors.New("reason"))) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to be %s", unversioned.StatusReasonForbidden)
|
2015-01-29 04:11:29 +00:00
|
|
|
}
|
2015-03-24 02:56:22 +00:00
|
|
|
if !IsUnauthorized(NewUnauthorized("reason")) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to be %s", unversioned.StatusReasonUnauthorized)
|
2015-03-24 02:56:22 +00:00
|
|
|
}
|
2015-12-10 18:32:29 +00:00
|
|
|
if !IsServerTimeout(NewServerTimeout(api.Resource("tests"), "reason", 0)) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to be %s", unversioned.StatusReasonServerTimeout)
|
2015-03-24 02:56:22 +00:00
|
|
|
}
|
2015-12-10 18:32:29 +00:00
|
|
|
if time, ok := SuggestsClientDelay(NewServerTimeout(api.Resource("tests"), "doing something", 10)); time != 10 || !ok {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to be %s", unversioned.StatusReasonServerTimeout)
|
2015-01-29 04:11:29 +00:00
|
|
|
}
|
2015-03-24 02:56:22 +00:00
|
|
|
if time, ok := SuggestsClientDelay(NewTimeoutError("test reason", 10)); time != 10 || !ok {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to be %s", unversioned.StatusReasonTimeout)
|
2015-03-24 02:56:22 +00:00
|
|
|
}
|
2015-12-10 18:32:29 +00:00
|
|
|
if !IsMethodNotSupported(NewMethodNotSupported(api.Resource("foos"), "delete")) {
|
2015-09-09 21:59:11 +00:00
|
|
|
t.Errorf("expected to be %s", unversioned.StatusReasonMethodNotAllowed)
|
2015-01-12 05:12:49 +00:00
|
|
|
}
|
2014-09-03 21:16:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewInvalid(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2015-11-06 23:30:52 +00:00
|
|
|
Err *field.Error
|
2015-09-09 21:59:11 +00:00
|
|
|
Details *unversioned.StatusDetails
|
2014-09-03 21:16:00 +00:00
|
|
|
}{
|
|
|
|
{
|
2015-11-10 20:59:41 +00:00
|
|
|
field.Duplicate(field.NewPath("field[0].name"), "bar"),
|
2015-09-09 21:59:11 +00:00
|
|
|
&unversioned.StatusDetails{
|
2015-12-10 18:32:29 +00:00
|
|
|
Kind: "Kind",
|
2015-06-05 13:45:59 +00:00
|
|
|
Name: "name",
|
2015-09-09 21:59:11 +00:00
|
|
|
Causes: []unversioned.StatusCause{{
|
|
|
|
Type: unversioned.CauseTypeFieldValueDuplicate,
|
2014-09-03 21:16:00 +00:00
|
|
|
Field: "field[0].name",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-11-10 20:59:41 +00:00
|
|
|
field.Invalid(field.NewPath("field[0].name"), "bar", "detail"),
|
2015-09-09 21:59:11 +00:00
|
|
|
&unversioned.StatusDetails{
|
2015-12-10 18:32:29 +00:00
|
|
|
Kind: "Kind",
|
2015-06-05 13:45:59 +00:00
|
|
|
Name: "name",
|
2015-09-09 21:59:11 +00:00
|
|
|
Causes: []unversioned.StatusCause{{
|
|
|
|
Type: unversioned.CauseTypeFieldValueInvalid,
|
2014-09-03 21:16:00 +00:00
|
|
|
Field: "field[0].name",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-11-10 20:59:41 +00:00
|
|
|
field.NotFound(field.NewPath("field[0].name"), "bar"),
|
2015-09-09 21:59:11 +00:00
|
|
|
&unversioned.StatusDetails{
|
2015-12-10 18:32:29 +00:00
|
|
|
Kind: "Kind",
|
2015-06-05 13:45:59 +00:00
|
|
|
Name: "name",
|
2015-09-09 21:59:11 +00:00
|
|
|
Causes: []unversioned.StatusCause{{
|
|
|
|
Type: unversioned.CauseTypeFieldValueNotFound,
|
2014-09-03 21:16:00 +00:00
|
|
|
Field: "field[0].name",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-11-10 20:59:41 +00:00
|
|
|
field.NotSupported(field.NewPath("field[0].name"), "bar", nil),
|
2015-09-09 21:59:11 +00:00
|
|
|
&unversioned.StatusDetails{
|
2015-12-10 18:32:29 +00:00
|
|
|
Kind: "Kind",
|
2015-06-05 13:45:59 +00:00
|
|
|
Name: "name",
|
2015-09-09 21:59:11 +00:00
|
|
|
Causes: []unversioned.StatusCause{{
|
|
|
|
Type: unversioned.CauseTypeFieldValueNotSupported,
|
2014-09-03 21:16:00 +00:00
|
|
|
Field: "field[0].name",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-11-14 20:26:04 +00:00
|
|
|
field.Required(field.NewPath("field[0].name"), ""),
|
2015-09-09 21:59:11 +00:00
|
|
|
&unversioned.StatusDetails{
|
2015-12-10 18:32:29 +00:00
|
|
|
Kind: "Kind",
|
2015-06-05 13:45:59 +00:00
|
|
|
Name: "name",
|
2015-09-09 21:59:11 +00:00
|
|
|
Causes: []unversioned.StatusCause{{
|
|
|
|
Type: unversioned.CauseTypeFieldValueRequired,
|
2014-09-03 21:16:00 +00:00
|
|
|
Field: "field[0].name",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
vErr, expected := testCase.Err, testCase.Details
|
2015-06-30 23:43:33 +00:00
|
|
|
expected.Causes[0].Message = vErr.ErrorBody()
|
2015-12-10 18:32:29 +00:00
|
|
|
err := NewInvalid(api.Kind("Kind"), "name", field.ErrorList{vErr})
|
2014-11-21 00:01:42 +00:00
|
|
|
status := err.(*StatusError).ErrStatus
|
2015-09-09 21:59:11 +00:00
|
|
|
if status.Code != 422 || status.Reason != unversioned.StatusReasonInvalid {
|
2014-09-03 21:16:00 +00:00
|
|
|
t.Errorf("%d: unexpected status: %#v", i, status)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expected, status.Details) {
|
2014-09-28 04:48:35 +00:00
|
|
|
t.Errorf("%d: expected %#v, got %#v", i, expected, status.Details)
|
2014-09-03 21:16:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_reasonForError(t *testing.T) {
|
2015-09-09 21:59:11 +00:00
|
|
|
if e, a := unversioned.StatusReasonUnknown, reasonForError(nil); e != a {
|
2014-09-03 21:16:00 +00:00
|
|
|
t.Errorf("unexpected reason type: %#v", a)
|
|
|
|
}
|
|
|
|
}
|
2014-09-23 00:37:12 +00:00
|
|
|
|
|
|
|
type TestType struct{}
|
|
|
|
|
2015-12-08 03:01:12 +00:00
|
|
|
func (obj *TestType) GetObjectKind() unversioned.ObjectKind { return unversioned.EmptyObjectKind }
|
2014-09-23 00:37:12 +00:00
|
|
|
|
|
|
|
func TestFromObject(t *testing.T) {
|
|
|
|
table := []struct {
|
|
|
|
obj runtime.Object
|
|
|
|
message string
|
|
|
|
}{
|
2015-09-09 21:59:11 +00:00
|
|
|
{&unversioned.Status{Message: "foobar"}, "foobar"},
|
2014-09-23 00:37:12 +00:00
|
|
|
{&TestType{}, "unexpected object: &{}"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range table {
|
|
|
|
if e, a := item.message, FromObject(item.obj).Error(); e != a {
|
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|