mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 10:33:35 +00:00
vendor files
This commit is contained in:
42
vendor/k8s.io/apimachinery/pkg/fields/BUILD
generated
vendored
Normal file
42
vendor/k8s.io/apimachinery/pkg/fields/BUILD
generated
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"fields_test.go",
|
||||
"selector_test.go",
|
||||
],
|
||||
importpath = "k8s.io/apimachinery/pkg/fields",
|
||||
library = ":go_default_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"doc.go",
|
||||
"fields.go",
|
||||
"requirements.go",
|
||||
"selector.go",
|
||||
],
|
||||
importpath = "k8s.io/apimachinery/pkg/fields",
|
||||
deps = ["//vendor/k8s.io/apimachinery/pkg/selection:go_default_library"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
19
vendor/k8s.io/apimachinery/pkg/fields/doc.go
generated
vendored
Normal file
19
vendor/k8s.io/apimachinery/pkg/fields/doc.go
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
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 fields implements a simple field system, parsing and matching
|
||||
// selectors with sets of fields.
|
||||
package fields // import "k8s.io/apimachinery/pkg/fields"
|
62
vendor/k8s.io/apimachinery/pkg/fields/fields.go
generated
vendored
Normal file
62
vendor/k8s.io/apimachinery/pkg/fields/fields.go
generated
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
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 fields
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Fields allows you to present fields independently from their storage.
|
||||
type Fields interface {
|
||||
// Has returns whether the provided field exists.
|
||||
Has(field string) (exists bool)
|
||||
|
||||
// Get returns the value for the provided field.
|
||||
Get(field string) (value string)
|
||||
}
|
||||
|
||||
// Set is a map of field:value. It implements Fields.
|
||||
type Set map[string]string
|
||||
|
||||
// String returns all fields listed as a human readable string.
|
||||
// Conveniently, exactly the format that ParseSelector takes.
|
||||
func (ls Set) String() string {
|
||||
selector := make([]string, 0, len(ls))
|
||||
for key, value := range ls {
|
||||
selector = append(selector, key+"="+value)
|
||||
}
|
||||
// Sort for determinism.
|
||||
sort.StringSlice(selector).Sort()
|
||||
return strings.Join(selector, ",")
|
||||
}
|
||||
|
||||
// Has returns whether the provided field exists in the map.
|
||||
func (ls Set) Has(field string) bool {
|
||||
_, exists := ls[field]
|
||||
return exists
|
||||
}
|
||||
|
||||
// Get returns the value in the map for the provided field.
|
||||
func (ls Set) Get(field string) string {
|
||||
return ls[field]
|
||||
}
|
||||
|
||||
// AsSelector converts fields into a selectors.
|
||||
func (ls Set) AsSelector() Selector {
|
||||
return SelectorFromSet(ls)
|
||||
}
|
57
vendor/k8s.io/apimachinery/pkg/fields/fields_test.go
generated
vendored
Normal file
57
vendor/k8s.io/apimachinery/pkg/fields/fields_test.go
generated
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
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 fields
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func matches(t *testing.T, ls Set, want string) {
|
||||
if ls.String() != want {
|
||||
t.Errorf("Expected '%s', but got '%s'", want, ls.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetString(t *testing.T) {
|
||||
matches(t, Set{"x": "y"}, "x=y")
|
||||
matches(t, Set{"foo": "bar"}, "foo=bar")
|
||||
matches(t, Set{"foo": "bar", "baz": "qup"}, "baz=qup,foo=bar")
|
||||
}
|
||||
|
||||
func TestFieldHas(t *testing.T) {
|
||||
fieldHasTests := []struct {
|
||||
Ls Fields
|
||||
Key string
|
||||
Has bool
|
||||
}{
|
||||
{Set{"x": "y"}, "x", true},
|
||||
{Set{"x": ""}, "x", true},
|
||||
{Set{"x": "y"}, "foo", false},
|
||||
}
|
||||
for _, lh := range fieldHasTests {
|
||||
if has := lh.Ls.Has(lh.Key); has != lh.Has {
|
||||
t.Errorf("%#v.Has(%#v) => %v, expected %v", lh.Ls, lh.Key, has, lh.Has)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldGet(t *testing.T) {
|
||||
ls := Set{"x": "y"}
|
||||
if ls.Get("x") != "y" {
|
||||
t.Errorf("Set.Get is broken")
|
||||
}
|
||||
}
|
30
vendor/k8s.io/apimachinery/pkg/fields/requirements.go
generated
vendored
Normal file
30
vendor/k8s.io/apimachinery/pkg/fields/requirements.go
generated
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
Copyright 2016 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 fields
|
||||
|
||||
import "k8s.io/apimachinery/pkg/selection"
|
||||
|
||||
// Requirements is AND of all requirements.
|
||||
type Requirements []Requirement
|
||||
|
||||
// Requirement contains a field, a value, and an operator that relates the field and value.
|
||||
// This is currently for reading internal selection information of field selector.
|
||||
type Requirement struct {
|
||||
Operator selection.Operator
|
||||
Field string
|
||||
Value string
|
||||
}
|
455
vendor/k8s.io/apimachinery/pkg/fields/selector.go
generated
vendored
Normal file
455
vendor/k8s.io/apimachinery/pkg/fields/selector.go
generated
vendored
Normal file
@ -0,0 +1,455 @@
|
||||
/*
|
||||
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 fields
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"k8s.io/apimachinery/pkg/selection"
|
||||
)
|
||||
|
||||
// Selector represents a field selector.
|
||||
type Selector interface {
|
||||
// Matches returns true if this selector matches the given set of fields.
|
||||
Matches(Fields) bool
|
||||
|
||||
// Empty returns true if this selector does not restrict the selection space.
|
||||
Empty() bool
|
||||
|
||||
// RequiresExactMatch allows a caller to introspect whether a given selector
|
||||
// requires a single specific field to be set, and if so returns the value it
|
||||
// requires.
|
||||
RequiresExactMatch(field string) (value string, found bool)
|
||||
|
||||
// Transform returns a new copy of the selector after TransformFunc has been
|
||||
// applied to the entire selector, or an error if fn returns an error.
|
||||
// If for a given requirement both field and value are transformed to empty
|
||||
// string, the requirement is skipped.
|
||||
Transform(fn TransformFunc) (Selector, error)
|
||||
|
||||
// Requirements converts this interface to Requirements to expose
|
||||
// more detailed selection information.
|
||||
Requirements() Requirements
|
||||
|
||||
// String returns a human readable string that represents this selector.
|
||||
String() string
|
||||
|
||||
// Make a deep copy of the selector.
|
||||
DeepCopySelector() Selector
|
||||
}
|
||||
|
||||
// Everything returns a selector that matches all fields.
|
||||
func Everything() Selector {
|
||||
return andTerm{}
|
||||
}
|
||||
|
||||
type hasTerm struct {
|
||||
field, value string
|
||||
}
|
||||
|
||||
func (t *hasTerm) Matches(ls Fields) bool {
|
||||
return ls.Get(t.field) == t.value
|
||||
}
|
||||
|
||||
func (t *hasTerm) Empty() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *hasTerm) RequiresExactMatch(field string) (value string, found bool) {
|
||||
if t.field == field {
|
||||
return t.value, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func (t *hasTerm) Transform(fn TransformFunc) (Selector, error) {
|
||||
field, value, err := fn(t.field, t.value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(field) == 0 && len(value) == 0 {
|
||||
return Everything(), nil
|
||||
}
|
||||
return &hasTerm{field, value}, nil
|
||||
}
|
||||
|
||||
func (t *hasTerm) Requirements() Requirements {
|
||||
return []Requirement{{
|
||||
Field: t.field,
|
||||
Operator: selection.Equals,
|
||||
Value: t.value,
|
||||
}}
|
||||
}
|
||||
|
||||
func (t *hasTerm) String() string {
|
||||
return fmt.Sprintf("%v=%v", t.field, EscapeValue(t.value))
|
||||
}
|
||||
|
||||
func (t *hasTerm) DeepCopySelector() Selector {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(hasTerm)
|
||||
*out = *t
|
||||
return out
|
||||
}
|
||||
|
||||
type notHasTerm struct {
|
||||
field, value string
|
||||
}
|
||||
|
||||
func (t *notHasTerm) Matches(ls Fields) bool {
|
||||
return ls.Get(t.field) != t.value
|
||||
}
|
||||
|
||||
func (t *notHasTerm) Empty() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *notHasTerm) RequiresExactMatch(field string) (value string, found bool) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
func (t *notHasTerm) Transform(fn TransformFunc) (Selector, error) {
|
||||
field, value, err := fn(t.field, t.value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(field) == 0 && len(value) == 0 {
|
||||
return Everything(), nil
|
||||
}
|
||||
return ¬HasTerm{field, value}, nil
|
||||
}
|
||||
|
||||
func (t *notHasTerm) Requirements() Requirements {
|
||||
return []Requirement{{
|
||||
Field: t.field,
|
||||
Operator: selection.NotEquals,
|
||||
Value: t.value,
|
||||
}}
|
||||
}
|
||||
|
||||
func (t *notHasTerm) String() string {
|
||||
return fmt.Sprintf("%v!=%v", t.field, EscapeValue(t.value))
|
||||
}
|
||||
|
||||
func (t *notHasTerm) DeepCopySelector() Selector {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(notHasTerm)
|
||||
*out = *t
|
||||
return out
|
||||
}
|
||||
|
||||
type andTerm []Selector
|
||||
|
||||
func (t andTerm) Matches(ls Fields) bool {
|
||||
for _, q := range t {
|
||||
if !q.Matches(ls) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (t andTerm) Empty() bool {
|
||||
if t == nil {
|
||||
return true
|
||||
}
|
||||
if len([]Selector(t)) == 0 {
|
||||
return true
|
||||
}
|
||||
for i := range t {
|
||||
if !t[i].Empty() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (t andTerm) RequiresExactMatch(field string) (string, bool) {
|
||||
if t == nil || len([]Selector(t)) == 0 {
|
||||
return "", false
|
||||
}
|
||||
for i := range t {
|
||||
if value, found := t[i].RequiresExactMatch(field); found {
|
||||
return value, found
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func (t andTerm) Transform(fn TransformFunc) (Selector, error) {
|
||||
next := make([]Selector, 0, len([]Selector(t)))
|
||||
for _, s := range []Selector(t) {
|
||||
n, err := s.Transform(fn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !n.Empty() {
|
||||
next = append(next, n)
|
||||
}
|
||||
}
|
||||
return andTerm(next), nil
|
||||
}
|
||||
|
||||
func (t andTerm) Requirements() Requirements {
|
||||
reqs := make([]Requirement, 0, len(t))
|
||||
for _, s := range []Selector(t) {
|
||||
rs := s.Requirements()
|
||||
reqs = append(reqs, rs...)
|
||||
}
|
||||
return reqs
|
||||
}
|
||||
|
||||
func (t andTerm) String() string {
|
||||
var terms []string
|
||||
for _, q := range t {
|
||||
terms = append(terms, q.String())
|
||||
}
|
||||
return strings.Join(terms, ",")
|
||||
}
|
||||
|
||||
func (t andTerm) DeepCopySelector() Selector {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]Selector, len(t))
|
||||
for i := range t {
|
||||
out[i] = t[i].DeepCopySelector()
|
||||
}
|
||||
return andTerm(out)
|
||||
}
|
||||
|
||||
// SelectorFromSet returns a Selector which will match exactly the given Set. A
|
||||
// nil Set is considered equivalent to Everything().
|
||||
func SelectorFromSet(ls Set) Selector {
|
||||
if ls == nil {
|
||||
return Everything()
|
||||
}
|
||||
items := make([]Selector, 0, len(ls))
|
||||
for field, value := range ls {
|
||||
items = append(items, &hasTerm{field: field, value: value})
|
||||
}
|
||||
if len(items) == 1 {
|
||||
return items[0]
|
||||
}
|
||||
return andTerm(items)
|
||||
}
|
||||
|
||||
// valueEscaper prefixes \,= characters with a backslash
|
||||
var valueEscaper = strings.NewReplacer(
|
||||
// escape \ characters
|
||||
`\`, `\\`,
|
||||
// then escape , and = characters to allow unambiguous parsing of the value in a fieldSelector
|
||||
`,`, `\,`,
|
||||
`=`, `\=`,
|
||||
)
|
||||
|
||||
// EscapeValue escapes an arbitrary literal string for use as a fieldSelector value
|
||||
func EscapeValue(s string) string {
|
||||
return valueEscaper.Replace(s)
|
||||
}
|
||||
|
||||
// InvalidEscapeSequence indicates an error occurred unescaping a field selector
|
||||
type InvalidEscapeSequence struct {
|
||||
sequence string
|
||||
}
|
||||
|
||||
func (i InvalidEscapeSequence) Error() string {
|
||||
return fmt.Sprintf("invalid field selector: invalid escape sequence: %s", i.sequence)
|
||||
}
|
||||
|
||||
// UnescapedRune indicates an error occurred unescaping a field selector
|
||||
type UnescapedRune struct {
|
||||
r rune
|
||||
}
|
||||
|
||||
func (i UnescapedRune) Error() string {
|
||||
return fmt.Sprintf("invalid field selector: unescaped character in value: %v", i.r)
|
||||
}
|
||||
|
||||
// UnescapeValue unescapes a fieldSelector value and returns the original literal value.
|
||||
// May return the original string if it contains no escaped or special characters.
|
||||
func UnescapeValue(s string) (string, error) {
|
||||
// if there's no escaping or special characters, just return to avoid allocation
|
||||
if !strings.ContainsAny(s, `\,=`) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
v := bytes.NewBuffer(make([]byte, 0, len(s)))
|
||||
inSlash := false
|
||||
for _, c := range s {
|
||||
if inSlash {
|
||||
switch c {
|
||||
case '\\', ',', '=':
|
||||
// omit the \ for recognized escape sequences
|
||||
v.WriteRune(c)
|
||||
default:
|
||||
// error on unrecognized escape sequences
|
||||
return "", InvalidEscapeSequence{sequence: string([]rune{'\\', c})}
|
||||
}
|
||||
inSlash = false
|
||||
continue
|
||||
}
|
||||
|
||||
switch c {
|
||||
case '\\':
|
||||
inSlash = true
|
||||
case ',', '=':
|
||||
// unescaped , and = characters are not allowed in field selector values
|
||||
return "", UnescapedRune{r: c}
|
||||
default:
|
||||
v.WriteRune(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Ending with a single backslash is an invalid sequence
|
||||
if inSlash {
|
||||
return "", InvalidEscapeSequence{sequence: "\\"}
|
||||
}
|
||||
|
||||
return v.String(), nil
|
||||
}
|
||||
|
||||
// ParseSelectorOrDie takes a string representing a selector and returns an
|
||||
// object suitable for matching, or panic when an error occur.
|
||||
func ParseSelectorOrDie(s string) Selector {
|
||||
selector, err := ParseSelector(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return selector
|
||||
}
|
||||
|
||||
// ParseSelector takes a string representing a selector and returns an
|
||||
// object suitable for matching, or an error.
|
||||
func ParseSelector(selector string) (Selector, error) {
|
||||
return parseSelector(selector,
|
||||
func(lhs, rhs string) (newLhs, newRhs string, err error) {
|
||||
return lhs, rhs, nil
|
||||
})
|
||||
}
|
||||
|
||||
// ParseAndTransformSelector parses the selector and runs them through the given TransformFunc.
|
||||
func ParseAndTransformSelector(selector string, fn TransformFunc) (Selector, error) {
|
||||
return parseSelector(selector, fn)
|
||||
}
|
||||
|
||||
// TransformFunc transforms selectors.
|
||||
type TransformFunc func(field, value string) (newField, newValue string, err error)
|
||||
|
||||
// splitTerms returns the comma-separated terms contained in the given fieldSelector.
|
||||
// Backslash-escaped commas are treated as data instead of delimiters, and are included in the returned terms, with the leading backslash preserved.
|
||||
func splitTerms(fieldSelector string) []string {
|
||||
if len(fieldSelector) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
terms := make([]string, 0, 1)
|
||||
startIndex := 0
|
||||
inSlash := false
|
||||
for i, c := range fieldSelector {
|
||||
switch {
|
||||
case inSlash:
|
||||
inSlash = false
|
||||
case c == '\\':
|
||||
inSlash = true
|
||||
case c == ',':
|
||||
terms = append(terms, fieldSelector[startIndex:i])
|
||||
startIndex = i + 1
|
||||
}
|
||||
}
|
||||
|
||||
terms = append(terms, fieldSelector[startIndex:])
|
||||
|
||||
return terms
|
||||
}
|
||||
|
||||
const (
|
||||
notEqualOperator = "!="
|
||||
doubleEqualOperator = "=="
|
||||
equalOperator = "="
|
||||
)
|
||||
|
||||
// termOperators holds the recognized operators supported in fieldSelectors.
|
||||
// doubleEqualOperator and equal are equivalent, but doubleEqualOperator is checked first
|
||||
// to avoid leaving a leading = character on the rhs value.
|
||||
var termOperators = []string{notEqualOperator, doubleEqualOperator, equalOperator}
|
||||
|
||||
// splitTerm returns the lhs, operator, and rhs parsed from the given term, along with an indicator of whether the parse was successful.
|
||||
// no escaping of special characters is supported in the lhs value, so the first occurance of a recognized operator is used as the split point.
|
||||
// the literal rhs is returned, and the caller is responsible for applying any desired unescaping.
|
||||
func splitTerm(term string) (lhs, op, rhs string, ok bool) {
|
||||
for i := range term {
|
||||
remaining := term[i:]
|
||||
for _, op := range termOperators {
|
||||
if strings.HasPrefix(remaining, op) {
|
||||
return term[0:i], op, term[i+len(op):], true
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", "", "", false
|
||||
}
|
||||
|
||||
func parseSelector(selector string, fn TransformFunc) (Selector, error) {
|
||||
parts := splitTerms(selector)
|
||||
sort.StringSlice(parts).Sort()
|
||||
var items []Selector
|
||||
for _, part := range parts {
|
||||
if part == "" {
|
||||
continue
|
||||
}
|
||||
lhs, op, rhs, ok := splitTerm(part)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid selector: '%s'; can't understand '%s'", selector, part)
|
||||
}
|
||||
unescapedRHS, err := UnescapeValue(rhs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch op {
|
||||
case notEqualOperator:
|
||||
items = append(items, ¬HasTerm{field: lhs, value: unescapedRHS})
|
||||
case doubleEqualOperator:
|
||||
items = append(items, &hasTerm{field: lhs, value: unescapedRHS})
|
||||
case equalOperator:
|
||||
items = append(items, &hasTerm{field: lhs, value: unescapedRHS})
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid selector: '%s'; can't understand '%s'", selector, part)
|
||||
}
|
||||
}
|
||||
if len(items) == 1 {
|
||||
return items[0].Transform(fn)
|
||||
}
|
||||
return andTerm(items).Transform(fn)
|
||||
}
|
||||
|
||||
// OneTermEqualSelector returns an object that matches objects where one field/field equals one value.
|
||||
// Cannot return an error.
|
||||
func OneTermEqualSelector(k, v string) Selector {
|
||||
return &hasTerm{field: k, value: v}
|
||||
}
|
||||
|
||||
// AndSelectors creates a selector that is the logical AND of all the given selectors
|
||||
func AndSelectors(selectors ...Selector) Selector {
|
||||
return andTerm(selectors)
|
||||
}
|
397
vendor/k8s.io/apimachinery/pkg/fields/selector_test.go
generated
vendored
Normal file
397
vendor/k8s.io/apimachinery/pkg/fields/selector_test.go
generated
vendored
Normal file
@ -0,0 +1,397 @@
|
||||
/*
|
||||
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 fields
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSplitTerms(t *testing.T) {
|
||||
testcases := map[string][]string{
|
||||
// Simple selectors
|
||||
`a`: {`a`},
|
||||
`a=avalue`: {`a=avalue`},
|
||||
`a=avalue,b=bvalue`: {`a=avalue`, `b=bvalue`},
|
||||
`a=avalue,b==bvalue,c!=cvalue`: {`a=avalue`, `b==bvalue`, `c!=cvalue`},
|
||||
|
||||
// Empty terms
|
||||
``: nil,
|
||||
`a=a,`: {`a=a`, ``},
|
||||
`,a=a`: {``, `a=a`},
|
||||
|
||||
// Escaped values
|
||||
`k=\,,k2=v2`: {`k=\,`, `k2=v2`}, // escaped comma in value
|
||||
`k=\\,k2=v2`: {`k=\\`, `k2=v2`}, // escaped backslash, unescaped comma
|
||||
`k=\\\,,k2=v2`: {`k=\\\,`, `k2=v2`}, // escaped backslash and comma
|
||||
`k=\a\b\`: {`k=\a\b\`}, // non-escape sequences
|
||||
`k=\`: {`k=\`}, // orphan backslash
|
||||
|
||||
// Multi-byte
|
||||
`함=수,목=록`: {`함=수`, `목=록`},
|
||||
}
|
||||
|
||||
for selector, expectedTerms := range testcases {
|
||||
if terms := splitTerms(selector); !reflect.DeepEqual(terms, expectedTerms) {
|
||||
t.Errorf("splitSelectors(`%s`): Expected\n%#v\ngot\n%#v", selector, expectedTerms, terms)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitTerm(t *testing.T) {
|
||||
testcases := map[string]struct {
|
||||
lhs string
|
||||
op string
|
||||
rhs string
|
||||
ok bool
|
||||
}{
|
||||
// Simple terms
|
||||
`a=value`: {lhs: `a`, op: `=`, rhs: `value`, ok: true},
|
||||
`b==value`: {lhs: `b`, op: `==`, rhs: `value`, ok: true},
|
||||
`c!=value`: {lhs: `c`, op: `!=`, rhs: `value`, ok: true},
|
||||
|
||||
// Empty or invalid terms
|
||||
``: {lhs: ``, op: ``, rhs: ``, ok: false},
|
||||
`a`: {lhs: ``, op: ``, rhs: ``, ok: false},
|
||||
|
||||
// Escaped values
|
||||
`k=\,`: {lhs: `k`, op: `=`, rhs: `\,`, ok: true},
|
||||
`k=\=`: {lhs: `k`, op: `=`, rhs: `\=`, ok: true},
|
||||
`k=\\\a\b\=\,\`: {lhs: `k`, op: `=`, rhs: `\\\a\b\=\,\`, ok: true},
|
||||
|
||||
// Multi-byte
|
||||
`함=수`: {lhs: `함`, op: `=`, rhs: `수`, ok: true},
|
||||
}
|
||||
|
||||
for term, expected := range testcases {
|
||||
lhs, op, rhs, ok := splitTerm(term)
|
||||
if lhs != expected.lhs || op != expected.op || rhs != expected.rhs || ok != expected.ok {
|
||||
t.Errorf(
|
||||
"splitTerm(`%s`): Expected\n%s,%s,%s,%v\nGot\n%s,%s,%s,%v",
|
||||
term,
|
||||
expected.lhs, expected.op, expected.rhs, expected.ok,
|
||||
lhs, op, rhs, ok,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEscapeValue(t *testing.T) {
|
||||
// map values to their normalized escaped values
|
||||
testcases := map[string]string{
|
||||
``: ``,
|
||||
`a`: `a`,
|
||||
`=`: `\=`,
|
||||
`,`: `\,`,
|
||||
`\`: `\\`,
|
||||
`\=\,\`: `\\\=\\\,\\`,
|
||||
}
|
||||
|
||||
for unescapedValue, escapedValue := range testcases {
|
||||
actualEscaped := EscapeValue(unescapedValue)
|
||||
if actualEscaped != escapedValue {
|
||||
t.Errorf("EscapeValue(%s): expected %s, got %s", unescapedValue, escapedValue, actualEscaped)
|
||||
}
|
||||
|
||||
actualUnescaped, err := UnescapeValue(escapedValue)
|
||||
if err != nil {
|
||||
t.Errorf("UnescapeValue(%s): unexpected error %v", escapedValue, err)
|
||||
}
|
||||
if actualUnescaped != unescapedValue {
|
||||
t.Errorf("UnescapeValue(%s): expected %s, got %s", escapedValue, unescapedValue, actualUnescaped)
|
||||
}
|
||||
}
|
||||
|
||||
// test invalid escape sequences
|
||||
invalidTestcases := []string{
|
||||
`\`, // orphan slash is invalid
|
||||
`\\\`, // orphan slash is invalid
|
||||
`\a`, // unrecognized escape sequence is invalid
|
||||
}
|
||||
for _, invalidValue := range invalidTestcases {
|
||||
_, err := UnescapeValue(invalidValue)
|
||||
if _, ok := err.(InvalidEscapeSequence); !ok || err == nil {
|
||||
t.Errorf("UnescapeValue(%s): expected invalid escape sequence error, got %#v", invalidValue, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectorParse(t *testing.T) {
|
||||
testGoodStrings := []string{
|
||||
"x=a,y=b,z=c",
|
||||
"",
|
||||
"x!=a,y=b",
|
||||
`x=a||y\=b`,
|
||||
`x=a\=\=b`,
|
||||
}
|
||||
testBadStrings := []string{
|
||||
"x=a||y=b",
|
||||
"x==a==b",
|
||||
"x=a,b",
|
||||
"x in (a)",
|
||||
"x in (a,b,c)",
|
||||
"x",
|
||||
}
|
||||
for _, test := range testGoodStrings {
|
||||
lq, err := ParseSelector(test)
|
||||
if err != nil {
|
||||
t.Errorf("%v: error %v (%#v)\n", test, err, err)
|
||||
}
|
||||
if test != lq.String() {
|
||||
t.Errorf("%v restring gave: %v\n", test, lq.String())
|
||||
}
|
||||
}
|
||||
for _, test := range testBadStrings {
|
||||
_, err := ParseSelector(test)
|
||||
if err == nil {
|
||||
t.Errorf("%v: did not get expected error\n", test)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeterministicParse(t *testing.T) {
|
||||
s1, err := ParseSelector("x=a,a=x")
|
||||
s2, err2 := ParseSelector("a=x,x=a")
|
||||
if err != nil || err2 != nil {
|
||||
t.Errorf("Unexpected parse error")
|
||||
}
|
||||
if s1.String() != s2.String() {
|
||||
t.Errorf("Non-deterministic parse")
|
||||
}
|
||||
}
|
||||
|
||||
func expectMatch(t *testing.T, selector string, ls Set) {
|
||||
lq, err := ParseSelector(selector)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to parse %v as a selector\n", selector)
|
||||
return
|
||||
}
|
||||
if !lq.Matches(ls) {
|
||||
t.Errorf("Wanted %s to match '%s', but it did not.\n", selector, ls)
|
||||
}
|
||||
}
|
||||
|
||||
func expectNoMatch(t *testing.T, selector string, ls Set) {
|
||||
lq, err := ParseSelector(selector)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to parse %v as a selector\n", selector)
|
||||
return
|
||||
}
|
||||
if lq.Matches(ls) {
|
||||
t.Errorf("Wanted '%s' to not match '%s', but it did.", selector, ls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEverything(t *testing.T) {
|
||||
if !Everything().Matches(Set{"x": "y"}) {
|
||||
t.Errorf("Nil selector didn't match")
|
||||
}
|
||||
if !Everything().Empty() {
|
||||
t.Errorf("Everything was not empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectorMatches(t *testing.T) {
|
||||
expectMatch(t, "", Set{"x": "y"})
|
||||
expectMatch(t, "x=y", Set{"x": "y"})
|
||||
expectMatch(t, "x=y,z=w", Set{"x": "y", "z": "w"})
|
||||
expectMatch(t, "x!=y,z!=w", Set{"x": "z", "z": "a"})
|
||||
expectMatch(t, "notin=in", Set{"notin": "in"}) // in and notin in exactMatch
|
||||
expectNoMatch(t, "x=y", Set{"x": "z"})
|
||||
expectNoMatch(t, "x=y,z=w", Set{"x": "w", "z": "w"})
|
||||
expectNoMatch(t, "x!=y,z!=w", Set{"x": "z", "z": "w"})
|
||||
|
||||
fieldset := Set{
|
||||
"foo": "bar",
|
||||
"baz": "blah",
|
||||
"complex": `=value\,\`,
|
||||
}
|
||||
expectMatch(t, "foo=bar", fieldset)
|
||||
expectMatch(t, "baz=blah", fieldset)
|
||||
expectMatch(t, "foo=bar,baz=blah", fieldset)
|
||||
expectMatch(t, `foo=bar,baz=blah,complex=\=value\\\,\\`, fieldset)
|
||||
expectNoMatch(t, "foo=blah", fieldset)
|
||||
expectNoMatch(t, "baz=bar", fieldset)
|
||||
expectNoMatch(t, "foo=bar,foobar=bar,baz=blah", fieldset)
|
||||
}
|
||||
|
||||
func TestOneTermEqualSelector(t *testing.T) {
|
||||
if !OneTermEqualSelector("x", "y").Matches(Set{"x": "y"}) {
|
||||
t.Errorf("No match when match expected.")
|
||||
}
|
||||
if OneTermEqualSelector("x", "y").Matches(Set{"x": "z"}) {
|
||||
t.Errorf("Match when none expected.")
|
||||
}
|
||||
}
|
||||
|
||||
func expectMatchDirect(t *testing.T, selector, ls Set) {
|
||||
if !SelectorFromSet(selector).Matches(ls) {
|
||||
t.Errorf("Wanted %s to match '%s', but it did not.\n", selector, ls)
|
||||
}
|
||||
}
|
||||
|
||||
func expectNoMatchDirect(t *testing.T, selector, ls Set) {
|
||||
if SelectorFromSet(selector).Matches(ls) {
|
||||
t.Errorf("Wanted '%s' to not match '%s', but it did.", selector, ls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetMatches(t *testing.T) {
|
||||
labelset := Set{
|
||||
"foo": "bar",
|
||||
"baz": "blah",
|
||||
}
|
||||
expectMatchDirect(t, Set{}, labelset)
|
||||
expectMatchDirect(t, Set{"foo": "bar"}, labelset)
|
||||
expectMatchDirect(t, Set{"baz": "blah"}, labelset)
|
||||
expectMatchDirect(t, Set{"foo": "bar", "baz": "blah"}, labelset)
|
||||
expectNoMatchDirect(t, Set{"foo": "=blah"}, labelset)
|
||||
expectNoMatchDirect(t, Set{"baz": "=bar"}, labelset)
|
||||
expectNoMatchDirect(t, Set{"foo": "=bar", "foobar": "bar", "baz": "blah"}, labelset)
|
||||
}
|
||||
|
||||
func TestNilMapIsValid(t *testing.T) {
|
||||
selector := Set(nil).AsSelector()
|
||||
if selector == nil {
|
||||
t.Errorf("Selector for nil set should be Everything")
|
||||
}
|
||||
if !selector.Empty() {
|
||||
t.Errorf("Selector for nil set should be Empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetIsEmpty(t *testing.T) {
|
||||
if !(Set{}).AsSelector().Empty() {
|
||||
t.Errorf("Empty set should be empty")
|
||||
}
|
||||
if !(andTerm(nil)).Empty() {
|
||||
t.Errorf("Nil andTerm should be empty")
|
||||
}
|
||||
if (&hasTerm{}).Empty() {
|
||||
t.Errorf("hasTerm should not be empty")
|
||||
}
|
||||
if (¬HasTerm{}).Empty() {
|
||||
t.Errorf("notHasTerm should not be empty")
|
||||
}
|
||||
if !(andTerm{andTerm{}}).Empty() {
|
||||
t.Errorf("Nested andTerm should be empty")
|
||||
}
|
||||
if (andTerm{&hasTerm{"a", "b"}}).Empty() {
|
||||
t.Errorf("Nested andTerm should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequiresExactMatch(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
S Selector
|
||||
Label string
|
||||
Value string
|
||||
Found bool
|
||||
}{
|
||||
"empty set": {Set{}.AsSelector(), "test", "", false},
|
||||
"empty hasTerm": {&hasTerm{}, "test", "", false},
|
||||
"skipped hasTerm": {&hasTerm{"a", "b"}, "test", "", false},
|
||||
"valid hasTerm": {&hasTerm{"test", "b"}, "test", "b", true},
|
||||
"valid hasTerm no value": {&hasTerm{"test", ""}, "test", "", true},
|
||||
"valid notHasTerm": {¬HasTerm{"test", "b"}, "test", "", false},
|
||||
"valid notHasTerm no value": {¬HasTerm{"test", ""}, "test", "", false},
|
||||
"nil andTerm": {andTerm(nil), "test", "", false},
|
||||
"empty andTerm": {andTerm{}, "test", "", false},
|
||||
"nested andTerm": {andTerm{andTerm{}}, "test", "", false},
|
||||
"nested andTerm matches": {andTerm{&hasTerm{"test", "b"}}, "test", "b", true},
|
||||
"andTerm with non-match": {andTerm{&hasTerm{}, &hasTerm{"test", "b"}}, "test", "b", true},
|
||||
}
|
||||
for k, v := range testCases {
|
||||
value, found := v.S.RequiresExactMatch(v.Label)
|
||||
if value != v.Value {
|
||||
t.Errorf("%s: expected value %s, got %s", k, v.Value, value)
|
||||
}
|
||||
if found != v.Found {
|
||||
t.Errorf("%s: expected found %t, got %t", k, v.Found, found)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransform(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
selector string
|
||||
transform func(field, value string) (string, string, error)
|
||||
result string
|
||||
isEmpty bool
|
||||
}{
|
||||
{
|
||||
name: "empty selector",
|
||||
selector: "",
|
||||
transform: func(field, value string) (string, string, error) { return field, value, nil },
|
||||
result: "",
|
||||
isEmpty: true,
|
||||
},
|
||||
{
|
||||
name: "no-op transform",
|
||||
selector: "a=b,c=d",
|
||||
transform: func(field, value string) (string, string, error) { return field, value, nil },
|
||||
result: "a=b,c=d",
|
||||
isEmpty: false,
|
||||
},
|
||||
{
|
||||
name: "transform one field",
|
||||
selector: "a=b,c=d",
|
||||
transform: func(field, value string) (string, string, error) {
|
||||
if field == "a" {
|
||||
return "e", "f", nil
|
||||
}
|
||||
return field, value, nil
|
||||
},
|
||||
result: "e=f,c=d",
|
||||
isEmpty: false,
|
||||
},
|
||||
{
|
||||
name: "remove field to make empty",
|
||||
selector: "a=b",
|
||||
transform: func(field, value string) (string, string, error) { return "", "", nil },
|
||||
result: "",
|
||||
isEmpty: true,
|
||||
},
|
||||
{
|
||||
name: "remove only one field",
|
||||
selector: "a=b,c=d,e=f",
|
||||
transform: func(field, value string) (string, string, error) {
|
||||
if field == "c" {
|
||||
return "", "", nil
|
||||
}
|
||||
return field, value, nil
|
||||
},
|
||||
result: "a=b,e=f",
|
||||
isEmpty: false,
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
result, err := ParseAndTransformSelector(tc.selector, tc.transform)
|
||||
if err != nil {
|
||||
t.Errorf("[%d] unexpected error during Transform: %v", i, err)
|
||||
}
|
||||
if result.Empty() != tc.isEmpty {
|
||||
t.Errorf("[%d] expected empty: %t, got: %t", i, tc.isEmpty, result.Empty())
|
||||
}
|
||||
if result.String() != tc.result {
|
||||
t.Errorf("[%d] unexpected result: %s", i, result.String())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user