public async Task WhenRunningSmartDetectorAnalyzeItIsDisposedIfItImplementsIDisposable() { this.smartDetector = new DisposableTestSmartDetector { ExpectedResourceType = ResourceType.VirtualMachine }; // Run the Smart Detector ISmartDetectorRunner runner = this.testContainer.Resolve <ISmartDetectorRunner>(); await runner.AnalyzeAsync(this.analysisRequest, true, default(CancellationToken)); Assert.IsTrue(((DisposableTestSmartDetector)this.smartDetector).WasDisposed); }
private void TestInitialize(ResourceType requestResourceType, ResourceType smartDetectorResourceType) { this.testContainer = new UnityContainer(); this.testContainer.RegisterType <ISmartDetectorRunner, SmartDetectorRunner>(); this.testContainer.RegisterInstance(new Mock <IExtendedTracer>().Object); ResourceIdentifier resourceId; switch (requestResourceType) { case ResourceType.Subscription: resourceId = new ResourceIdentifier(requestResourceType, "subscriptionId", string.Empty, string.Empty); break; case ResourceType.ResourceGroup: resourceId = new ResourceIdentifier(requestResourceType, "subscriptionId", "resourceGroup", string.Empty); break; default: resourceId = new ResourceIdentifier(requestResourceType, "subscriptionId", "resourceGroup", "resourceName"); break; } this.resourceIds = new List <string>() { resourceId.ToResourceId() }; this.request = new SmartDetectorExecutionRequest { ResourceIds = this.resourceIds, Cadence = TimeSpan.FromDays(1), SmartDetectorId = "1" }; var smartDetectorManifest = new SmartDetectorManifest("1", "Test Smart Detector", "Test Smart Detector description", Version.Parse("1.0"), "assembly", "class", new List <ResourceType>() { smartDetectorResourceType }, new List <int> { 60 }, null); this.smartDetectorPackage = new SmartDetectorPackage(smartDetectorManifest, new Dictionary <string, byte[]> { ["TestSmartDetectorLibrary"] = Array.Empty <byte>() }); var smartDetectorRepositoryMock = new Mock <ISmartDetectorRepository>(); smartDetectorRepositoryMock .Setup(x => x.ReadSmartDetectorPackageAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .ReturnsAsync(() => this.smartDetectorPackage); this.testContainer.RegisterInstance <ISmartDetectorRepository>(smartDetectorRepositoryMock.Object); this.testContainer.RegisterInstance <IInternalAnalysisServicesFactory>(new Mock <IInternalAnalysisServicesFactory>().Object); this.smartDetector = new TestSmartDetector { ExpectedResourceType = smartDetectorResourceType }; var smartDetectorLoaderMock = new Mock <ISmartDetectorLoader>(); smartDetectorLoaderMock .Setup(x => x.LoadSmartDetector(this.smartDetectorPackage)) .Returns(this.smartDetector); this.testContainer.RegisterInstance <ISmartDetectorLoader>(smartDetectorLoaderMock.Object); var azureResourceManagerClientMock = new Mock <IExtendedAzureResourceManagerClient>(); azureResourceManagerClientMock .Setup(x => x.GetAllResourceGroupsInSubscriptionAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .ReturnsAsync((string subscriptionId, CancellationToken cancellationToken) => new List <ResourceIdentifier>() { new ResourceIdentifier(ResourceType.ResourceGroup, subscriptionId, "resourceGroupName", string.Empty) }); azureResourceManagerClientMock .Setup(x => x.GetAllResourcesInSubscriptionAsync(It.IsAny <string>(), It.IsAny <IEnumerable <ResourceType> >(), It.IsAny <CancellationToken>())) .ReturnsAsync((string subscriptionId, IEnumerable <ResourceType> resourceTypes, CancellationToken cancellationToken) => new List <ResourceIdentifier>() { new ResourceIdentifier(ResourceType.VirtualMachine, subscriptionId, "resourceGroupName", "resourceName") }); azureResourceManagerClientMock .Setup(x => x.GetAllResourcesInResourceGroupAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IEnumerable <ResourceType> >(), It.IsAny <CancellationToken>())) .ReturnsAsync((string subscriptionId, string resourceGroupName, IEnumerable <ResourceType> resourceTypes, CancellationToken cancellationToken) => new List <ResourceIdentifier>() { new ResourceIdentifier(ResourceType.VirtualMachine, subscriptionId, resourceGroupName, "resourceName") }); this.testContainer.RegisterInstance <IExtendedAzureResourceManagerClient>(azureResourceManagerClientMock.Object); this.testContainer.RegisterInstance <IQueryRunInfoProvider>(new Mock <IQueryRunInfoProvider>().Object); var stateRepositoryMock = new Mock <IStateRepository>(); var stateRepositoryFactoryMock = new Mock <IStateRepositoryFactory>(); stateRepositoryFactoryMock.Setup(m => m.Create(It.IsAny <string>(), It.IsAny <string>())).Returns(stateRepositoryMock.Object); this.testContainer.RegisterInstance <IStateRepositoryFactory>(stateRepositoryFactoryMock.Object); }
private void TestInitialize(ResourceType requestResourceType, ResourceType smartDetectorResourceType) { this.testContainer = new UnityContainer(); this.testContainer.RegisterType <ISmartDetectorRunner, SmartDetectorRunner>(); this.testContainer.RegisterInstance(new Mock <ITracer>().Object); ResourceIdentifier resourceId; switch (requestResourceType) { case ResourceType.Subscription: resourceId = new ResourceIdentifier(requestResourceType, "subscriptionId", string.Empty, string.Empty); break; case ResourceType.ResourceGroup: resourceId = new ResourceIdentifier(requestResourceType, "subscriptionId", "resourceGroup", string.Empty); break; default: resourceId = new ResourceIdentifier(requestResourceType, "subscriptionId", "resourceGroup", "resourceName"); break; } this.resourceIds = new List <string> { resourceId.ToResourceId() }; this.analysisRequest = new SmartDetectorAnalysisRequest { ResourceIds = this.resourceIds, Cadence = TimeSpan.FromDays(1), AlertRuleResourceId = "alertRule", SmartDetectorId = "1", DetectorParameters = new Dictionary <string, object> { { "param1", "value1" }, { "param2", 2 }, } }; this.alertResolutionCheckRequest = new ContractsAlertResolutionCheckRequest { OriginalAnalysisRequest = new SmartDetectorAnalysisRequest { ResourceIds = this.resourceIds, Cadence = TimeSpan.FromDays(1), AlertRuleResourceId = "alertRule", SmartDetectorId = "2", DetectorParameters = new Dictionary <string, object> { { "param1", "value1" }, { "param2", 2 }, } }, AlertCorrelationHash = "correlationHash", TargetResource = resourceId.ToResourceId(), AlertFireTime = new DateTime(1985, 7, 3) }; var smartDetectorManifest = new SmartDetectorManifest( "1", "Test Smart Detector", "Test Smart Detector description", Version.Parse("1.0"), "TestSmartDetectorLibrary", "class", new List <ResourceType>() { smartDetectorResourceType }, new List <int> { 60 }, null, null); this.smartDetectorPackage = new SmartDetectorPackage(new Dictionary <string, byte[]> { ["manifest.json"] = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(smartDetectorManifest)), ["TestSmartDetectorLibrary"] = Array.Empty <byte>(), }); var autoResolveSmartDetectorManifest = new SmartDetectorManifest( "2", "Test Auto Resolve Smart Detector", "Test Auto Resolve Smart Detector description", Version.Parse("1.0"), "TestSmartDetectorLibrary", "class", new List <ResourceType>() { smartDetectorResourceType }, new List <int> { 60 }, null, null); this.autoResolveSmartDetectorPackage = new SmartDetectorPackage(new Dictionary <string, byte[]> { ["manifest.json"] = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(autoResolveSmartDetectorManifest)), ["TestSmartDetectorLibrary"] = Array.Empty <byte>(), }); var smartDetectorRepositoryMock = new Mock <ISmartDetectorRepository>(); smartDetectorRepositoryMock .Setup(x => x.ReadSmartDetectorPackageAsync("1", It.IsAny <CancellationToken>())) .ReturnsAsync(() => this.smartDetectorPackage); smartDetectorRepositoryMock .Setup(x => x.ReadSmartDetectorPackageAsync("2", It.IsAny <CancellationToken>())) .ReturnsAsync(() => this.autoResolveSmartDetectorPackage); this.testContainer.RegisterInstance(smartDetectorRepositoryMock.Object); this.testContainer.RegisterInstance(new Mock <IInternalAnalysisServicesFactory>().Object); this.smartDetector = new TestSmartDetector { ExpectedResourceType = smartDetectorResourceType }; this.autoResolveSmartDetector = new TestAutoResolveSmartDetector { ExpectedResourceType = smartDetectorResourceType }; var smartDetectorLoaderMock = new Mock <ISmartDetectorLoader>(); smartDetectorLoaderMock .Setup(x => x.LoadSmartDetector(this.smartDetectorPackage)) .Returns(() => this.smartDetector); smartDetectorLoaderMock .Setup(x => x.LoadSmartDetector(this.autoResolveSmartDetectorPackage)) .Returns(() => this.autoResolveSmartDetector); this.testContainer.RegisterInstance(smartDetectorLoaderMock.Object); var azureResourceManagerClientMock = new Mock <IExtendedAzureResourceManagerClient>(); azureResourceManagerClientMock .Setup(x => x.GetAllResourceGroupsInSubscriptionAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .ReturnsAsync((string subscriptionId, CancellationToken cancellationToken) => new List <ResourceIdentifier>() { new ResourceIdentifier(ResourceType.ResourceGroup, subscriptionId, "resourceGroupName", string.Empty) }); azureResourceManagerClientMock .Setup(x => x.GetAllResourcesInSubscriptionAsync(It.IsAny <string>(), It.IsAny <IEnumerable <ResourceType> >(), It.IsAny <CancellationToken>())) .ReturnsAsync((string subscriptionId, IEnumerable <ResourceType> resourceTypes, CancellationToken cancellationToken) => new List <ResourceIdentifier>() { new ResourceIdentifier(ResourceType.VirtualMachine, subscriptionId, "resourceGroupName", "resourceName") }); azureResourceManagerClientMock .Setup(x => x.GetAllResourcesInResourceGroupAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IEnumerable <ResourceType> >(), It.IsAny <CancellationToken>())) .ReturnsAsync((string subscriptionId, string resourceGroupName, IEnumerable <ResourceType> resourceTypes, CancellationToken cancellationToken) => new List <ResourceIdentifier>() { new ResourceIdentifier(ResourceType.VirtualMachine, subscriptionId, resourceGroupName, "resourceName") }); this.testContainer.RegisterInstance(azureResourceManagerClientMock.Object); this.stateRepository = new Dictionary <string, object>(); this.stateRepositoryMock = new Mock <IStateRepository>(); this.stateRepositoryMock .Setup(m => m.StoreStateAsync(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <CancellationToken>())) .Callback <string, object, CancellationToken>((key, value, token) => this.stateRepository[key] = value) .Returns(Task.CompletedTask); this.stateRepositoryMock .Setup(m => m.GetStateAsync <ResolutionState>(It.IsAny <string>(), It.IsAny <CancellationToken>())) .Returns <string, CancellationToken>((key, token) => Task.FromResult((ResolutionState)(this.stateRepository.ContainsKey(key) ? this.stateRepository[key] : null))); this.stateRepositoryMock .Setup(m => m.DeleteStateAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .Callback <string, CancellationToken>((key, token) => this.stateRepository.Remove(key)) .Returns(Task.CompletedTask); this.stateRepositoryFactoryMock = new Mock <IStateRepositoryFactory>(); this.stateRepositoryFactoryMock.Setup(m => m.Create(It.IsAny <string>(), It.IsAny <string>())).Returns(this.stateRepositoryMock.Object); this.testContainer.RegisterInstance(this.stateRepositoryFactoryMock.Object); }