/// <summary> /// Helper method to write the data in a stream to the console /// </summary> /// <param name="description">The description text that will be written before the stream data</param> /// <param name="ms">Stream containing the data to write</param> /// <param name="context">The BizUnit context object which holds state and is passed between test steps</param> public static void WriteStreamToConsole(string description, MemoryStream ms, Context context) { ms.Seek(0, SeekOrigin.Begin); StreamReader sr = new StreamReader(ms); context.LogData( description, sr.ReadToEnd() ); ms.Seek(0, SeekOrigin.Begin); }
/// <summary> /// TestStepBase.Execute() implementation /// </summary> /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param> public override void Execute(Context context) { Thread.Sleep(Timeout); // Get the list of files in the directory string [] filelist = Directory.GetFiles( DirectoryPath, SearchPattern ); if ( filelist.Length == 0) { // Expecting more than one file throw new ApplicationException( String.Format( "Directory contains no files matching the pattern!" ) ); } // For each file in the file list foreach (string filePath in filelist) { context.LogInfo("FileReadMultipleStep validating file: {0}", filePath); Stream fileData = StreamHelper.LoadFileToStream(filePath, Timeout); context.LogData("File: " + filePath, fileData); fileData.Seek(0, SeekOrigin.Begin); // Check it against the validate steps to see if it matches one of them foreach(var subStep in _subSteps) { try { // Try the validation and catch the exception fileData = subStep.Execute(fileData, context); } catch (Exception ex) { context.LogException(ex); throw; } } if(DeleteFiles) { File.Delete(filePath); } } }
/// <summary> /// ITestStep.Execute() implementation /// </summary> /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param> public override void Execute(Context context) { if (DelayBeforeCheck > 0) { context.LogInfo("Waiting for {0} seconds before checking the event log.", DelayBeforeCheck); System.Threading.Thread.Sleep(DelayBeforeCheck * 1000); } EventLogEntryType entryType = (EventLogEntryType)Enum.Parse(typeof(EventLogEntryType), Type, true); DateTime cutOffTime = context.TestCaseStart; // Note: event log time is always truncated, so the cut off time also need sto be! cutOffTime = cutOffTime.Subtract(new TimeSpan(0, 0, 0, 0, context.TestCaseStart.Millisecond + 1)); bool found = false; using (EventLog log = new EventLog(EventLog, Machine)) { EventLogEntryCollection entries = log.Entries; context.LogInfo("Scanning {0} event log entries from log: '{1}' on machine: '{2}', cutOffTime: '{3}'.", entries.Count, EventLog, Machine, cutOffTime.ToString("HH:mm:ss.fff dd/MM/yyyy")); for (int i = entries.Count - 1; i >= 0; i--) { EventLogEntry entry = entries[i]; if (0 > (DateTime.Compare(entry.TimeGenerated, cutOffTime))) { context.LogInfo("Scanning of event log stopped, event.TimeGenerated: {0}, cutOffTime: {1}", entry.TimeGenerated.ToString("HH:mm:ss.fff dd/MM/yyyy"), cutOffTime.ToString("HH:mm:ss.fff dd/MM/yyyy")); found = false; break; } context.LogInfo("Checking entry, Source: {0}, EntryType: {1}, EventId: {2}", entry.Source, entry.EntryType, entry.InstanceId); // Note: EventId is optional... if (((entry.Source == Source) && (entry.EntryType == entryType)) && (((EventId > 0) && (entry.InstanceId == EventId)) || (EventId == 0))) { foreach (string validationRegex in _validationRegExs) { string matchPattern = validationRegex; Match match = Regex.Match(entry.Message, matchPattern); if (match.Success) { found = true; context.LogInfo("Successfully matched event log entry generated at '{0}'.", entry.TimeGenerated); context.LogData("Event log entry.", entry.Message); break; } found = false; } } if (found) { break; } } } // Check that its ok if (!FailIfFound && !found) { throw new ApplicationException("Failed to find expected event log entry."); } if (FailIfFound && found) { throw new ApplicationException("Found event log entry which should not be present."); } }