2017-09-28 16:34:24 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 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 explain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFindField(t *testing.T) {
|
|
|
|
schema := resources.LookupResource(schema.GroupVersionKind{
|
|
|
|
Group: "",
|
|
|
|
Version: "v1",
|
|
|
|
Kind: "OneKind",
|
|
|
|
})
|
|
|
|
if schema == nil {
|
|
|
|
t.Fatal("Counldn't find schema v1.OneKind")
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2018-05-12 09:24:28 +00:00
|
|
|
name string
|
2017-09-28 16:34:24 +00:00
|
|
|
path []string
|
|
|
|
|
|
|
|
err string
|
|
|
|
expectedPath string
|
|
|
|
}{
|
|
|
|
{
|
2018-05-12 09:24:28 +00:00
|
|
|
name: "test1",
|
2017-09-28 16:34:24 +00:00
|
|
|
path: []string{},
|
|
|
|
expectedPath: "OneKind",
|
|
|
|
},
|
|
|
|
{
|
2018-05-12 09:24:28 +00:00
|
|
|
name: "test2",
|
2017-09-28 16:34:24 +00:00
|
|
|
path: []string{"field1"},
|
|
|
|
expectedPath: "OneKind.field1",
|
|
|
|
},
|
|
|
|
{
|
2018-05-12 09:24:28 +00:00
|
|
|
name: "test3",
|
2017-09-28 16:34:24 +00:00
|
|
|
path: []string{"field1", "array"},
|
|
|
|
expectedPath: "OtherKind.array",
|
|
|
|
},
|
|
|
|
{
|
2018-05-12 09:24:28 +00:00
|
|
|
name: "test4",
|
2017-09-28 16:34:24 +00:00
|
|
|
path: []string{"field1", "what?"},
|
|
|
|
err: `field "what?" does not exist`,
|
|
|
|
},
|
2017-11-01 13:34:36 +00:00
|
|
|
{
|
2018-05-12 09:24:28 +00:00
|
|
|
name: "test5",
|
2017-11-01 13:34:36 +00:00
|
|
|
path: []string{"field1", ""},
|
|
|
|
err: `field "" does not exist`,
|
|
|
|
},
|
2017-09-28 16:34:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 09:24:28 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
path, err := LookupSchemaForField(schema, tt.path)
|
2017-09-28 16:34:24 +00:00
|
|
|
|
2018-05-12 09:24:28 +00:00
|
|
|
gotErr := ""
|
|
|
|
if err != nil {
|
|
|
|
gotErr = err.Error()
|
|
|
|
}
|
2017-09-28 16:34:24 +00:00
|
|
|
|
2018-05-12 09:24:28 +00:00
|
|
|
gotPath := ""
|
|
|
|
if path != nil {
|
|
|
|
gotPath = path.GetPath().String()
|
|
|
|
}
|
2017-09-28 16:34:24 +00:00
|
|
|
|
2018-05-12 09:24:28 +00:00
|
|
|
if gotErr != tt.err || gotPath != tt.expectedPath {
|
|
|
|
t.Errorf("LookupSchemaForField(schema, %v) = (path: %q, err: %q), expected (path: %q, err: %q)",
|
|
|
|
tt.path, gotPath, gotErr, tt.expectedPath, tt.err)
|
|
|
|
}
|
|
|
|
})
|
2017-09-28 16:34:24 +00:00
|
|
|
}
|
|
|
|
}
|