navigation_step_parser_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2014 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 config
  14. import (
  15. "reflect"
  16. "strings"
  17. "testing"
  18. "k8s.io/apimachinery/pkg/util/diff"
  19. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  20. )
  21. type stepParserTest struct {
  22. path string
  23. expectedNavigationSteps navigationSteps
  24. expectedError string
  25. }
  26. func TestParseWithDots(t *testing.T) {
  27. test := stepParserTest{
  28. path: "clusters.my.dot.delimited.name.server",
  29. expectedNavigationSteps: navigationSteps{
  30. steps: []navigationStep{
  31. {"clusters", reflect.TypeOf(make(map[string]*clientcmdapi.Cluster))},
  32. {"my.dot.delimited.name", reflect.TypeOf(clientcmdapi.Cluster{})},
  33. {"server", reflect.TypeOf("")},
  34. },
  35. },
  36. }
  37. test.run(t)
  38. }
  39. func TestParseWithDotsEndingWithName(t *testing.T) {
  40. test := stepParserTest{
  41. path: "contexts.10.12.12.12",
  42. expectedNavigationSteps: navigationSteps{
  43. steps: []navigationStep{
  44. {"contexts", reflect.TypeOf(make(map[string]*clientcmdapi.Context))},
  45. {"10.12.12.12", reflect.TypeOf(clientcmdapi.Context{})},
  46. },
  47. },
  48. }
  49. test.run(t)
  50. }
  51. func TestParseWithBadValue(t *testing.T) {
  52. test := stepParserTest{
  53. path: "user.bad",
  54. expectedNavigationSteps: navigationSteps{
  55. steps: []navigationStep{},
  56. },
  57. expectedError: "unable to parse user.bad after [] at api.Config",
  58. }
  59. test.run(t)
  60. }
  61. func (test stepParserTest) run(t *testing.T) {
  62. actualSteps, err := newNavigationSteps(test.path)
  63. if len(test.expectedError) != 0 {
  64. if err == nil {
  65. t.Errorf("Did not get %v", test.expectedError)
  66. } else {
  67. if !strings.Contains(err.Error(), test.expectedError) {
  68. t.Errorf("Expected %v, but got %v", test.expectedError, err)
  69. }
  70. }
  71. return
  72. }
  73. if err != nil {
  74. t.Errorf("Unexpected error: %v", err)
  75. }
  76. if !reflect.DeepEqual(test.expectedNavigationSteps, *actualSteps) {
  77. t.Errorf("diff: %v", diff.ObjectDiff(test.expectedNavigationSteps, *actualSteps))
  78. t.Errorf("expected: %#v\n actual: %#v", test.expectedNavigationSteps, *actualSteps)
  79. }
  80. }