SystemDeserializationTests.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using RackPeek.Domain.Persistence;
  2. using RackPeek.Domain.Persistence.Yaml;
  3. using RackPeek.Domain.Resources.SystemResources;
  4. using System.Reflection;
  5. using DocMigrator.Yaml;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using YamlDotNet.Serialization;
  9. using YamlDotNet.Serialization.NamingConventions;
  10. namespace Tests.Yaml;
  11. public class SystemDeserializationTests
  12. {
  13. public static async Task<IResourceCollection> CreateSut(string yaml)
  14. {
  15. var tempDir = Path.Combine(
  16. Path.GetTempPath(),
  17. "RackPeekTests",
  18. Guid.NewGuid().ToString("N"));
  19. Directory.CreateDirectory(tempDir);
  20. var filePath = Path.Combine(tempDir, "config.yaml");
  21. await File.WriteAllTextAsync(filePath, yaml);
  22. var services = new ServiceCollection();
  23. services.AddLogging();
  24. services.AddSingleton<RackPeekConfigMigrationDeserializer>(sp =>
  25. {
  26. var logger = sp.GetRequiredService<ILogger<YamlMigrationDeserializer<YamlRoot>>>();
  27. // TODO: Add options
  28. var deserializerBuilder = new DeserializerBuilder()
  29. .WithNamingConvention(CamelCaseNamingConvention.Instance)
  30. .IgnoreUnmatchedProperties();
  31. var serializerBuilder = new SerializerBuilder()
  32. .WithNamingConvention(CamelCaseNamingConvention.Instance);
  33. return new RackPeekConfigMigrationDeserializer(
  34. sp,
  35. logger,
  36. deserializerBuilder,
  37. serializerBuilder);
  38. });
  39. var scope = services.BuildServiceProvider().CreateScope();
  40. var yamlResourceCollection =
  41. new YamlResourceCollection(filePath, new PhysicalTextFileStore(), new ResourceCollection(), scope.ServiceProvider.GetRequiredService<RackPeekConfigMigrationDeserializer>());
  42. await yamlResourceCollection.LoadAsync();
  43. return yamlResourceCollection;
  44. }
  45. [Fact]
  46. public async Task deserialize_yaml_kind_System()
  47. {
  48. // type: Hypervisor | Baremetal | VM | Container
  49. // Given
  50. var yaml = @"
  51. resources:
  52. - kind: System
  53. type: Hypervisor
  54. name: home-virtualization-host
  55. os: proxmox
  56. cores: 2
  57. ram: 12.5gb
  58. drives:
  59. - size: 2Tb
  60. - size: 1tb
  61. runsOn: dell-c6400-node-01
  62. ";
  63. var sut = await CreateSut(yaml);
  64. // When
  65. var resources = await sut.GetAllOfTypeAsync<SystemResource>();
  66. // Then
  67. var resource = Assert.Single(resources);
  68. Assert.IsType<SystemResource>(resource);
  69. var system = resource;
  70. Assert.NotNull(system);
  71. Assert.Equal("Hypervisor", system.Type);
  72. Assert.Equal("home-virtualization-host", system.Name);
  73. Assert.Equal("proxmox", system.Os);
  74. Assert.Equal(2, system.Cores);
  75. Assert.Equal(12.5, system.Ram);
  76. // Drives
  77. Assert.NotNull(system.Drives);
  78. Assert.Equal(2048, system.Drives[0].Size);
  79. Assert.Equal(1024, system.Drives[1].Size);
  80. Assert.Equal("dell-c6400-node-01", system.RunsOn);
  81. }
  82. }