github.com/GuanceCloud/cliutils@v1.1.21/pipeline/manager/manager_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 // Package manager for managing pipeline scripts 7 package manager 8 9 import ( 10 "testing" 11 12 "github.com/GuanceCloud/cliutils/point" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestManger(t *testing.T) { 17 m := NewManager(NewManagerCfg(nil, nil)) 18 m.LoadScripts(NSDefault, map[point.Category]map[string]string{ 19 point.Logging: { 20 "abc.p": "if true {}", 21 "def.p": "if true {}", 22 }, 23 point.DialTesting: { 24 "abc.p": "if true {}", 25 }, 26 point.Profiling: { 27 "abc.p": "if true {}", 28 }, 29 }, nil) 30 31 m.LoadScripts(NSRemote, map[point.Category]map[string]string{ 32 point.Logging: { 33 "xyz.p": "if true {}", 34 "def.p": "if true {}", 35 }, 36 }, nil) 37 38 rl := m.GetScriptRelation() 39 rl.relation = map[point.Category]map[string]string{ 40 point.DialTesting: { 41 "x1": "1.p", 42 "x2": "2.p", 43 }, 44 } 45 46 rl.UpdateRelation(0, map[point.Category]map[string]string{ 47 point.Logging: { 48 "x1": "a1.p", 49 "x2": "abc.p", 50 }, 51 }) 52 53 m.UpdateDefaultScript(map[point.Category]string{ 54 point.Logging: "def.p", 55 }) 56 57 // L: xyz.p, def.p (R), abc.p Df: def.p Rl: x1 -> a1, x2 -> abc 58 // T: abc.p, 59 cases := []struct { 60 cat point.Category 61 source, ns string 62 name [2]string 63 notFound bool 64 }{ 65 { 66 cat: point.Logging, 67 source: "abc", 68 name: [2]string{"abc.p", "abc.p"}, 69 ns: NSDefault, 70 }, 71 { 72 cat: point.Logging, 73 source: "def", 74 name: [2]string{"def.p", "def.p"}, 75 ns: NSRemote, 76 }, 77 { 78 cat: point.Logging, 79 source: "xyz", 80 name: [2]string{"xyz.p", "xyz.p"}, 81 ns: NSRemote, 82 }, 83 { 84 cat: point.Logging, 85 source: "x1", 86 name: [2]string{"a1.p", "def.p"}, 87 ns: NSRemote, 88 }, 89 { 90 cat: point.Logging, 91 source: "x2", 92 name: [2]string{"abc.p", "abc.p"}, 93 ns: NSDefault, 94 }, 95 96 { 97 cat: point.Logging, 98 source: "x3", 99 name: [2]string{"x3.p", "def.p"}, 100 ns: NSRemote, 101 }, 102 { 103 cat: point.DialTesting, 104 source: "x3", 105 name: [2]string{"x3.p", ""}, 106 notFound: true, 107 }, 108 } 109 110 t.Run("GetScriptName", func(t *testing.T) { 111 for _, tt := range cases { 112 t.Run(tt.source, func(t *testing.T) { 113 if tt.source == "x1" { 114 a := 1 115 _ = a 116 } 117 name, _ := ScriptName(rl, tt.cat, point.NewPointV2(tt.source, point.NewKVs(map[string]interface{}{ 118 "ns": tt.ns, 119 })), nil) 120 assert.Equal(t, tt.name[0], name) 121 if s, ok := m.QueryScript(tt.cat, name); ok { 122 if tt.notFound { 123 t.Error("not found") 124 return 125 } 126 assert.Equal(t, tt.name[1], s.name) 127 assert.Equal(t, tt.ns, s.ns) 128 } else { 129 if !tt.notFound { 130 t.Error("found") 131 } 132 } 133 }) 134 } 135 }) 136 137 }