// Searches IoT Hub for messages
        async Task <SearchLogResult> SearchIoTHubLogs(Func <string, bool> predicate, SearchLogOptions options = null)
        {
            var maxAttempts     = options?.MaxAttempts ?? this.Configuration.EnsureHasEventMaximumTries;
            var processedEvents = new HashSet <string>();

            for (int i = 0; i < maxAttempts; i++)
            {
                if (i > 0)
                {
                    var timeToWait = i * this.Configuration.EnsureHasEventDelayBetweenReadsInSeconds;
                    if (!string.IsNullOrEmpty(options?.Description))
                    {
                        TestLogger.Log($"IoT Hub message '{options.Description}' not found, attempt {i}/{maxAttempts}, waiting {timeToWait} secs");
                    }
                    else
                    {
                        TestLogger.Log($"IoT Hub message not found, attempt {i}/{maxAttempts}, waiting {timeToWait} secs");
                    }

                    await Task.Delay(TimeSpan.FromSeconds(timeToWait));
                }

                foreach (var item in this.IoTHubMessages.GetEvents())
                {
                    var bodyText = item.Body.Count > 0 ? Encoding.UTF8.GetString(item.Body) : string.Empty;
                    processedEvents.Add(bodyText);
                    if (predicate(bodyText))
                    {
                        return(new SearchLogResult(true, processedEvents, bodyText));
                    }
                }
            }

            return(new SearchLogResult(false, processedEvents, string.Empty));
        }
        async Task <SearchLogResult> SearchUdpLogs(Func <string, bool> predicate, SearchLogOptions options = null)
        {
            var maxAttempts     = options?.MaxAttempts ?? this.Configuration.EnsureHasEventMaximumTries;
            var processedEvents = new HashSet <string>();

            for (int i = 0; i < maxAttempts; i++)
            {
                if (i > 0)
                {
                    var timeToWait = i * this.Configuration.EnsureHasEventDelayBetweenReadsInSeconds;
                    if (!string.IsNullOrEmpty(options?.Description))
                    {
                        TestLogger.Log($"UDP log message '{options.Description}' not found, attempt {i}/{maxAttempts}, waiting {timeToWait} secs");
                    }
                    else
                    {
                        TestLogger.Log($"UDP log message not found, attempt {i}/{maxAttempts}, waiting {timeToWait} secs");
                    }

                    await Task.Delay(TimeSpan.FromSeconds(timeToWait));
                }

                foreach (var item in this.udpLogListener.GetEvents())
                {
                    processedEvents.Add(item);
                    if (predicate(item))
                    {
                        return(new SearchLogResult(true, processedEvents));
                    }
                }
            }

            return(new SearchLogResult(false, processedEvents));
        }
Example #3
0
 // Asserts leaf device message payload exists. It searches inside the payload "data" property. Has built-in retries
 public async Task AssertIoTHubDeviceMessageExistsAsync(string deviceID, string expectedDataValue, SearchLogOptions options = null)
 {
     await this.AssertIoTHubDeviceMessageExistsAsync(deviceID, "data", expectedDataValue, options);
 }
Example #4
0
        public async Task AssertIoTHubDeviceMessageExistsAsync(string deviceID, string targetJsonProperty, string expectedJsonValue, SearchLogOptions options = null)
        {
            var assertionLevel = this.Configuration.IoTHubAssertLevel;

            if (options != null && options.TreatAsError.HasValue)
            {
                assertionLevel = options.TreatAsError.Value ? LogValidationAssertLevel.Error : LogValidationAssertLevel.Warning;
            }

            if (assertionLevel == LogValidationAssertLevel.Ignore)
            {
                return;
            }

            var searchResult = await this.SearchIoTHubMessageAsync(
                (eventData, eventDeviceID, eventDataMessageBody) => this.IsDeviceMessage(deviceID, targetJsonProperty, expectedJsonValue, eventDeviceID, eventDataMessageBody),
                new SearchLogOptions
            {
                Description  = options?.Description ?? $"\"{targetJsonProperty}\": {expectedJsonValue}",
                TreatAsError = options?.TreatAsError,
            });

            if (assertionLevel == LogValidationAssertLevel.Error)
            {
                var logs = string.Join("\n\t", searchResult.Logs.TakeLast(5));
                Assert.True(searchResult.Found, $"Searching for \"{targetJsonProperty}\": {expectedJsonValue} failed for device {deviceID}. Current log content: [{logs}]");
            }
            else if (assertionLevel == LogValidationAssertLevel.Warning)
            {
                if (!searchResult.Found)
                {
                    var logs = string.Join("\n\t", searchResult.Logs.TakeLast(5));
                    TestLogger.Log($"[WARN] \"{targetJsonProperty}\": {expectedJsonValue} for device {deviceID} found in logs? {searchResult.Found}. Logs: [{logs}]");
                }
            }
        }
Example #5
0
        // Searches IoT Hub for messages
        internal async Task <SearchLogResult> SearchIoTHubMessageAsync(Func <EventData, string, string, bool> predicate, SearchLogOptions options = null)
        {
            var maxAttempts     = options?.MaxAttempts ?? this.Configuration.EnsureHasEventMaximumTries;
            var processedEvents = new HashSet <SearchLogEvent>();

            for (int i = 0; i < maxAttempts; i++)
            {
                if (i > 0)
                {
                    var timeToWait = i * this.Configuration.EnsureHasEventDelayBetweenReadsInSeconds;
                    if (!string.IsNullOrEmpty(options?.Description))
                    {
                        TestLogger.Log($"IoT Hub message '{options.Description}' not found, attempt {i}/{maxAttempts}, waiting {timeToWait} secs");
                    }
                    else
                    {
                        TestLogger.Log($"IoT Hub message not found, attempt {i}/{maxAttempts}, waiting {timeToWait} secs");
                    }

                    await Task.Delay(TimeSpan.FromSeconds(timeToWait));
                }

                foreach (var item in this.IoTHubMessages.GetEvents())
                {
                    try
                    {
                        var bodyText       = item.Body.Count > 0 ? Encoding.UTF8.GetString(item.Body) : string.Empty;
                        var searchLogEvent = new SearchLogEvent
                        {
                            SourceId = item.GetDeviceId(),
                            Message  = bodyText
                        };

                        processedEvents.Add(searchLogEvent);

                        item.SystemProperties.TryGetValue("iothub-connection-device-id", out var deviceId);
                        if (predicate(item, deviceId?.ToString() ?? string.Empty, bodyText))
                        {
                            return(new SearchLogResult(true, processedEvents, bodyText));
                        }
                    }
                    catch (Exception ex)
                    {
                        TestLogger.Log("Error searching in IoT Hub message log: " + ex.ToString());
                    }
                }
            }

            return(new SearchLogResult(false, processedEvents, string.Empty));
        }
Example #6
0
 // Searches IoT Hub for messages
 async Task <SearchLogResult> SearchIoTHubLogs(Func <string, bool> predicate, SearchLogOptions options = null)
 {
     return(await this.SearchIoTHubLogs(evt => predicate(evt.Message), options));
 }
Example #7
0
        // Asserts Network Server Module log exists. It has built-in retries and delays
        public async Task <SearchLogResult> AssertNetworkServerModuleLogExistsAsync(Func <string, bool> predicate, SearchLogOptions options)
        {
            if (this.Configuration.NetworkServerModuleLogAssertLevel == LogValidationAssertLevel.Ignore)
            {
                return(null);
            }

            SearchLogResult searchResult;

            if (this.udpLogListener != null)
            {
                searchResult = await this.SearchUdpLogs(predicate, options);
            }
            else
            {
                searchResult = await this.SearchIoTHubLogs(predicate, options);
            }

            if (!searchResult.Found)
            {
                if (this.Configuration.NetworkServerModuleLogAssertLevel == LogValidationAssertLevel.Error)
                {
                    var logs = string.Join("\n\t", searchResult.Logs.TakeLast(5));
                    Assert.True(searchResult.Found, $"Searching for {options?.Description ?? "??"} failed. Current log content: [{logs}]");
                }
                else if (this.Configuration.NetworkServerModuleLogAssertLevel == LogValidationAssertLevel.Warning)
                {
                    if (!searchResult.Found)
                    {
                        var logs = string.Join("\n\t", searchResult.Logs.TakeLast(5));
                        TestLogger.Log($"[WARN] '{options?.Description ?? "??"}' found in logs? {searchResult.Found}. Logs: [{logs}]");
                    }
                }
            }

            return(searchResult);
        }
Example #8
0
        public async Task <SearchLogResult> SearchNetworkServerModuleAsync(Func <string, bool> predicate, SearchLogOptions options = null)
        {
            SearchLogResult searchResult;

            if (this.udpLogListener != null)
            {
                searchResult = await this.SearchUdpLogs(predicate, options);
            }
            else
            {
                searchResult = await this.SearchIoTHubLogs(predicate, options);
            }

            return(searchResult);
        }