github.com/cilium/cilium@v1.16.2/pkg/health/client/tree_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package client 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestComputeMaxLevel(t *testing.T) { 13 uu := map[string]struct { 14 n *node 15 max int 16 }{ 17 "empty": {}, 18 "single": { 19 n: &node{val: "n1", meta: "m1"}, 20 }, 21 "flat": { 22 n: &node{val: "n1", meta: "m1", nodes: []*node{ 23 {val: "n2", meta: "m2"}, 24 {val: "n3", meta: "m3"}, 25 }}, 26 max: 1, 27 }, 28 "multi": { 29 n: &node{val: "n1", meta: "m1", nodes: []*node{ 30 {val: "n1_1", meta: "m1_1", nodes: []*node{ 31 {val: "n1_1_1", meta: "m1_1_1", nodes: []*node{ 32 {val: "n1_1_1_1", meta: "m1_1_1_1"}, 33 {val: "n1_1_1_2", meta: "m1_1_1_2"}, 34 }}, 35 }}, 36 {val: "n2", meta: "m2", nodes: []*node{ 37 {val: "n2_1", meta: "m2_1", nodes: []*node{ 38 {val: "n2_1_1", meta: "m2_1_1"}, 39 }}, 40 }}, 41 {val: "n3", meta: "m3"}, 42 }}, 43 max: 3, 44 }, 45 } 46 47 for k := range uu { 48 u := uu[k] 49 t.Run(k, func(t *testing.T) { 50 assert.Equal(t, u.max, computeMaxLevel(0, u.n)) 51 }) 52 } 53 } 54 55 func TestTreeAddNode(t *testing.T) { 56 r := newRoot("fred") 57 r.addNode("a", nil) 58 b := r.addBranch("b") 59 b.addNode("b1", nil) 60 r.addNode("c", nil) 61 r.addNodeWithMeta("d", "blee", nil) 62 63 assert.Equal(t, `fred 64 ├── a 65 ├── b 66 │ └── b1 67 ├── c 68 └── d blee 69 `, r.String()) 70 } 71 72 func TestTreeAddBranch(t *testing.T) { 73 r := newRoot("fred") 74 r.addBranch("a") 75 b := r.addBranch("b") 76 b1 := b.addBranch("b1") 77 b1.addNode("b1_1", nil) 78 r.addBranch("c") 79 r.addBranchWithMeta("d", "blee") 80 81 assert.Equal(t, `fred 82 ├── a 83 ├── b 84 │ └── b1 85 │ └── b1_1 86 ├── c 87 └── d blee 88 `, r.String()) 89 } 90 91 func TestTreeFind(t *testing.T) { 92 r := newRoot("fred") 93 r.addBranch("a") 94 b := r.addBranch("b") 95 b1 := b.addBranch("b1") 96 b11 := b1.addNode("b1_1", nil) 97 c := r.addNode("c", nil) 98 r.addBranchWithMeta("d", "blee") 99 100 uu := map[string]struct { 101 v string 102 e *node 103 }{ 104 "none": { 105 v: "bozo", 106 }, 107 "leaf": { 108 v: "b1_1", 109 e: b11, 110 }, 111 "node": { 112 v: "c", 113 e: c, 114 }, 115 "branch": { 116 v: "b1", 117 e: b1, 118 }, 119 } 120 for k := range uu { 121 u := uu[k] 122 t.Run(k, func(t *testing.T) { 123 assert.Equal(t, u.e, r.find(u.v)) 124 }) 125 } 126 }