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