public async Task RotatingFileNameTest() { string testName = "RotatingFileNameTest"; string filter = "KinesisTapxyz.log*"; string logFileName = "KinesisTapxyz.log"; await CreateAndRunWatcher(testName, filter, async (logRecords) => { int accumulatedRecords = 0; string filePath = Path.Combine(TestUtility.GetTestHome(), testName, logFileName); WriteRandomRecords(filePath, out int records, out string lastLine1, GenerateSingleLineRecord); accumulatedRecords += records; RenameLogFile(filePath); WriteRandomRecords(filePath, out records, out string lastLine2, GenerateSingleLineRecord); accumulatedRecords += records; await Task.Delay(2000); //Make sure that the last record of both batches are captured Assert.NotNull(logRecords.FirstOrDefault(l => lastLine1.Equals(l.GetMessage(null)))); Assert.NotNull(logRecords.FirstOrDefault(l => lastLine2.Equals(l.GetMessage(null)))); Assert.Equal(accumulatedRecords, logRecords.Count); //All records captured }); }
private async Task CreateAndRunWatcher <TData>(string testName, string filter, IConfiguration config, Func <List <IEnvelope>, Task> testBody, IRecordParser <TData, LogContext> recordParser) { //Create a distinct directory based on testName so that tests can run in parallel string testDir = Path.Combine(TestUtility.GetTestHome(), testName); //The following will creates all directories and subdirectories in the specified path unless they already exist. Directory.CreateDirectory(testDir); //Clean up before the test rather than after so that we can inspect the files DeleteFiles(testDir, "*.*"); ListEventSink logRecords = new ListEventSink(); DirectorySource <TData, LogContext> watcher = new DirectorySource <TData, LogContext> (testDir, filter, 1000, new PluginContext(config, NullLogger.Instance, null), recordParser, DirectorySourceFactory.CreateLogSourceInfo); watcher.Subscribe(logRecords); watcher.Start(); await testBody(logRecords); watcher.Stop(); }
public async Task IncludeSubdirectoriesTest() { var testName = "IncludeSubdirectoriesTest"; var filter = "*.*"; var subDir1 = "CPU"; var subDir2 = "Memory"; var directory = new TestDirectory(testName) { SubDirectories = new TestDirectory[] { new TestDirectory(subDir1), new TestDirectory(subDir2) } }; var config = TestUtility.GetConfig("Sources", "IncludeSubdirectories"); await CreateAndRunWatcher(testName, filter, config, async (logRecords) => { var filePath1 = Path.Combine(TestUtility.GetTestHome(), testName, subDir1, "test"); var filePath2 = Path.Combine(TestUtility.GetTestHome(), testName, subDir2, "test"); this.WriteLog(filePath1, "this is a test"); this.WriteLog(filePath2, "this is another test"); await Task.Delay(2000); Assert.Equal(2, logRecords.Count); var env1 = (ILogEnvelope)logRecords[0]; Assert.Equal("test", env1.FileName); Assert.Equal(filePath1, env1.FilePath); var env2 = (ILogEnvelope)logRecords[1]; Assert.Equal("test", env2.FileName); Assert.Equal(filePath2, env2.FilePath); }, new SingleLineRecordParser(), directory); }
public void TestObjectDecorationWithFileName(string sinkId) { string id = sinkId + (Utility.IsWindows ? string.Empty : TestUtility.LINUX); MemoryLogger logger = new MemoryLogger(null); MockEventSource <IDictionary <string, string> > mockEventSource = CreateEventsource <IDictionary <string, string> >("InitialPositionUnspecified"); MockEventSink sink = CreateEventSink(id, logger); mockEventSource.Subscribe(sink); string text = "some text"; Dictionary <string, string> data = new Dictionary <string, string>() { { "data", text } }; DateTime timestamp = DateTime.UtcNow; string filePath = Path.Combine(TestUtility.GetTestHome(), "test.log"); long position = 11; long lineNumber = 1; mockEventSource.MockLogEvent(data, timestamp, text, filePath, position, lineNumber); string result = sink.Records[0]; Assert.Equal($"{{\"data\":\"{text}\",\"ComputerName\":\"{ComputerOrHostName}\",\"FileName\":\"{Path.GetFileName(filePath)}\",\"Position\":\"{position}\"}}", result); }
public void TestDirectorySourceMetricsOnSubscribe() { IConfiguration config = GetConfig("directorySourceTest"); string id = config[ConfigConstants.ID]; MemoryLogger logger = new MemoryLogger(null); KinesisTapMetricsSource metrics = new KinesisTapMetricsSource(new PluginContext(null, null, null, null, null)); DirectorySource <string, LogContext> source = new DirectorySource <string, LogContext>( TestUtility.GetTestHome(), "*.*", 1000, new PluginContext(config, logger, metrics), new SingeLineRecordParser()); MockMetricsSink metricsSink = new MockMetricsSink(3600, new PluginContext(config, logger, metrics)); source.Start(); metrics.Subscribe(metricsSink); metricsSink.Stop(); source.Stop(); Assert.Equal(2, metricsSink.AccumlatedValues.Count); Assert.Equal(0, TestUtility.GetMetricsCount(metricsSink.AccumlatedValues)); }