formatting (#1847)

This commit is contained in:
Cheick Keita
2022-04-26 10:22:35 -07:00
committed by GitHub
parent 8003b1d3e0
commit 5f8e381fcb
55 changed files with 1097 additions and 1915 deletions

View File

@ -1,21 +1,18 @@
using FsCheck;
using FsCheck.Xunit;
using Xunit.Abstractions;
using Microsoft.OneFuzz.Service;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
using System;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Security;
using System.Text.Json;
using FsCheck;
using FsCheck.Xunit;
using Microsoft.OneFuzz.Service;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
using Xunit.Abstractions;
namespace Tests
{
namespace Tests {
public class OrmGenerators
{
public static Gen<BaseEvent> BaseEvent()
{
public class OrmGenerators {
public static Gen<BaseEvent> BaseEvent() {
return Gen.OneOf(new[] {
Arb.Generate<EventNodeHeartbeat>().Select(e => e as BaseEvent),
Arb.Generate<EventTaskHeartbeat>().Select(e => e as BaseEvent),
@ -30,15 +27,13 @@ namespace Tests
});
}
public static Gen<Uri> Uri()
{
public static Gen<Uri> Uri() {
return Arb.Generate<IPv4Address>().Select(
arg => new Uri($"https://{arg.Item.ToString()}:8080")
);
}
public static Gen<WebhookMessageLog> WebhookMessageLog()
{
public static Gen<WebhookMessageLog> WebhookMessageLog() {
return Arb.Generate<Tuple<Tuple<Guid, BaseEvent, Guid, string, Guid>, Tuple<WebhookMessageState, int>>>().Select(
arg => new WebhookMessageLog(
EventId: arg.Item1.Item1,
@ -52,8 +47,7 @@ namespace Tests
));
}
public static Gen<Node> Node()
{
public static Gen<Node> Node() {
return Arb.Generate<Tuple<Tuple<DateTimeOffset?, string, Guid?, Guid, NodeState>, Tuple<Guid?, DateTimeOffset, string, bool, bool, bool>>>().Select(
arg => new Node(
InitializedAt: arg.Item1.Item1,
@ -69,8 +63,7 @@ namespace Tests
DebugKeepNode: arg.Item2.Item6));
}
public static Gen<ProxyForward> ProxyForward()
{
public static Gen<ProxyForward> ProxyForward() {
return Arb.Generate<Tuple<Tuple<string, int, Guid, Guid, Guid?, int>, Tuple<IPv4Address, DateTimeOffset>>>().Select(
arg =>
new ProxyForward(
@ -86,8 +79,7 @@ namespace Tests
);
}
public static Gen<Proxy> Proxy()
{
public static Gen<Proxy> Proxy() {
return Arb.Generate<Tuple<Tuple<string, Guid, DateTimeOffset?, VmState, Authentication, string?, Error?>, Tuple<string, ProxyHeartbeat?, bool>>>().Select(
arg =>
new Proxy(
@ -105,8 +97,7 @@ namespace Tests
);
}
public static Gen<EventMessage> EventMessage()
{
public static Gen<EventMessage> EventMessage() {
return Arb.Generate<Tuple<Guid, BaseEvent, Guid, string>>().Select(
arg =>
new EventMessage(
@ -119,8 +110,7 @@ namespace Tests
);
}
public static Gen<NetworkConfig> NetworkConfig()
{
public static Gen<NetworkConfig> NetworkConfig() {
return Arb.Generate<Tuple<IPv4Address, IPv4Address>>().Select(
arg =>
new NetworkConfig(
@ -130,8 +120,7 @@ namespace Tests
);
}
public static Gen<NetworkSecurityGroupConfig> NetworkSecurityGroupConfig()
{
public static Gen<NetworkSecurityGroupConfig> NetworkSecurityGroupConfig() {
return Arb.Generate<Tuple<string[], IPv4Address[]>>().Select(
arg =>
new NetworkSecurityGroupConfig(
@ -141,8 +130,7 @@ namespace Tests
);
}
public static Gen<InstanceConfig> InstanceConfig()
{
public static Gen<InstanceConfig> InstanceConfig() {
return Arb.Generate<Tuple<
Tuple<string, Guid[]?, bool?, string[], NetworkConfig, NetworkSecurityGroupConfig, AzureVmExtensionConfig?>,
Tuple<string, IDictionary<string, ApiAccessRule>?, IDictionary<Guid, Guid[]>?, IDictionary<string, string>?, IDictionary<string, string>?>>>().Select(
@ -165,8 +153,7 @@ namespace Tests
);
}
public static Gen<Task> Task()
{
public static Gen<Task> Task() {
return Arb.Generate<Tuple<
Tuple<Guid, Guid, TaskState, Os, TaskConfig, Error?, Authentication?>,
Tuple<DateTimeOffset?, DateTimeOffset?, UserInfo?>>>().Select(
@ -186,8 +173,7 @@ namespace Tests
)
);
}
public static Gen<Scaleset> Scaleset()
{
public static Gen<Scaleset> Scaleset() {
return Arb.Generate<Tuple<
Tuple<string, Guid, ScalesetState, Authentication?, string, string, string>,
Tuple<int, bool, bool, bool, List<ScalesetNodeState>, Guid?, Guid?>,
@ -216,8 +202,7 @@ namespace Tests
}
public static Gen<Webhook> Webhook()
{
public static Gen<Webhook> Webhook() {
return Arb.Generate<Tuple<Guid, string, Uri?, List<EventType>, string, WebhookMessageFormat>>().Select(
arg =>
new Webhook(
@ -231,8 +216,7 @@ namespace Tests
);
}
public static Gen<WebhookMessage> WebhookMessage()
{
public static Gen<WebhookMessage> WebhookMessage() {
return Arb.Generate<Tuple<Guid, BaseEvent, Guid, string, Guid>>().Select(
arg =>
new WebhookMessage(
@ -246,8 +230,7 @@ namespace Tests
); ;
}
public static Gen<WebhookMessageEventGrid> WebhookMessageEventGrid()
{
public static Gen<WebhookMessageEventGrid> WebhookMessageEventGrid() {
return Arb.Generate<Tuple<string, string, BaseEvent, Guid, DateTimeOffset>>().Select(
arg =>
new WebhookMessageEventGrid(
@ -264,8 +247,7 @@ namespace Tests
public static Gen<Report> Report()
{
public static Gen<Report> Report() {
return Arb.Generate<Tuple<string, BlobRef, List<string>, Guid, int>>().Select(
arg =>
new Report(
@ -292,16 +274,14 @@ namespace Tests
);
}
public static Gen<Container> Container()
{
public static Gen<Container> Container() {
return Arb.Generate<Tuple<NonNull<string>>>().Select(
arg => new Container(string.Join("", arg.Item1.Get.Where(c => char.IsLetterOrDigit(c) || c == '-'))!)
);
}
public static Gen<Notification> Notification()
{
public static Gen<Notification> Notification() {
return Arb.Generate<Tuple<Container, Guid, NotificationTemplate>>().Select(
arg => new Notification(
Container: arg.Item1,
@ -311,8 +291,7 @@ namespace Tests
);
}
public static Gen<Job> Job()
{
public static Gen<Job> Job() {
return Arb.Generate<Tuple<Guid, JobState, JobConfig, string?, DateTimeOffset?, List<JobTaskInfo>?, UserInfo>>().Select(
arg => new Job(
JobId: arg.Item1,
@ -325,106 +304,85 @@ namespace Tests
}
}
public class OrmArb
{
public static Arbitrary<Uri> Uri()
{
public class OrmArb {
public static Arbitrary<Uri> Uri() {
return Arb.From(OrmGenerators.Uri());
}
public static Arbitrary<BaseEvent> BaseEvent()
{
public static Arbitrary<BaseEvent> BaseEvent() {
return Arb.From(OrmGenerators.BaseEvent());
}
public static Arbitrary<Node> Node()
{
public static Arbitrary<Node> Node() {
return Arb.From(OrmGenerators.Node());
}
public static Arbitrary<ProxyForward> ProxyForward()
{
public static Arbitrary<ProxyForward> ProxyForward() {
return Arb.From(OrmGenerators.ProxyForward());
}
public static Arbitrary<Proxy> Proxy()
{
public static Arbitrary<Proxy> Proxy() {
return Arb.From(OrmGenerators.Proxy());
}
public static Arbitrary<EventMessage> EventMessage()
{
public static Arbitrary<EventMessage> EventMessage() {
return Arb.From(OrmGenerators.EventMessage());
}
public static Arbitrary<NetworkConfig> NetworkConfig()
{
public static Arbitrary<NetworkConfig> NetworkConfig() {
return Arb.From(OrmGenerators.NetworkConfig());
}
public static Arbitrary<NetworkSecurityGroupConfig> NetworkSecurityConfig()
{
public static Arbitrary<NetworkSecurityGroupConfig> NetworkSecurityConfig() {
return Arb.From(OrmGenerators.NetworkSecurityGroupConfig());
}
public static Arbitrary<InstanceConfig> InstanceConfig()
{
public static Arbitrary<InstanceConfig> InstanceConfig() {
return Arb.From(OrmGenerators.InstanceConfig());
}
public static Arbitrary<WebhookMessageLog> WebhookMessageLog()
{
public static Arbitrary<WebhookMessageLog> WebhookMessageLog() {
return Arb.From(OrmGenerators.WebhookMessageLog());
}
public static Arbitrary<Task> Task()
{
public static Arbitrary<Task> Task() {
return Arb.From(OrmGenerators.Task());
}
public static Arbitrary<Scaleset> Scaleset()
{
public static Arbitrary<Scaleset> Scaleset() {
return Arb.From(OrmGenerators.Scaleset());
}
public static Arbitrary<Webhook> Webhook()
{
public static Arbitrary<Webhook> Webhook() {
return Arb.From(OrmGenerators.Webhook());
}
public static Arbitrary<WebhookMessage> WebhookMessage()
{
public static Arbitrary<WebhookMessage> WebhookMessage() {
return Arb.From(OrmGenerators.WebhookMessage());
}
public static Arbitrary<Report> Report()
{
public static Arbitrary<Report> Report() {
return Arb.From(OrmGenerators.Report());
}
public static Arbitrary<Container> Container()
{
public static Arbitrary<Container> Container() {
return Arb.From(OrmGenerators.Container());
}
public static Arbitrary<Notification> Notification()
{
public static Arbitrary<Notification> Notification() {
return Arb.From(OrmGenerators.Notification());
}
public static Arbitrary<WebhookMessageEventGrid> WebhookMessageEventGrid()
{
public static Arbitrary<WebhookMessageEventGrid> WebhookMessageEventGrid() {
return Arb.From(OrmGenerators.WebhookMessageEventGrid());
}
public static Arbitrary<Job> Job()
{
public static Arbitrary<Job> Job() {
return Arb.From(OrmGenerators.Job());
}
}
public static class EqualityComparison
{
public static class EqualityComparison {
private static HashSet<Type> _baseTypes = new HashSet<Type>(
new[]{
typeof(byte),
@ -443,26 +401,20 @@ namespace Tests
typeof(DateTimeOffset?),
typeof(SecureString)
});
static bool IEnumerableEqual<T>(IEnumerable<T>? a, IEnumerable<T>? b)
{
if (a is null && b is null)
{
static bool IEnumerableEqual<T>(IEnumerable<T>? a, IEnumerable<T>? b) {
if (a is null && b is null) {
return true;
}
if (a!.Count() != b!.Count())
{
if (a!.Count() != b!.Count()) {
return false;
}
if (a!.Count() == 0 && b!.Count() == 0)
{
if (a!.Count() == 0 && b!.Count() == 0) {
return true;
}
foreach (var v in a!.Zip(b!))
{
if (!AreEqual(v.First, v.Second))
{
foreach (var v in a!.Zip(b!)) {
if (!AreEqual(v.First, v.Second)) {
return false;
}
}
@ -470,8 +422,7 @@ namespace Tests
return true;
}
static bool IDictionaryEqual<TKey, TValue>(IDictionary<TKey, TValue>? a, IDictionary<TKey, TValue>? b, Func<TValue, TValue, bool> cmp)
{
static bool IDictionaryEqual<TKey, TValue>(IDictionary<TKey, TValue>? a, IDictionary<TKey, TValue>? b, Func<TValue, TValue, bool> cmp) {
if (a is null && b is null)
return true;
@ -484,8 +435,7 @@ namespace Tests
return a!.Any(v => cmp(v.Value, b[v.Key]));
}
static bool IDictionaryEqual<TKey, TValue>(IDictionary<TKey, TValue>? a, IDictionary<TKey, TValue>? b)
{
static bool IDictionaryEqual<TKey, TValue>(IDictionary<TKey, TValue>? a, IDictionary<TKey, TValue>? b) {
if (a is null && b is null)
return true;
@ -499,8 +449,7 @@ namespace Tests
}
public static bool AreEqual<T>(T r1, T r2)
{
public static bool AreEqual<T>(T r1, T r2) {
var t = typeof(T);
if (r1 is null && r2 is null)
@ -509,8 +458,7 @@ namespace Tests
if (_baseTypes.Contains(t))
return r1!.Equals(r2);
foreach (var p in t.GetProperties())
{
foreach (var p in t.GetProperties()) {
var v1 = p.GetValue(r1);
var v2 = p.GetValue(r2);
var tt = p.PropertyType;
@ -524,14 +472,12 @@ namespace Tests
if (_baseTypes.Contains(tt) && !v1!.Equals(v2))
return false;
if (tt.GetInterface("IEnumerable") is not null)
{
if (tt.GetInterface("IEnumerable") is not null) {
if (!IEnumerableEqual(v1 as IEnumerable<Object>, v2 as IEnumerable<Object>))
return false;
}
if (tt.GetInterface("IDictionary") is not null)
{
if (tt.GetInterface("IDictionary") is not null) {
if (!IDictionaryEqual(v1 as IDictionary<Object, Object>, v2 as IDictionary<Object, Object>))
return false;
}
@ -540,19 +486,16 @@ namespace Tests
}
}
public class OrmModelsTest
{
public class OrmModelsTest {
EntityConverter _converter = new EntityConverter();
ITestOutputHelper _output;
public OrmModelsTest(ITestOutputHelper output)
{
public OrmModelsTest(ITestOutputHelper output) {
Arb.Register<OrmArb>();
_output = output;
}
bool Test<T>(T e) where T : EntityBase
{
bool Test<T>(T e) where T : EntityBase {
var v = _converter.ToTableEntity(e);
var r = _converter.ToRecord<T>(v);
return EqualityComparison.AreEqual(e, r);
@ -560,64 +503,54 @@ namespace Tests
}
[Property]
public bool Node(Node node)
{
public bool Node(Node node) {
return Test(node);
}
[Property]
public bool ProxyForward(ProxyForward proxyForward)
{
public bool ProxyForward(ProxyForward proxyForward) {
return Test(proxyForward);
}
[Property]
public bool Proxy(Proxy proxy)
{
public bool Proxy(Proxy proxy) {
return Test(proxy);
}
[Property]
public bool Task(Task task)
{
public bool Task(Task task) {
return Test(task);
}
[Property]
public bool InstanceConfig(InstanceConfig cfg)
{
public bool InstanceConfig(InstanceConfig cfg) {
return Test(cfg);
}
[Property]
public bool Scaleset(Scaleset ss)
{
public bool Scaleset(Scaleset ss) {
return Test(ss);
}
[Property]
public bool WebhookMessageLog(WebhookMessageLog log)
{
public bool WebhookMessageLog(WebhookMessageLog log) {
return Test(log);
}
[Property]
public bool Webhook(Webhook wh)
{
public bool Webhook(Webhook wh) {
return Test(wh);
}
[Property]
public bool Notification(Notification n)
{
public bool Notification(Notification n) {
return Test(n);
}
[Property]
public bool Job(Job j)
{
public bool Job(Job j) {
return Test(j);
}
@ -635,244 +568,205 @@ namespace Tests
}
public class OrmJsonSerialization
{
public class OrmJsonSerialization {
JsonSerializerOptions _opts = EntityConverter.GetJsonSerializerOptions();
ITestOutputHelper _output;
public OrmJsonSerialization(ITestOutputHelper output)
{
public OrmJsonSerialization(ITestOutputHelper output) {
Arb.Register<OrmArb>();
_output = output;
}
string serialize<T>(T x)
{
string serialize<T>(T x) {
return JsonSerializer.Serialize(x, _opts);
}
T? deserialize<T>(string json)
{
T? deserialize<T>(string json) {
return JsonSerializer.Deserialize<T>(json, _opts);
}
bool Test<T>(T v)
{
bool Test<T>(T v) {
var j = serialize(v);
var r = deserialize<T>(j);
return EqualityComparison.AreEqual(v, r);
}
[Property]
public bool Node(Node node)
{
public bool Node(Node node) {
return Test(node);
}
[Property]
public bool ProxyForward(ProxyForward proxyForward)
{
public bool ProxyForward(ProxyForward proxyForward) {
return Test(proxyForward);
}
[Property]
public bool Proxy(Proxy proxy)
{
public bool Proxy(Proxy proxy) {
return Test(proxy);
}
[Property]
public bool Task(Task task)
{
public bool Task(Task task) {
return Test(task);
}
[Property]
public bool InstanceConfig(InstanceConfig cfg)
{
public bool InstanceConfig(InstanceConfig cfg) {
return Test(cfg);
}
[Property]
public bool Scaleset(Scaleset ss)
{
public bool Scaleset(Scaleset ss) {
return Test(ss);
}
[Property]
public bool WebhookMessageLog(WebhookMessageLog log)
{
public bool WebhookMessageLog(WebhookMessageLog log) {
return Test(log);
}
[Property]
public bool Webhook(Webhook wh)
{
public bool Webhook(Webhook wh) {
return Test(wh);
}
[Property]
public bool WebhookMessageEventGrid(WebhookMessageEventGrid evt)
{
public bool WebhookMessageEventGrid(WebhookMessageEventGrid evt) {
return Test(evt);
}
[Property]
public bool WebhookMessage(WebhookMessage msg)
{
public bool WebhookMessage(WebhookMessage msg) {
return Test(msg);
}
[Property]
public bool TaskHeartbeatEntry(TaskHeartbeatEntry e)
{
public bool TaskHeartbeatEntry(TaskHeartbeatEntry e) {
return Test(e);
}
[Property]
public bool NodeCommand(NodeCommand e)
{
public bool NodeCommand(NodeCommand e) {
return Test(e);
}
[Property]
public bool NodeTasks(NodeTasks e)
{
public bool NodeTasks(NodeTasks e) {
return Test(e);
}
[Property]
public bool ProxyHeartbeat(ProxyHeartbeat e)
{
public bool ProxyHeartbeat(ProxyHeartbeat e) {
return Test(e);
}
[Property]
public bool ProxyConfig(ProxyConfig e)
{
public bool ProxyConfig(ProxyConfig e) {
return Test(e);
}
[Property]
public bool TaskDetails(TaskDetails e)
{
public bool TaskDetails(TaskDetails e) {
return Test(e);
}
[Property]
public bool TaskVm(TaskVm e)
{
public bool TaskVm(TaskVm e) {
return Test(e);
}
[Property]
public bool TaskPool(TaskPool e)
{
public bool TaskPool(TaskPool e) {
return Test(e);
}
[Property]
public bool TaskContainers(TaskContainers e)
{
public bool TaskContainers(TaskContainers e) {
return Test(e);
}
[Property]
public bool TaskConfig(TaskConfig e)
{
public bool TaskConfig(TaskConfig e) {
return Test(e);
}
[Property]
public bool TaskEventSummary(TaskEventSummary e)
{
public bool TaskEventSummary(TaskEventSummary e) {
return Test(e);
}
[Property]
public bool NodeAssignment(NodeAssignment e)
{
public bool NodeAssignment(NodeAssignment e) {
return Test(e);
}
[Property]
public bool KeyvaultExtensionConfig(KeyvaultExtensionConfig e)
{
public bool KeyvaultExtensionConfig(KeyvaultExtensionConfig e) {
return Test(e);
}
[Property]
public bool AzureMonitorExtensionConfig(AzureMonitorExtensionConfig e)
{
public bool AzureMonitorExtensionConfig(AzureMonitorExtensionConfig e) {
return Test(e);
}
[Property]
public bool AzureVmExtensionConfig(AzureVmExtensionConfig e)
{
public bool AzureVmExtensionConfig(AzureVmExtensionConfig e) {
return Test(e);
}
[Property]
public bool NetworkConfig(NetworkConfig e)
{
public bool NetworkConfig(NetworkConfig e) {
return Test(e);
}
[Property]
public bool NetworkSecurityGroupConfig(NetworkSecurityGroupConfig e)
{
public bool NetworkSecurityGroupConfig(NetworkSecurityGroupConfig e) {
return Test(e);
}
[Property]
public bool Report(Report e)
{
public bool Report(Report e) {
return Test(e);
}
[Property]
public bool Notification(Notification e)
{
public bool Notification(Notification e) {
return Test(e);
}
[Property]
public bool NoReproReport(NoReproReport e)
{
public bool NoReproReport(NoReproReport e) {
return Test(e);
}
[Property]
public bool CrashTestResult(CrashTestResult e)
{
public bool CrashTestResult(CrashTestResult e) {
return Test(e);
}
[Property]
public bool NotificationTemplate(NotificationTemplate e)
{
public bool NotificationTemplate(NotificationTemplate e) {
return Test(e);
}
[Property]
public bool RegressionReportOrReport(RegressionReportOrReport e)
{
public bool RegressionReportOrReport(RegressionReportOrReport e) {
return Test(e);
}
[Property]
public bool Job(Job e)
{
public bool Job(Job e) {
return Test(e);
}