From 6cfd04530e764ff198e8348ba857081b6b7e274e Mon Sep 17 00:00:00 2001 From: Gabriel Weyer Date: Sun, 13 Oct 2024 09:34:10 +1100 Subject: [PATCH] Fix parsing VM price C# was expecting properties to be pascal case. --- coster/.nuke/build.schema.json | 122 +++++++++--------- .../AzureVmCoster/Services/VmPricingParser.cs | 7 +- .../AzureVmCosterTests.csproj | 6 + .../Services/VmPricingParserTests.cs | 41 ++++++ ...-pricing_germany-west-central_windows.json | 19 +++ 5 files changed, 134 insertions(+), 61 deletions(-) create mode 100644 coster/tests/AzureVmCosterTests/Services/VmPricingParserTests.cs create mode 100644 coster/tests/AzureVmCosterTests/TestPricing/vm-pricing_germany-west-central_windows.json diff --git a/coster/.nuke/build.schema.json b/coster/.nuke/build.schema.json index ba33ece..62c1bb4 100644 --- a/coster/.nuke/build.schema.json +++ b/coster/.nuke/build.schema.json @@ -1,19 +1,63 @@ { "$schema": "http://json-schema.org/draft-04/schema#", - "$ref": "#/definitions/build", - "title": "Build Schema", + "properties": { + "Configuration": { + "type": "string", + "description": "Configuration to build - Default is 'Debug' (local) or 'Release' (server)", + "enum": [ + "Debug", + "Release" + ] + }, + "Solution": { + "type": "string", + "description": "Path to a solution file that is automatically loaded" + } + }, "definitions": { - "build": { - "type": "object", + "Host": { + "type": "string", + "enum": [ + "AppVeyor", + "AzurePipelines", + "Bamboo", + "Bitbucket", + "Bitrise", + "GitHubActions", + "GitLab", + "Jenkins", + "Rider", + "SpaceAutomation", + "TeamCity", + "Terminal", + "TravisCI", + "VisualStudio", + "VSCode" + ] + }, + "ExecutableTarget": { + "type": "string", + "enum": [ + "Clean", + "Compile", + "GenerateCoverage", + "Restore", + "Test", + "VerifyFormat" + ] + }, + "Verbosity": { + "type": "string", + "description": "", + "enum": [ + "Verbose", + "Normal", + "Minimal", + "Quiet" + ] + }, + "NukeBuild": { "properties": { - "Configuration": { - "type": "string", - "description": "Configuration to build - Default is 'Debug' (local) or 'Release' (server)", - "enum": [ - "Debug", - "Release" - ] - }, "Continue": { "type": "boolean", "description": "Indicates to continue a previously failed build attempt" @@ -23,25 +67,8 @@ "description": "Shows the help text for this build assembly" }, "Host": { - "type": "string", "description": "Host for execution. Default is 'automatic'", - "enum": [ - "AppVeyor", - "AzurePipelines", - "Bamboo", - "Bitbucket", - "Bitrise", - "GitHubActions", - "GitLab", - "Jenkins", - "Rider", - "SpaceAutomation", - "TeamCity", - "Terminal", - "TravisCI", - "VisualStudio", - "VSCode" - ] + "$ref": "#/definitions/Host" }, "NoLogo": { "type": "boolean", @@ -70,47 +97,22 @@ "type": "array", "description": "List of targets to be skipped. Empty list skips all dependencies", "items": { - "type": "string", - "enum": [ - "Clean", - "Compile", - "GenerateCoverage", - "Restore", - "Test", - "VerifyFormat" - ] + "$ref": "#/definitions/ExecutableTarget" } }, - "Solution": { - "type": "string", - "description": "Path to a solution file that is automatically loaded" - }, "Target": { "type": "array", "description": "List of targets to be invoked. Default is '{default_target}'", "items": { - "type": "string", - "enum": [ - "Clean", - "Compile", - "GenerateCoverage", - "Restore", - "Test", - "VerifyFormat" - ] + "$ref": "#/definitions/ExecutableTarget" } }, "Verbosity": { - "type": "string", "description": "Logging verbosity during build execution. Default is 'Normal'", - "enum": [ - "Minimal", - "Normal", - "Quiet", - "Verbose" - ] + "$ref": "#/definitions/Verbosity" } } } - } + }, + "$ref": "#/definitions/NukeBuild" } diff --git a/coster/src/AzureVmCoster/Services/VmPricingParser.cs b/coster/src/AzureVmCoster/Services/VmPricingParser.cs index 8c9b33b..7da85ee 100644 --- a/coster/src/AzureVmCoster/Services/VmPricingParser.cs +++ b/coster/src/AzureVmCoster/Services/VmPricingParser.cs @@ -17,12 +17,17 @@ public List Parse() var allVmPricing = new List(); + JsonSerializerOptions options = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + foreach (var pricingFile in pricingFiles) { var fileInfo = new FileInfo(pricingFile); var fileIdentifier = FileIdentifier.From(fileInfo); - var fileVmPricing = JsonSerializer.Deserialize>(File.ReadAllText(pricingFile)); + var fileVmPricing = JsonSerializer.Deserialize>(File.ReadAllText(pricingFile), options); if (fileVmPricing == null || fileVmPricing.Count == 0) { diff --git a/coster/tests/AzureVmCosterTests/AzureVmCosterTests.csproj b/coster/tests/AzureVmCosterTests/AzureVmCosterTests.csproj index 8c076af..eb435fc 100644 --- a/coster/tests/AzureVmCosterTests/AzureVmCosterTests.csproj +++ b/coster/tests/AzureVmCosterTests/AzureVmCosterTests.csproj @@ -46,4 +46,10 @@ + + + Always + + + diff --git a/coster/tests/AzureVmCosterTests/Services/VmPricingParserTests.cs b/coster/tests/AzureVmCosterTests/Services/VmPricingParserTests.cs new file mode 100644 index 0000000..7538e49 --- /dev/null +++ b/coster/tests/AzureVmCosterTests/Services/VmPricingParserTests.cs @@ -0,0 +1,41 @@ +using AzureVmCoster.Services; + +namespace AzureVmCosterTests.Services; + +public class VmPricingParserTests +{ + private readonly VmPricingParser _parser = new("TestPricing/"); + + [Fact] + public void GivenValidPrice_ThenParseVm() + { + // Act + var prices = _parser.Parse(); + + // Assert + var expectedPrices = new List + { + new() + { + Region = "germany-west-central", + OperatingSystem = "windows", + Instance = "B2ts v2", + VCpu = 2, + Ram = 1, + PayAsYouGo = 0.019m, + PayAsYouGoWithAzureHybridBenefit = 0.0107m, + OneYearSavingsPlan = 0.0153m, + OneYearSavingsPlanWithAzureHybridBenefit = 0.0082m, + ThreeYearSavingsPlan = 0.0131m, + ThreeYearSavingsPlanWithAzureHybridBenefit = 0.0059m, + OneYearReserved = 0.0135m, + OneYearReservedWithAzureHybridBenefit = 0.0063m, + ThreeYearReserved = 0.0113m, + ThreeYearReservedWithAzureHybridBenefit = 0.0041m, + Spot = 0.0063m, + SpotWithAzureHybridBenefit = 0.0036m + } + }; + prices.Should().BeEquivalentTo(expectedPrices); + } +} diff --git a/coster/tests/AzureVmCosterTests/TestPricing/vm-pricing_germany-west-central_windows.json b/coster/tests/AzureVmCosterTests/TestPricing/vm-pricing_germany-west-central_windows.json new file mode 100644 index 0000000..de2d952 --- /dev/null +++ b/coster/tests/AzureVmCosterTests/TestPricing/vm-pricing_germany-west-central_windows.json @@ -0,0 +1,19 @@ +[ + { + "instance": "B2ts v2", + "vCpu": 2, + "ram": 1, + "payAsYouGo": 0.019, + "payAsYouGoWithAzureHybridBenefit": 0.0107, + "oneYearSavingsPlan": 0.0153, + "oneYearSavingsPlanWithAzureHybridBenefit": 0.0082, + "threeYearSavingsPlan": 0.0131, + "threeYearSavingsPlanWithAzureHybridBenefit": 0.0059, + "oneYearReserved": 0.0135, + "oneYearReservedWithAzureHybridBenefit": 0.0063, + "threeYearReserved": 0.0113, + "threeYearReservedWithAzureHybridBenefit": 0.0041, + "spot": 0.0063, + "spotWithAzureHybridBenefit": 0.0036 + } +]