1 package tree
2
3 import (
4 "fmt"
5 "strings"
6 "testing"
7
8 l5dcharts "github.com/linkerd/linkerd2/pkg/charts/linkerd2"
9 )
10
11 func TestTreeGetString(t *testing.T) {
12
13 vals, err := l5dcharts.NewValues()
14 if err != nil {
15 t.Fatalf("expected no error; got %s", err)
16 }
17
18 values, err := MarshalToTree(vals)
19 if err != nil {
20 t.Fatalf("expected no error; got %s", err)
21 }
22
23 testCases := []struct {
24 tree Tree
25 path []string
26 value string
27 err error
28 }{
29 {
30 values,
31 []string{"global", "namespace"},
32 "",
33 fmt.Errorf("could not find node global"),
34 },
35 {
36 values,
37 []string{"global"},
38 "",
39 fmt.Errorf("could not find node global"),
40 },
41 {
42 values,
43 []string{"proxy", "image"},
44 "",
45 fmt.Errorf("expected string at node image but found a different type"),
46 },
47 {
48 values,
49 []string{"proxy", "logFormat"},
50 "plain",
51 nil,
52 },
53 }
54
55 for _, tc := range testCases {
56 tc := tc
57 t.Run(fmt.Sprintf("%s: %s, %v", strings.Join(tc.path, "/"), tc.value, tc.err), func(t *testing.T) {
58 finalValue, err := tc.tree.GetString(tc.path...)
59 if err != nil {
60 if tc.err != nil {
61 assert(t, err.Error(), tc.err.Error())
62 } else {
63 t.Fatalf("expected no error; got %s", err)
64 }
65 }
66
67 assert(t, finalValue, tc.value)
68 })
69 }
70 }
71
72 func assert(t *testing.T, received, expected string) {
73 if expected != received {
74 t.Fatalf("Expected %v, got %v", expected, received)
75 }
76 }
77
View as plain text