Exemplo n.º 1
0
        public async Task GetLogsStreamWithFiltersTest()
        {
            // Arrange
            string            iotHub            = "foo.azure-devices.net";
            string            deviceId          = "dev1";
            string            moduleId          = "mod1";
            Option <int>      tail              = Option.Some(10);
            Option <int>      since             = Option.Some(1552887267);
            CancellationToken cancellationToken = CancellationToken.None;

            byte[] dockerLogsStreamBytes = DockerFraming.Frame(TestLogTexts);
            var    runtimeInfoProvider   = new Mock <IRuntimeInfoProvider>();

            runtimeInfoProvider.Setup(r => r.GetModuleLogs(moduleId, true, tail, since, cancellationToken))
            .ReturnsAsync(new MemoryStream(dockerLogsStreamBytes));
            runtimeInfoProvider.Setup(r => r.GetModules(It.IsAny <CancellationToken>()))
            .ReturnsAsync(new[] { new ModuleRuntimeInfo(moduleId, "docker", ModuleStatus.Running, "foo", 0, Option.None <DateTime>(), Option.None <DateTime>()) });

            var logsProcessor = new LogsProcessor(new LogMessageParser(iotHub, deviceId));
            var logsProvider  = new LogsProvider(runtimeInfoProvider.Object, logsProcessor);

            var filter     = new ModuleLogFilter(tail, since, Option.Some(6), Option.Some("Starting"));
            var logOptions = new ModuleLogOptions(LogsContentEncoding.Gzip, LogsContentType.Text, filter);

            var receivedBytes = new List <byte>();

            Task Callback(ArraySegment <byte> bytes)
            {
                receivedBytes.AddRange(bytes.ToArray());
                return(Task.CompletedTask);
            }

            // Act
            await logsProvider.GetLogsStream(moduleId, logOptions, Callback, cancellationToken);

            await Task.Delay(TimeSpan.FromSeconds(3));

            // Assert
            Assert.NotEmpty(receivedBytes);
            string receivedText = Compression.DecompressFromGzip(receivedBytes.ToArray())
                                  .Skip(8)
                                  .ToArray()
                                  .FromBytes();

            Assert.Equal(TestLogTexts[0], receivedText);
        }
Exemplo n.º 2
0
        public async Task GetLogsStreamTest()
        {
            // Arrange
            string            iotHub            = "foo.azure-devices.net";
            string            deviceId          = "dev3";
            string            moduleId          = "mod3";
            Option <int>      tail              = Option.None <int>();
            Option <string>   since             = Option.None <string>();
            Option <string>   until             = Option.None <string>();
            Option <bool>     includeTimestamp  = Option.None <bool>();
            CancellationToken cancellationToken = CancellationToken.None;

            byte[] dockerLogsStreamBytes = DockerFraming.Frame(TestLogTexts);
            var    runtimeInfoProvider   = new Mock <IRuntimeInfoProvider>();

            runtimeInfoProvider.Setup(r => r.GetModuleLogs(moduleId, true, tail, since, until, includeTimestamp, cancellationToken))
            .ReturnsAsync(new MemoryStream(dockerLogsStreamBytes));
            runtimeInfoProvider.Setup(r => r.GetModules(It.IsAny <CancellationToken>()))
            .ReturnsAsync(new[] { new ModuleRuntimeInfo(moduleId, "docker", ModuleStatus.Running, "foo", 0, Option.None <DateTime>(), Option.None <DateTime>()) });

            var logsProcessor = new LogsProcessor(new LogMessageParser(iotHub, deviceId));
            var logsProvider  = new LogsProvider(runtimeInfoProvider.Object, logsProcessor);

            var logOptions = new ModuleLogOptions(LogsContentEncoding.None, LogsContentType.Text, ModuleLogFilter.Empty, LogOutputFraming.None, Option.None <LogsOutputGroupingConfig>(), true);

            var receivedBytes = new List <byte>();

            Task Callback(ArraySegment <byte> bytes)
            {
                receivedBytes.AddRange(bytes.ToArray());
                return(Task.CompletedTask);
            }

            // Act
            await logsProvider.GetLogsStream(moduleId, logOptions, Callback, cancellationToken);

            // Assert
            Assert.NotEmpty(receivedBytes);
            Assert.Equal(string.Join(string.Empty, TestLogTexts).ToBytes(), receivedBytes);
        }
Exemplo n.º 3
0
        public async Task GetLogsStreamWithMultipleModulesWithRegexMatchTest()
        {
            // Arrange
            string            iotHub            = "foo.azure-devices.net";
            string            deviceId          = "dev1";
            Option <int>      tail              = Option.None <int>();
            Option <int>      since             = Option.None <int>();
            CancellationToken cancellationToken = CancellationToken.None;

            string moduleId1 = "mod1";
            string moduleId2 = "mod2";

            var filter = new ModuleLogFilter(tail, since, Option.None <int>(), Option.Some("bad"));

            byte[] dockerLogsStreamBytes1 = DockerFraming.Frame(TestLogTexts);
            byte[] dockerLogsStreamBytes2 = DockerFraming.Frame(TestLogTexts);

            var modulesList = new List <ModuleRuntimeInfo>
            {
                new ModuleRuntimeInfo(moduleId1, "docker", ModuleStatus.Running, "foo", 0, Option.None <DateTime>(), Option.None <DateTime>()),
                new ModuleRuntimeInfo(moduleId2, "docker", ModuleStatus.Running, "foo", 0, Option.None <DateTime>(), Option.None <DateTime>())
            };

            var runtimeInfoProvider = new Mock <IRuntimeInfoProvider>();

            runtimeInfoProvider.Setup(r => r.GetModuleLogs(moduleId1, true, tail, since, cancellationToken))
            .ReturnsAsync(new MemoryStream(dockerLogsStreamBytes1));
            runtimeInfoProvider.Setup(r => r.GetModuleLogs(moduleId2, true, tail, since, cancellationToken))
            .ReturnsAsync(new MemoryStream(dockerLogsStreamBytes2));
            runtimeInfoProvider.Setup(r => r.GetModules(It.IsAny <CancellationToken>()))
            .ReturnsAsync(modulesList);

            var logsProcessor = new LogsProcessor(new LogMessageParser(iotHub, deviceId));
            var logsProvider  = new LogsProvider(runtimeInfoProvider.Object, logsProcessor);

            string regex      = "mod";
            var    logOptions = new ModuleLogOptions(LogsContentEncoding.Gzip, LogsContentType.Text, filter);

            var receivedBytes = new List <byte[]>();

            Task Callback(ArraySegment <byte> bytes)
            {
                receivedBytes.Add(bytes.ToArray());
                return(Task.CompletedTask);
            }

            var expectedTextLines = new List <string> {
                TestLogTexts[3], TestLogTexts[4], TestLogTexts[3], TestLogTexts[4]
            };

            expectedTextLines.Sort();

            // Act
            await logsProvider.GetLogsStream(regex, logOptions, Callback, cancellationToken);

            await Task.Delay(TimeSpan.FromSeconds(6));

            // Assert
            Assert.NotEmpty(receivedBytes);
            List <string> receivedText = receivedBytes
                                         .Select(
                r =>
                Compression.DecompressFromGzip(r)
                .Skip(8)
                .ToArray()
                .FromBytes())
                                         .ToList();

            receivedText.Sort();

            Assert.Equal(expectedTextLines, receivedText);
        }