From c86ef86d63c761138c1e25a30cc0d519b93cb6ab Mon Sep 17 00:00:00 2001 From: Jonathan Preddy Date: Wed, 23 Jul 2014 15:14:31 -0400 Subject: [PATCH] More tests for filters --- TimberWinR.UnitTests/Configuration.cs | 234 +++++++++++++++----------- TimberWinR.UnitTests/testconf.xml | 10 +- TimberWinR/Filters/FilterBase.cs | 9 + TimberWinR/Filters/GrokFilter.cs | 33 +++- TimberWinR/Inputs/FieldDefinitions.cs | 35 ++++ 5 files changed, 222 insertions(+), 99 deletions(-) diff --git a/TimberWinR.UnitTests/Configuration.cs b/TimberWinR.UnitTests/Configuration.cs index 9b18ca7..40af279 100644 --- a/TimberWinR.UnitTests/Configuration.cs +++ b/TimberWinR.UnitTests/Configuration.cs @@ -77,135 +77,124 @@ namespace TimberWinR.UnitTests public void NumOfIISW3C() { Assert.AreEqual(1, c.IISW3C.ToArray().Length); + } + + [Test] + public void NumOfFilters() + { + Assert.AreEqual(3, c.Filters.ToArray().Length); } [Test] public void FieldsOfEvents() { - Dictionary fields = new Dictionary() + List fields = new List() { - { "EventLog", typeof(string) }, - { "RecordNumber", typeof(int) }, - { "TimeGenerated", typeof(DateTime) }, - { "TimeWritten", typeof(DateTime) }, - { "EventID", typeof(int) }, - { "EventType", typeof(int) }, - { "EventTypeName", typeof(string) }, - { "EventCategory", typeof(int) }, - { "EventCategoryName", typeof(string) }, - { "SourceName", typeof(string) }, - { "Strings", typeof(string) }, - { "ComputerName", typeof(string) }, - { "SID", typeof(string) }, - { "Message", typeof(string) }, - { "Data", typeof(string) } + new FieldDefinition("EventLog", typeof(string)), + new FieldDefinition("RecordNumber", typeof(int)), + new FieldDefinition("TimeGenerated", typeof(DateTime)), + new FieldDefinition("TimeWritten", typeof(DateTime)), + new FieldDefinition("EventID", typeof(int)), + new FieldDefinition("EventType", typeof(int)), + new FieldDefinition("EventTypeName", typeof(string)), + new FieldDefinition("EventCategory", typeof(int)), + new FieldDefinition("EventCategoryName", typeof(string)), + new FieldDefinition("SourceName", typeof(string)), + new FieldDefinition("Strings", typeof(string)), + new FieldDefinition("ComputerName", typeof(string)), + new FieldDefinition("SID", typeof(string)), + new FieldDefinition("Message", typeof(string)), + new FieldDefinition("Data", typeof(string)) }; - foreach (FieldDefinition field in c.Events.ToArray()[0].Fields) - { - Assert.Contains(field.Name, fields.Keys); - } + + CollectionAssert.AreEqual(fields, c.Events.ToArray()[0].Fields); } [Test] public void FieldsOfLogs() { - Dictionary fields = new Dictionary() + List fields = new List() { - { "LogFilename", typeof(string) }, - { "Index", typeof(int) }, - { "Text", typeof(string) } + new FieldDefinition("LogFilename", typeof(string)), + new FieldDefinition("Index", typeof(int)), + new FieldDefinition("Text", typeof(string)) }; - foreach (FieldDefinition field in c.Logs.ToArray()[0].Fields) - { - Assert.Contains(field.Name, fields.Keys); - } - foreach (FieldDefinition field in c.Logs.ToArray()[1].Fields) - { - Assert.Contains(field.Name, fields.Keys); - } - foreach (FieldDefinition field in c.Logs.ToArray()[2].Fields) - { - Assert.Contains(field.Name, fields.Keys); - } + CollectionAssert.AreEqual(fields, c.Logs.ToArray()[0].Fields); + CollectionAssert.AreEqual(fields, c.Logs.ToArray()[1].Fields); + CollectionAssert.AreEqual(fields, c.Logs.ToArray()[2].Fields); } [Test] public void FieldsOfIIS() { - Dictionary fields = new Dictionary() + List fields = new List() { - { "LogFilename", typeof(string) }, - { "LogRow", typeof(int) }, - { "UserIP", typeof(string) }, - { "UserName", typeof(string) }, - { "Date", typeof(DateTime) }, - { "Time", typeof(DateTime) }, - { "ServiceInstance", typeof(string) }, - { "HostName", typeof(string) }, - { "ServerIP", typeof(string) }, - { "TimeTaken", typeof(int) }, - { "BytesSent", typeof(int) }, - { "BytesReceived", typeof(int) }, - { "StatusCode", typeof(int) }, - { "Win32StatusCode", typeof(int) }, - { "RequestType", typeof(string) }, - { "Target", typeof(string) }, - { "Parameters", typeof(string) } + new FieldDefinition("LogFilename", typeof(string)), + new FieldDefinition("LogRow", typeof(int)), + new FieldDefinition("UserIP", typeof(string)), + new FieldDefinition("UserName", typeof(string)), + new FieldDefinition("Date", typeof(DateTime)), + new FieldDefinition("Time", typeof(DateTime)), + new FieldDefinition("ServiceInstance", typeof(string)), + new FieldDefinition("HostName", typeof(string)), + new FieldDefinition("ServerIP", typeof(string)), + new FieldDefinition("TimeTaken", typeof(int)), + new FieldDefinition("BytesSent", typeof(int)), + new FieldDefinition("BytesReceived", typeof(int)), + new FieldDefinition("StatusCode", typeof(int)), + new FieldDefinition("Win32StatusCode", typeof(int)), + new FieldDefinition("RequestType", typeof(string)), + new FieldDefinition("Target", typeof(string)), + new FieldDefinition("Parameters", typeof(string)) }; foreach (var iis in c.IIS.ToArray()) { - foreach (FieldDefinition field in iis.Fields) - { - Assert.Contains(field.Name, fields.Keys); - } + CollectionAssert.AreEquivalent(fields, c.IIS.ToArray()[0].Fields); } - } [Test] public void FieldsOfIISW3C() { - Dictionary fields = new Dictionary() + List fields = new List() { - { "LogFilename", typeof(string) }, - { "LogRow", typeof(int) }, - { "date", typeof(DateTime) }, - { "time", typeof(DateTime) }, - { "c-ip", typeof(string) }, - { "cs-username", typeof(string) }, - { "s-sitename", typeof(string) }, - { "s-computername", typeof(int) }, - { "s-ip", typeof(string) }, - { "s-port", typeof(int) }, - { "cs-method", typeof(string) }, - { "cs-uri-stem", typeof(string) }, - { "cs-uri-query", typeof(string) }, - { "sc-status", typeof(int) }, - { "sc-substatus", typeof(int) }, - { "sc-win32-status", typeof(int) }, - { "sc-bytes", typeof(int) }, - { "cs-bytes", typeof(int) }, - { "time-taken", typeof(int) }, - { "cs-version", typeof(string) }, - { "cs-host", typeof(string) }, - { "cs(User-Agent)", typeof(string) }, - { "cs(Cookie)", typeof(string) }, - { "cs(Referer)", typeof(string) }, - { "s-event", typeof(string) }, - { "s-process-type", typeof(string) }, - { "s-user-time", typeof(double) }, - { "s-kernel-time", typeof(double) }, - { "s-page-faults", typeof(int) }, - { "s-total-procs", typeof(int) }, - { "s-active-procs", typeof(int) }, - { "s-stopped-procs", typeof(int) } + new FieldDefinition("LogFilename", typeof(string)), + new FieldDefinition("LogRow", typeof(int)), + new FieldDefinition("date", typeof(DateTime)), + new FieldDefinition("time", typeof(DateTime)), + new FieldDefinition("c-ip", typeof(string)), + new FieldDefinition("cs-username", typeof(string)), + new FieldDefinition("s-sitename", typeof(string)), + new FieldDefinition("s-computername", typeof(int)), + new FieldDefinition("s-ip", typeof(string)), + new FieldDefinition("s-port", typeof(int)), + new FieldDefinition("cs-method", typeof(string)), + new FieldDefinition("cs-uri-stem", typeof(string)), + new FieldDefinition("cs-uri-query", typeof(string)), + new FieldDefinition("sc-status", typeof(int)), + new FieldDefinition("sc-substatus", typeof(int)), + new FieldDefinition("sc-win32-status", typeof(int)), + new FieldDefinition("sc-bytes", typeof(int)), + new FieldDefinition("cs-bytes", typeof(int)), + new FieldDefinition("time-taken", typeof(int)), + new FieldDefinition("cs-version", typeof(string)), + new FieldDefinition("cs-host", typeof(string)), + new FieldDefinition("cs(User-Agent)", typeof(string)), + new FieldDefinition("cs(Cookie)", typeof(string)), + new FieldDefinition("cs(Referer)", typeof(string)), + new FieldDefinition("s-event", typeof(string)), + new FieldDefinition("s-process-type", typeof(string)), + new FieldDefinition("s-user-time", typeof(double)), + new FieldDefinition("s-kernel-time", typeof(double)), + new FieldDefinition("s-page-faults", typeof(int)), + new FieldDefinition("s-total-procs", typeof(int)), + new FieldDefinition("s-active-procs", typeof(int)), + new FieldDefinition("s-stopped-procs", typeof(int)) }; - foreach (FieldDefinition field in c.IISW3C.ToArray()[0].Fields) - { - Assert.Contains(field.Name, fields.Keys); - } + + CollectionAssert.AreEquivalent(fields, c.IISW3C.ToArray()[0].Fields); } [Test] @@ -305,6 +294,57 @@ namespace TimberWinR.UnitTests Assert.AreEqual(dirTime, iisw3c.DirTime); Assert.AreEqual(consolidateLogs, iisw3c.ConsolidateLogs); Assert.IsEmpty(iisw3c.ICheckpoint); - } + } + + [Test] + public void ParametersOfGrokFilters() + { + List addFields = new List(); + List removeFields = new List(); + + string field = "Text"; + string match = "%{IPAddress:ip1} %{IPAddress:ip2}"; + addFields.Add(new GrokFilter.FieldValuePair("field1", "%{foo}")); + bool dropIfMatch = true; + removeFields.Add("ip1"); + foreach (var filter in c.Filters) + { + if (filter.GetType() == typeof(GrokFilter)) + { + Console.WriteLine(((GrokFilter)filter).AddFields[0].Field); + Console.WriteLine(((GrokFilter)filter).AddFields[0].Value); + + Assert.AreEqual(field, ((GrokFilter)filter).Field); + Assert.AreEqual(match, ((GrokFilter)filter).Match); + CollectionAssert.AreEqual(addFields, ((GrokFilter)filter).AddFields); + Assert.AreEqual(dropIfMatch, ((GrokFilter)filter).DropIfMatch); + Assert.AreEqual(removeFields, ((GrokFilter)filter).RemoveFields); + } + } + } + + [Test] + public void ParametersOfDateFilters() + { + List patterns = new List(); + + string field = "timestamp"; + string target = "@timestamp"; + bool convertToUTC = true; + patterns.Add("MMM d HH:mm:ss"); + patterns.Add("MMM dd HH:mm:ss"); + patterns.Add("ISO8601"); + + foreach (var filter in c.Filters) + { + if (filter.GetType() == typeof(DateFilter)) + { + Assert.AreEqual(field, ((DateFilter)filter).Field); + Assert.AreEqual(target, ((DateFilter)filter).Target); + Assert.AreEqual(convertToUTC, ((DateFilter)filter).ConvertToUTC); + CollectionAssert.AreEquivalent(patterns, ((DateFilter)filter).Patterns); + } + } + } } } diff --git a/TimberWinR.UnitTests/testconf.xml b/TimberWinR.UnitTests/testconf.xml index 7de93cc..edc6552 100644 --- a/TimberWinR.UnitTests/testconf.xml +++ b/TimberWinR.UnitTests/testconf.xml @@ -18,10 +18,18 @@ - + + + + + + MMM d HH:mm:ss + MMM dd HH:mm:ss + ISO8601 + diff --git a/TimberWinR/Filters/FilterBase.cs b/TimberWinR/Filters/FilterBase.cs index 5d53ac6..53dd882 100644 --- a/TimberWinR/Filters/FilterBase.cs +++ b/TimberWinR/Filters/FilterBase.cs @@ -54,6 +54,15 @@ namespace TimberWinR.Filters { if (prop != null) { + if (prop.PropertyType == typeof(List<>)) + { + sb.Append(String.Format("\t{0}: ", prop.Name)); + foreach (var element in prop.GetValue(this, null) as List) + { + sb.Append(String.Format("{0},", element)); + } + sb.Append("\n"); + } sb.Append(String.Format("\t{0}: {1}\n", prop.Name, prop.GetValue(this, null))); } diff --git a/TimberWinR/Filters/GrokFilter.cs b/TimberWinR/Filters/GrokFilter.cs index 9a439b2..1477742 100644 --- a/TimberWinR/Filters/GrokFilter.cs +++ b/TimberWinR/Filters/GrokFilter.cs @@ -14,8 +14,8 @@ namespace TimberWinR.Filters { public new const string TagName = "Grok"; - public string Match { get; private set; } public string Field { get; private set; } + public string Match { get; private set; } public List AddFields { get; private set; } public bool DropIfMatch { get; private set; } public List RemoveFields { get; private set; } @@ -144,6 +144,37 @@ namespace TimberWinR.Filters Field = field; Value = value; } + + public override bool Equals(System.Object obj) + { + // If parameter is null return false. + if (obj == null) + { + return false; + } + + // If parameter cannot be cast to Point return false. + FieldValuePair p = obj as FieldValuePair; + if ((System.Object)p == null) + { + return false; + } + + // Return true if the fields match: + return (Field == p.Field) && (Value == p.Value); + } + + public bool Equals(FieldValuePair p) + { + // If parameter is null return false: + if ((object)p == null) + { + return false; + } + + // Return true if the fields match: + return (Field == p.Field) && (Value == p.Value); + } } private void ParseAddTags(XElement parent) diff --git a/TimberWinR/Inputs/FieldDefinitions.cs b/TimberWinR/Inputs/FieldDefinitions.cs index f687cb1..0d61655 100644 --- a/TimberWinR/Inputs/FieldDefinitions.cs +++ b/TimberWinR/Inputs/FieldDefinitions.cs @@ -37,6 +37,41 @@ namespace TimberWinR.Inputs Name = fieldName; FieldType = fieldType; } + public override string ToString() + { + return String.Format("{0}", Name); + } + + public override bool Equals(System.Object obj) + { + // If parameter is null return false. + if (obj == null) + { + return false; + } + + // If parameter cannot be cast to Point return false. + FieldDefinition p = obj as FieldDefinition; + if ((System.Object)p == null) + { + return false; + } + + // Return true if the fields match: + return (Name == p.Name) && (FieldType == p.FieldType); + } + + public bool Equals(FieldDefinition p) + { + // If parameter is null return false: + if ((object)p == null) + { + return false; + } + + // Return true if the fields match: + return (Name == p.Name) && (FieldType == p.FieldType); + } } public class FieldDefinitions : IEnumerable