2015-09-25 00:48:26 +03:00
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, Joyent, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test create/start/stop/delete/etc. work flows
|
|
|
|
*/
|
|
|
|
|
|
|
|
var f = require('util').format;
|
|
|
|
|
|
|
|
var vasync = require('vasync');
|
|
|
|
|
|
|
|
var h = require('./helpers');
|
|
|
|
var test = require('tape');
|
|
|
|
|
|
|
|
var common = require('../../lib/common');
|
|
|
|
|
|
|
|
var VM_ALIAS = 'node-triton-test-vm-1';
|
|
|
|
var VM_IMAGE = 'base-64@15.2.0';
|
|
|
|
var VM_PACKAGE = 't4-standard-128M';
|
|
|
|
|
2015-09-25 20:45:16 +03:00
|
|
|
var opts = {
|
|
|
|
skip: !h.CONFIG.destructiveAllowed
|
|
|
|
};
|
2015-09-25 00:48:26 +03:00
|
|
|
|
|
|
|
// global variable to hold vm instance JSON
|
|
|
|
var instance;
|
|
|
|
|
2015-09-25 22:24:37 +03:00
|
|
|
|
2015-09-25 00:48:26 +03:00
|
|
|
// --- Tests
|
|
|
|
|
2015-09-25 20:45:16 +03:00
|
|
|
if (opts.skip) {
|
|
|
|
console.error('** skipping manage workflow tests');
|
|
|
|
console.error('** set "destructiveAllowed" to enable');
|
|
|
|
}
|
2015-09-25 22:24:37 +03:00
|
|
|
test('triton manage workflow', opts, function (tt) {
|
2015-09-29 21:45:34 +03:00
|
|
|
tt.comment('using test profile:');
|
|
|
|
Object.keys(h.CONFIG).forEach(function (key) {
|
|
|
|
var value = h.CONFIG[key];
|
|
|
|
tt.comment(f(' %s: %s', key, value));
|
|
|
|
});
|
2015-09-25 22:24:37 +03:00
|
|
|
|
2015-09-25 20:45:16 +03:00
|
|
|
// create a test machine (blocking) and output JSON
|
2015-09-25 22:24:37 +03:00
|
|
|
tt.test('triton create', function (t) {
|
2015-09-29 21:45:34 +03:00
|
|
|
h.safeTriton(t, ['create', '-wjn', VM_ALIAS, VM_IMAGE, VM_PACKAGE],
|
2015-09-25 20:45:16 +03:00
|
|
|
function (stdout) {
|
|
|
|
|
|
|
|
// parse JSON response
|
|
|
|
var lines = stdout.trim().split('\n');
|
|
|
|
t.equal(lines.length, 2, 'correct number of JSON lines');
|
|
|
|
try {
|
|
|
|
lines = lines.map(function (line) {
|
|
|
|
return JSON.parse(line);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
t.fail('failed to parse JSON');
|
|
|
|
t.end();
|
|
|
|
}
|
2015-09-25 00:48:26 +03:00
|
|
|
|
2015-09-25 20:45:16 +03:00
|
|
|
instance = lines[1];
|
|
|
|
t.equal(lines[0].id, lines[1].id, 'correct UUID given');
|
|
|
|
t.equal(lines[1].state, 'running', 'correct machine state');
|
2015-09-25 00:48:26 +03:00
|
|
|
|
2015-09-25 20:45:16 +03:00
|
|
|
t.end();
|
|
|
|
});
|
2015-09-25 00:48:26 +03:00
|
|
|
});
|
|
|
|
|
2015-09-25 20:45:16 +03:00
|
|
|
// test `triton instance -j` with the UUID, the alias, and the short ID
|
2015-09-25 22:24:37 +03:00
|
|
|
tt.test('triton instance', function (t) {
|
2015-09-25 20:45:16 +03:00
|
|
|
var uuid = instance.id;
|
|
|
|
var shortId = common.uuidToShortId(uuid);
|
|
|
|
vasync.parallel({
|
|
|
|
funcs: [
|
|
|
|
function (cb) {
|
2015-09-29 21:45:34 +03:00
|
|
|
h.safeTriton(t, ['instance', '-j', VM_ALIAS],
|
|
|
|
function (stdout) {
|
2015-09-25 20:45:16 +03:00
|
|
|
cb(null, stdout);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function (cb) {
|
2015-09-29 21:45:34 +03:00
|
|
|
h.safeTriton(t, ['instance', '-j', uuid],
|
|
|
|
function (stdout) {
|
2015-09-25 20:45:16 +03:00
|
|
|
cb(null, stdout);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function (cb) {
|
2015-09-29 21:45:34 +03:00
|
|
|
h.safeTriton(t, ['instance', '-j', shortId],
|
|
|
|
function (stdout) {
|
2015-09-25 20:45:16 +03:00
|
|
|
cb(null, stdout);
|
|
|
|
});
|
2015-09-25 22:24:37 +03:00
|
|
|
}
|
|
|
|
]
|
2015-09-25 20:45:16 +03:00
|
|
|
}, function (err, results) {
|
|
|
|
if (h.ifErr(t, err, 'no error'))
|
|
|
|
return t.end();
|
|
|
|
|
|
|
|
var output;
|
|
|
|
try {
|
|
|
|
output = results.operations.map(function (op) {
|
|
|
|
return JSON.parse(op.result);
|
2015-09-25 00:48:26 +03:00
|
|
|
});
|
2015-09-25 20:45:16 +03:00
|
|
|
} catch (e) {
|
|
|
|
t.fail('failed to parse JSON');
|
|
|
|
t.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
output.forEach(function (res) {
|
|
|
|
t.deepEqual(output[0], res, 'same data');
|
2015-09-25 00:48:26 +03:00
|
|
|
});
|
|
|
|
|
2015-09-25 20:45:16 +03:00
|
|
|
t.end();
|
2015-09-25 00:48:26 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-29 19:53:34 +03:00
|
|
|
// remove instance
|
2015-09-25 22:24:37 +03:00
|
|
|
tt.test('triton delete', function (t) {
|
2015-09-29 21:45:34 +03:00
|
|
|
h.safeTriton(t, ['delete', '-w', instance.id], function (stdout) {
|
2015-09-25 20:45:16 +03:00
|
|
|
t.end();
|
|
|
|
});
|
2015-09-25 00:48:26 +03:00
|
|
|
});
|
2015-09-25 22:24:37 +03:00
|
|
|
|
2015-09-29 19:53:34 +03:00
|
|
|
// create a test machine (non-blocking)
|
|
|
|
tt.test('triton create', function (t) {
|
2015-09-29 21:45:34 +03:00
|
|
|
h.safeTriton(t, ['create', '-jn', VM_ALIAS, VM_IMAGE, VM_PACKAGE],
|
2015-09-29 19:53:34 +03:00
|
|
|
function (stdout) {
|
|
|
|
|
|
|
|
// parse JSON response
|
|
|
|
var lines = stdout.trim().split('\n');
|
|
|
|
t.equal(lines.length, 1, 'correct number of JSON lines');
|
|
|
|
var d;
|
|
|
|
try {
|
|
|
|
d = JSON.parse(lines[0]);
|
|
|
|
} catch (e) {
|
|
|
|
t.fail('failed to parse JSON');
|
|
|
|
t.end();
|
|
|
|
}
|
|
|
|
instance = d;
|
|
|
|
|
|
|
|
t.equal(d.state, 'provisioning', 'correct machine state');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// wait for the machine to start
|
|
|
|
tt.test('triton wait', function (t) {
|
2015-09-29 21:45:34 +03:00
|
|
|
h.safeTriton(t, ['wait', instance.id],
|
2015-09-29 19:53:34 +03:00
|
|
|
function (stdout) {
|
|
|
|
|
|
|
|
// parse JSON response
|
|
|
|
var lines = stdout.trim().split('\n');
|
|
|
|
t.equal(lines.length, 2, 'correct number of stdout lines');
|
|
|
|
|
|
|
|
t.ok(lines[0].match(/\(states: running, failed\)$/),
|
|
|
|
'first line correct');
|
|
|
|
t.ok(lines[1].match(/moved to state running$/),
|
|
|
|
'second line correct');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// stop the machine
|
|
|
|
tt.test('triton stop', function (t) {
|
2015-09-29 21:45:34 +03:00
|
|
|
h.safeTriton(t, ['stop', '-w', VM_ALIAS],
|
2015-09-29 19:53:34 +03:00
|
|
|
function (stdout) {
|
|
|
|
t.ok(stdout.match(/^Stop instance/, 'correct stdout'));
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// wait for the machine to stop
|
|
|
|
tt.test('triton confirm stopped', function (t) {
|
2015-09-29 21:45:34 +03:00
|
|
|
h.safeTriton(t, {json: true, args: ['instance', '-j', VM_ALIAS]},
|
|
|
|
function (d) {
|
2015-09-29 19:53:34 +03:00
|
|
|
instance = d;
|
|
|
|
|
|
|
|
t.equal(d.state, 'stopped', 'machine stopped');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// start the machine
|
|
|
|
tt.test('triton start', function (t) {
|
2015-09-29 21:45:34 +03:00
|
|
|
h.safeTriton(t, ['start', '-w', VM_ALIAS],
|
2015-09-29 19:53:34 +03:00
|
|
|
function (stdout) {
|
|
|
|
t.ok(stdout.match(/^Start instance/, 'correct stdout'));
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// wait for the machine to start
|
|
|
|
tt.test('triton confirm running', function (t) {
|
2015-09-29 21:45:34 +03:00
|
|
|
h.safeTriton(t, {json: true, args: ['instance', '-j', VM_ALIAS]},
|
|
|
|
function (d) {
|
|
|
|
|
2015-09-29 19:53:34 +03:00
|
|
|
instance = d;
|
|
|
|
|
|
|
|
t.equal(d.state, 'running', 'machine running');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// remove test instance
|
|
|
|
tt.test('triton cleanup (delete)', function (t) {
|
2015-09-29 21:45:34 +03:00
|
|
|
h.safeTriton(t, ['delete', '-w', instance.id], function (stdout) {
|
2015-09-29 19:53:34 +03:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-25 00:48:26 +03:00
|
|
|
});
|