...
1
16
17 package config
18
19 import (
20 "reflect"
21 "strings"
22 "testing"
23
24 "github.com/google/go-cmp/cmp"
25 clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
26 )
27
28 type stepParserTest struct {
29 path string
30 expectedNavigationSteps navigationSteps
31 expectedError string
32 }
33
34 func TestParseWithDots(t *testing.T) {
35 test := stepParserTest{
36 path: "clusters.my.dot.delimited.name.server",
37 expectedNavigationSteps: navigationSteps{
38 steps: []navigationStep{
39 {"clusters", reflect.TypeOf(make(map[string]*clientcmdapi.Cluster))},
40 {"my.dot.delimited.name", reflect.TypeOf(clientcmdapi.Cluster{})},
41 {"server", reflect.TypeOf("")},
42 },
43 },
44 }
45
46 test.run(t)
47 }
48
49 func TestParseWithDotsEndingWithName(t *testing.T) {
50 test := stepParserTest{
51 path: "contexts.10.12.12.12",
52 expectedNavigationSteps: navigationSteps{
53 steps: []navigationStep{
54 {"contexts", reflect.TypeOf(make(map[string]*clientcmdapi.Context))},
55 {"10.12.12.12", reflect.TypeOf(clientcmdapi.Context{})},
56 },
57 },
58 }
59
60 test.run(t)
61 }
62
63 func TestParseWithBadValue(t *testing.T) {
64 test := stepParserTest{
65 path: "user.bad",
66 expectedNavigationSteps: navigationSteps{
67 steps: []navigationStep{},
68 },
69 expectedError: "unable to parse user.bad after [] at api.Config",
70 }
71
72 test.run(t)
73 }
74
75 func TestParseWithNoMatchingValue(t *testing.T) {
76 test := stepParserTest{
77 path: "users.jheiss.exec.command",
78 expectedNavigationSteps: navigationSteps{
79 steps: []navigationStep{},
80 },
81 expectedError: "unable to parse one or more field values of users.jheiss.exec",
82 }
83
84 test.run(t)
85 }
86
87 func (test stepParserTest) run(t *testing.T) {
88 actualSteps, err := newNavigationSteps(test.path)
89 if len(test.expectedError) != 0 {
90 if err == nil {
91 t.Errorf("Did not get %v", test.expectedError)
92 } else {
93 if !strings.Contains(err.Error(), test.expectedError) {
94 t.Errorf("Expected %v, but got %v", test.expectedError, err)
95 }
96 }
97 return
98 }
99
100 if err != nil {
101 t.Errorf("Unexpected error: %v", err)
102 }
103
104 if !reflect.DeepEqual(test.expectedNavigationSteps, *actualSteps) {
105 t.Errorf("diff: %v", cmp.Diff(test.expectedNavigationSteps, *actualSteps))
106 t.Errorf("expected: %#v\n actual: %#v", test.expectedNavigationSteps, *actualSteps)
107 }
108 }
109
View as plain text