public async Task GetTextLogsTest()
        {
            var filter = new ModuleLogFilter(Option.Some(100), Option.Some(1501000), Option.Some(3), 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 LogsRequestHandler(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 <LogsResponse> >();

            Assert.NotNull(logsResponseList);
            Assert.Single(logsResponseList);
            LogsResponse logsResponse = logsResponseList[0];

            Assert.Equal(mod1, logsResponse.Id);
            Assert.True(logsResponse.Payload.HasValue);
            Assert.False(logsResponse.PayloadBytes.HasValue);
            Assert.Equal(mod1Logs, logsResponse.Payload.OrDefault());
        }
Beispiel #2
0
        public async Task GetJsonGzipLogsTest()
        {
            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.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());
        }
        public static IEnumerable <object[]> GetLogsUploadRequestHandlerData()
        {
            string sasUrl = $"https://test1.blob.core.windows.net/cont2?st={Guid.NewGuid()}";

            yield return(new object[] { @"{""schemaVersion"":""1.0"",""sasUrl"":""<sasurl>"",""items"":{""id"":""edgeAgent""}}".Replace("<sasurl>", sasUrl), "edgeAgent", sasUrl, LogsContentEncoding.None, LogsContentType.Json, ModuleLogFilter.Empty });

            sasUrl = $"https://test1.blob.core.windows.net/cont2?st={Guid.NewGuid()}";
            yield return(new object[] { @"{""schemaVersion"":""1.0"",""sasUrl"":""<sasurl>"",""items"":{""id"":""edgeAgent""},""encoding"":""gzip""}".Replace("<sasurl>", sasUrl), "edgeAgent", sasUrl, LogsContentEncoding.Gzip, LogsContentType.Json, ModuleLogFilter.Empty });

            sasUrl = $"https://test1.blob.core.windows.net/cont2?st={Guid.NewGuid()}";
            yield return(new object[] { @"{""schemaVersion"":""1.0"",""sasUrl"":""<sasurl>"",""items"":{""id"":""mod1""},""encoding"":""gzip"",""contentType"":""text""}".Replace("<sasurl>", sasUrl), "mod1", sasUrl, LogsContentEncoding.Gzip, LogsContentType.Text, ModuleLogFilter.Empty });

            sasUrl = $"https://test1.blob.core.windows.net/cont2?st={Guid.NewGuid()}";
            yield return(new object[] { @"{""schemaVersion"":""1.0"",""sasUrl"":""<sasurl>"",""items"":{""id"":""edgeHub""},""encoding"":""none"",""contentType"":""json""}".Replace("<sasurl>", sasUrl), "edgeHub", sasUrl, LogsContentEncoding.None, LogsContentType.Json, ModuleLogFilter.Empty });

            sasUrl = $"https://test1.blob.core.windows.net/cont2?st={Guid.NewGuid()}";
            var filter = new ModuleLogFilter(Option.Some(100), Option.Some(1501000), Option.Some(3), Option.Some("ERR"));

            yield return(new object[] { @"{""schemaVersion"":""1.0"",""sasUrl"":""<sasurl>"",""items"":{""id"":""edgeAgent"",""filter"":<filter>}}".Replace("<sasurl>", sasUrl).Replace("<filter>", filter.ToJson()), "edgeAgent", sasUrl, LogsContentEncoding.None, LogsContentType.Json, filter });

            sasUrl = $"https://test1.blob.core.windows.net/cont2?st={Guid.NewGuid()}";
            filter = new ModuleLogFilter(Option.None <int>(), Option.Some(1501000), Option.None <int>(), Option.Some("ERR"));
            yield return(new object[] { @"{""schemaVersion"":""1.0"",""sasUrl"":""<sasurl>"",""items"":{""id"":""edgeAgent"",""filter"":<filter>}}".Replace("<sasurl>", sasUrl).Replace("<filter>", filter.ToJson()), "edgeAgent", sasUrl, LogsContentEncoding.None, LogsContentType.Json, filter });

            sasUrl = $"https://test1.blob.core.windows.net/cont2?st={Guid.NewGuid()}";
            filter = new ModuleLogFilter(Option.Some(100), Option.None <int>(), Option.Some(3), Option.None <string>());
            yield return(new object[] { @"{""schemaVersion"":""1.0"",""sasUrl"":""<sasurl>"",""items"":{""id"":""edgeAgent"",""filter"":<filter>}}".Replace("<sasurl>", sasUrl).Replace("<filter>", filter.ToJson()), "edgeAgent", sasUrl, LogsContentEncoding.None, LogsContentType.Json, filter });
        }
        public async Task TestLogsUploadAllTaskRequest()
        {
            // Arrange
            string sasUrl = $"https://test1.blob.core.windows.net/cont2?st={Guid.NewGuid()}";
            var    filter = new ModuleLogFilter(Option.Some(100), Option.Some(1501000), Option.Some(3), Option.Some("ERR"));
            LogsContentEncoding contentEncoding = LogsContentEncoding.None;
            LogsContentType     contentType     = LogsContentType.Json;

            string payload =
                @"{
                    ""schemaVersion"": ""1.0"",
                    ""sasUrl"": ""<sasurl>"",
                    ""items"": {
                        ""id"": "".*"",
                        ""filter"": <filter>
                    },
                    ""encoding"": ""none"",
                    ""contentType"": ""json""
                }"
                .Replace("<sasurl>", sasUrl)
                .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 logsUploader = new Mock <ILogsUploader>();
            var logsProvider = new Mock <ILogsProvider>();

            var module1LogOptions = new ModuleLogOptions(contentEncoding, contentType, filter, LogOutputFraming.None, Option.None <LogsOutputGroupingConfig>(), false);
            var mod1LogBytes      = new byte[100];

            logsProvider.Setup(l => l.GetLogs(mod1, module1LogOptions, It.IsAny <CancellationToken>()))
            .ReturnsAsync(mod1LogBytes);
            logsUploader.Setup(l => l.Upload(sasUrl, mod1, mod1LogBytes, contentEncoding, contentType))
            .Returns(Task.CompletedTask);

            var module2LogOptions = new ModuleLogOptions(contentEncoding, contentType, filter, LogOutputFraming.None, Option.None <LogsOutputGroupingConfig>(), false);
            var mod2LogBytes      = new byte[80];

            logsProvider.Setup(l => l.GetLogs(mod2, module2LogOptions, It.IsAny <CancellationToken>()))
            .ReturnsAsync(mod2LogBytes);
            logsUploader.Setup(l => l.Upload(sasUrl, mod2, mod2LogBytes, contentEncoding, contentType))
            .Returns(Task.CompletedTask);

            var module3LogOptions = new ModuleLogOptions(contentEncoding, contentType, filter, LogOutputFraming.None, Option.None <LogsOutputGroupingConfig>(), false);
            var mod3LogBytes      = new byte[120];

            logsProvider.Setup(l => l.GetLogs(mod3, module3LogOptions, It.IsAny <CancellationToken>()))
            .ReturnsAsync(mod3LogBytes);
            logsUploader.Setup(l => l.Upload(sasUrl, mod3, mod3LogBytes, contentEncoding, contentType))
            .Returns(Task.CompletedTask);

            // Act
            var             logsUploadRequestHandler = new LogsUploadRequestHandler(logsUploader.Object, logsProvider.Object, runtimeInfoProvider.Object);
            Option <string> response = await logsUploadRequestHandler.HandleRequest(Option.Maybe(payload), CancellationToken.None);

            // Assert
            Assert.False(response.HasValue);
            logsProvider.VerifyAll();
            logsUploader.VerifyAll();
            runtimeInfoProvider.VerifyAll();
        }
Beispiel #5
0
        public async Task TestLogsUploadAllTaskRequest()
        {
            // Arrange
            string sasUrl = $"https://test1.blob.core.windows.net/cont2?st={Guid.NewGuid()}";
            // Note: When LogsContentType = Json, edgeAgent automatically set the "includeTimestamp" to be true.
            var filter = new ModuleLogFilter(Option.Some(100), Option.Some("1501000"), Option.None <string>(), Option.Some(3), Option.Some(true), Option.Some("ERR"));
            LogsContentEncoding contentEncoding = LogsContentEncoding.None;
            LogsContentType     contentType     = LogsContentType.Json;

            string payload =
                @"{
                    ""schemaVersion"": ""1.0"",
                    ""sasUrl"": ""<sasurl>"",
                    ""items"": {
                        ""id"": "".*"",
                        ""filter"": <filter>
                    },
                    ""encoding"": ""none"",
                    ""contentType"": ""json""
                }"
                .Replace("<sasurl>", sasUrl)
                .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 logsUploader = new Mock <IRequestsUploader>();
            var logsProvider = new Mock <ILogsProvider>();

            var module1LogOptions = new ModuleLogOptions(contentEncoding, contentType, filter, LogOutputFraming.None, Option.None <LogsOutputGroupingConfig>(), false);
            var mod1LogBytes      = new byte[100];

            logsProvider.Setup(l => l.GetLogs(mod1, It.IsAny <ModuleLogOptions>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(mod1LogBytes);
            logsUploader.Setup(l => l.UploadLogs(sasUrl, mod1, mod1LogBytes, contentEncoding, contentType))
            .Returns(Task.CompletedTask);

            var module2LogOptions = new ModuleLogOptions(contentEncoding, contentType, filter, LogOutputFraming.None, Option.None <LogsOutputGroupingConfig>(), false);
            var mod2LogBytes      = new byte[80];

            logsProvider.Setup(l => l.GetLogs(mod2, It.IsAny <ModuleLogOptions>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(mod2LogBytes);
            logsUploader.Setup(l => l.UploadLogs(sasUrl, mod2, mod2LogBytes, contentEncoding, contentType))
            .Returns(Task.CompletedTask);

            var module3LogOptions = new ModuleLogOptions(contentEncoding, contentType, filter, LogOutputFraming.None, Option.None <LogsOutputGroupingConfig>(), false);
            var mod3LogBytes      = new byte[120];

            logsProvider.Setup(l => l.GetLogs(mod3, It.IsAny <ModuleLogOptions>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(mod3LogBytes);
            logsUploader.Setup(l => l.UploadLogs(sasUrl, mod3, mod3LogBytes, contentEncoding, contentType))
            .Returns(Task.CompletedTask);

            // Act
            var             logsUploadRequestHandler = new ModuleLogsUploadRequestHandler(logsUploader.Object, logsProvider.Object, runtimeInfoProvider.Object);
            Option <string> response = await logsUploadRequestHandler.HandleRequest(Option.Maybe(payload), CancellationToken.None);

            // Assert
            Assert.True(response.HasValue);
            var taskStatusResponse = response.OrDefault().FromJson <TaskStatusResponse>();

            Assert.NotNull(taskStatusResponse);
            Assert.NotEmpty(taskStatusResponse.CorrelationId);
            Assert.Equal(string.Empty, taskStatusResponse.Message);

            await WaitForBackgroundTaskCompletion(taskStatusResponse.CorrelationId).TimeoutAfter(TimeSpan.FromSeconds(5));

            logsProvider.VerifyAll();
            logsUploader.VerifyAll();
            runtimeInfoProvider.VerifyAll();
        }