public async Task RunFail_NullReferenceException() { var appSettings = new AppSettings(); var diskOptions = new DiskStorageOptions(); DiskOptionsPercentageSetup.Setup(appSettings.TempFolder, diskOptions, 99.9f); var healthCheck = new HealthCheckContext(); await new DiskStorageHealthCheck(diskOptions).CheckHealthAsync(healthCheck); }
public void SetupTest() { var appSettings = new AppSettings(); var diskOptions = new DiskStorageOptions(); DiskOptionsPercentageSetup.Setup(appSettings.TempFolder, diskOptions); var value = diskOptions.GetType().GetRuntimeFields().FirstOrDefault(); Assert.IsNotNull(value); }
/// <summary> /// Add a health check for disk storage. /// </summary> /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param> /// <param name="setup">The action method to configure the health check parameters.</param> /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'diskstorage' will be used for the name.</param> /// <param name="failureStatus"> /// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then /// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported. /// </param> /// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param> /// <returns>The <see cref="IHealthChecksBuilder"/>.</returns></param> public static IHealthChecksBuilder AddDiskStorageHealthCheck(this IHealthChecksBuilder builder, Action <DiskStorageOptions> setup, string name = default, HealthStatus?failureStatus = default, IEnumerable <string> tags = default) { var options = new DiskStorageOptions(); setup?.Invoke(options); return(builder.Add(new HealthCheckRegistration( name ?? DISK_NAME, sp => new DiskStorageHealthCheck(options), failureStatus, tags))); }
public async Task RunSuccessful() { var appSettings = new AppSettings(); var diskOptions = new DiskStorageOptions(); DiskOptionsPercentageSetup.Setup(appSettings.TempFolder, diskOptions, 0.000001f); var healthCheck = new HealthCheckContext(); var result = await new DiskStorageHealthCheck(diskOptions).CheckHealthAsync(healthCheck); Assert.AreEqual(HealthStatus.Healthy, result.Status); }
public async Task RunFailNonExistDisk() { var diskOptions = new DiskStorageOptions(); diskOptions.AddDrive("NonExistDisk:", 10); var healthCheck = new HealthCheckContext { Registration = new HealthCheckRegistration("te", new DiskStorageHealthCheck(diskOptions), null, null) }; var result = await new DiskStorageHealthCheck(diskOptions).CheckHealthAsync(healthCheck); Assert.AreEqual(HealthStatus.Unhealthy, result.Status); Assert.IsTrue(result.Description.Contains("is not present on system")); }
public async Task RunFailNotEnoughSpace() { var appSettings = new AppSettings(); var diskOptions = new DiskStorageOptions(); DiskOptionsPercentageSetup.Setup(appSettings.TempFolder, diskOptions, 1.01f); var healthCheck = new HealthCheckContext { Registration = new HealthCheckRegistration("te", new DiskStorageHealthCheck(diskOptions), null, null) }; var result = await new DiskStorageHealthCheck(diskOptions).CheckHealthAsync(healthCheck); Assert.AreEqual(HealthStatus.Unhealthy, result.Status); Assert.IsTrue(result.Description.Contains("Minimum configured megabytes for disk")); }
public async Task Upload_File_And_Save_Local_Disk_Test() { if (!_context.AllowMySQL) { Assert.True(true); return; } // 1. Arrange Mock <IFormFile> mockFile = new Mock <IFormFile>(); FileStream sourceImg = System.IO.File.OpenRead(@"Artifacts\connect-iot-to-internet.jpg"); MemoryStream memoryStream = new MemoryStream(); await sourceImg.CopyToAsync(memoryStream); sourceImg.Close(); memoryStream.Position = 0; string fileName = "connect-iot-to-internet.jpg"; mockFile.Setup(f => f.Length).Returns(memoryStream.Length).Verifiable(); mockFile.Setup(f => f.FileName).Returns(fileName).Verifiable(); mockFile.Setup(f => f.OpenReadStream()).Returns(memoryStream).Verifiable(); mockFile .Setup(f => f.CopyToAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>())) .Returns((Stream stream, CancellationToken token) => memoryStream.CopyToAsync(stream)) .Verifiable(); DiskStorageOptions diskOptions = new DiskStorageOptions { AllowDayFolder = true, Path = "~/UploadFiles" }; DatabaseOptions databaseOptions = new Core.Persistences.DatabaseOptions { ConnectionString = _context.MySqlDatabaseConnection.ConnectionString, ConnectionType = Core.Persistences.ConnectionType.MySQL, Datasource = _context.MySqlDatabaseConnection.DataSource }; LetPortal.Portal.Options.Files.FileOptions localFileOptions = FileOptions; localFileOptions.DiskStorageOptions = diskOptions; localFileOptions.FileStorageType = FileStorageType.Disk; #pragma warning disable CA2000 // Dispose objects before losing scope FileEFRepository fileRepository = new FileEFRepository(_context.GetMySQLContext()); #pragma warning restore CA2000 // Dispose objects before losing scope IOptionsMonitor <FilePublishOptions> filePublishOptionsMock = Mock.Of <IOptionsMonitor <FilePublishOptions> >(_ => _.CurrentValue == FilePublishOptions); IOptionsMonitor <LetPortal.Portal.Options.Files.FileOptions> fileOptionsMock = Mock.Of <IOptionsMonitor <LetPortal.Portal.Options.Files.FileOptions> >(_ => _.CurrentValue == localFileOptions); IOptionsMonitor <FileValidatorOptions> fileValidatorMock = Mock.Of <IOptionsMonitor <FileValidatorOptions> >(_ => _.CurrentValue == localFileOptions.ValidatorOptions); IOptionsMonitor <DiskStorageOptions> diskStorageOptionsMock = Mock.Of <IOptionsMonitor <DiskStorageOptions> >(_ => _.CurrentValue == diskOptions); CheckFileExtensionRule checkFileExtensionRule = new CheckFileExtensionRule(fileValidatorMock); CheckFileSizeRule checkFileSizeRule = new CheckFileSizeRule(fileValidatorMock); DiskFileConnectorExecution diskStorage = new DiskFileConnectorExecution(diskStorageOptionsMock); FileService fileService = new FileService( fileOptionsMock, filePublishOptionsMock, new List <IFileConnectorExecution> { diskStorage }, new List <IFileValidatorRule> { checkFileExtensionRule, checkFileSizeRule }, fileRepository); // Act LetPortal.Portal.Models.Files.ResponseUploadFile result = await fileService.UploadFileAsync(mockFile.Object, "tester", false); memoryStream.Close(); fileRepository.Dispose(); // 3. Assert Assert.True(!string.IsNullOrEmpty(result.FileId)); }
public DiskStorage(IOptions <DiskStorageOptions> options) { _options = options.Value; }