improved tests and error handling

This commit is contained in:
Kai Jan Kriegel 2020-12-21 13:44:29 +01:00
parent 1e84827c13
commit 7bb1d8d8a5
4 changed files with 45 additions and 18 deletions

View File

@ -98,6 +98,7 @@ namespace FabAccessAPI {
}
public class UnauthorizedException : Exception{}
public class UnsupportedMechanismException : Exception{}
/// <summary>
/// THIS IS VERY INCOMPLETE!

View File

@ -2,6 +2,7 @@
using FabAccessAPI.Schema;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace FabAccessAPI {
@ -14,6 +15,8 @@ namespace FabAccessAPI {
private Machines? _machines = null;
#endregion
public TcpRpcClient? RpcClient => _rpcClient;
#region Log
private static readonly log4net.ILog _Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
@ -40,9 +43,11 @@ namespace FabAccessAPI {
var authCap = await _bootstrapCap.Auth();
_auth = new Auth(authCap);
var mechs = await _auth.GetMechanisms().ConfigureAwait(false);
_Log.Debug($"The Server supports the following auth mechs: {mechs}");
_Log.Debug($"The Server supports the following auth mechs: {string.Join(", ", mechs)}");
// TODO: Check that the requested auth mech is actually available.
if (!mechs.Contains(mech)) {
throw new UnsupportedMechanismException();
}
await _auth.Authenticate(mech, kvs).ConfigureAwait(false);
}

View File

@ -5,6 +5,9 @@ using System.Linq;
using System.Threading.Tasks;
namespace FabAccessAPI {
public class MachineException : Exception { }
/// <summary>
/// Wraps a capability for accessing the Machines subsystem of BFFH
/// </summary>
@ -43,7 +46,7 @@ namespace FabAccessAPI {
var mach = (await _machinesCap.GetMachine(name).ConfigureAwait(false)).Item1;
if (mach == null) {
//TODO: Throw a more specific exception!
throw new Exception();
throw new MachineException();
}
return new Machine(mach);
}

View File

@ -10,9 +10,15 @@ using Microsoft.Extensions.Logging;
namespace FabAccessAPI_Test {
public class Tests {
#region Log
private static readonly log4net.ILog _Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
private static ILoggerFactory _loggerFactory;
private Connection _connection;
[OneTimeSetUp]
public void Setup() {
public void InitialSetup() {
XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
_loggerFactory = LoggerFactory.Create(builder => {
builder
@ -23,27 +29,39 @@ namespace FabAccessAPI_Test {
Logging.LoggerFactory = _loggerFactory;
}
[SetUp]
public void Setup() {
var rpcClient = new TcpRpcClient();
rpcClient.Connect("127.0.0.1", 59661);
_connection = new Connection(rpcClient);
}
[TearDown]
public void Teardown() {
_connection.RpcClient?.Dispose();
_connection = null;
}
[Test]
public void Connect() {
var rpcClient = new TcpRpcClient();
rpcClient.Connect("::1", 59661);
var con = new Connection(rpcClient);
Assert.AreEqual(ConnectionState.Active, rpcClient.State);
Assert.AreEqual(ConnectionState.Active, _connection.RpcClient.State);
}
[Test]
public async Task Authenticate() {
await _connection.Auth("PLAIN", new Dictionary<string, object>{{"Username", "Testuser"}, {"Password", "secret"}});
}
[Test]
public async Task Machines() {
var rpcClient = new TcpRpcClient();
rpcClient.Connect("::1", 59661);
var con = new Connection(rpcClient);
await _connection.Auth("PLAIN", new Dictionary<string, object>{{"Username", "Testuser"}, {"Password", "secret"}});
var machines = await _connection.AccessMachines();
var machines = await con.AccessMachines();
var testmachine = await machines.GetMachine("Testmachine");
Assert.NotNull(testmachine);
var minfo = await testmachine.GetMInfo();
Assert.NotNull(minfo);
Console.WriteLine($"Name: {minfo.Name}, Description: {minfo.Description}, State: {minfo.State.ToString()}");
var testmachine = await machines.GetMachine("Testmachine");
Assert.NotNull(testmachine);
var minfo = await testmachine.GetMInfo();
Assert.NotNull(minfo);
_Log.Info($"Name: {minfo.Name}, Description: {minfo.Description}, State: {minfo.State.ToString()}");
}
}
}