Exemplo n.º 1
0
        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 = @"{""id"": ""all"",  ""sasUrl"": ""<sasurl>"", ""filter"": <filter>}".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);
            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);
            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);
            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();
        }
        public async Task TestLogsUploadRequestWithInvalidSchemaVersion(string payload, Type exception)
        {
            // Arrange
            var logsUploader = new Mock <ILogsUploader>();
            var logsProvider = new Mock <ILogsProvider>();

            var runtimeInfoProvider = Mock.Of <IRuntimeInfoProvider>();

            // Act
            var logsUploadRequestHandler = new LogsUploadRequestHandler(logsUploader.Object, logsProvider.Object, runtimeInfoProvider);
            await Assert.ThrowsAsync(exception, () => logsUploadRequestHandler.HandleRequest(Option.Maybe(payload), CancellationToken.None));
        }
        public async Task TestLogsUploadRequest(string payload, string id, string sasUrl, LogsContentEncoding contentEncoding, LogsContentType contentType, ModuleLogFilter filter)
        {
            // Arrange
            var logsUploader     = new Mock <ILogsUploader>();
            var logsProvider     = new Mock <ILogsProvider>();
            var uploadBytes      = new byte[100];
            var moduleLogOptions = new ModuleLogOptions(contentEncoding, contentType, filter, LogOutputFraming.None, Option.None <LogsOutputGroupingConfig>(), false);

            if (contentType == LogsContentType.Text)
            {
                Func <ArraySegment <byte>, Task> getLogsCallback = bytes => Task.CompletedTask;
                logsUploader.Setup(l => l.GetUploaderCallback(sasUrl, id, contentEncoding, contentType))
                .ReturnsAsync(getLogsCallback);
                logsProvider.Setup(l => l.GetLogsStream(id, moduleLogOptions, getLogsCallback, It.IsAny <CancellationToken>()))
                .Returns(Task.CompletedTask);
            }
            else
            {
                logsProvider.Setup(l => l.GetLogs(id, moduleLogOptions, It.IsAny <CancellationToken>()))
                .ReturnsAsync(uploadBytes);
                logsUploader.Setup(l => l.Upload(sasUrl, id, uploadBytes, contentEncoding, contentType))
                .Returns(Task.CompletedTask);
            }

            IEnumerable <ModuleRuntimeInfo> moduleRuntimeInfoList = new List <ModuleRuntimeInfo>
            {
                new ModuleRuntimeInfo(id, "docker", ModuleStatus.Running, string.Empty, 0, Option.None <DateTime>(), Option.None <DateTime>())
            };
            var runtimeInfoProvider = Mock.Of <IRuntimeInfoProvider>(r => r.GetModules(It.IsAny <CancellationToken>()) == Task.FromResult(moduleRuntimeInfoList));

            // Act
            var             logsUploadRequestHandler = new LogsUploadRequestHandler(logsUploader.Object, logsProvider.Object, runtimeInfoProvider);
            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();
            Mock.Get(runtimeInfoProvider).VerifyAll();
        }
Exemplo n.º 4
0
        public async Task TestLogsUploadRequest(string payload, string id, string sasUrl, LogsContentEncoding contentEncoding, LogsContentType contentType, ModuleLogFilter filter)
        {
            // Arrange
            var logsUploader     = new Mock <ILogsUploader>();
            var logsProvider     = new Mock <ILogsProvider>();
            var uploadBytes      = new byte[100];
            var moduleLogOptions = new ModuleLogOptions(contentEncoding, contentType, filter);

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

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

            // Assert
            Assert.False(response.HasValue);
            logsProvider.VerifyAll();
            logsUploader.VerifyAll();
        }
        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.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();
        }