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

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timsavery committed Mar 11, 2012
1 parent 866e8dd commit a32bf70
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 188 deletions.
68 changes: 56 additions & 12 deletions lib/client.js
@@ -1,20 +1,64 @@
var Request = require('./request');
var request = require('./request');

module.exports = function Client(options) {
this.host = options.host || '127.0.0.1';
this.port = options.port || 8080;
this.account = options.account || '';
this.apiKey = options.apiKey || '';
this.apiSecret = options.apiSecret || '';

this.listVirtualMachines = function(callback) {
new Request({
this.host = options.host || 'localhost',
this.port = options.port || 8080,
this.account = options.account || '',
this.apiKey = options.apiKey || '',
this.apiSecret = options.apiSecret || ''
this.buildArgs = function(command, params) {
var args = {
host: this.host,
port: this.port,
account: this.account,
apiKey: this.apiKey,
apiSecret: this.apiSecret,
command: 'listVirtualMachines'
}).exec(callback);
apiSecret: this.apiSecret
};

args.command = command;
args.params = params;

return args;
};

this.listVirtualMachines = function(callback) {
var args = this.buildArgs('listVirtualMachines');

new request(args).exec(callback);
};

this.deployVirtualMachine = function(templateId, serviceOfferingId, zoneId, callback) {
var args = this.buildArgs('deployVirtualMachine', {
templateId: templateId,
serviceOfferingId: serviceOfferingId,
zoneId: zoneId
});

new request(args).exec(callback);
};

this.destroyVirtualMachine = function(id, callback) {
var args = this.buildArgs('destroyVirtualMachine', {
id: id
});

request(args).exec(callback);
};

this.stopVirtualMachine = function(id, callback) {
var args = this.buildArgs('stopVirtualMachine', {
id: id
});

request(args).exec(callback);
};

this.startVirtualMachine = function(id, callback) {
var args = this.buildArgs('startVirtualMachine', {
id: id
});

request(args).exec(callback);
};
};
33 changes: 15 additions & 18 deletions lib/request.js
Expand Up @@ -23,34 +23,31 @@ module.exports = function Request(options) {
this.uri = 'http://#host:#port/client/api?#querystring'
.replace(/#host/, this.options.host)
.replace(/#port/, this.options.port)
.replace(/#querystring/, this.querystring.toString());
.replace(/#querystring/, this.querystring.toString());
};

module.exports.prototype.exec = function(callback) {
var self = this;

if (!callback) callback = function() {};
HttpRequest(this.uri, function(err, res, body) {

HttpRequest(this.uri, function(err, res, body) {
if (err) {
return callback({ status: 'error', data: err });
}

var data = JSON.parse(body)[self.options.command.toLowerCase() + 'response'];

if (res.statusCode == 200) {
callback({
status: 'ok',
data: data
});
} else {
callback({
status: 'error',
error: err
data: data
});
} else {
var data = JSON.parse(body)[self.options.command.toLowerCase() + 'response'];

if (res.statusCode == 200) {
callback({
status: 'ok',
data: data
});
} else {
callback({
status: 'error',
error: data.errortext
});
}
}
});
};
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -8,8 +8,9 @@
{ "name": "Tim Savery" , "email": "tsavery@chathamfinancial.com" }]
, "scripts" : { "test": "vows" }
, "keywords" : [ "cloudstack", "api", "cloud" ]
, "dependencies" : { "request": "2.1.x" }
, "devDependencies" : { "vows": "0.6.x", "nock": "0.10.x" }
, "dependencies" : { "request": "2.2.x" }
, "devDependencies" : { "nodeunit": "0.7.x", "nock": "0.10.x" }
, "license" : "MIT"
, "main" : "./lib/cloudstack"
, "scripts" : { "test": "NOCK=1 nodeunit test" }
}
108 changes: 21 additions & 87 deletions test/client-test.js
@@ -1,91 +1,25 @@
var vows = require('vows')
, assert = require('assert')
, client = require('../lib/client')
var client = require('../lib/client')
, nock = require('nock');

vows.describe('Client Tests').addBatch({
'when client is constructed': {
'with a null options object': {
topic: function() {
return new client({});
},
'the host property defaults to localhost': function(topic) {
assert.equal(topic.host, '127.0.0.1');
},
'the port property defaults to 8080': function(topic) {
assert.equal(topic.port, 8080);
},
'the account property defaults to empty string': function(topic) {
assert.equal(topic.account, '');
},
'the apiKey property defaults to empty string': function(topic) {
assert.equal(topic.apiKey, '');
},
'the apiSecret property defaults to empty string': function(topic) {
assert.equal(topic.apiSecret, '');
}
},
'with a complete options object': {
topic: function() {
return new client({
host: 'cloudhost',
port: 54321,
account: 'TestUser',
apiKey: 'TestUserApiKey',
apiSecret: 'TestUserApiSecret'
});
},
'the host property is equal to the specified value': function(topic) {
assert.equal(topic.host, 'cloudhost');
},
'the port property is equal to the specified value': function(topic) {
assert.equal(topic.port, 54321);
},
'the account property is equal to the specified value': function(topic) {
assert.equal(topic.account, 'TestUser');
},
'the apiKey property is equal to the specified value': function(topic) {
assert.equal(topic.apiKey, 'TestUserApiKey');
},
'the apiSecret property is equal to the specified value': function(topic) {
assert.equal(topic.apiSecret, 'TestUserApiSecret');
}
}
},

'when listVirtualMachines is called': {
'and no machines are running': {
'the result': {
topic: function() {
if(process.env.NOCK) {
nock("http://cloudhost:54321")
.get("/client/api?account=TestUser&apiKey=TestUserApiKey&command=listVirtualMachines&domainId=1&response=json&signature=acSwdq7p634LFmIvR3em%2FZzlKEE%3D")
.reply(200, {
listvirtualmachinesresponse: {}
});
}
var args = {
host: 'controller',
account: 'TestUser',
apiKey: 'TestUserApiKey',
apiSecret: 'TestUserApiSecret'
};

new client({
host: 'cloudhost',
port: 54321,
account: 'TestUser',
apiKey: 'TestUserApiKey',
apiSecret: 'TestUserApiSecret'
}).listVirtualMachines(this.callback);
},
'has a "status" property': function(topic) {
assert.isDefined(topic.status);
},
'has a "data" property': function(topic) {
assert.isDefined(topic.data);
},
'the value of the status property is "ok"': function(topic) {
assert.equal(topic.status, "ok");
},
'the value of the data property is an empty object': function(topic) {
assert.deepEqual(topic.data, {});
}
}
}
exports['listVirtualMachines'] = function(test) {
if(process.env.NOCK) {
nock("http://controller:8080")
.get("/client/api?account=TestUser&apiKey=TestUserApiKey&command=listVirtualMachines&domainId=1&response=json&signature=acSwdq7p634LFmIvR3em%2FZzlKEE%3D")
.reply(200, {
listvirtualmachinesresponse: {}
});
}
}).export(module, { error: false });

new client(args).listVirtualMachines(function(res) {
test.equal(res.status, 'ok');
test.deepEqual(res.data, {});
test.done();
});
};
33 changes: 14 additions & 19 deletions test/cloudstack-test.js
@@ -1,20 +1,15 @@
var vows = require('vows')
, assert = require('assert')
, Client = require('../lib/client')
, CloudStack = require('../lib/cloudstack');
var CloudstackClient = require('../lib/client');

vows.describe('CloudStack Object Tests').addBatch({
'When you call .createClient': {
topic: function() {
return CloudStack.createClient({
host: 'cloudhost'
});
},
'an object of type Client is returned': function(topic) {
assert.instanceOf(topic, Client);
},
'the options are passed to the constructor': function(topic) {
assert.equal(topic.host, 'cloudhost');
}
}
}).export(module);
exports['CreateClient'] = function(test) {
var client = (require('../lib/cloudstack')).createClient({
host: 'cloudhost'
});

test.equal(client.host, 'cloudhost');
test.equal(client.port, 8080);
test.equal(client.apiKey, '');
test.equal(client.apiSecret, '');
test.equal(client.account, '');

test.done();
};
82 changes: 32 additions & 50 deletions test/querystring-test.js
@@ -1,50 +1,32 @@
var vows = require('vows')
, assert = require('assert')
, QueryString = require('../lib/querystring');

vows.describe('QueryString Tests').addBatch({
'when constructed with an "apiKey" value in the options': {
topic: function() {
return new QueryString({
apiKey: 'ApiKeyString'
});
},
'the params object has a property called "apiKey"': function(topic) {
assert.isDefined(topic.params['apiKey']);
},
'the "apiKey" property has the correct value': function(topic) {
assert.equal(topic.params['apiKey'], 'ApiKeyString');
}
},
'when a parameter is added': {
topic: function() {
var qs = new QueryString();
qs.addParam('Property', 'Property Value');

return qs;
},
'the property now exists in the .params collection': function(topic) {
assert.isDefined(topic.params['Property']);
},
'the property value has been set and uri encoded': function(topic) {
assert.equal(topic.params['Property'], 'Property%20Value');
}
},
'when converted to a string': {
topic: function() {
return new QueryString({
apiKey: 'ApiKeyString',
apiSecret: 'ApiSecretString'
}).toString();
},
'the apiKey is included as a parameter': function(topic) {
assert.include(topic, 'apiKey=ApiKeyString');
},
'the signature is included as a parameter': function(topic) {
assert.include(topic, 'signature=');
},
'the signature is generated properly': function(topic) {
assert.include(topic, 'signature=9MQbwfKzCXVlfuNV80wmrXnzL%2B4%3D');
}
}
}).export(module);
var querystring = require('../lib/querystring');

exports['when constructed with an "apiKey" value in the options'] = function(test) {
var qs = new querystring({
apiKey: 'TestApiKey'
});

test.equal(qs.params.apiKey, 'TestApiKey');

test.done();
};

exports['when a parameter is added'] = function(test) {
var qs = new querystring();
qs.addParam('Property', 'Property Value');

test.equal(qs.params.Property, 'Property%20Value');

test.done();
};

exports['when converted to a string'] = function(test) {
var qs = new querystring({
apiKey: 'ApiKeyString',
apiSecret: 'ApiSecretString'
}).toString();

test.ok(qs.match(/apiKey=ApiKeyString/));
test.ok(qs.match(/signature=9MQbwfKzCXVlfuNV80wmrXnzL%2B4%3D/));

test.done();
};

0 comments on commit a32bf70

Please sign in to comment.