Exemplo n.º 1
0
 public TestBase(TestReporter testReporter, IMetricsScraper scraper, ModuleClient moduleClient)
 {
     log.LogInformation($"Making test {this.TestName}");
     this.testReporter = testReporter.MakeSubcategory(this.TestName);
     this.scraper      = scraper;
     this.moduleClient = moduleClient;
 }
Exemplo n.º 2
0
        static async Task <int> MainAsync()
        {
            try
            {
                Logger.LogInformation("Validate Metrics Main() started.");

                (CancellationTokenSource cts, ManualResetEventSlim completed, Option <object> handler) = ShutdownHandler.Init(TimeSpan.FromSeconds(5), Logger);

                IConfiguration configuration = new ConfigurationBuilder()
                                               .SetBasePath(Directory.GetCurrentDirectory())
                                               .AddJsonFile("config/appsettings.json", optional: true)
                                               .AddEnvironmentVariables()
                                               .Build();

                using (ModuleClient moduleClient = await ModuleClient.CreateFromEnvironmentAsync())
                    using (MetricsScraper scraper = new MetricsScraper(new List <string> {
                        "http://edgeHub:9600/metrics", "http://edgeAgent:9600/metrics"
                    }))
                    {
                        await moduleClient.OpenAsync();

                        await moduleClient.SetMethodHandlerAsync(
                            "ValidateMetrics",
                            async (MethodRequest methodRequest, object _) =>
                        {
                            Logger.LogInformation("Validating metrics");

                            TestReporter testReporter = new TestReporter("Metrics Validation");
                            List <TestBase> tests     = new List <TestBase>
                            {
                                new ValidateNumberOfMessagesSent(testReporter, scraper, moduleClient),
                                new ValidateDocumentedMetrics(testReporter, scraper, moduleClient),
                                new ValidateHostRanges(testReporter, scraper, moduleClient),
                            };

                            using (testReporter.MeasureDuration())
                            {
                                await Task.WhenAll(tests.Select(test => test.Start(cts.Token)));
                            }

                            return(new MethodResponse(Encoding.UTF8.GetBytes(testReporter.ReportResults()), (int)HttpStatusCode.OK));
                        },
                            null);

                        Logger.LogInformation("Ready to validate metrics");
                        await cts.Token.WhenCanceled();
                    }

                completed.Set();
                handler.ForEach(h => GC.KeepAlive(h));
                Logger.LogInformation("Validate Metrics Main() finished.");
            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString());
            }

            return(0);
        }
Exemplo n.º 3
0
        public TestReporter MakeSubcategory(string name)
        {
            if (this.subcategories == null)
            {
                this.subcategories = new List <TestReporter>();
            }

            TestReporter newCategory = new TestReporter(name);

            this.subcategories.Add(newCategory);

            return(newCategory);
        }
Exemplo n.º 4
0
        static async Task <int> MainAsync()
        {
            try
            {
                Logger.LogInformation("Validate Metrics Main() started.");

                (CancellationTokenSource cts, ManualResetEventSlim completed, Option <object> handler) = ShutdownHandler.Init(TimeSpan.FromSeconds(5), Logger);

                IConfiguration configuration = new ConfigurationBuilder()
                                               .SetBasePath(Directory.GetCurrentDirectory())
                                               .AddJsonFile("config/appsettings.json", optional: true)
                                               .AddEnvironmentVariables()
                                               .Build();

                var transportType = configuration.GetValue("ClientTransportType", Microsoft.Azure.Devices.Client.TransportType.Mqtt);

                Logger.LogInformation("Make Client");
                using (ModuleClient moduleClient = await ModuleUtil.CreateModuleClientAsync(
                           transportType,
                           new ClientOptions(),
                           ModuleUtil.DefaultTimeoutErrorDetectionStrategy,
                           ModuleUtil.DefaultTransientRetryStrategy,
                           Logger))
                    using (MetricsScraper scraper = new MetricsScraper(new List <string> {
                        "http://edgeHub:9600/metrics", "http://edgeAgent:9600/metrics"
                    }))
                    {
                        Logger.LogInformation("Open Async");
                        await moduleClient.OpenAsync();

                        Logger.LogInformation("Set method handler");
                        await moduleClient.SetMethodHandlerAsync(
                            "ValidateMetrics",
                            async (MethodRequest methodRequest, object _) =>
                        {
                            Logger.LogInformation("Validating metrics");

                            TestReporter testReporter = new TestReporter("Metrics Validation");
                            List <TestBase> tests     = new List <TestBase>
                            {
                                new ValidateMessages(testReporter, scraper, moduleClient, transportType),
                                new ValidateDocumentedMetrics(testReporter, scraper, moduleClient),
                                // new ValidateHostRanges(testReporter, scraper, moduleClient),
                            };

                            using (testReporter.MeasureDuration())
                            {
                                await Task.WhenAll(tests.Select(test => test.Start(cts.Token)));
                            }

                            var result = new MethodResponse(Encoding.UTF8.GetBytes(testReporter.ReportResults()), (int)HttpStatusCode.OK);

                            Logger.LogInformation($"Finished validating metrics. Result size: {result.Result.Length}");
                            return(result);
                        },
                            null);

                        moduleClient.SetConnectionStatusChangesHandler((status, reason)
                                                                       => Logger.LogWarning($"Module to Edge Hub connection changed Status: {status} Reason: {reason}"));

                        Logger.LogInformation("Ready to validate metrics");
                        await cts.Token.WhenCanceled();
                    }

                completed.Set();
                handler.ForEach(h => GC.KeepAlive(h));
                Logger.LogInformation("Validate Metrics Main() finished.");
            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString());
            }

            return(0);
        }
 public ValidateNumberOfMessagesSent(TestReporter testReporter, IMetricsScraper scraper, ModuleClient moduleClient)
     : base(testReporter, scraper, moduleClient)
 {
 }
Exemplo n.º 6
0
 public ValidateDocumentedMetrics(TestReporter testReporter, IMetricsScraper scraper, ModuleClient moduleClient)
     : base(testReporter, scraper, moduleClient)
 {
 }