зеркало из https://github.com/Azure/benchpress.git
refactor
This commit is contained in:
Родитель
5e91d822d0
Коммит
a8de07e362
|
@ -1,4 +1,6 @@
|
|||
namespace AzureTestGen;
|
||||
using AzureTestGen.ResourceTypes;
|
||||
|
||||
namespace AzureTestGen.LanguageProviders;
|
||||
|
||||
public interface ILanguageProvider
|
||||
{
|
||||
|
@ -7,6 +9,6 @@ public interface ILanguageProvider
|
|||
public string Variable(string name);
|
||||
public string Value(object value);
|
||||
public string Library(ResourceType resourceType);
|
||||
public string SDKFunction(SDKFunction sdkFunction);
|
||||
public string SDK(SDKFunction sdkFunction);
|
||||
public string ParameterList(params string[] parameters);
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
namespace AzureTestGen;
|
||||
using AzureTestGen.ResourceTypes;
|
||||
|
||||
namespace AzureTestGen.LanguageProviders;
|
||||
|
||||
public class PowershellLanguageProvider : ILanguageProvider
|
||||
{
|
||||
|
@ -22,7 +24,7 @@ public class PowershellLanguageProvider : ILanguageProvider
|
|||
return b ? "$true" : "$false";
|
||||
|
||||
default:
|
||||
return value.ToString();
|
||||
return value.ToString()!;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,25 +41,17 @@ public class PowershellLanguageProvider : ILanguageProvider
|
|||
public string Library(ResourceType resourceType)
|
||||
{
|
||||
const string prefix = "$PSScriptRoot/BenchPress/Helpers/Azure/";
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.ResourceGroup:
|
||||
return prefix + "ResourceGroups.ps1";
|
||||
case ResourceType.VirtualMachine:
|
||||
return prefix + "VirtualMachines.ps1";
|
||||
default:
|
||||
throw new Exception($"Unknown resource type: {resourceType}");
|
||||
}
|
||||
return prefix + resourceType.FunctionPrefix + ".ps1";
|
||||
}
|
||||
|
||||
public string SDKFunction(SDKFunction sdkFunction)
|
||||
public string SDK(SDKFunction sdkFunction)
|
||||
{
|
||||
switch (sdkFunction.Kind)
|
||||
{
|
||||
case TestType.ResourceExists:
|
||||
return $"Get-{sdkFunction.ResourceType.FunctionPrefix()}Exists";
|
||||
return $"Get-{sdkFunction.ResourceType.FunctionPrefix}Exists";
|
||||
case TestType.Region:
|
||||
return $"Check-{sdkFunction.ResourceType.FunctionPrefix()}Region";
|
||||
return $"Check-{sdkFunction.ResourceType.FunctionPrefix}Region";
|
||||
default:
|
||||
throw new Exception($"Unknown test type: {sdkFunction.Kind}");
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
// See https://aka.ms/new-console-template for more information
|
||||
using AzureTestGen;
|
||||
using AzureTestGen.LanguageProviders;
|
||||
|
||||
var language = new PowershellLanguageProvider();
|
||||
var generator = new TestGenerator(language);
|
||||
|
@ -9,7 +10,7 @@ var rgMetadata = new TestMetadata(
|
|||
"Microsoft.Resources/resourceGroups",
|
||||
"MyResourceGroup",
|
||||
new Dictionary<string, object>() {
|
||||
{ "Location", "westus" }
|
||||
{ "location", "westus" }
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -17,7 +18,8 @@ var vmMetadata = new TestMetadata(
|
|||
"Microsoft.Compute/virtualMachines",
|
||||
"MyVM",
|
||||
new Dictionary<string, object>() {
|
||||
{ "Location", "westus" },
|
||||
{ "location", "westus" },
|
||||
{ "resourceGroup", "myResourceGroup"},
|
||||
{ "Size", "Standard_D1_v2" },
|
||||
{ "OSDiskSize", "30" },
|
||||
{ "VMSize", "Standard_D1_v2" },
|
||||
|
@ -51,20 +53,17 @@ var vmMetadata = new TestMetadata(
|
|||
|
||||
#endregion
|
||||
|
||||
var rgExistsTest = new TestDefinition() {
|
||||
Type = TestType.ResourceExists,
|
||||
Metadata = rgMetadata
|
||||
};
|
||||
var rgExistsTest = new TestDefinition(
|
||||
rgMetadata,
|
||||
TestType.ResourceExists);
|
||||
|
||||
var vmExistsTest = new TestDefinition() {
|
||||
Type = TestType.ResourceExists,
|
||||
Metadata = vmMetadata
|
||||
};
|
||||
var vmExistsTest = new TestDefinition(
|
||||
vmMetadata,
|
||||
TestType.ResourceExists);
|
||||
|
||||
var vmCheckRegion = new TestDefinition() {
|
||||
Type = TestType.Region,
|
||||
Metadata = vmMetadata
|
||||
};
|
||||
var vmCheckRegion = new TestDefinition(
|
||||
vmMetadata,
|
||||
TestType.Region);
|
||||
|
||||
var generatedTest = generator.Generate(
|
||||
new [] {rgExistsTest, vmExistsTest, vmCheckRegion},
|
||||
|
|
|
@ -1,82 +0,0 @@
|
|||
namespace AzureTestGen;
|
||||
|
||||
public enum ResourceType
|
||||
{
|
||||
ResourceGroup,
|
||||
VirtualMachine,
|
||||
}
|
||||
|
||||
public static class ResourceTypeParser {
|
||||
internal const string ResourceGroupTypeId = "Microsoft.Resources/resourceGroups";
|
||||
internal const string VirtualMachineTypeId = "Microsoft.Compute/virtualMachines";
|
||||
|
||||
public static ResourceType Parse(string resourceType) {
|
||||
switch (resourceType) {
|
||||
case ResourceGroupTypeId:
|
||||
return ResourceType.ResourceGroup;
|
||||
case VirtualMachineTypeId:
|
||||
return ResourceType.VirtualMachine;
|
||||
|
||||
default:
|
||||
throw new Exception($"Unknown resource type: {resourceType}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class ResourceTypeExtensions
|
||||
{
|
||||
public static string FullName(this ResourceType resourceType)
|
||||
{
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.ResourceGroup:
|
||||
return ResourceTypeParser.ResourceGroupTypeId;
|
||||
case ResourceType.VirtualMachine:
|
||||
return ResourceTypeParser.VirtualMachineTypeId;
|
||||
|
||||
default:
|
||||
throw new Exception($"Unknown resource type: {resourceType}");
|
||||
}
|
||||
}
|
||||
|
||||
public static string Prefix(this ResourceType resourceType)
|
||||
{
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.ResourceGroup:
|
||||
return "rg";
|
||||
case ResourceType.VirtualMachine:
|
||||
return "vm";
|
||||
default:
|
||||
throw new Exception($"Unknown resource type: {resourceType}");
|
||||
}
|
||||
}
|
||||
|
||||
public static string FriendlyName(this ResourceType resourceType)
|
||||
{
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.ResourceGroup:
|
||||
return "Resource Group";
|
||||
case ResourceType.VirtualMachine:
|
||||
return "Virtual Machine";
|
||||
default:
|
||||
throw new Exception($"Unknown resource type: {resourceType}");
|
||||
}
|
||||
}
|
||||
|
||||
public static string FunctionPrefix(this ResourceType resourceType)
|
||||
{
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceType.ResourceGroup:
|
||||
return "ResourceGroup";
|
||||
case ResourceType.VirtualMachine:
|
||||
return "VirtualMachine";
|
||||
default:
|
||||
throw new Exception($"Unknown resource type: {resourceType}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
namespace AzureTestGen.ResourceTypes;
|
||||
|
||||
public class ResourceGroup : ResourceType
|
||||
{
|
||||
public ResourceGroup()
|
||||
{
|
||||
}
|
||||
|
||||
public const string Id = "Microsoft.Resources/resourceGroups";
|
||||
|
||||
public override string FullName => Id;
|
||||
|
||||
public override string FriendlyName => "Resource Groups";
|
||||
|
||||
public override string Prefix => "rg";
|
||||
|
||||
public override string FunctionPrefix => "ResourceGroup";
|
||||
|
||||
public override IEnumerable<KeyValuePair<string, object>> GetResourceParameters(TestMetadata m)
|
||||
{
|
||||
return new [] {
|
||||
Param("name", m.ResourceName)
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
namespace AzureTestGen.ResourceTypes;
|
||||
|
||||
public abstract class ResourceType
|
||||
{
|
||||
internal const string ResourceGroupTypeId = "Microsoft.Resources/resourceGroups";
|
||||
|
||||
public static ResourceType Create(string resourceType)
|
||||
{
|
||||
switch (resourceType)
|
||||
{
|
||||
case ResourceGroup.Id:
|
||||
return new ResourceGroup();
|
||||
case VirtualMachine.Id:
|
||||
return new VirtualMachine();
|
||||
|
||||
default:
|
||||
throw new Exception($"Unknown resource type: {resourceType}");
|
||||
}
|
||||
}
|
||||
public abstract string FullName { get; }
|
||||
public abstract string FriendlyName { get; }
|
||||
public abstract string Prefix { get; }
|
||||
public abstract string FunctionPrefix { get; }
|
||||
public abstract IEnumerable<KeyValuePair<string, object>> GetResourceParameters(TestMetadata m);
|
||||
|
||||
protected static KeyValuePair<string, object> Param(string name, object value)
|
||||
{
|
||||
return new KeyValuePair<string, object>(name, value);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
namespace AzureTestGen.ResourceTypes;
|
||||
|
||||
public class VirtualMachine : ResourceType
|
||||
{
|
||||
public VirtualMachine()
|
||||
{
|
||||
}
|
||||
|
||||
public const string Id = "Microsoft.Compute/virtualMachines";
|
||||
|
||||
public override string FullName => Id;
|
||||
|
||||
public override string FriendlyName => "Virtual Machines";
|
||||
|
||||
public override string Prefix => "vm";
|
||||
|
||||
public override string FunctionPrefix => "VirtualMachine";
|
||||
|
||||
public override IEnumerable<KeyValuePair<string, object>> GetResourceParameters(TestMetadata m)
|
||||
{
|
||||
return new [] {
|
||||
Param("name", m.ResourceName),
|
||||
Param("resourceGroup", m.ExtraProperties["resourceGroup"])
|
||||
};
|
||||
}
|
||||
}
|
|
@ -1,14 +1,13 @@
|
|||
using AzureTestGen.ResourceTypes;
|
||||
|
||||
namespace AzureTestGen;
|
||||
|
||||
public class SDKFunction
|
||||
{
|
||||
public static SDKFunction Default(TestDefinition definition)
|
||||
public SDKFunction(TestDefinition definition)
|
||||
{
|
||||
return new SDKFunction
|
||||
{
|
||||
Kind = definition.Type,
|
||||
ResourceType = definition.Metadata.ResourceType,
|
||||
};
|
||||
Kind = definition.Type;
|
||||
ResourceType = definition.Metadata.ResourceType;
|
||||
}
|
||||
|
||||
public TestType Kind { get; set; }
|
||||
|
|
|
@ -2,6 +2,12 @@ namespace AzureTestGen;
|
|||
|
||||
public class TestDefinition
|
||||
{
|
||||
public TestDefinition(TestMetadata metadata, TestType type)
|
||||
{
|
||||
Metadata = metadata;
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public TestMetadata Metadata { get; set; }
|
||||
public TestType Type { get; set; }
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Stubble.Core.Builders;
|
||||
using AzureTestGen.LanguageProviders;
|
||||
|
||||
namespace AzureTestGen;
|
||||
|
||||
|
@ -56,7 +57,9 @@ public class TestGenerator
|
|||
|
||||
private TestViewModel GenerateCheckRegionViewModel(TestDefinition definition, string template)
|
||||
{
|
||||
/*
|
||||
var valueToCheckVariable = LanguageProvider.Variable($"{definition.Metadata.ResourceType.Prefix()}Region");
|
||||
|
||||
return new TestViewModel
|
||||
{
|
||||
Name = LanguageProvider.Escape($"Check {definition.Metadata.ResourceType.FriendlyName()} region"),
|
||||
|
@ -65,36 +68,59 @@ public class TestGenerator
|
|||
GetValueFunctionParameterList = LanguageProvider.ParameterList(
|
||||
LanguageProvider.Value(definition.Metadata.ResourceName),
|
||||
valueToCheckVariable),
|
||||
ValueToCheck = LanguageProvider.Value(definition.Metadata.ExtraProperties["Location"]),
|
||||
ValueToCheck = LanguageProvider.Value(definition.Metadata.ExtraProperties["location"]),
|
||||
ActualValueVariable = LanguageProvider.Variable($"check"),
|
||||
GetValueFunctionName = LanguageProvider.SDKFunction(SDKFunction.Default(definition)),
|
||||
ExpectedValue = LanguageProvider.Value(true)
|
||||
};
|
||||
*/
|
||||
var getParameters = definition.Metadata.ResourceType
|
||||
.GetResourceParameters(definition.Metadata)
|
||||
.Select(p => new KeyValuePair<string, string>(
|
||||
LanguageProvider.Variable(p.Key),
|
||||
LanguageProvider.Value(p.Value)))
|
||||
.Append(new KeyValuePair<string, string>(
|
||||
LanguageProvider.Variable($"{definition.Metadata.ResourceType.Prefix}Region"),
|
||||
LanguageProvider.Value(definition.Metadata.ExtraProperties["location"])));
|
||||
|
||||
return new TestViewModel
|
||||
{
|
||||
Parameters = getParameters,
|
||||
Name = LanguageProvider.Escape($"Check {definition.Metadata.ResourceType.FriendlyName} region"),
|
||||
Description = LanguageProvider.Escape($"Check that {definition.Metadata.ResourceType.FriendlyName} is in the right region"),
|
||||
GetValueFunctionParameterList = LanguageProvider.ParameterList(getParameters.Select(p => p.Key).ToArray()),
|
||||
ActualValueVariable = LanguageProvider.Variable($"check"),
|
||||
GetValueFunctionName = LanguageProvider.SDK(new SDKFunction(definition)),
|
||||
ExpectedValue = LanguageProvider.Value(true)
|
||||
};
|
||||
}
|
||||
|
||||
private TestViewModel GenerateResourceExistsViewModel(TestDefinition definition, string template)
|
||||
{
|
||||
var valueToCheckVariable = LanguageProvider.Variable($"{definition.Metadata.ResourceType.Prefix()}Name");
|
||||
var getParameters = definition.Metadata.ResourceType
|
||||
.GetResourceParameters(definition.Metadata)
|
||||
.Select(p => new KeyValuePair<string, string>(
|
||||
LanguageProvider.Variable(p.Key),
|
||||
LanguageProvider.Value(p.Value)));
|
||||
|
||||
return new TestViewModel
|
||||
{
|
||||
Name = LanguageProvider.Escape($"Verify that {definition.Metadata.ResourceType.FriendlyName()} exists"),
|
||||
Description = LanguageProvider.Escape($"It should contain a {definition.Metadata.ResourceType.FriendlyName()} named {definition.Metadata.ResourceName}"),
|
||||
ValueToCheckVariable = valueToCheckVariable,
|
||||
GetValueFunctionParameterList = LanguageProvider.ParameterList(valueToCheckVariable),
|
||||
ValueToCheck = LanguageProvider.Value(definition.Metadata.ResourceName),
|
||||
Parameters = getParameters,
|
||||
Name = LanguageProvider.Escape($"Verify that {definition.Metadata.ResourceType.FriendlyName} exists"),
|
||||
Description = LanguageProvider.Escape($"It should contain a {definition.Metadata.ResourceType.FriendlyName} named {definition.Metadata.ResourceName}"),
|
||||
GetValueFunctionParameterList = LanguageProvider.ParameterList(getParameters.Select(p => p.Key).ToArray()),
|
||||
ActualValueVariable = LanguageProvider.Variable($"exists"),
|
||||
GetValueFunctionName = LanguageProvider.SDKFunction(SDKFunction.Default(definition)),
|
||||
GetValueFunctionName = LanguageProvider.SDK(new SDKFunction(definition)),
|
||||
ExpectedValue = LanguageProvider.Value(true)
|
||||
};
|
||||
}
|
||||
|
||||
private class TestViewModel
|
||||
private struct TestViewModel
|
||||
{
|
||||
public string ValueToCheckVariable { get; set; }
|
||||
public IEnumerable<KeyValuePair<string, string>> Parameters { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string GetValueFunctionParameterList { get; set; }
|
||||
public string ValueToCheck { get; set; }
|
||||
public string ActualValueVariable { get; set; }
|
||||
public string GetValueFunctionName { get; set; }
|
||||
public string ExpectedValue { get; set; }
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
using AzureTestGen.ResourceTypes;
|
||||
|
||||
namespace AzureTestGen;
|
||||
|
||||
public class TestMetadata
|
||||
{
|
||||
public TestMetadata(string resourceType, string resourceName, IDictionary<string, object> extraProperties)
|
||||
{
|
||||
ResourceType = ResourceTypeParser.Parse(resourceType);
|
||||
ResourceType = ResourceType.Create(resourceType);
|
||||
ResourceName = resourceName;
|
||||
ExtraProperties = extraProperties;
|
||||
}
|
||||
|
|
|
@ -8,7 +8,9 @@ BeforeAll{
|
|||
Describe '{{ Name }}' {
|
||||
it '{{ Description }}' {
|
||||
#arrange
|
||||
{{ ValueToCheckVariable }} = {{{ ValueToCheck }}}
|
||||
{{ #Parameters }}
|
||||
{{ Key }} = {{{ Value }}}
|
||||
{{ /Parameters}}
|
||||
|
||||
#act
|
||||
{{ ActualValueVariable }} = {{GetValueFunctionName}} {{{GetValueFunctionParameterList}}}
|
||||
|
|
Загрузка…
Ссылка в новой задаче