PUBAPI-1233/PUBAPI-1234 - let fwrule and snapshot tests work even

when there isn't an existing machine to test with.
This commit is contained in:
Marsell Kukuljevic 2016-02-08 00:50:47 +11:00
parent e6334db9e1
commit 690c6e5198
3 changed files with 90 additions and 8 deletions

View File

@ -23,6 +23,7 @@ var RULE = 'FROM any TO vm $id ALLOW tcp PORT 80';
var RULE2 = 'FROM any TO vm $id BLOCK tcp port 25'; var RULE2 = 'FROM any TO vm $id BLOCK tcp port 25';
var INST; var INST;
var ID; var ID;
var FAKE_INST_UUID = '89bcb9de-f174-4f20-bfa8-27d9749e6a2c';
// --- Tests // --- Tests
@ -33,11 +34,17 @@ test('triton fwrule', function (tt) {
return t.end(); return t.end();
var rows = stdout.split('\n'); var rows = stdout.split('\n');
INST = JSON.parse(rows[0]).id; try {
t.ok(INST); INST = JSON.parse(rows[0]).id;
RULE = RULE.replace('$id', INST);
RULE = RULE.replace('$id', INST); RULE2 = RULE2.replace('$id', INST);
RULE2 = RULE2.replace('$id', INST); } catch (e) {
// if we don't have a VM already running to test with, we'll
// run most tests with a fake UUID, and skip any tests that
// require an actual machine UUID
RULE = RULE.replace('$id', FAKE_INST_UUID);
RULE2 = RULE2.replace('$id', FAKE_INST_UUID);
}
t.end(); t.end();
}); });
@ -148,6 +155,9 @@ test('triton fwrule', function (tt) {
t.ok(machines[0].match(/ID\s+NAME\s+IMG\s+BRAND/)); t.ok(machines[0].match(/ID\s+NAME\s+IMG\s+BRAND/));
machines.shift(); machines.shift();
if (!INST)
return t.end();
t.equal(machines.length, 1, 'triton fwrule instances expected ' + t.equal(machines.length, 1, 'triton fwrule instances expected ' +
'num machines'); 'num machines');

View File

@ -19,6 +19,7 @@ var test = require('tape');
var SNAP_NAME = 'test-snapshot'; var SNAP_NAME = 'test-snapshot';
var INST; var INST;
var DESTROY_INST = false;
// --- Tests // --- Tests
@ -29,10 +30,21 @@ test('triton snapshot', function (tt) {
return t.end(); return t.end();
var rows = stdout.split('\n'); var rows = stdout.split('\n');
INST = JSON.parse(rows[0]).id;
t.ok(INST);
t.end(); try {
INST = JSON.parse(rows[0]).id;
return t.end();
} catch (e) {
h.createMachine(function onCreate(err2, instId) {
if (h.ifErr(t, err2, 'triton instance create'))
return t.end();
INST = instId;
DESTROY_INST = true;
t.end();
});
}
}); });
}); });
@ -113,4 +125,13 @@ test('triton snapshot', function (tt) {
t.end(); t.end();
}); });
}); });
tt.test('teardown', function (t) {
if (!DESTROY_INST)
return t.end();
h.triton('instance delete ' + INST, function () {
t.end();
});
});
}); });

View File

@ -121,6 +121,8 @@ function triton(args, opts, cb) {
}, cb); }, cb);
} }
/* /*
* triton wrapper that: * triton wrapper that:
* - tests no error is present * - tests no error is present
@ -172,6 +174,54 @@ function createClient() {
} }
/*
* Create a small instance.
*/
function createMachine(cb) {
function jsonToObjs(jsons) {
return jsons.split('\n').map(function (json) {
try {
return JSON.parse(json);
} catch (e) {}
}).filter(function (obj) {
return obj;
});
}
triton('package list -j', function (err, pkgJson) {
if (err)
return cb(err);
// pick the smallest package (ram-wise)
var pkgs = jsonToObjs(pkgJson);
var pkg = pkgs.sort(function (x, y) {
return (x.memory > y.memory) ? 1 : -1;
})[0];
triton('image list -j', function (err2, imgJson) {
if (err2)
return cb(err2);
// pick any smartos image
var imgs = jsonToObjs(imgJson);
var img = imgs.filter(function (i) {
return i.os === 'smartos';
})[0];
triton('instance create -w ' + img.id + ' ' + pkg.id,
function (err3, stdout) {
if (err3)
return cb(err3);
var match = stdout.match(/Created .+? \((.+)\)/);
var inst = match[1];
cb(null, inst);
});
});
});
}
// --- exports // --- exports
module.exports = { module.exports = {
@ -179,5 +229,6 @@ module.exports = {
triton: triton, triton: triton,
safeTriton: safeTriton, safeTriton: safeTriton,
createClient: createClient, createClient: createClient,
createMachine: createMachine,
ifErr: testcommon.ifErr ifErr: testcommon.ifErr
}; };