github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_create_point_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 funcs 7 8 import ( 9 "testing" 10 "time" 11 12 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 13 "github.com/GuanceCloud/cliutils/point" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestCreatePoint(t *testing.T) { 18 cases := []struct { 19 name, in string 20 allPl map[string]string 21 outkey string 22 expect []ptinput.PlInputPt 23 fail bool 24 }{ 25 { 26 name: "default", 27 in: `{"a": "1", "b": 2, "c": {"d" : "x1"}}`, 28 allPl: map[string]string{ 29 "main.p": ` 30 d = load_json(_) 31 r = {} 32 for x in d["c"] { 33 r[x] = d["c"][x] 34 } 35 r["b"] = d["b"] 36 create_point("n1", {"a": d["a"]}, r) 37 `, 38 }, 39 outkey: "abc", 40 expect: []ptinput.PlInputPt{ 41 ptinput.NewPlPoint(point.Metric, "n1", map[string]string{ 42 "a": "1", 43 }, map[string]any{ 44 "d": "x1", 45 "b": float64(2.0), 46 }, time.Time{}), 47 }, 48 }, 49 { 50 name: "default-use", 51 in: `{"a": "1", "b": 2, "c": {"d" : "x1"}}`, 52 allPl: map[string]string{ 53 "main.p": ` 54 d = load_json(_) 55 r = {} 56 for x in d["c"] { 57 r[x] = d["c"][x] 58 } 59 r["b"] = d["b"] 60 create_point("n1", {"a": d["a"]}, r, category="M",after_use="abc.p") 61 `, 62 "abc.p": `add_key("aa", 1)`, 63 }, 64 outkey: "abc", 65 expect: []ptinput.PlInputPt{ 66 ptinput.NewPlPoint(point.Metric, "n1", map[string]string{ 67 "a": "1", 68 }, map[string]any{ 69 "d": "x1", 70 "b": float64(2.0), 71 "aa": int64(1), 72 }, time.Time{}), 73 }, 74 }, 75 { 76 name: "default-use", 77 in: `{"a": "1", "b": 2, "c": {"d" : "x1"}}`, 78 allPl: map[string]string{ 79 "main.p": ` 80 d = load_json(_) 81 r = {} 82 for x in d["c"] { 83 r[x] = d["c"][x] 84 } 85 r["b"] = d["b"] 86 create_point("n1", {"a": d["a"]}, r, ts = 1, after_use="abc.p") 87 `, 88 "abc.p": `add_key("aa", 1)`, 89 }, 90 outkey: "abc", 91 expect: []ptinput.PlInputPt{ 92 ptinput.NewPlPoint(point.Metric, "n1", map[string]string{ 93 "a": "1", 94 }, map[string]any{ 95 "d": "x1", 96 "b": float64(2.0), 97 "aa": int64(1), 98 }, time.Unix(0, 1)), 99 }, 100 }, 101 { 102 name: "failed-use", 103 in: `{"a": "1", "b": 2, "c": {"d" : "x1"}}`, 104 allPl: map[string]string{ 105 "main.p": ` 106 d = load_json(_) 107 r = {} 108 for x in d["c"] { 109 r[x] = d["c"][x] 110 } 111 r["b"] = d["b"] 112 create_point("n1", {"a": d["a"]}, r, after_use="absc.p") 113 `, 114 "abc.p": `add_key("aa", 1)`, 115 }, 116 outkey: "abc", 117 fail: true, 118 expect: []ptinput.PlInputPt{ 119 ptinput.NewPlPoint(point.Metric, "n1", map[string]string{ 120 "a": "1", 121 }, map[string]any{ 122 "d": "x1", 123 "b": float64(2.0), 124 "aa": int64(1), 125 }, time.Time{}), 126 }, 127 }, 128 { 129 name: "L", 130 in: `{"a": "1", "b": 2, "c": {"d" : "x1"}}`, 131 allPl: map[string]string{ 132 "main.p": ` 133 d = load_json(_) 134 r = {} 135 for x in d["c"] { 136 r[x] = d["c"][x] 137 } 138 r["b"] = d["b"] 139 create_point("n1", {"a": d["a"]}, r) 140 create_point("n1", {"a": d["a"]}, r, category="L") 141 create_point("n1", {"a": d["a"]}, r, category="M") 142 create_point("n1", {"a": d["a"]}, r, category="T") 143 create_point("n1", {"a": d["a"]}, r, category="R") 144 create_point("n1", {"a": d["a"]}, r, category="N") 145 create_point("n1", {"a": d["a"]}, r, category="O") 146 create_point("n1", {"a": d["a"]}, r, category="CO") 147 create_point("n1", {"a": d["a"]}, r, category="S") 148 `, 149 }, 150 outkey: "abc", 151 expect: []ptinput.PlInputPt{ 152 ptinput.NewPlPoint(point.Metric, "n1", map[string]string{ 153 "a": "1", 154 }, map[string]any{ 155 "d": "x1", 156 "b": float64(2.0), 157 }, time.Time{}), 158 ptinput.NewPlPoint(point.Logging, "n1", map[string]string{ 159 "a": "1", 160 }, map[string]any{ 161 "d": "x1", 162 "b": float64(2.0), 163 }, time.Time{}), 164 ptinput.NewPlPoint(point.Metric, "n1", map[string]string{ 165 "a": "1", 166 }, map[string]any{ 167 "d": "x1", 168 "b": float64(2.0), 169 }, time.Time{}), 170 ptinput.NewPlPoint(point.Tracing, "n1", map[string]string{ 171 "a": "1", 172 }, map[string]any{ 173 "d": "x1", 174 "b": float64(2.0), 175 }, time.Time{}), 176 ptinput.NewPlPoint(point.RUM, "n1", map[string]string{ 177 "a": "1", 178 }, map[string]any{ 179 "d": "x1", 180 "b": float64(2.0), 181 }, time.Time{}), 182 ptinput.NewPlPoint(point.Network, "n1", map[string]string{ 183 "a": "1", 184 }, map[string]any{ 185 "d": "x1", 186 "b": float64(2.0), 187 }, time.Time{}), 188 ptinput.NewPlPoint(point.Object, "n1", map[string]string{ 189 "a": "1", 190 }, map[string]any{ 191 "d": "x1", 192 "b": float64(2.0), 193 }, time.Time{}), 194 ptinput.NewPlPoint(point.CustomObject, "n1", map[string]string{ 195 "a": "1", 196 }, map[string]any{ 197 "d": "x1", 198 "b": float64(2.0), 199 }, time.Time{}), 200 ptinput.NewPlPoint(point.Security, "n1", map[string]string{ 201 "a": "1", 202 }, map[string]any{ 203 "d": "x1", 204 "b": float64(2.0), 205 }, time.Time{}), 206 }, 207 }, 208 { 209 name: "L", 210 in: `{"a": "1", "b": 2, "c": {"d" : "x1"}}`, 211 allPl: map[string]string{ 212 "main.p": ` 213 d = load_json(_) 214 r = {} 215 for x in d["c"] { 216 r[x] = d["c"][x] 217 } 218 r["b"] = d["b"] 219 create_point("n1", {"a": d["a"]}, r) 220 create_point("n1", {"a": d["a"]}, r, category="logging") 221 create_point("n1", {"a": d["a"]}, r, category="metric") 222 create_point("n1", {"a": d["a"]}, r, category="tracing") 223 create_point("n1", {"a": d["a"]}, r, category="rum") 224 create_point("n1", {"a": d["a"]}, r, category="network") 225 create_point("n1", {"a": d["a"]}, r, category="object") 226 create_point("n1", {"a": d["a"]}, r, category="custom_object") 227 create_point("n1", {"a": d["a"]}, r, category="security") 228 `, 229 }, 230 outkey: "abc", 231 expect: []ptinput.PlInputPt{ 232 ptinput.NewPlPoint(point.Metric, "n1", map[string]string{ 233 "a": "1", 234 }, map[string]any{ 235 "d": "x1", 236 "b": float64(2.0), 237 }, time.Time{}), 238 ptinput.NewPlPoint(point.Logging, "n1", map[string]string{ 239 "a": "1", 240 }, map[string]any{ 241 "d": "x1", 242 "b": float64(2.0), 243 }, time.Time{}), 244 ptinput.NewPlPoint(point.Metric, "n1", map[string]string{ 245 "a": "1", 246 }, map[string]any{ 247 "d": "x1", 248 "b": float64(2.0), 249 }, time.Time{}), 250 ptinput.NewPlPoint(point.Tracing, "n1", map[string]string{ 251 "a": "1", 252 }, map[string]any{ 253 "d": "x1", 254 "b": float64(2.0), 255 }, time.Time{}), 256 ptinput.NewPlPoint(point.RUM, "n1", map[string]string{ 257 "a": "1", 258 }, map[string]any{ 259 "d": "x1", 260 "b": float64(2.0), 261 }, time.Time{}), 262 ptinput.NewPlPoint(point.Network, "n1", map[string]string{ 263 "a": "1", 264 }, map[string]any{ 265 "d": "x1", 266 "b": float64(2.0), 267 }, time.Time{}), 268 ptinput.NewPlPoint(point.Object, "n1", map[string]string{ 269 "a": "1", 270 }, map[string]any{ 271 "d": "x1", 272 "b": float64(2.0), 273 }, time.Time{}), 274 ptinput.NewPlPoint(point.CustomObject, "n1", map[string]string{ 275 "a": "1", 276 }, map[string]any{ 277 "d": "x1", 278 "b": float64(2.0), 279 }, time.Time{}), 280 ptinput.NewPlPoint(point.Security, "n1", map[string]string{ 281 "a": "1", 282 }, map[string]any{ 283 "d": "x1", 284 "b": float64(2.0), 285 }, time.Time{}), 286 }, 287 }, 288 } 289 290 for idx, tc := range cases { 291 t.Run(tc.name, func(t *testing.T) { 292 pls, errs := NewTestingRunner2(tc.allPl) 293 if len(errs) != 0 { 294 if tc.fail { 295 t.Logf("[%d]expect error: %v", idx, errs) 296 } else { 297 t.Errorf("[%d] failed: %v", idx, errs) 298 } 299 return 300 } 301 runner, ok := pls["main.p"] 302 if !ok { 303 t.Fatal(ok) 304 } 305 306 pt := ptinput.NewPlPoint( 307 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 308 errR := runScript(runner, pt) 309 310 t.Log(pt.Fields()) 311 if errR != nil { 312 if tc.fail { 313 return 314 } 315 t.Fatal(errR) 316 } 317 318 pts := pt.GetSubPoint() 319 320 if len(pts) != len(tc.expect) { 321 t.Fatal("len(pt)!= len(tc.expect)") 322 } 323 for i, pt := range pts { 324 expect := tc.expect[i] 325 assert.Equal(t, expect.Tags(), pt.Tags()) 326 assert.Equal(t, expect.Fields(), pt.Fields()) 327 assert.Equal(t, expect.GetPtName(), pt.GetPtName()) 328 assert.Equal(t, expect.Category(), pt.Category()) 329 if !expect.PtTime().IsZero() { 330 assert.Equal(t, expect.PtTime(), pt.PtTime()) 331 } 332 } 333 }) 334 } 335 }