diff --git a/src/ApiService/ApiService/TestHooks/EventsTestHooks.cs b/src/ApiService/ApiService/TestHooks/EventsTestHooks.cs index f294ca22dd..09bf8307ac 100644 --- a/src/ApiService/ApiService/TestHooks/EventsTestHooks.cs +++ b/src/ApiService/ApiService/TestHooks/EventsTestHooks.cs @@ -25,10 +25,8 @@ public async Task LogEvent([HttpTrigger(AuthorizationLevel.Ano _log.Info("Log event"); var s = await req.ReadAsStringAsync(); - var baseEvent = JsonSerializer.Deserialize(s!, EntityConverter.GetJsonSerializerOptions()); - var t = BaseEvent.GetTypeInfo(baseEvent!.GetEventType()); - var evt = (JsonSerializer.Deserialize(s!, t, EntityConverter.GetJsonSerializerOptions())) as BaseEvent; - _events.LogEvent(evt!); + var msg = JsonSerializer.Deserialize(s!, EntityConverter.GetJsonSerializerOptions()); + _events.LogEvent(msg!.Event); var resp = req.CreateResponse(HttpStatusCode.OK); return resp; } diff --git a/src/ApiService/ApiService/TestHooks/ExtensionsTestHooks.cs b/src/ApiService/ApiService/TestHooks/ExtensionsTestHooks.cs new file mode 100644 index 0000000000..e33ebeb049 --- /dev/null +++ b/src/ApiService/ApiService/TestHooks/ExtensionsTestHooks.cs @@ -0,0 +1,53 @@ +using System.Net; +using System.Threading.Tasks; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Azure.Functions.Worker.Http; +using Microsoft.OneFuzz.Service; + + +#if DEBUG +namespace ApiService.TestHooks { + + public class ExtensionsTestHooks { + + private readonly ILogTracer _log; + private readonly IConfigOperations _configOps; + private readonly IExtensions _extensions; + + public ExtensionsTestHooks(ILogTracer log, IConfigOperations configOps, IExtensions extensions) { + _log = log.WithTag("TestHooks", nameof(ExtensionsTestHooks)); + _configOps = configOps; + _extensions = extensions; + } + + [Function("GenericExtensionsHook")] + public async Task GenericExtensions([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/extensions/genericExtensions")] HttpRequestData req) { + _log.Info("Get Generic extensions"); + + var query = UriExtension.GetQueryComponents(req.Url); + + Os os; + if (query["os"].ToLowerInvariant() == "windows") { + os = Os.Windows; + } else if (query["os"].ToLowerInvariant() == "linux") { + os = Os.Linux; + } else { + var err = req.CreateResponse(HttpStatusCode.BadRequest); + await err.WriteAsJsonAsync(new { error = $"unsupported os {query["os"]}" }); + return err; + } + + var ext = await (_extensions as Extensions)!.GenericExtensions(query["region"], os); + var resp = req.CreateResponse(HttpStatusCode.OK); + + await resp.WriteAsJsonAsync(ext); + + return resp; + } + + + + } +} + +#endif diff --git a/src/ApiService/ApiService/TestHooks/IpOperationsTestHooks.cs b/src/ApiService/ApiService/TestHooks/IpOperationsTestHooks.cs new file mode 100644 index 0000000000..469eca2dd7 --- /dev/null +++ b/src/ApiService/ApiService/TestHooks/IpOperationsTestHooks.cs @@ -0,0 +1,95 @@ +using System.Net; +using System.Threading.Tasks; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Azure.Functions.Worker.Http; +using Microsoft.OneFuzz.Service; + + +#if DEBUG +namespace ApiService.TestHooks { + public class IpOperationsTestHooks { + private readonly ILogTracer _log; + private readonly IConfigOperations _configOps; + private readonly IIpOperations _ipOps; + + public IpOperationsTestHooks(ILogTracer log, IConfigOperations configOps, IIpOperations ipOps) { + _log = log.WithTag("TestHooks", nameof(IpOperationsTestHooks)); + _configOps = configOps; + _ipOps = ipOps; + } + + [Function("GetPublicNicTestHook")] + public async Task GetPublicNic([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/ipOps/publicNic")] HttpRequestData req) { + _log.Info("Get public nic"); + + var query = UriExtension.GetQueryComponents(req.Url); + + var rg = query["rg"]; + var name = query["name"]; + + var nic = await _ipOps.GetPublicNic(rg, name); + + var resp = req.CreateResponse(HttpStatusCode.OK); + await resp.WriteStringAsync(nic.Get().Value.Data.Name); + return resp; + } + + [Function("GetIpTestHook")] + public async Task GetIp([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/ipOps/ip")] HttpRequestData req) { + _log.Info("Get public nic"); + + var query = UriExtension.GetQueryComponents(req.Url); + + var rg = query["rg"]; + var name = query["name"]; + + var ip = await _ipOps.GetIp(rg, name); + + var resp = req.CreateResponse(HttpStatusCode.OK); + await resp.WriteStringAsync(ip.Get().Value.Data.Name); + return resp; + } + + + [Function("DeletePublicNicTestHook")] + public async Task DeletePublicNic([HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "testhooks/ipOps/publicNic")] HttpRequestData req) { + _log.Info("Get public nic"); + + var query = UriExtension.GetQueryComponents(req.Url); + + var rg = query["rg"]; + var name = query["name"]; + var yes = UriExtension.GetBoolValue("force", query, false); + + if (yes) { + await _ipOps.DeleteNic(rg, name); + var resp = req.CreateResponse(HttpStatusCode.OK); + return resp; + } else { + var resp = req.CreateResponse(HttpStatusCode.NotModified); + return resp; + } + } + + [Function("DeleteIpTestHook")] + public async Task DeleteIp([HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "testhooks/ipOps/ip")] HttpRequestData req) { + _log.Info("Get public nic"); + + var query = UriExtension.GetQueryComponents(req.Url); + + var rg = query["rg"]; + var name = query["name"]; + var yes = UriExtension.GetBoolValue("force", query, false); + + if (yes) { + await _ipOps.DeleteIp(rg, name); + var resp = req.CreateResponse(HttpStatusCode.OK); + return resp; + } else { + var resp = req.CreateResponse(HttpStatusCode.NotModified); + return resp; + } + } + } +} +#endif diff --git a/src/ApiService/Tests/OrmModelsTest.cs b/src/ApiService/Tests/OrmModelsTest.cs index 8468cc4c4e..a4b98c9f81 100644 --- a/src/ApiService/Tests/OrmModelsTest.cs +++ b/src/ApiService/Tests/OrmModelsTest.cs @@ -901,6 +901,10 @@ public bool EventNodeCreated(EventNodeCreated e) { return Test(e); } + [Property] + public bool EventMessage(EventMessage e) { + return Test(e); + } /* //Sample function on how repro a failing test run, using Replay