| 1 |
/* |
| 2 |
Copyright 2014 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package responsewriters |
| 18 |
|
| 19 |
import ( |
| 20 |
stderrs "errors" |
| 21 |
"net/http" |
| 22 |
"reflect" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"k8s.io/apimachinery/pkg/api/errors" |
| 26 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 28 |
) |
| 29 |
|
| 30 |
func TestBadStatusErrorToAPIStatus(t *testing.T) { |
| 31 |
err := errors.StatusError{} |
| 32 |
actual := ErrorToAPIStatus(&err) |
| 33 |
expected := &metav1.Status{ |
| 34 |
TypeMeta: metav1.TypeMeta{Kind: "Status", APIVersion: "v1"}, |
| 35 |
Status: metav1.StatusFailure, |
| 36 |
Code: 500, |
| 37 |
} |
| 38 |
if !reflect.DeepEqual(actual, expected) { |
| 39 |
t.Errorf("%s: Expected %#v, Got %#v", actual, expected, actual) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestAPIStatus(t *testing.T) { |
| 44 |
cases := map[error]metav1.Status{ |
| 45 |
errors.NewNotFound(schema.GroupResource{Group: "legacy.kubernetes.io", Resource: "foos"}, "bar"): { |
| 46 |
Status: metav1.StatusFailure, |
| 47 |
Code: http.StatusNotFound, |
| 48 |
Reason: metav1.StatusReasonNotFound, |
| 49 |
Message: "foos.legacy.kubernetes.io \"bar\" not found", |
| 50 |
Details: &metav1.StatusDetails{ |
| 51 |
Group: "legacy.kubernetes.io", |
| 52 |
Kind: "foos", |
| 53 |
Name: "bar", |
| 54 |
}, |
| 55 |
}, |
| 56 |
errors.NewAlreadyExists(schema.GroupResource{Resource: "foos"}, "bar"): { |
| 57 |
Status: metav1.StatusFailure, |
| 58 |
Code: http.StatusConflict, |
| 59 |
Reason: "AlreadyExists", |
| 60 |
Message: "foos \"bar\" already exists", |
| 61 |
Details: &metav1.StatusDetails{ |
| 62 |
Group: "", |
| 63 |
Kind: "foos", |
| 64 |
Name: "bar", |
| 65 |
}, |
| 66 |
}, |
| 67 |
errors.NewConflict(schema.GroupResource{Resource: "foos"}, "bar", stderrs.New("failure")): { |
| 68 |
Status: metav1.StatusFailure, |
| 69 |
Code: http.StatusConflict, |
| 70 |
Reason: "Conflict", |
| 71 |
Message: "Operation cannot be fulfilled on foos \"bar\": failure", |
| 72 |
Details: &metav1.StatusDetails{ |
| 73 |
Group: "", |
| 74 |
Kind: "foos", |
| 75 |
Name: "bar", |
| 76 |
}, |
| 77 |
}, |
| 78 |
} |
| 79 |
for k, v := range cases { |
| 80 |
actual := ErrorToAPIStatus(k) |
| 81 |
v.APIVersion = "v1" |
| 82 |
v.Kind = "Status" |
| 83 |
if !reflect.DeepEqual(actual, &v) { |
| 84 |
t.Errorf("%s: Expected %#v, Got %#v", k, v, actual) |
| 85 |
} |
| 86 |
} |
| 87 |
} |