1
16
17 package explain
18
19 import (
20 "testing"
21
22 "k8s.io/apimachinery/pkg/runtime/schema"
23 )
24
25 func TestFindField(t *testing.T) {
26 schema := resources.LookupResource(schema.GroupVersionKind{
27 Group: "",
28 Version: "v1",
29 Kind: "OneKind",
30 })
31 if schema == nil {
32 t.Fatal("Counldn't find schema v1.OneKind")
33 }
34
35 tests := []struct {
36 name string
37 path []string
38
39 err string
40 expectedPath string
41 }{
42 {
43 name: "test1",
44 path: []string{},
45 expectedPath: "OneKind",
46 },
47 {
48 name: "test2",
49 path: []string{"field1"},
50 expectedPath: "OneKind.field1",
51 },
52 {
53 name: "test3",
54 path: []string{"field1", "array"},
55 expectedPath: "OtherKind.array",
56 },
57 {
58 name: "test4",
59 path: []string{"field1", "what?"},
60 err: `field "what?" does not exist`,
61 },
62 {
63 name: "test5",
64 path: []string{"field1", ""},
65 err: `field "" does not exist`,
66 },
67 }
68
69 for _, tt := range tests {
70 t.Run(tt.name, func(t *testing.T) {
71 path, err := LookupSchemaForField(schema, tt.path)
72
73 gotErr := ""
74 if err != nil {
75 gotErr = err.Error()
76 }
77
78 gotPath := ""
79 if path != nil {
80 gotPath = path.GetPath().String()
81 }
82
83 if gotErr != tt.err || gotPath != tt.expectedPath {
84 t.Errorf("LookupSchemaForField(schema, %v) = (path: %q, err: %q), expected (path: %q, err: %q)",
85 tt.path, gotPath, gotErr, tt.expectedPath, tt.err)
86 }
87 })
88 }
89 }
90 func TestCrdFindField(t *testing.T) {
91 schema := resources.LookupResource(schema.GroupVersionKind{
92 Group: "",
93 Version: "v1",
94 Kind: "CrdKind",
95 })
96 if schema == nil {
97 t.Fatal("Counldn't find schema v1.CrdKind")
98 }
99
100 tests := []struct {
101 name string
102 path []string
103
104 err string
105 expectedPath string
106 }{
107 {
108 name: "test1",
109 path: []string{},
110 expectedPath: "CrdKind",
111 },
112 }
113
114 for _, tt := range tests {
115 t.Run(tt.name, func(t *testing.T) {
116 path, err := LookupSchemaForField(schema, tt.path)
117
118 gotErr := ""
119 if err != nil {
120 gotErr = err.Error()
121 }
122
123 gotPath := ""
124 if path != nil {
125 gotPath = path.GetPath().String()
126 }
127
128 if gotErr != tt.err || gotPath != tt.expectedPath {
129 t.Errorf("LookupSchemaForField(schema, %v) = (path: %q, err: %q), expected (path: %q, err: %q)",
130 tt.path, gotPath, gotErr, tt.expectedPath, tt.err)
131 }
132 })
133 }
134 }
135
View as plain text