public async Task GetTextLogsTest() { var filter = new ModuleLogFilter(Option.Some(100), Option.Some("1501000"), Option.None <string>(), Option.Some(3), Option.None <bool>(), Option.Some("ERR")); LogsContentEncoding contentEncoding = LogsContentEncoding.None; LogsContentType contentType = LogsContentType.Text; string payload = @"{ ""schemaVersion"": ""1.0"", ""items"": { ""id"": ""m1"", ""filter"": <filter> }, ""encoding"": ""none"", ""contentType"": ""text"" }" .Replace("<filter>", filter.ToJson()); string mod1 = "m1"; string mod2 = "m2"; string mod3 = "m3"; var moduleRuntimeInfoList = new List <ModuleRuntimeInfo> { new ModuleRuntimeInfo(mod1, "docker", ModuleStatus.Running, string.Empty, 0, Option.None <DateTime>(), Option.None <DateTime>()), new ModuleRuntimeInfo(mod2, "docker", ModuleStatus.Running, string.Empty, 0, Option.None <DateTime>(), Option.None <DateTime>()), new ModuleRuntimeInfo(mod3, "docker", ModuleStatus.Running, string.Empty, 0, Option.None <DateTime>(), Option.None <DateTime>()) }; var runtimeInfoProvider = new Mock <IRuntimeInfoProvider>(); runtimeInfoProvider.Setup(r => r.GetModules(It.IsAny <CancellationToken>())) .ReturnsAsync(moduleRuntimeInfoList); var logsProvider = new Mock <ILogsProvider>(); var module1LogOptions = new ModuleLogOptions(contentEncoding, contentType, filter, LogOutputFraming.None, Option.None <LogsOutputGroupingConfig>(), false); string mod1Logs = new[] { "Log line 1\n", "Log line 2\n", "Log line 3\n" }.Join(string.Empty); logsProvider.Setup(l => l.GetLogs(mod1, module1LogOptions, It.IsAny <CancellationToken>())) .ReturnsAsync(mod1Logs.ToBytes()); // Act var logsRequestHandler = new ModuleLogsRequestHandler(logsProvider.Object, runtimeInfoProvider.Object); Option <string> response = await logsRequestHandler.HandleRequest(Option.Maybe(payload), CancellationToken.None); // Assert Assert.True(response.HasValue); logsProvider.VerifyAll(); runtimeInfoProvider.VerifyAll(); var logsResponseList = response.OrDefault().FromJson <List <ModuleLogsResponse> >(); Assert.NotNull(logsResponseList); Assert.Single(logsResponseList); ModuleLogsResponse logsResponse = logsResponseList[0]; Assert.Equal(mod1, logsResponse.Id); Assert.True(logsResponse.Payload.HasValue); Assert.False(logsResponse.PayloadBytes.HasValue); Assert.Equal(mod1Logs, logsResponse.Payload.OrDefault()); }
public async Task GetJsonGzipLogsTest() { var filter = new ModuleLogFilter(Option.Some(100), Option.Some("1501000"), Option.None <string>(), Option.Some(3), Option.Some("ERR")); LogsContentEncoding contentEncoding = LogsContentEncoding.Gzip; LogsContentType contentType = LogsContentType.Json; string payload = @"{ ""schemaVersion"": ""1.0"", ""items"": { ""id"": ""m1"", ""filter"": <filter> }, ""encoding"": ""gzip"", ""contentType"": ""json"" }" .Replace("<filter>", filter.ToJson()); string iotHub = "foo.azure-devices.net"; string deviceId = "d1"; string mod1 = "m1"; string mod2 = "m2"; string mod3 = "m3"; var moduleRuntimeInfoList = new List <ModuleRuntimeInfo> { new ModuleRuntimeInfo(mod1, "docker", ModuleStatus.Running, string.Empty, 0, Option.None <DateTime>(), Option.None <DateTime>()), new ModuleRuntimeInfo(mod2, "docker", ModuleStatus.Running, string.Empty, 0, Option.None <DateTime>(), Option.None <DateTime>()), new ModuleRuntimeInfo(mod3, "docker", ModuleStatus.Running, string.Empty, 0, Option.None <DateTime>(), Option.None <DateTime>()) }; var runtimeInfoProvider = new Mock <IRuntimeInfoProvider>(); runtimeInfoProvider.Setup(r => r.GetModules(It.IsAny <CancellationToken>())) .ReturnsAsync(moduleRuntimeInfoList); var logsProvider = new Mock <ILogsProvider>(); var module1LogOptions = new ModuleLogOptions(contentEncoding, contentType, filter, LogOutputFraming.None, Option.None <LogsOutputGroupingConfig>(), false); var mod1Logs = new List <ModuleLogMessage> { new ModuleLogMessage(iotHub, deviceId, mod1, "0", 6, Option.None <DateTime>(), "log line 1"), new ModuleLogMessage(iotHub, deviceId, mod1, "0", 6, Option.None <DateTime>(), "log line 2"), new ModuleLogMessage(iotHub, deviceId, mod1, "0", 6, Option.None <DateTime>(), "log line 3") }; byte[] mod1LogBytes = Compression.CompressToGzip(mod1Logs.ToBytes()); logsProvider.Setup(l => l.GetLogs(mod1, module1LogOptions, It.IsAny <CancellationToken>())) .ReturnsAsync(mod1LogBytes); // Act var logsRequestHandler = new ModuleLogsRequestHandler(logsProvider.Object, runtimeInfoProvider.Object); Option <string> response = await logsRequestHandler.HandleRequest(Option.Maybe(payload), CancellationToken.None); // Assert Assert.True(response.HasValue); logsProvider.VerifyAll(); runtimeInfoProvider.VerifyAll(); var logsResponseList = response.OrDefault().FromJson <List <ModuleLogsResponse> >(); Assert.NotNull(logsResponseList); Assert.Single(logsResponseList); ModuleLogsResponse logsResponse = logsResponseList[0]; Assert.Equal(mod1, logsResponse.Id); Assert.False(logsResponse.Payload.HasValue); Assert.True(logsResponse.PayloadBytes.HasValue); Assert.Equal(mod1LogBytes, logsResponse.PayloadBytes.OrDefault()); }