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 UnauthorizedException : Exception{}
public class UnsupportedMechanismException : Exception{}
/// <summary> /// <summary>
/// THIS IS VERY INCOMPLETE! /// THIS IS VERY INCOMPLETE!

View File

@ -2,6 +2,7 @@
using FabAccessAPI.Schema; using FabAccessAPI.Schema;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace FabAccessAPI { namespace FabAccessAPI {
@ -14,6 +15,8 @@ namespace FabAccessAPI {
private Machines? _machines = null; private Machines? _machines = null;
#endregion #endregion
public TcpRpcClient? RpcClient => _rpcClient;
#region Log #region Log
private static readonly log4net.ILog _Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private static readonly log4net.ILog _Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion #endregion
@ -40,9 +43,11 @@ namespace FabAccessAPI {
var authCap = await _bootstrapCap.Auth(); var authCap = await _bootstrapCap.Auth();
_auth = new Auth(authCap); _auth = new Auth(authCap);
var mechs = await _auth.GetMechanisms().ConfigureAwait(false); 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); await _auth.Authenticate(mech, kvs).ConfigureAwait(false);
} }

View File

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

View File

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