Skip to content

Commit

Permalink
refactor(test): convert all tests to use mocha
Browse files Browse the repository at this point in the history
Mocha is now used for all tests, leading to improved readability,
flexibility in test structure, as well as increased ability to
test asynchronous scenarios.
  • Loading branch information
mbroadst committed Mar 30, 2015
1 parent f162e62 commit 9c2e824
Show file tree
Hide file tree
Showing 22 changed files with 1,309 additions and 1,120 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ TESTS = $(shell find ./test/* -name "*.test.js")
NPM_BIN = ./node_modules/.bin

jshint:
$(NPM_BIN)/jshint lib test tools
$(NPM_BIN)/jshint lib test

fixjsstyle:
fixjsstyle -r lib -r test --strict --jslint_error=all
Expand Down
156 changes: 87 additions & 69 deletions src/hypervisor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
// #include "event_impl.h"
// #include "domain.h"
// #include "node_device.h"
// #include "network.h"
// #include "network_filter.h"
// #include "interface.h"
// #include "secret.h"
// #include "storage_pool.h"
// #include "storage_volume.h"
// #include "error.h"

#include "secret.h"
#include "storage_pool.h"
#include "storage_volume.h"
#include "network.h"
#include "interface.h"
#include "hypervisor.h"

#define HYPERVISOR_NOARGS_WORKER_METHOD(name) \
Expand Down Expand Up @@ -56,7 +56,7 @@ namespace NodeLibvirt {
// static Persistent<String> domain_event_graphics_authscheme_sym;

Persistent<Function> Hypervisor::constructor;

Persistent<FunctionTemplate> Hypervisor::constructor_template;
void Hypervisor::Initialize(Handle<Object> exports)
{
NanScope();
Expand All @@ -66,34 +66,34 @@ void Hypervisor::Initialize(Handle<Object> exports)
t->InstanceTemplate()->SetInternalFieldCount(1);

// methods
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
NODE_SET_PROTOTYPE_METHOD(t, "disconnect", Disconnect);

NODE_SET_PROTOTYPE_METHOD(t, "getCapabilities", GetCapabilities);
NODE_SET_PROTOTYPE_METHOD(t, "getHostname", GetHostname);
NODE_SET_PROTOTYPE_METHOD(t, "getSysInfo", GetSysInfo);
NODE_SET_PROTOTYPE_METHOD(t, "getType", GetType);
NODE_SET_PROTOTYPE_METHOD(t, "getConnectionUri", GetConnectionUri);
NODE_SET_PROTOTYPE_METHOD(t, "getVersion", GetVersion);
NODE_SET_PROTOTYPE_METHOD(t, "getLibVirtVersion", GetLibVirtVersion);
NODE_SET_PROTOTYPE_METHOD(t, "isConnectionEncrypted", IsConnectionEncrypted);
NODE_SET_PROTOTYPE_METHOD(t, "isConnectionSecure", IsConnectionSecure);
NODE_SET_PROTOTYPE_METHOD(t, "isConnectionAlive", IsConnectionAlive);
NODE_SET_PROTOTYPE_METHOD(t, "getMaxVcpus", GetMaxVcpus);
NODE_SET_PROTOTYPE_METHOD(t, "setKeepAlive", SetKeepAlive);
NODE_SET_PROTOTYPE_METHOD(t, "getBaselineCPU", GetBaselineCPU);
NODE_SET_PROTOTYPE_METHOD(t, "compareCPU", CompareCPU);

NODE_SET_PROTOTYPE_METHOD(t, "listDefinedDomains", ListDefinedDomains);
NODE_SET_PROTOTYPE_METHOD(t, "listDefinedNetworks", ListDefinedNetworks);
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
NODE_SET_PROTOTYPE_METHOD(t, "disconnect", Disconnect);

NODE_SET_PROTOTYPE_METHOD(t, "getCapabilities", GetCapabilities);
NODE_SET_PROTOTYPE_METHOD(t, "getHostname", GetHostname);
NODE_SET_PROTOTYPE_METHOD(t, "getSysInfo", GetSysInfo);
NODE_SET_PROTOTYPE_METHOD(t, "getType", GetType);
NODE_SET_PROTOTYPE_METHOD(t, "getConnectionUri", GetConnectionUri);
NODE_SET_PROTOTYPE_METHOD(t, "getVersion", GetVersion);
NODE_SET_PROTOTYPE_METHOD(t, "getLibVirtVersion", GetLibVirtVersion);
NODE_SET_PROTOTYPE_METHOD(t, "isConnectionEncrypted", IsConnectionEncrypted);
NODE_SET_PROTOTYPE_METHOD(t, "isConnectionSecure", IsConnectionSecure);
NODE_SET_PROTOTYPE_METHOD(t, "isConnectionAlive", IsConnectionAlive);
NODE_SET_PROTOTYPE_METHOD(t, "getMaxVcpus", GetMaxVcpus);
NODE_SET_PROTOTYPE_METHOD(t, "setKeepAlive", SetKeepAlive);
NODE_SET_PROTOTYPE_METHOD(t, "getBaselineCPU", GetBaselineCPU);
NODE_SET_PROTOTYPE_METHOD(t, "compareCPU", CompareCPU);

NODE_SET_PROTOTYPE_METHOD(t, "listDefinedDomains", ListDefinedDomains);
NODE_SET_PROTOTYPE_METHOD(t, "listDefinedNetworks", ListDefinedNetworks);
NODE_SET_PROTOTYPE_METHOD(t, "listDefinedStoragePools", ListDefinedStoragePools);
NODE_SET_PROTOTYPE_METHOD(t, "listDefinedInterfaces", ListDefinedInterfaces);
NODE_SET_PROTOTYPE_METHOD(t, "listActiveDomains", ListActiveDomains);
NODE_SET_PROTOTYPE_METHOD(t, "listActiveInterfaces", ListActiveInterfaces);
NODE_SET_PROTOTYPE_METHOD(t, "listActiveNetworks", ListActiveNetworks);
NODE_SET_PROTOTYPE_METHOD(t, "listActiveStoragePools", ListActiveStoragePools);
NODE_SET_PROTOTYPE_METHOD(t, "listNetworkFilters", ListNetworkFilters);
NODE_SET_PROTOTYPE_METHOD(t, "listSecrets", ListSecrets);
NODE_SET_PROTOTYPE_METHOD(t, "listDefinedInterfaces", ListDefinedInterfaces);
NODE_SET_PROTOTYPE_METHOD(t, "listActiveDomains", ListActiveDomains);
NODE_SET_PROTOTYPE_METHOD(t, "listActiveInterfaces", ListActiveInterfaces);
NODE_SET_PROTOTYPE_METHOD(t, "listActiveNetworks", ListActiveNetworks);
NODE_SET_PROTOTYPE_METHOD(t, "listActiveStoragePools", ListActiveStoragePools);
NODE_SET_PROTOTYPE_METHOD(t, "listNetworkFilters", ListNetworkFilters);
NODE_SET_PROTOTYPE_METHOD(t, "listSecrets", ListSecrets);

NODE_SET_PROTOTYPE_METHOD(t, "getNumberOfDefinedDomains", GetNumberOfDefinedDomains);
NODE_SET_PROTOTYPE_METHOD(t, "getNumberOfDefinedInterfaces", GetNumberOfDefinedInterfaces);
Expand All @@ -106,13 +106,45 @@ void Hypervisor::Initialize(Handle<Object> exports)
NODE_SET_PROTOTYPE_METHOD(t, "getNumberOfSecrets", GetNumberOfSecrets);
NODE_SET_PROTOTYPE_METHOD(t, "getNumberOfActiveStoragePools", GetNumberOfActiveStoragePools);

// NODE API
// NODE
NODE_SET_PROTOTYPE_METHOD(t, "listNodeDevices", ListNodeDevices);
NODE_SET_PROTOTYPE_METHOD(t, "getNodeSecurityModel", GetNodeSecurityModel);
NODE_SET_PROTOTYPE_METHOD(t, "getNodeInfo", GetNodeInfo);
NODE_SET_PROTOTYPE_METHOD(t, "getNodeFreeMemory", GetNodeFreeMemory);
NODE_SET_PROTOTYPE_METHOD(t, "getNodeCellsFreeMemory", GetNodeCellsFreeMemory);


// INTERFACE
NODE_SET_PROTOTYPE_METHOD(t, "lookupInterfaceByName", Interface::LookupByName);
NODE_SET_PROTOTYPE_METHOD(t, "lookupInterfaceByMacAddress", Interface::LookupByMacAddress);
NODE_SET_PROTOTYPE_METHOD(t, "defineInterface", Interface::Define);

// NETWORK
NODE_SET_PROTOTYPE_METHOD(t, "createNetwork", Network::Create);
NODE_SET_PROTOTYPE_METHOD(t, "lookupNetworkByName", Network::LookupByName);
NODE_SET_PROTOTYPE_METHOD(t, "lookupNetworkByUUID", Network::LookupByUUID);
NODE_SET_PROTOTYPE_METHOD(t, "defineNetwork", Network::Define);

// NETWORK FILTER
// NODE_SET_PROTOTYPE_METHOD(t, "defineNetworkFilter", NetworkFilter::Define);
// NODE_SET_PROTOTYPE_METHOD(t, "lookupNetworkFilterByName", NetworkFilter::LookupByName);
// NODE_SET_PROTOTYPE_METHOD(t, "lookupNetworkFilterByUUID", NetworkFilter::LookupByUUID);

// STORAGE POOL
NODE_SET_PROTOTYPE_METHOD(t, "createStoragePool", StoragePool::Create);
NODE_SET_PROTOTYPE_METHOD(t, "defineStoragePool", StoragePool::Define);
NODE_SET_PROTOTYPE_METHOD(t, "lookupStoragePoolByName", StoragePool::LookupByName);
NODE_SET_PROTOTYPE_METHOD(t, "lookupStoragePoolByUUID", StoragePool::LookupByUUID);
NODE_SET_PROTOTYPE_METHOD(t, "lookupStoragePoolByVolume", StoragePool::LookupByVolume);

// STORAGE VOLUME
NODE_SET_PROTOTYPE_METHOD(t, "lookupStorageVolumeByKey", StorageVolume::LookupByKey);
NODE_SET_PROTOTYPE_METHOD(t, "lookupStorageVolumeByPath", StorageVolume::LookupByPath);

// SECRET
NODE_SET_PROTOTYPE_METHOD(t, "defineSecret", Secret::Define);
NODE_SET_PROTOTYPE_METHOD(t, "lookupSecretByUsage", Secret::LookupByUsage);
NODE_SET_PROTOTYPE_METHOD(t, "lookupSecretByUUID", Secret::LookupByUUID);


/*
// NODE
Expand All @@ -130,27 +162,7 @@ void Hypervisor::Initialize(Handle<Object> exports)
NODE_SET_PROTOTYPE_METHOD(t, "lookupDomainByName", Domain::LookupByName);
NODE_SET_PROTOTYPE_METHOD(t, "lookupDomainByUUID", Domain::LookupByUUID);
// NETWORK
NODE_SET_PROTOTYPE_METHOD(t, "createNetwork", Network::Create);
NODE_SET_PROTOTYPE_METHOD(t, "lookupNetworkByName", Network::LookupByName);
NODE_SET_PROTOTYPE_METHOD(t, "lookupNetworkByUUID", Network::LookupByUUID);
NODE_SET_PROTOTYPE_METHOD(t, "defineNetwork", Network::Define);
NODE_SET_PROTOTYPE_METHOD(t, "defineNetworkFilter", NetworkFilter::Define);
NODE_SET_PROTOTYPE_METHOD(t, "lookupNetworkFilterByName", NetworkFilter::LookupByName);
NODE_SET_PROTOTYPE_METHOD(t, "lookupNetworkFilterByUUID", NetworkFilter::LookupByUUID);
NODE_SET_PROTOTYPE_METHOD(t, "lookupInterfaceByName", Interface::LookupByName);
NODE_SET_PROTOTYPE_METHOD(t, "lookupInterfaceByMacAddress", Interface::LookupByMacAddress);
NODE_SET_PROTOTYPE_METHOD(t, "defineInterface", Interface::Define);
NODE_SET_PROTOTYPE_METHOD(t, "defineSecret", Secret::Define);
NODE_SET_PROTOTYPE_METHOD(t, "lookupSecretByUsage", Secret::LookupByUsage);
NODE_SET_PROTOTYPE_METHOD(t, "lookupSecretByUUID", Secret::LookupByUUID);
NODE_SET_PROTOTYPE_METHOD(t, "createStoragePool", StoragePool::Create);
NODE_SET_PROTOTYPE_METHOD(t, "defineStoragePool", StoragePool::Define);
NODE_SET_PROTOTYPE_METHOD(t, "lookupStoragePoolByName", StoragePool::LookupByName);
NODE_SET_PROTOTYPE_METHOD(t, "lookupStoragePoolByUUID", StoragePool::LookupByUUID);
NODE_SET_PROTOTYPE_METHOD(t, "lookupStoragePoolByVolume", StoragePool::LookupByVolume);
NODE_SET_PROTOTYPE_METHOD(t, "lookupStorageVolumeByKey", StorageVolume::LookupByKey);
NODE_SET_PROTOTYPE_METHOD(t, "lookupStorageVolumeByPath", StorageVolume::LookupByPath);
Local<ObjectTemplate> object_tmpl = t->InstanceTemplate();
Expand Down Expand Up @@ -216,6 +228,7 @@ void Hypervisor::Initialize(Handle<Object> exports)
target->Set(String::NewSymbol("Hypervisor"), t->GetFunction());
*/

NanAssignPersistent(constructor_template, t);
NanAssignPersistent(constructor, t->GetFunction());
exports->Set(NanNew("Hypervisor"), t->GetFunction());
}
Expand All @@ -234,6 +247,11 @@ Hypervisor::~Hypervisor()
{
}

virConnectPtr Hypervisor::Connection() const
{
return conn_;
}

NAN_METHOD(Hypervisor::New)
{
NanScope();
Expand Down Expand Up @@ -402,7 +420,7 @@ void Hypervisor::GetVersionWorker::Execute()
{
HYPERVISOR_ASSERT_CONNECTION();

unsigned long version;
unsigned long version;
int result = virConnectGetVersion(Connection(), &version);
if (result == -1) {
SetVirError(virGetLastError());
Expand Down Expand Up @@ -685,43 +703,43 @@ void Hypervisor::CompareCPUWorker::Execute()


HYPERVISOR_NOARGS_WORKER_METHOD(ListDefinedDomains)
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListDefinedDomains,
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListDefinedDomains,
virConnectNumOfDefinedDomains, virConnectListDefinedDomains)

HYPERVISOR_NOARGS_WORKER_METHOD(ListDefinedNetworks)
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListDefinedNetworks,
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListDefinedNetworks,
virConnectNumOfDefinedNetworks, virConnectListDefinedNetworks)

HYPERVISOR_NOARGS_WORKER_METHOD(ListDefinedStoragePools)
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListDefinedStoragePools,
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListDefinedStoragePools,
virConnectNumOfDefinedStoragePools, virConnectListDefinedStoragePools)

HYPERVISOR_NOARGS_WORKER_METHOD(ListDefinedInterfaces)
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListDefinedInterfaces,
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListDefinedInterfaces,
virConnectNumOfDefinedInterfaces, virConnectListDefinedInterfaces)

HYPERVISOR_NOARGS_WORKER_METHOD(ListActiveDomains)
HYPERVISOR_INT_LIST_RETURN_EXECUTE(ListActiveDomains,
HYPERVISOR_INT_LIST_RETURN_EXECUTE(ListActiveDomains,
virConnectNumOfDomains, virConnectListDomains)

HYPERVISOR_NOARGS_WORKER_METHOD(ListActiveInterfaces)
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListActiveInterfaces,
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListActiveInterfaces,
virConnectNumOfInterfaces, virConnectListInterfaces)

HYPERVISOR_NOARGS_WORKER_METHOD(ListNetworkFilters)
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListNetworkFilters,
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListNetworkFilters,
virConnectNumOfNWFilters, virConnectListNWFilters)

HYPERVISOR_NOARGS_WORKER_METHOD(ListActiveNetworks)
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListActiveNetworks,
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListActiveNetworks,
virConnectNumOfNetworks, virConnectListNetworks)

HYPERVISOR_NOARGS_WORKER_METHOD(ListSecrets)
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListSecrets,
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListSecrets,
virConnectNumOfSecrets, virConnectListSecrets)

HYPERVISOR_NOARGS_WORKER_METHOD(ListActiveStoragePools)
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListActiveStoragePools,
HYPERVISOR_STRING_LIST_RETURN_EXECUTE(ListActiveStoragePools,
virConnectNumOfStoragePools, virConnectListStoragePools)


Expand Down Expand Up @@ -1212,7 +1230,7 @@ Handle<Value> Hypervisor::GetNodeInfo(const Arguments& args) {
// of an event is only valid for the duration of execution of the callback
// If the callback wishes to keep the domain object after the callback,
// it shall take a reference to it, by calling virDomainRef
virDomainRef(dom);
Local<Value> argv[3];
Expand Down Expand Up @@ -1256,7 +1274,7 @@ Handle<Value> Hypervisor::GetNodeInfo(const Arguments& args) {
// of an event is only valid for the duration of execution of the callback
// If the callback wishes to keep the domain object after the callback,
// it shall take a reference to it, by calling virDomainRef
virDomainRef(dom);
Local<Value> argv[2];
Expand Down
15 changes: 14 additions & 1 deletion src/hypervisor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "worker.h"

using namespace v8;
using namespace node;
using namespace std;

#define HYPERVISOR_LIBVIRTWORKER(WorkerName) \
Expand Down Expand Up @@ -52,17 +53,29 @@ class Hypervisor : public ObjectWrap
public:
static void Initialize(Handle<Object> exports);

virConnectPtr Connection() const;

private:
explicit Hypervisor(string uri, string user, string pass, bool readOnly);
~Hypervisor();

static v8::Persistent<v8::Function> constructor;
static Persistent<Function> constructor;
static Persistent<FunctionTemplate> constructor_template;

virConnectPtr conn_;
string uri_;
string username_;
string password_;
bool readOnly_;

friend class Interface;
friend class Network;
friend class NetworkFilter;
friend class NodeDevice;
friend class Secret;
friend class StorageVolume;
friend class StoragePool;

private:
static NAN_METHOD(New);
static NAN_METHOD(Connect);
Expand Down
14 changes: 7 additions & 7 deletions src/interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ void Interface::Initialize()
Local<FunctionTemplate> t = FunctionTemplate::New();
t->InstanceTemplate()->SetInternalFieldCount(1);

NODE_SET_PROTOTYPE_METHOD(t, "start", Interface::Start);
NODE_SET_PROTOTYPE_METHOD(t, "stop", Interface::Stop);
NODE_SET_PROTOTYPE_METHOD(t, "getName", Interface::GetName);
NODE_SET_PROTOTYPE_METHOD(t, "getMacAddress", Interface::GetMacAddress);
NODE_SET_PROTOTYPE_METHOD(t, "isActive", Interface::IsActive);
NODE_SET_PROTOTYPE_METHOD(t, "undefine", Interface::Undefine);
NODE_SET_PROTOTYPE_METHOD(t, "toXml", Interface::ToXml);
NODE_SET_PROTOTYPE_METHOD(t, "start", Start);
NODE_SET_PROTOTYPE_METHOD(t, "stop", Stop);
NODE_SET_PROTOTYPE_METHOD(t, "getName", GetName);
NODE_SET_PROTOTYPE_METHOD(t, "getMacAddress", GetMacAddress);
NODE_SET_PROTOTYPE_METHOD(t, "isActive", IsActive);
NODE_SET_PROTOTYPE_METHOD(t, "undefine", Undefine);
NODE_SET_PROTOTYPE_METHOD(t, "toXml", ToXml);

Local<ObjectTemplate> object_tmpl = t->InstanceTemplate();

Expand Down
7 changes: 3 additions & 4 deletions src/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef SRC_INTERFACE_H_
#define SRC_INTERFACE_H_

#include <nan.h>
#include "node_libvirt.h"

namespace NodeLibvirt {

Expand All @@ -12,12 +12,11 @@ class Interface : public ObjectWrap
static void Initialize();

private:
explicit Interface();
friend class Hypervisor;

static Persistent<FunctionTemplate> constructor_template;
virInterfacePtr interface_;

friend class Hypervisor;

private:
static NAN_METHOD(Start);
static NAN_METHOD(Stop);
Expand Down
Loading

0 comments on commit 9c2e824

Please sign in to comment.