1
16
17 package explain
18
19 import (
20 "reflect"
21 "testing"
22
23 "k8s.io/apimachinery/pkg/api/meta/testrestmapper"
24 "k8s.io/apimachinery/pkg/runtime/schema"
25 "k8s.io/kubectl/pkg/scheme"
26 )
27
28 func TestSplitAndParseResourceRequest(t *testing.T) {
29 tests := []struct {
30 name string
31 inResource string
32
33 expectedGVR schema.GroupVersionResource
34 expectedFieldsPath []string
35 expectedErr bool
36 }{
37 {
38 name: "no trailing period",
39 inResource: "pods.field2.field3",
40
41 expectedGVR: schema.GroupVersionResource{Resource: "pods", Version: "v1"},
42 expectedFieldsPath: []string{"field2", "field3"},
43 },
44 {
45 name: "trailing period with correct fieldsPath",
46 inResource: "service.field2.field3.",
47
48 expectedGVR: schema.GroupVersionResource{Resource: "services", Version: "v1"},
49 expectedFieldsPath: []string{"field2", "field3"},
50 },
51 {
52 name: "field with dots 1",
53 inResource: `service.field2['field\.with\.dots']`,
54
55 expectedGVR: schema.GroupVersionResource{Resource: "services", Version: "v1"},
56 expectedFieldsPath: []string{"field2", "field.with.dots"},
57 },
58 {
59 name: "field with dots 2",
60 inResource: `service.field2.field\.with\.dots`,
61
62 expectedGVR: schema.GroupVersionResource{Resource: "services", Version: "v1"},
63 expectedFieldsPath: []string{"field2", "field.with.dots"},
64 },
65 {
66 name: "trailing period with incorrect fieldsPath",
67 inResource: "node.field2.field3.",
68
69 expectedGVR: schema.GroupVersionResource{Resource: "nodes", Version: "v1"},
70 expectedFieldsPath: []string{"field2", "field3", ""},
71 expectedErr: true,
72 },
73 }
74
75 mapper := testrestmapper.TestOnlyStaticRESTMapper(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...)
76 for _, tt := range tests {
77 t.Run(tt.name, func(t *testing.T) {
78 gotGVR, gotFieldsPath, err := SplitAndParseResourceRequest(tt.inResource, mapper)
79 if err != nil {
80 t.Errorf("unexpected error: %v", err)
81 }
82
83 if !reflect.DeepEqual(tt.expectedGVR, gotGVR) && !tt.expectedErr {
84 t.Errorf("%s: expected inResource: %s, got: %s", tt.name, tt.expectedGVR, gotGVR)
85 }
86
87 if !reflect.DeepEqual(tt.expectedFieldsPath, gotFieldsPath) && !tt.expectedErr {
88 t.Errorf("%s: expected fieldsPath: %s, got: %s", tt.name, tt.expectedFieldsPath, gotFieldsPath)
89 }
90 })
91 }
92 }
93
94 func TestSplitAndParseResourceRequestWithMatchingPrefix(t *testing.T) {
95 tests := []struct {
96 name string
97 inResource string
98
99 expectedGVR schema.GroupVersionResource
100 expectedFieldsPath []string
101 expectedErr bool
102 }{
103 {
104 name: "no trailing period",
105 inResource: "pods.field2.field3",
106
107 expectedGVR: schema.GroupVersionResource{Resource: "pods", Version: "v1"},
108 expectedFieldsPath: []string{"field2", "field3"},
109 },
110 {
111 name: "trailing period with correct fieldsPath",
112 inResource: "service.field2.field3.",
113
114 expectedGVR: schema.GroupVersionResource{Resource: "services", Version: "v1"},
115 expectedFieldsPath: []string{"field2", "field3"},
116 },
117 {
118 name: "field with dots 1",
119 inResource: `service.field2['field\.with\.dots']`,
120
121 expectedGVR: schema.GroupVersionResource{Resource: "services", Version: "v1"},
122 expectedFieldsPath: []string{"field2", "field.with.dots"},
123 },
124 {
125 name: "field with dots 2",
126 inResource: `service.field2.field\.with\.dots`,
127
128 expectedGVR: schema.GroupVersionResource{Resource: "services", Version: "v1"},
129 expectedFieldsPath: []string{"field2", "field.with.dots"},
130 },
131 {
132 name: "trailing period with incorrect fieldsPath",
133 inResource: "node.field2.field3.",
134
135 expectedGVR: schema.GroupVersionResource{Resource: "nodes", Version: "v1"},
136 expectedFieldsPath: []string{"field2", "field3", ""},
137 expectedErr: true,
138 },
139 }
140
141 mapper := testrestmapper.TestOnlyStaticRESTMapper(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...)
142 for _, tt := range tests {
143 t.Run(tt.name, func(t *testing.T) {
144 gotGVR, gotFieldsPath, err := SplitAndParseResourceRequestWithMatchingPrefix(tt.inResource, mapper)
145 if err != nil {
146 t.Errorf("unexpected error: %v", err)
147 }
148
149 if !reflect.DeepEqual(tt.expectedGVR, gotGVR) && !tt.expectedErr {
150 t.Errorf("%s: expected inResource: %s, got: %s", tt.name, tt.expectedGVR, gotGVR)
151 }
152
153 if !reflect.DeepEqual(tt.expectedFieldsPath, gotFieldsPath) && !tt.expectedErr {
154 t.Errorf("%s: expected fieldsPath: %s, got: %s", tt.name, tt.expectedFieldsPath, gotFieldsPath)
155 }
156 })
157 }
158 }
159
View as plain text