|
| 1 | +using System; |
| 2 | +using Xunit; |
| 3 | +using GrokNet; |
| 4 | + |
| 5 | +namespace GrokNetTests |
| 6 | +{ |
| 7 | + public class UnitTests |
| 8 | + { |
| 9 | + [Fact] |
| 10 | + public void ParseEmptyTest() |
| 11 | + { |
| 12 | + string grokPattern = ""; |
| 13 | + Grok act = new Grok(grokPattern); |
| 14 | + string logs = ""; |
| 15 | + |
| 16 | + GrokResult grokResult = act.Parse(logs); |
| 17 | + |
| 18 | + Assert.NotNull(grokResult); |
| 19 | + Assert.Empty(grokResult); |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void PatternCountTest() |
| 25 | + { |
| 26 | + string grokPattern = "%{MONTHDAY:month}-%{MONTHDAY:day}-%{MONTHDAY:year} %{TIME:timestamp};%{WORD:id};%{LOGLEVEL:loglevel};%{WORD:func};%{GREEDYDATA:msg}"; |
| 27 | + Grok act = new Grok(grokPattern); |
| 28 | + string logs = @"06-21-19 21:00:13:589241;15;INFO;main;DECODED: 775233900043 DECODED BY: 18500738 DISTANCE: 1.5165 |
| 29 | + 06-21-19 21:00:13:589265;156;WARN;main;DECODED: 775233900043 EMPTY DISTANCE: --------"; |
| 30 | + |
| 31 | + GrokResult grokResult = act.Parse(logs); |
| 32 | + |
| 33 | + Assert.Equal(16, grokResult.Count); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void MonthDayPatternTest() |
| 38 | + { |
| 39 | + Grok act = new Grok("%{MONTHDAY:month}-%{MONTHDAY:day}-%{MONTHDAY:year} %{TIME:timestamp};%{WORD:id};%{LOGLEVEL:loglevel};%{WORD:func};%{GREEDYDATA:msg}"); |
| 40 | + string logs = @"06-21-19 21:00:13:589241;15;INFO;main;DECODED: 775233900043 DECODED BY: 18500738 DISTANCE: 1.5165 |
| 41 | + 06-21-19 21:00:13:589265;156;WARN;main;DECODED: 775233900043 EMPTY DISTANCE: --------"; |
| 42 | + |
| 43 | + GrokResult grokResult = act.Parse(logs); |
| 44 | + |
| 45 | + Assert.Equal("month", grokResult[0].Key); |
| 46 | + Assert.Equal("06", grokResult[0].Value); |
| 47 | + Assert.Equal("day", grokResult[1].Key); |
| 48 | + Assert.Equal("21", grokResult[1].Value); |
| 49 | + Assert.Equal("year", grokResult[2].Key); |
| 50 | + Assert.Equal("19", grokResult[2].Value); |
| 51 | + Assert.Equal("month", grokResult[8].Key); |
| 52 | + Assert.Equal("06", grokResult[8].Value); |
| 53 | + Assert.Equal("day", grokResult[9].Key); |
| 54 | + Assert.Equal("21", grokResult[9].Value); |
| 55 | + Assert.Equal("year", grokResult[10].Key); |
| 56 | + Assert.Equal("19", grokResult[10].Value); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void TimePatternTest() |
| 61 | + { |
| 62 | + Grok act = new Grok("%{MONTHDAY:month}-%{MONTHDAY:day}-%{MONTHDAY:year} %{TIME:timestamp};%{WORD:id};%{LOGLEVEL:loglevel};%{WORD:func};%{GREEDYDATA:msg}"); |
| 63 | + string logs = @"06-21-19 21:00:13:589241;15;INFO;main;DECODED: 775233900043 DECODED BY: 18500738 DISTANCE: 1.5165 |
| 64 | + 06-21-19 21:00:13:589265;156;WARN;main;DECODED: 775233900043 EMPTY DISTANCE: --------"; |
| 65 | + |
| 66 | + GrokResult grokResult = act.Parse(logs); |
| 67 | + |
| 68 | + Assert.Equal("21:00:13:589241", grokResult[3].Value); |
| 69 | + Assert.Equal("21:00:13:589265", grokResult[11].Value); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void EmailPatternTest() |
| 74 | + { |
| 75 | + Grok act = new Grok("%{EMAILADDRESS:email}:%{GREEDYDATA:comment}"); |
| 76 | + string logs = @"[email protected]:Free as in Free Beer"; |
| 77 | + |
| 78 | + GrokResult grokResult = act.Parse(logs); |
| 79 | + |
| 80 | + Assert.Equal("[email protected]", grokResult[0].Value); |
| 81 | + Assert.Equal("Free as in Free Beer", grokResult[1].Value); |
| 82 | + Assert.Equal("email", grokResult[0].Key); |
| 83 | + Assert.Equal("comment", grokResult[1].Key); |
| 84 | + |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments