github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_drop_origin_data_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 ) 15 16 func TestDropOriginData(t *testing.T) { 17 cases := []struct { 18 name, pl, in string 19 key string 20 fail bool 21 }{ 22 { 23 name: "value type: string", 24 in: `162.62.81.1 - - [29/Nov/2021:07:30:50 +0000] "POST /?signature=b8d8ea×tamp=1638171049 HTTP/1.1" 200 413 "-" "Mozilla/4.0"`, 25 pl: ` 26 grok(_, "%{IPORHOST:client_ip} %{NOTSPACE} %{NOTSPACE} \\[%{HTTPDATE:time}\\] \"%{DATA} %{GREEDYDATA} HTTP/%{NUMBER}\" %{INT:status_code} %{INT:bytes}") 27 drop_origin_data() 28 `, 29 key: "message", 30 fail: false, 31 }, 32 } 33 34 for idx, tc := range cases { 35 t.Run(tc.name, func(t *testing.T) { 36 runner, err := NewTestingRunner(tc.pl) 37 if err != nil { 38 if tc.fail { 39 t.Logf("[%d]expect error: %s", idx, err) 40 } else { 41 t.Errorf("[%d] failed: %s", idx, err) 42 } 43 return 44 } 45 pt := ptinput.NewPlPoint( 46 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 47 errR := runScript(runner, pt) 48 49 if errR != nil { 50 t.Fatal(errR) 51 } 52 53 if v, _, e := pt.Get(tc.key); e == nil { 54 t.Errorf("[%d] failed: key `%s` value `%v`", idx, tc.key, v) 55 } else { 56 t.Logf("[%d] PASS", idx) 57 } 58 }) 59 } 60 }