2015-04-23 20:57:30 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-04-23 20:57:30 +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 fieldpath
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-01-17 03:38:19 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2016-11-18 21:26:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
2015-04-23 20:57:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExtractFieldPathAsString(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
fieldPath string
|
|
|
|
obj interface{}
|
|
|
|
expectedValue string
|
|
|
|
expectedMessageFragment string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "not an API object",
|
|
|
|
fieldPath: "metadata.name",
|
|
|
|
obj: "",
|
|
|
|
expectedMessageFragment: "expected struct",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok - namespace",
|
|
|
|
fieldPath: "metadata.namespace",
|
2016-11-18 21:26:53 +00:00
|
|
|
obj: &v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-04-23 20:57:30 +00:00
|
|
|
Namespace: "object-namespace",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedValue: "object-namespace",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok - name",
|
|
|
|
fieldPath: "metadata.name",
|
2016-11-18 21:26:53 +00:00
|
|
|
obj: &v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-04-23 20:57:30 +00:00
|
|
|
Name: "object-name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedValue: "object-name",
|
|
|
|
},
|
2015-02-20 05:36:23 +00:00
|
|
|
{
|
|
|
|
name: "ok - labels",
|
|
|
|
fieldPath: "metadata.labels",
|
2016-11-18 21:26:53 +00:00
|
|
|
obj: &v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-02-20 05:36:23 +00:00
|
|
|
Labels: map[string]string{"key": "value"},
|
|
|
|
},
|
|
|
|
},
|
2016-06-03 14:03:01 +00:00
|
|
|
expectedValue: "key=\"value\"",
|
2015-02-20 05:36:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok - labels bslash n",
|
|
|
|
fieldPath: "metadata.labels",
|
2016-11-18 21:26:53 +00:00
|
|
|
obj: &v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-02-20 05:36:23 +00:00
|
|
|
Labels: map[string]string{"key": "value\n"},
|
|
|
|
},
|
|
|
|
},
|
2016-06-03 14:03:01 +00:00
|
|
|
expectedValue: "key=\"value\\n\"",
|
2015-02-20 05:36:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok - annotations",
|
|
|
|
fieldPath: "metadata.annotations",
|
2016-11-18 21:26:53 +00:00
|
|
|
obj: &v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-02-20 05:36:23 +00:00
|
|
|
Annotations: map[string]string{"builder": "john-doe"},
|
|
|
|
},
|
|
|
|
},
|
2016-06-03 14:03:01 +00:00
|
|
|
expectedValue: "builder=\"john-doe\"",
|
2015-02-20 05:36:23 +00:00
|
|
|
},
|
|
|
|
|
2015-04-23 20:57:30 +00:00
|
|
|
{
|
|
|
|
name: "invalid expression",
|
|
|
|
fieldPath: "metadata.whoops",
|
2016-11-18 21:26:53 +00:00
|
|
|
obj: &v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-04-23 20:57:30 +00:00
|
|
|
Namespace: "object-namespace",
|
|
|
|
},
|
|
|
|
},
|
2016-10-11 19:32:50 +00:00
|
|
|
expectedMessageFragment: "unsupported fieldPath",
|
2015-04-23 20:57:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
actual, err := ExtractFieldPathAsString(tc.obj, tc.fieldPath)
|
|
|
|
if err != nil {
|
|
|
|
if tc.expectedMessageFragment != "" {
|
|
|
|
if !strings.Contains(err.Error(), tc.expectedMessageFragment) {
|
2016-10-11 19:32:50 +00:00
|
|
|
t.Errorf("%v: unexpected error message: %q, expected to contain %q", tc.name, err, tc.expectedMessageFragment)
|
2015-04-23 20:57:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Errorf("%v: unexpected error: %v", tc.name, err)
|
|
|
|
}
|
|
|
|
} else if e := tc.expectedValue; e != "" && e != actual {
|
2016-10-11 19:32:50 +00:00
|
|
|
t.Errorf("%v: unexpected result; got %q, expected %q", tc.name, actual, e)
|
2015-04-23 20:57:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|