Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stas committed May 3, 2022
1 parent fd28ba3 commit 49ac48c
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/ApiService/ApiService/TestHooks/EventsTestHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ public async Task<HttpResponseData> LogEvent([HttpTrigger(AuthorizationLevel.Ano
_log.Info("Log event");

var s = await req.ReadAsStringAsync();
var baseEvent = JsonSerializer.Deserialize<BaseEvent>(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<EventMessage>(s!, EntityConverter.GetJsonSerializerOptions());
_events.LogEvent(msg!.Event);
var resp = req.CreateResponse(HttpStatusCode.OK);
return resp;
}
Expand Down
53 changes: 53 additions & 0 deletions src/ApiService/ApiService/TestHooks/ExtensionsTestHooks.cs
Original file line number Diff line number Diff line change
@@ -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<HttpResponseData> 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
95 changes: 95 additions & 0 deletions src/ApiService/ApiService/TestHooks/IpOperationsTestHooks.cs
Original file line number Diff line number Diff line change
@@ -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<HttpResponseData> 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<HttpResponseData> 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<HttpResponseData> 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<HttpResponseData> 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
4 changes: 4 additions & 0 deletions src/ApiService/Tests/OrmModelsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 49ac48c

Please sign in to comment.