mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 18:43:34 +00:00
175
vendor/k8s.io/apimachinery/pkg/util/validation/field/errors_test.go
generated
vendored
175
vendor/k8s.io/apimachinery/pkg/util/validation/field/errors_test.go
generated
vendored
@ -1,175 +0,0 @@
|
||||
/*
|
||||
Copyright 2014 The Kubernetes Authors.
|
||||
|
||||
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 field
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMakeFuncs(t *testing.T) {
|
||||
testCases := []struct {
|
||||
fn func() *Error
|
||||
expected ErrorType
|
||||
}{
|
||||
{
|
||||
func() *Error { return Invalid(NewPath("f"), "v", "d") },
|
||||
ErrorTypeInvalid,
|
||||
},
|
||||
{
|
||||
func() *Error { return NotSupported(NewPath("f"), "v", nil) },
|
||||
ErrorTypeNotSupported,
|
||||
},
|
||||
{
|
||||
func() *Error { return Duplicate(NewPath("f"), "v") },
|
||||
ErrorTypeDuplicate,
|
||||
},
|
||||
{
|
||||
func() *Error { return NotFound(NewPath("f"), "v") },
|
||||
ErrorTypeNotFound,
|
||||
},
|
||||
{
|
||||
func() *Error { return Required(NewPath("f"), "d") },
|
||||
ErrorTypeRequired,
|
||||
},
|
||||
{
|
||||
func() *Error { return InternalError(NewPath("f"), fmt.Errorf("e")) },
|
||||
ErrorTypeInternal,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
err := testCase.fn()
|
||||
if err.Type != testCase.expected {
|
||||
t.Errorf("expected Type %q, got %q", testCase.expected, err.Type)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorUsefulMessage(t *testing.T) {
|
||||
{
|
||||
s := Invalid(nil, nil, "").Error()
|
||||
t.Logf("message: %v", s)
|
||||
if !strings.Contains(s, "null") {
|
||||
t.Errorf("error message did not contain 'null': %s", s)
|
||||
}
|
||||
}
|
||||
|
||||
s := Invalid(NewPath("foo"), "bar", "deet").Error()
|
||||
t.Logf("message: %v", s)
|
||||
for _, part := range []string{"foo", "bar", "deet", ErrorTypeInvalid.String()} {
|
||||
if !strings.Contains(s, part) {
|
||||
t.Errorf("error message did not contain expected part '%v'", part)
|
||||
}
|
||||
}
|
||||
|
||||
type complicated struct {
|
||||
Baz int
|
||||
Qux string
|
||||
Inner interface{}
|
||||
KV map[string]int
|
||||
}
|
||||
s = Invalid(
|
||||
NewPath("foo"),
|
||||
&complicated{
|
||||
Baz: 1,
|
||||
Qux: "aoeu",
|
||||
Inner: &complicated{Qux: "asdf"},
|
||||
KV: map[string]int{"Billy": 2},
|
||||
},
|
||||
"detail",
|
||||
).Error()
|
||||
t.Logf("message: %v", s)
|
||||
for _, part := range []string{
|
||||
"foo", ErrorTypeInvalid.String(),
|
||||
"Baz", "Qux", "Inner", "KV", "detail",
|
||||
"1", "aoeu", "Billy", "2",
|
||||
// "asdf", TODO: re-enable once we have a better nested printer
|
||||
} {
|
||||
if !strings.Contains(s, part) {
|
||||
t.Errorf("error message did not contain expected part '%v'", part)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestToAggregate(t *testing.T) {
|
||||
testCases := struct {
|
||||
ErrList []ErrorList
|
||||
NumExpectedErrs []int
|
||||
}{
|
||||
[]ErrorList{
|
||||
nil,
|
||||
{},
|
||||
{Invalid(NewPath("f"), "v", "d")},
|
||||
{Invalid(NewPath("f"), "v", "d"), Invalid(NewPath("f"), "v", "d")},
|
||||
{Invalid(NewPath("f"), "v", "d"), InternalError(NewPath(""), fmt.Errorf("e"))},
|
||||
},
|
||||
[]int{
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
2,
|
||||
},
|
||||
}
|
||||
|
||||
if len(testCases.ErrList) != len(testCases.NumExpectedErrs) {
|
||||
t.Errorf("Mismatch: length of NumExpectedErrs does not match length of ErrList")
|
||||
}
|
||||
for i, tc := range testCases.ErrList {
|
||||
agg := tc.ToAggregate()
|
||||
numErrs := 0
|
||||
|
||||
if agg != nil {
|
||||
numErrs = len(agg.Errors())
|
||||
}
|
||||
if numErrs != testCases.NumExpectedErrs[i] {
|
||||
t.Errorf("[%d] Expected %d, got %d", i, testCases.NumExpectedErrs[i], numErrs)
|
||||
}
|
||||
|
||||
if len(tc) == 0 {
|
||||
if agg != nil {
|
||||
t.Errorf("[%d] Expected nil, got %#v", i, agg)
|
||||
}
|
||||
} else if agg == nil {
|
||||
t.Errorf("[%d] Expected non-nil", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrListFilter(t *testing.T) {
|
||||
list := ErrorList{
|
||||
Invalid(NewPath("test.field"), "", ""),
|
||||
Invalid(NewPath("field.test"), "", ""),
|
||||
Duplicate(NewPath("test"), "value"),
|
||||
}
|
||||
if len(list.Filter(NewErrorTypeMatcher(ErrorTypeDuplicate))) != 2 {
|
||||
t.Errorf("should not filter")
|
||||
}
|
||||
if len(list.Filter(NewErrorTypeMatcher(ErrorTypeInvalid))) != 1 {
|
||||
t.Errorf("should filter")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotSupported(t *testing.T) {
|
||||
notSupported := NotSupported(NewPath("f"), "v", []string{"a", "b", "c"})
|
||||
expected := `Unsupported value: "v": supported values: "a", "b", "c"`
|
||||
if notSupported.ErrorBody() != expected {
|
||||
t.Errorf("Expected: %s\n, but got: %s\n", expected, notSupported.ErrorBody())
|
||||
}
|
||||
}
|
123
vendor/k8s.io/apimachinery/pkg/util/validation/field/path_test.go
generated
vendored
123
vendor/k8s.io/apimachinery/pkg/util/validation/field/path_test.go
generated
vendored
@ -1,123 +0,0 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
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 field
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPath(t *testing.T) {
|
||||
testCases := []struct {
|
||||
op func(*Path) *Path
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
func(p *Path) *Path { return p },
|
||||
"root",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.Child("first") },
|
||||
"root.first",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.Child("second") },
|
||||
"root.first.second",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.Index(0) },
|
||||
"root.first.second[0]",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.Child("third") },
|
||||
"root.first.second[0].third",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.Index(93) },
|
||||
"root.first.second[0].third[93]",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.parent },
|
||||
"root.first.second[0].third",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.parent },
|
||||
"root.first.second[0]",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.Key("key") },
|
||||
"root.first.second[0][key]",
|
||||
},
|
||||
}
|
||||
|
||||
root := NewPath("root")
|
||||
p := root
|
||||
for i, tc := range testCases {
|
||||
p = tc.op(p)
|
||||
if p.String() != tc.expected {
|
||||
t.Errorf("[%d] Expected %q, got %q", i, tc.expected, p.String())
|
||||
}
|
||||
if p.Root() != root {
|
||||
t.Errorf("[%d] Wrong root: %#v", i, p.Root())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathMultiArg(t *testing.T) {
|
||||
testCases := []struct {
|
||||
op func(*Path) *Path
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
func(p *Path) *Path { return p },
|
||||
"root.first",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.Child("second", "third") },
|
||||
"root.first.second.third",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.Index(0) },
|
||||
"root.first.second.third[0]",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.parent },
|
||||
"root.first.second.third",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.parent },
|
||||
"root.first.second",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.parent },
|
||||
"root.first",
|
||||
},
|
||||
{
|
||||
func(p *Path) *Path { return p.parent },
|
||||
"root",
|
||||
},
|
||||
}
|
||||
|
||||
root := NewPath("root", "first")
|
||||
p := root
|
||||
for i, tc := range testCases {
|
||||
p = tc.op(p)
|
||||
if p.String() != tc.expected {
|
||||
t.Errorf("[%d] Expected %q, got %q", i, tc.expected, p.String())
|
||||
}
|
||||
if p.Root() != root.Root() {
|
||||
t.Errorf("[%d] Wrong root: %#v", i, p.Root())
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user