explain_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package explain
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/apimachinery/pkg/api/meta/testrestmapper"
  18. "k8s.io/kubernetes/pkg/kubectl/scheme"
  19. )
  20. func TestSplitAndParseResourceRequest(t *testing.T) {
  21. tests := []struct {
  22. name string
  23. inresource string
  24. expectedInResource string
  25. expectedFieldsPath []string
  26. expectedErr bool
  27. }{
  28. {
  29. name: "no trailing period",
  30. inresource: "field1.field2.field3",
  31. expectedInResource: "field1",
  32. expectedFieldsPath: []string{"field2", "field3"},
  33. },
  34. {
  35. name: "trailing period with correct fieldsPath",
  36. inresource: "field1.field2.field3.",
  37. expectedInResource: "field1",
  38. expectedFieldsPath: []string{"field2", "field3"},
  39. },
  40. {
  41. name: "trailing period with incorrect fieldsPath",
  42. inresource: "field1.field2.field3.",
  43. expectedInResource: "field1",
  44. expectedFieldsPath: []string{"field2", "field3", ""},
  45. expectedErr: true,
  46. },
  47. }
  48. mapper := testrestmapper.TestOnlyStaticRESTMapper(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...)
  49. for _, tt := range tests {
  50. t.Run(tt.name, func(t *testing.T) {
  51. gotInResource, gotFieldsPath, err := SplitAndParseResourceRequest(tt.inresource, mapper)
  52. if err != nil {
  53. t.Errorf("unexpected error: %v", err)
  54. }
  55. if !reflect.DeepEqual(tt.expectedInResource, gotInResource) && !tt.expectedErr {
  56. t.Errorf("%s: expected inresource: %s, got: %s", tt.name, tt.expectedInResource, gotInResource)
  57. }
  58. if !reflect.DeepEqual(tt.expectedFieldsPath, gotFieldsPath) && !tt.expectedErr {
  59. t.Errorf("%s: expected fieldsPath: %s, got: %s", tt.name, tt.expectedFieldsPath, gotFieldsPath)
  60. }
  61. })
  62. }
  63. }