Skip to content

Commit

Permalink
oneandone blockstorage with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alibazlamit authored and rosskukulinski committed Dec 3, 2017
1 parent 9b70db7 commit 0a8815b
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 20 deletions.
31 changes: 20 additions & 11 deletions lib/pkgcloud/oneandone/blockstorage/client/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var Snapshot = require('../snapshot').Snapshot,
oneandone = require('liboneandone'),
Server = require('../../compute/server').Server;
/**
* client.getSnapshots
Expand All @@ -12,7 +13,7 @@ var Snapshot = require('../snapshot').Snapshot,
* @param {function} callback
* @returns {*}
*/
exports.getSnapshots = function (options, callback) {
exports.getSnapshots = function (server, callback) {
var self = this,
serverId = server instanceof Server ? server.id : server;
oneandone.listSnapshots(serverId, function (error, response, body) {
Expand All @@ -25,9 +26,15 @@ exports.getSnapshots = function (options, callback) {
return;
}
var object = JSON.parse(body);
callback(null, object.volumes.map(function (data) {
return new Volume(self, data);
}), res);
var result = [];
if (object instanceof Array) {
result = object;
} else {
result.push(object);
}
callback(null, result.map(function (data) {
return new Snapshot(self, data);
}));
});
};

Expand All @@ -41,8 +48,8 @@ exports.getSnapshots = function (options, callback) {
* @param {function} callback
* @returns {*}
*/
exports.createSnapshot = function (options, callback) {
var serverId = options.server instanceof Server ? options.server.id : options.server;
exports.createSnapshot = function (server, callback) {
var serverId = server instanceof Server ? server.id : server;

oneandone.createSnapshot(serverId, function (error, response, body) {
if (error) {
Expand All @@ -54,7 +61,7 @@ exports.createSnapshot = function (options, callback) {
return;
}
var snapshot = JSON.parse(body);
callback(null, snapshot);
callback(null, snapshot.snapshot);
});
};

Expand All @@ -69,8 +76,9 @@ exports.createSnapshot = function (options, callback) {
* @returns {*}
*/
exports.updateSnapshot = function (options, callback) {
var self = this;
var serverId = options.server instanceof Server ? options.server.id : options.server,
snapshotId = options.snapshot instanceof Snapshot ? options.snapshot.id : options.snapshot;
snapshotId = options.snapshot instanceof Object ? options.snapshot.id : options.snapshot;

oneandone.restoreSnapshot(serverId, snapshotId, function (error, response, body) {
if (error) {
Expand All @@ -82,7 +90,7 @@ exports.updateSnapshot = function (options, callback) {
return;
}
var snp = JSON.parse(body);
callback(null, new compute.Snapshot(self, snp));
callback(null, new Snapshot(self, snp));
});
};

Expand All @@ -96,8 +104,9 @@ exports.updateSnapshot = function (options, callback) {
* @returns {*}
*/
exports.deleteSnapshot = function (options, callback) {
var self = this;
var serverId = options.server instanceof Server ? options.server.id : options.server,
snapshotId = options.snapshot instanceof Snapshot ? options.snapshot.id : options.snapshot;
snapshotId = options.snapshot instanceof Object ? options.snapshot.id : options.snapshot;

oneandone.deleteSnapshot(serverId, snapshotId, function (error, response, body) {
if (error) {
Expand All @@ -109,6 +118,6 @@ exports.deleteSnapshot = function (options, callback) {
return;
}
var snp = JSON.parse(body);
callback(null, new compute.Snapshot(self, snp));
callback(null, new Snapshot(self, snp));
});
};
1 change: 0 additions & 1 deletion lib/pkgcloud/oneandone/blockstorage/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ util.inherits(Snapshot, base.Model);

Snapshot.prototype._setProperties = function (details) {
this.id = details.id;
this.name = details.name;
};

Snapshot.prototype.toJSON = function () {
Expand Down
79 changes: 79 additions & 0 deletions test/fixtures/oneandone/createSnapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"id": "92AA60BEC8333A21EDB9EAAA61852860",
"cloudpanel_id": "5061DE9",
"name": "My server",
"description": null,
"datacenter": {
"id": "D0F6D8C8ED29D3036F94C27BBB7BAD36",
"location": "USA",
"country_code": "US"
},
"creation_date": "2015-05-06T11:09:55+00:00",
"first_password": null,
"status": {
"state": "CONFIGURING",
"percent": null
},
"hardware": {
"fixed_instance_size_id": 0,
"vcore": 1,
"cores_per_processor": 1,
"ram": 1,
"hdds": [
{
"id": "77F5BC74C9B3532A37D31FEED070BE01",
"size": 40,
"is_main": true
}
]
},
"image": {
"id": "07C170D67C8EC776933FCFF1C299C1C5",
"name": "w2012r2datacenter64min"
},
"dvd": {
"id": "05F2EF8C89A12782B7DBE445256BD7A5",
"name": "centos7-64iso"
},
"snapshot": {
"id": "D609F69D08EB0C77D8EADE22F70462B4",
"creation_date": "2015-05-07T10:47:23+00:00"
},
"ips": [
{
"id": "696469547988D3945B3FE59DFCB93C90",
"ip": "10.4.141.161",
"type": "IPV4",
"reverse_dns": null,
"firewall_policy": {
"id": "5DE607955050915229D602931F942F32",
"name": "Linux"
},
"load_balancers": []
}
],
"alerts": {
"critical": [
{
"type": "agent",
"description": "MONITORING_AGENT_NOT_INSTALLED",
"date": "2015-05-07 10:47:23"
},
{
"type": "internal_ping",
"description": "CRITICAL - 10.4.141.161: rta nan, lost 100%",
"date": "2015-05-07 09:59:21"
}
]
},
"monitoring_policy": {
"id": "5DF232A92E9635249B8A4EB31C5B14F4",
"name": "My Monitoring Policy 1"
},
"private_networks": [
{
"id": "6B7051F17199EF9EA994CD3E4AA450E6",
"name": "New private network 1"
}
]
}
7 changes: 7 additions & 0 deletions test/fixtures/oneandone/snapshots.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"id": "D609F69D08EB0C77D8EADE22F70462B4",
"creation_date": "2015-04-06T23:48:38Z",
"deletion_date": "2015-04-09T23:48:38Z"
}
]
3 changes: 3 additions & 0 deletions test/oneandone/blockstorage/snapshot-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* Created by Ali Bazlamit on 8/30/2017.
*/
143 changes: 143 additions & 0 deletions test/oneandone/blockstorage/test-snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* Created by Ali Bazlamit on 8/30/2017.
*/
var server,
_snapshot,
blockStorage,
client;
var should = require('should'),
helpers = require('../../helpers'),
hock = require('hock'),
http = require('http'),
mock = !!process.env.MOCK,
Server = require('../../../lib/pkgcloud/core/compute/server').Server,
Snapshot = require('../../../lib/pkgcloud/oneandone/blockstorage/snapshot').Snapshot;

var srvr_options = {
name: 'create-test-oao',
flavor: '81504C620D98BCEBAA5202D145203B4B',
image: '6631A1589A2CC87FEA9B99AB07399281',
location: '4EFAD5836CE43ACA502FD5B99BEE44EF',
token: process.env.OAO_TOKEN
};

describe('Snapshot tests', function () {
this.timeout(18000000);
var hockInstance, mockServer;

before(function (done) {
client = helpers.createClient('oneandone', 'compute', srvr_options);
blockStorage = helpers.createClient('oneandone', 'blockstorage', srvr_options);
if (!mock) {

client.createServer(srvr_options, function (err, srv1) {
should.not.exist(err);
should.exist(srv1);
server = srv1;
server.setWait({ status: server.STATUS.running }, 5000, function (err) {
if (err) {
console.dir(err);
return;
}
blockStorage.createSnapshot(server, function (err, snapshot) {
should.not.exist(err);
should.exist(snapshot);
_snapshot = snapshot;
hockInstance && hockInstance.done();
done();
});
});
});
} else {
hockInstance = hock.createHock({ throwOnUnmatched: false });
hockInstance.filteringRequestBody(helpers.authFilter);
mockServer = http.createServer(hockInstance.handler);
mockServer.listen(12345, done);
}
});

after(function (done) {
if (hockInstance) {
mockServer.close(function () {
done();
});
}
else {
var deleteOps = {};
deleteOps.server = server;
deleteOps.snapshot = _snapshot;
server.setWait({ status: server.STATUS.running }, 5000, function (err) {
if (err) {
console.dir(err);
return;
}
blockStorage.deleteSnapshot(deleteOps, function (err, response) {
should.not.exist(err);
should.exist(response);
server.setWait({ status: server.STATUS.running }, 15000, function (err) {
if (err) {
console.dir(err);
return;
}
client.destroyServer(server, function (err, response) {
should.not.exist(err);
should.exist(response);
done();
});
});
});
});
}
});

it('the getSnapshots() method should return a list of snapshots', function (done) {
if (mock) {
hockInstance
.get('servers/{server_id}/snapshots')
.reply(200, helpers.loadFixture('oneandone/snapshots.json'));
}
server.setWait({ status: server.STATUS.running }, 5000, function (err) {
if (err) {
console.dir(err);
return;
}
blockStorage.getSnapshots(server, function (err, snapshots) {
should.not.exist(err);
should.exist(snapshots);

snapshots.should.be.an.Array;

snapshots.forEach(function (snp) {
snp.should.be.instanceOf(Snapshot);
});
hockInstance && hockInstance.done();
done();
});
});
});

it('the updateSnapshot() method should restore a snapshot into a server', function (done) {
if (mock) {
hockInstance
.get('/servers/92AA60BEC8333A21EDB9EAAA61852860/snapshots/D609F69D08EB0C77D8EADE22F70462B4')
.reply(202, helpers.loadFixture('oneandone/createSnapshot.json'));
}
var updateops = {};
updateops.server = server;
updateops.snapshot = _snapshot;
server.setWait({ status: server.STATUS.running }, 5000, function (err) {
if (err) {
console.dir(err);
return;
}
blockStorage.updateSnapshot(updateops, function (err, response) {
should.not.exist(err);
should.exist(response);
response.should.be.instanceOf(Snapshot);
hockInstance && hockInstance.done();
done();
});
});
});
});

4 changes: 2 additions & 2 deletions test/oneandone/compute/test-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ var should = require('should'),

var srvr_options = {
name: 'create-test-oao2',
flavor: 'S',
flavor: '81504C620D98BCEBAA5202D145203B4B',
image: '6631A1589A2CC87FEA9B99AB07399281',
location: 'Germany',
location: '4EFAD5836CE43ACA502FD5B99BEE44EF',
};
var image_options = {
name: 'pkgcloud image2',
Expand Down
13 changes: 7 additions & 6 deletions test/oneandone/compute/test-servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ var should = require('should'),
var client;
var options = {
name: 'create-test-oao2',
flavor: 'S',
flavor: '81504C620D98BCEBAA5202D145203B4B',
image: '6631A1589A2CC87FEA9B99AB07399281',
location: 'Germany',
location: '4EFAD5836CE43ACA502FD5B99BEE44EF',
token: process.env.OAO_TOKEN
};

Expand All @@ -30,11 +30,12 @@ describe('Server tests', function () {
srv1.image.id.should.equal(options.image);
done();
});
}else{
hockInstance = hock.createHock({throwOnUnmatched: false});
hockInstance.filteringRequestBody(helpers.authFilter);
mockServer = http.createServer(hockInstance.handler);
mockServer.listen(12345, done);
}
hockInstance = hock.createHock({throwOnUnmatched: false});
hockInstance.filteringRequestBody(helpers.authFilter);
mockServer = http.createServer(hockInstance.handler);
mockServer.listen(12345, done);
});

after(function (done) {
Expand Down

0 comments on commit 0a8815b

Please sign in to comment.