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

Commit

Permalink
added tests and used to validate the behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
stas committed May 16, 2022
1 parent 2e4209d commit 399c26b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ jobs:
- name: Setup .NET Core SDK
uses: actions/setup-dotnet@v2
with:
dotnet-version: '6.0.202'
dotnet-version: '6.0.300'
- name: Install dependencies
run: |
cd src/ApiService/
Expand Down
2 changes: 2 additions & 0 deletions src/ApiService/ApiService/OneFuzzTypes/ReturnTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public struct OneFuzzResultVoid {

public Error ErrorV => error;

public OneFuzzResultVoid() => (error, isOk) = (NoError, true);

private OneFuzzResultVoid(ErrorCode errorCode, string[] errors) => (error, isOk) = (new Error(errorCode, errors), false);

private OneFuzzResultVoid(Error err) => (error, isOk) = (err, false);
Expand Down
68 changes: 68 additions & 0 deletions src/ApiService/ApiService/TestHooks/VmssTestHooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.OneFuzz.Service;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;

#if DEBUG
namespace ApiService.TestHooks {
public class VmssTestHooks {

private readonly ILogTracer _log;
private readonly IConfigOperations _configOps;
private readonly IVmssOperations _vmssOps;

public VmssTestHooks(ILogTracer log, IConfigOperations configOps, IVmssOperations vmssOps) {
_log = log.WithTag("TestHooks", nameof(VmssTestHooks));
_configOps = configOps; ;
_vmssOps = vmssOps; ;
}


[Function("ListInstanceIdsTesHook")]
public async Task<HttpResponseData> ListInstanceIds([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/vmssOperations/listInstanceIds")] HttpRequestData req) {
_log.Info("list instance ids");
var query = UriExtension.GetQueryComponents(req.Url);
var name = UriExtension.GetGuid("name", query) ?? throw new Exception("name must be set");
var ids = await _vmssOps.ListInstanceIds(name);

var json = JsonSerializer.Serialize(ids, EntityConverter.GetJsonSerializerOptions());
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteStringAsync(json);
return resp;
}

[Function("GetInstanceIdsTesHook")]
public async Task<HttpResponseData> GetInstanceId([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "testhooks/vmssOperations/getInstanceId")] HttpRequestData req) {
_log.Info("list instance ids");
var query = UriExtension.GetQueryComponents(req.Url);
var name = UriExtension.GetGuid("name", query) ?? throw new Exception("name must be set");
var vmId = UriExtension.GetGuid("vmId", query) ?? throw new Exception("vmId must be set");
var id = await _vmssOps.GetInstanceId(name, vmId);

var json = JsonSerializer.Serialize(id, EntityConverter.GetJsonSerializerOptions());
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteStringAsync(json);
return resp;
}

[Function("UpdateScaleInProtectionTestHook")]
public async Task<HttpResponseData> UpdateScaleInProtection([HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "testhooks/vmssOperations/updateScaleInProtection")] HttpRequestData req) {
_log.Info("list instance ids");
var query = UriExtension.GetQueryComponents(req.Url);
var name = UriExtension.GetGuid("name", query) ?? throw new Exception("name must be set");
var vmId = UriExtension.GetGuid("vmId", query) ?? throw new Exception("vmId must be set");
var protectFromScaleIn = UriExtension.GetBool("protectFromScaleIn", query);
var id = await _vmssOps.UpdateScaleInProtection(name, vmId, protectFromScaleIn);

var json = JsonSerializer.Serialize(id, EntityConverter.GetJsonSerializerOptions());
var resp = req.CreateResponse(HttpStatusCode.OK);
await resp.WriteStringAsync(json);
return resp;
}
}
}

#endif
32 changes: 23 additions & 9 deletions src/ApiService/ApiService/onefuzzlib/VmssOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface IVmssOperations {

public class VmssOperations : IVmssOperations {

string INSTANCE_NOT_FOUND = " is not an active Virtual Machine Scale Set VM instanceId.";

ILogTracer _log;
ICreds _creds;

Expand Down Expand Up @@ -105,7 +107,7 @@ public async Async.Task<IDictionary<Guid, string>> ListInstanceIds(Guid name) {
}
}
}
} catch (CloudException ex) {
} catch (Exception ex) when (ex is RequestFailedException || ex is CloudException) {
_log.Exception(ex, $"vm does not exist {name}");
}
}
Expand All @@ -126,7 +128,7 @@ public async Async.Task<OneFuzzResult<VirtualMachineScaleSetVmResource>> GetInst
return OneFuzzResult<VirtualMachineScaleSetVmResource>.Ok(response);
}
}
} catch (CloudException ex) {
} catch (Exception ex) when (ex is RequestFailedException || ex is CloudException) {
_log.Exception(ex, $"unable to find vm instance: {name}:{vmId}");
return OneFuzzResult<VirtualMachineScaleSetVmResource>.Error(ErrorCode.UNABLE_TO_FIND, $"unable to find vm instance: {name}:{vmId}");
}
Expand Down Expand Up @@ -160,13 +162,25 @@ public async Async.Task<OneFuzzResultVoid> UpdateScaleInProtection(Guid name, Gu

var scaleSet = GetVmssResource(name);
var vmCollection = scaleSet.GetVirtualMachineScaleSetVms();
var r = await vmCollection.CreateOrUpdateAsync(WaitUntil.Started, instanceVm.Data.InstanceId, instanceVm.Data);
if (r.GetRawResponse().IsError) {
var msg = $"failed to update scale in protection on vm {vmId} for scaleset {name}";
_log.WithHttpStatus((r.GetRawResponse().Status, r.GetRawResponse().ReasonPhrase)).Error(msg);
return OneFuzzResultVoid.Error(ErrorCode.UNABLE_TO_UPDATE, msg);
} else {
return OneFuzzResultVoid.Ok();
try {
var r = await vmCollection.CreateOrUpdateAsync(WaitUntil.Started, instanceVm.Data.InstanceId, instanceVm.Data);
if (r.GetRawResponse().IsError) {
var msg = $"failed to update scale in protection on vm {vmId} for scaleset {name}";
_log.WithHttpStatus((r.GetRawResponse().Status, r.GetRawResponse().ReasonPhrase)).Error(msg);
return OneFuzzResultVoid.Error(ErrorCode.UNABLE_TO_UPDATE, msg);
} else {
return OneFuzzResultVoid.Ok();
}
} catch (Exception ex) when (ex is RequestFailedException || ex is CloudException) {

if (ex.Message.Contains(INSTANCE_NOT_FOUND) && protectFromScaleIn == false) {
_log.Info($"Tried to remove scale in protection on node {name} {vmId} but instance no longer exists");
return OneFuzzResultVoid.Ok();
} else {
var msg = $"failed to update scale in protection on vm {vmId} for scaleset {name}";
_log.Exception(ex, msg);
return OneFuzzResultVoid.Error(ErrorCode.UNABLE_TO_UPDATE, ex.Message);
}
}
}
}
Expand Down

0 comments on commit 399c26b

Please sign in to comment.