Esempio n. 1
0
        public async void TestMdmGetDimensionValuesAsync()
        {
            var metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = @"
                public async static Task<IEnumerable<string>> Run(DataProviders dataProviders) {
                    var filter = new List<Tuple<string, IEnumerable<string>>>
                    {
                        new Tuple<string, IEnumerable<string>>(""StampName"", new List<string>())
                    };

                    return await dataProviders.Mdm.GetDimensionValuesAsync(""Microsoft/Web/WebApps"", ""CpuTime"", filter, ""ServerName"", DateTime.UtcNow.AddMinutes(-30), DateTime.UtcNow);
                }";

            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config, Guid.NewGuid().ToString()));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                var result = await invoker.Invoke(new object[] { dataProviders }) as IEnumerable <string>;

                Assert.NotNull(result);
                Assert.True(result.Count() == 3);
            }
        }
Esempio n. 2
0
        public async void E2E_Test_RuntimeSlotMapData()
        {
            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            //read a sample csx file from local directory
            metadata.ScriptText = await File.ReadAllTextAsync("GetRuntimeSiteSlotMapData.csx");

            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                var appResource = new App(string.Empty, string.Empty, "my-api")
                {
                    Stamp = new HostingEnvironment(string.Empty, string.Empty, "waws-prod-bn1-71717c45")
                    {
                        Name = "waws-prod-bn1-71717c45"
                    }
                };

                var operationContext = new OperationContext <App>(appResource, string.Empty, string.Empty, true, string.Empty);
                var response         = new Response();

                Response result = (Response)await invoker.Invoke(new object[] { dataProviders, operationContext, response });

                Assert.Equal("my-api__a88nf", result.Dataset.First().Table.Rows[1][1]);
            }
        }
        public async void TestDetectorWithMDMConfigurationGists()
        {
            var references = new Dictionary <string, string>
            {
                { "mdm", GetMDMConfigurationGist() },
            };

            var metadata = new EntityMetadata(GetMDMDetector(), EntityType.Detector);

            var dataSourceConfiguration = new MockDataProviderConfigurationFactory();

            var config = dataSourceConfiguration.LoadConfigurations();

            ArmResource resource = new ArmResource("751A8C1D-EA9D-4FE7-8574-3096A81C2C08", "testResourceGroup", "Microsoft.AppPlatform", "Spring", "testResource", "FakeLocation");
            OperationContext <ArmResource> context = new OperationContext <ArmResource>(resource, "2019-12-09T00:10", "2019-12-09T23:54", true, "A9854948-807B-4371-B834-3EC78BB6635C");
            Response response = new Response();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config, incomingHeaders: new HeaderDictionary()
            {
                [HeaderConstants.LocationHeader] = resource.Location
            }));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports(), references.ToImmutableDictionary()))
            {
                await invoker.InitializeEntryPointAsync().ConfigureAwait(false);

                await invoker.Invoke(new object[] { dataProviders, context, response }).ConfigureAwait(false);

                Assert.Equal("Diagnostics.DataProviders.MdmLogDecorator", response.Insights[0].Message);
            }
        }
        public async void EntityInvoker_TestInvokeMethod()
        {
            using (EntityInvoker invoker = new EntityInvoker(ScriptTestDataHelper.GetRandomMetadata(), ImmutableArray.Create <string>()))
            {
                await invoker.InitializeEntryPointAsync();

                int result = (int)await invoker.Invoke(new object[] { 3 });

                Assert.Equal(9, result);
            }
        }
        public async void EntityInvoker_TestInvokeWithCompilationError(ScriptErrorType errorType)
        {
            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = ScriptTestDataHelper.GetInvalidCsxScript(errorType);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ImmutableArray.Create <string>()))
            {
                ScriptCompilationException ex = await Assert.ThrowsAsync <ScriptCompilationException>(async() =>
                {
                    await invoker.InitializeEntryPointAsync();
                    int result = (int)await invoker.Invoke(new object[] { 3 });
                    Assert.Equal(9, result);
                });

                Assert.NotEmpty(ex.CompilationOutput);
            }
        }
Esempio n. 6
0
        public async Task E2E_Test_WAWSObserverAsync()
        {
            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            //read a sample csx file from local directory
            metadata.ScriptText = await File.ReadAllTextAsync("BackupCheckDetector.csx");

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                var appResource = new App(string.Empty, string.Empty, "my-api")
                {
                    Stamp = new HostingEnvironment(string.Empty, string.Empty, "waws-prod-bn1-71717c45")
                };

                appResource.Stamp.TenantIdList = new List <string>()
                {
                    Guid.NewGuid().ToString()
                };

                var operationContext = new OperationContext <App>(appResource, null, null, true, null);

                var response = new Response();

                try
                {
                    Response result = (Response)await invoker.Invoke(new object[] { dataProviders, operationContext, response });
                }
                catch (ScriptCompilationException ex)
                {
                    foreach (var output in ex.CompilationOutput)
                    {
                        Trace.WriteLine(output);
                    }
                }
            }
        }
Esempio n. 7
0
        public async void DataProvders_TestKusto()
        {
            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = GetDataProviderScript("TestA");

            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config, Guid.NewGuid().ToString()));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                DataTable result = (DataTable)await invoker.Invoke(new object[] { dataProviders });

                Assert.NotNull(result);
            }
        }
        public async void EntityInvoker_TestDetectorWithGists()
        {
            var gist       = ScriptTestDataHelper.GetGist();
            var references = new Dictionary <string, string>
            {
                { "xxx", gist },
                { "yyy", "" },
                { "zzz", "" }
            };

            var metadata = new EntityMetadata(ScriptTestDataHelper.GetSentinel(), EntityType.Detector);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports(), references.ToImmutableDictionary()))
            {
                await invoker.InitializeEntryPointAsync();

                var result = (string)await invoker.Invoke(new object[] { });

                Assert.Equal(2, invoker.References.Count());
                Assert.False(string.IsNullOrWhiteSpace(result));
            }
        }
Esempio n. 9
0
        public async void DataProvders_TestKusto()
        {
            var metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = @"
                public async static Task<DataTable> Run(DataProviders dataProviders) {
                    return await dataProviders.Kusto.ExecuteQuery(""TestA"", ""waws-prod-mockstamp"");
                }";

            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config, Guid.NewGuid().ToString()));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                var result = (DataTable)await invoker.Invoke(new object[] { dataProviders });

                Assert.NotNull(result);
            }
        }
Esempio n. 10
0
        public async void TestMdmGetMetricNamesAsync()
        {
            var metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = @"
                public async static Task<IEnumerable<string>> Run(DataProviders dataProviders) {
                    return await dataProviders.Mdm.GetMetricNamesAsync(""Microsoft/Web/WebApps"");
                }";

            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config, Guid.NewGuid().ToString()));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                var result = await invoker.Invoke(new object[] { dataProviders }) as IEnumerable <string>;

                Assert.NotNull(result);
                Assert.True(result.Count() == 3);
            }
        }
Esempio n. 11
0
        public async void TestMdmGetTimeSeriesValuesAsync()
        {
            var metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = @"
                public async static Task<IEnumerable<DataTable>> Run(DataProviders dataProviders) {
                    var dimensions = new Dictionary<string, string> { { ""StampName"", ""kudu1"" } };
                    return await dataProviders.Mdm.GetTimeSeriesAsync(DateTime.UtcNow.AddMinutes(-10), DateTime.UtcNow, Sampling.Average | Sampling.Max | Sampling.Count, ""Microsoft/Web/WebApps"", ""CpuTime"", dimensions);
                }";

            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config, Guid.NewGuid().ToString()));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                var result = await invoker.Invoke(new object[] { dataProviders }) as IEnumerable <DataTable>;

                Assert.NotNull(result);
            }
        }
Esempio n. 12
0
        protected async Task <IActionResult> ExecuteQuery <TPostBodyResource>(TResource resource, CompilationBostBody <TPostBodyResource> jsonBody, string startTime, string endTime, string timeGrain)
        {
            if (jsonBody == null)
            {
                return(BadRequest("Missing body"));
            }

            if (string.IsNullOrWhiteSpace(jsonBody.Script))
            {
                return(BadRequest("Missing script in body"));
            }

            if (!DateTimeHelper.PrepareStartEndTimeWithTimeGrain(startTime, endTime, timeGrain, out DateTime startTimeUtc, out DateTime endTimeUtc, out TimeSpan timeGrainTimeSpan, out string errorMessage))
            {
                return(BadRequest(errorMessage));
            }

            await this._sourceWatcherService.Watcher.WaitForFirstCompletion();

            EntityMetadata metaData      = new EntityMetadata(jsonBody.Script);
            var            dataProviders = new DataProviders.DataProviders(_dataSourcesConfigService.Config);

            QueryResponse <DiagnosticApiResponse> queryRes = new QueryResponse <DiagnosticApiResponse>
            {
                InvocationOutput = new DiagnosticApiResponse()
            };

            Assembly tempAsm = null;

            this.Request.Headers.TryGetValue(HeaderConstants.RequestIdHeaderName, out StringValues requestIds);
            var compilerResponse = await _compilerHostClient.GetCompilationResponse(jsonBody.Script, requestIds.FirstOrDefault() ?? string.Empty);

            queryRes.CompilationOutput = compilerResponse;

            if (queryRes.CompilationOutput.CompilationSucceeded)
            {
                byte[] asmData = Convert.FromBase64String(compilerResponse.AssemblyBytes);
                byte[] pdbData = Convert.FromBase64String(compilerResponse.PdbBytes);

                tempAsm = Assembly.Load(asmData, pdbData);

                using (var invoker = new EntityInvoker(metaData, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
                {
                    invoker.InitializeEntryPoint(tempAsm);

                    // Verify Detector with other detectors in the system in case of conflicts
                    if (!VerifyEntity(invoker, ref queryRes))
                    {
                        return(Ok(queryRes));
                    }
                    OperationContext <TResource> cxt = PrepareContext(resource, startTimeUtc, endTimeUtc);
                    List <DataProviderMetadata>  dataProvidersMetadata = null;

                    try
                    {
                        var responseInput = new Response()
                        {
                            Metadata = RemovePIIFromDefinition(invoker.EntryPointDefinitionAttribute, cxt.IsInternalCall)
                        };
                        var invocationResponse = (Response)await invoker.Invoke(new object[] { dataProviders, cxt, responseInput });

                        invocationResponse.UpdateDetectorStatusFromInsights();

                        if (cxt.IsInternalCall)
                        {
                            dataProvidersMetadata = GetDataProvidersMetadata(dataProviders);
                        }

                        queryRes.RuntimeSucceeded = true;
                        queryRes.InvocationOutput = DiagnosticApiResponse.FromCsxResponse(invocationResponse, dataProvidersMetadata);
                    }
                    catch (Exception ex)
                    {
                        if (cxt.IsInternalCall)
                        {
                            queryRes.RuntimeSucceeded = false;
                            queryRes.InvocationOutput = CreateQueryExceptionResponse(ex, invoker.EntryPointDefinitionAttribute, cxt.IsInternalCall, GetDataProvidersMetadata(dataProviders));
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }

            return(Ok(queryRes));
        }