github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_grok_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 tu "github.com/GuanceCloud/cliutils/testutil" 15 ) 16 17 func TestGrok(t *testing.T) { 18 cases := []struct { 19 name, pl, in string 20 expected interface{} 21 fail bool 22 outkey string 23 }{ 24 { 25 name: "normal_return_t", 26 pl: ` 27 add_pattern("_second", "(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)") 28 add_pattern("_minute", "(?:[0-5][0-9])") 29 add_pattern("_hour", "(?:2[0123]|[01]?[0-9])") 30 add_pattern("time", "([^0-9]?)%{_hour:hour}:%{_minute:minute}(?::%{_second:second})([^0-9]?)") 31 add_key(grok_match_ok, grok(_, "%{time}"))`, 32 in: "12:13:14.123", 33 expected: true, 34 outkey: "grok_match_ok", 35 }, 36 { 37 name: "normal_return_f", 38 pl: ` 39 add_pattern("_second", "(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)") 40 add_pattern("_minute", "(?:[0-5][0-9])") 41 add_pattern("_hour", "(?:2[0123]|[01]?[0-9])") 42 add_pattern("time", "([^0-9]?)%{_hour:hour}:%{_minute:minute}(?::%{_second:second})([^0-9]?)") 43 add_key(grok_match_ok, grok(_, "%{time}"))`, 44 in: "12 :13:14.123", 45 expected: false, 46 outkey: "grok_match_ok", 47 }, 48 { 49 name: "normal_return_sample_t", 50 pl: ` 51 add_key(grok_match_ok, grok(_, "12 :13:14.123"))`, 52 in: "12 :13:14.123", 53 expected: true, 54 outkey: "grok_match_ok", 55 }, 56 { 57 name: "normal_return_sample_f", 58 pl: ` 59 add_key(grok_match_ok, grok(_, "12 :13:14.123"))`, 60 in: "12:13:14.123", 61 expected: false, 62 outkey: "grok_match_ok", 63 }, 64 { 65 name: "normal", 66 pl: ` 67 add_pattern("_second", "(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)") 68 add_pattern("_minute", "(?:[0-5][0-9])") 69 add_pattern("_hour", "(?:2[0123]|[01]?[0-9])") 70 add_pattern("time", "([^0-9]?)%{_hour:hour}:%{_minute:minute}(?::%{_second:second})([^0-9]?)") 71 grok(_, "%{time}")`, 72 in: "12:13:14.123", 73 expected: "14.123", 74 outkey: "second", 75 }, 76 { 77 name: "normal", 78 pl: ` 79 add_pattern("_second", "(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)") 80 add_pattern("_minute", "(?:[0-5][0-9])") 81 add_pattern("_hour", "(?:2[0123]|[01]?[0-9])") 82 add_pattern("time", "([^0-9]?)%{_hour:hour}:%{_minute:minute}(?::%{_second:second})([^0-9]?)") 83 grok(_, "%{time}")`, 84 in: "12:13:14", 85 expected: "13", 86 outkey: "minute", 87 }, 88 { 89 name: "normal", 90 pl: ` 91 add_pattern("_second", "(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)") 92 add_pattern("_minute", "(?:[0-5][0-9])") 93 add_pattern("_hour", "(?:2[0123]|[01]?[0-9])") 94 add_pattern("time", "([^0-9]?)%{_hour:hour}:%{_minute:minute}(?::%{_second:second})([^0-9]?)") 95 grok(_, "%{time}")`, 96 in: "12:13:14", 97 expected: "12", 98 outkey: "hour", 99 }, 100 { 101 name: "normal", 102 pl: ` 103 add_pattern("_second", "(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)") 104 add_pattern("_minute", "(?:[0-5][0-9])") 105 add_pattern("_hour", "(?:2[0123]|[01]?[0-9])") 106 add_pattern("time", "([^0-9]?)%{_hour:hour}:%{_minute:minute}(?::%{_second:second})([^0-9]?)") 107 grok(_, "%{time}")`, 108 in: "12:13:14", 109 expected: "14", 110 outkey: "second", 111 }, 112 { 113 name: "normal", 114 pl: ` 115 add_pattern("time", "%{NUMBER:time:float}") 116 grok(_, '''%{time} 117 %{WORD:word:string} 118 %{WORD:code:int} 119 %{WORD:w1}''')`, 120 in: `1.1 121 s 122 123cvf 123 aa222`, 124 expected: int64(0), 125 outkey: "code", 126 }, 127 { 128 name: "normal", 129 pl: ` 130 add_pattern("time", "%{NUMBER:time:float}") 131 grok(_, '''%{time} 132 %{WORD:word:string} 133 %{WORD:code:int} 134 %{WORD:w1}''')`, 135 in: `1.1 136 s 137 123 138 aa222`, 139 expected: int64(123), 140 outkey: "code", 141 }, 142 { 143 name: "normal", 144 pl: ` 145 add_pattern("time", "%{NUMBER:time:float}") 146 grok(_, '''%{time} 147 %{WORD:word:str} 148 %{WORD:code:int} 149 %{WORD:w1}''')`, 150 in: `1.1 151 s 152 123 153 aa222`, 154 expected: int64(123), 155 outkey: "code", 156 }, 157 { 158 name: "normal", 159 pl: ` 160 add_pattern("_second", "(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)") 161 add_pattern("_minute", "(?:[0-5][0-9])") 162 add_pattern("_hour", "(?:2[0123]|[01]?[0-9])") 163 add_pattern("time", "([^0-9]?)%{_hour:hour:string}:%{_minute:minute:int}(?::%{_second:second:float})([^0-9]?)") 164 grok(_, "%{WORD:date} %{time}")`, 165 in: "2021/1/11 2:13:14.123", 166 expected: float64(14.123), 167 outkey: "second", 168 }, 169 { 170 name: "trim_space", 171 in: " not_space ", 172 pl: `add_pattern("d", "[\\s\\S]*") 173 grok(_, "%{d:item}")`, 174 expected: "not_space", 175 outkey: "item", 176 }, 177 { 178 name: "trim_space, enable", 179 in: " not_space ", 180 pl: `add_pattern("d", "[\\s\\S]*") 181 grok(_, "%{d:item}", true)`, 182 expected: "not_space", 183 outkey: "item", 184 }, 185 { 186 name: "trim_space, disable", 187 in: " not_space ", 188 pl: `add_pattern("d", "[\\s\\S]*") 189 grok(_, "%{d:item}", false)`, 190 expected: " not_space ", 191 outkey: "item", 192 }, 193 } 194 195 for idx, tc := range cases { 196 t.Run(tc.name, func(t *testing.T) { 197 runner, err := NewTestingRunner(tc.pl) 198 if err != nil { 199 if tc.fail { 200 t.Logf("[%d]expect error: %s", idx, err) 201 } else { 202 t.Errorf("[%d] failed: %s", idx, err) 203 } 204 return 205 } 206 207 pt := ptinput.NewPlPoint( 208 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 209 errR := runScript(runner, pt) 210 211 if errR != nil { 212 t.Fatal(errR.Error()) 213 } 214 215 if tc.fail { 216 t.Logf("[%d]expect error: %s", idx, errR.Error()) 217 } 218 v, _, _ := pt.Get(tc.outkey) 219 tu.Equals(t, tc.expected, v) 220 t.Logf("[%d] PASS", idx) 221 }) 222 } 223 }