2015-09-01 10:31:00 +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 helpers for the integration tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
var error = console.error;
|
|
|
|
var assert = require('assert-plus');
|
2015-09-29 21:45:34 +03:00
|
|
|
var f = require('util').format;
|
2015-09-01 10:31:00 +03:00
|
|
|
var path = require('path');
|
2016-02-09 22:23:55 +02:00
|
|
|
var tabula = require('tabula');
|
2015-09-01 10:31:00 +03:00
|
|
|
|
2015-10-05 17:31:45 +03:00
|
|
|
var common = require('../../lib/common');
|
2015-12-08 21:59:45 +02:00
|
|
|
var mod_triton = require('../../');
|
2015-10-01 19:27:05 +03:00
|
|
|
var testcommon = require('../lib/testcommon');
|
2015-09-01 10:31:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-10-01 19:27:05 +03:00
|
|
|
var CONFIG;
|
2015-10-07 09:09:52 +03:00
|
|
|
var configPath = process.env.TRITON_TEST_CONFIG
|
|
|
|
? path.resolve(process.cwd(), process.env.TRITON_TEST_CONFIG)
|
|
|
|
: path.resolve(__dirname, '..', 'config.json');
|
|
|
|
try {
|
|
|
|
CONFIG = require(configPath);
|
|
|
|
assert.object(CONFIG, configPath);
|
|
|
|
if (CONFIG.profile && CONFIG.profileName) {
|
|
|
|
throw new Error(
|
|
|
|
'cannot specify both "profile" and "profileName" in ' +
|
|
|
|
configPath);
|
|
|
|
} else if (CONFIG.profile) {
|
|
|
|
assert.string(CONFIG.profile.url, 'CONFIG.profile.url');
|
|
|
|
assert.string(CONFIG.profile.account, 'CONFIG.profile.account');
|
|
|
|
assert.string(CONFIG.profile.keyId, 'CONFIG.profile.keyId');
|
|
|
|
assert.optionalBool(CONFIG.profile.insecure,
|
|
|
|
'CONFIG.profile.insecure');
|
|
|
|
} else if (CONFIG.profileName) {
|
2015-12-08 21:59:45 +02:00
|
|
|
CONFIG.profile = mod_triton.loadProfile({
|
2015-10-07 09:09:52 +03:00
|
|
|
configDir: path.join(process.env.HOME, '.triton'),
|
|
|
|
name: CONFIG.profileName
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw new Error('one of "profile" or "profileName" must be defined ' +
|
|
|
|
'in ' + configPath);
|
2015-10-01 19:27:05 +03:00
|
|
|
}
|
2015-10-07 22:19:26 +03:00
|
|
|
assert.optionalBool(CONFIG.allowWriteActions,
|
|
|
|
'test/config.json#allowWriteActions');
|
2015-10-07 09:09:52 +03:00
|
|
|
} catch (e) {
|
|
|
|
error('* * *');
|
|
|
|
error('node-triton integration tests require a config file. By default');
|
|
|
|
error('it looks for "test/config.json". Or you can set the');
|
|
|
|
error('TRITON_TEST_CONFIG envvar. E.g.:');
|
|
|
|
error('');
|
|
|
|
error(' TRITON_TEST_CONFIG=test/coal.json make test');
|
|
|
|
error('');
|
|
|
|
error('See "test/config.json.sample" for a starting point for a config.');
|
|
|
|
error('');
|
|
|
|
error('Warning: This test suite will create machines, images, etc. ');
|
|
|
|
error('using this CloudAPI and account. While it will do its best');
|
|
|
|
error('to clean up all resources, running the test suite against');
|
|
|
|
error('a public cloud could *cost* you money. :)');
|
|
|
|
error('* * *');
|
|
|
|
throw e;
|
2015-09-01 10:31:00 +03:00
|
|
|
}
|
2015-10-07 09:09:52 +03:00
|
|
|
if (CONFIG.profile.insecure === undefined)
|
|
|
|
CONFIG.profile.insecure = false;
|
2015-10-07 22:19:26 +03:00
|
|
|
if (CONFIG.allowWriteActions === undefined)
|
|
|
|
CONFIG.allowWriteActions = false;
|
2015-09-01 10:31:00 +03:00
|
|
|
|
2015-09-22 01:48:03 +03:00
|
|
|
var TRITON = [process.execPath, path.resolve(__dirname, '../../bin/triton')];
|
2015-09-01 10:31:00 +03:00
|
|
|
var UA = 'node-triton-test';
|
|
|
|
|
|
|
|
var LOG = require('../lib/log');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call the `triton` CLI with the given args.
|
2015-11-21 22:41:16 +02:00
|
|
|
*
|
|
|
|
* @param args {String|Array} Required. CLI arguments to `triton ...` (without
|
|
|
|
* the "triton"). This can be an array of args, or a string.
|
|
|
|
* @param opts {Object} Optional.
|
|
|
|
* - opts.cwd {String} cwd option to exec.
|
|
|
|
* @param cb {Function}
|
2015-09-01 10:31:00 +03:00
|
|
|
*/
|
2015-11-21 22:41:16 +02:00
|
|
|
function triton(args, opts, cb) {
|
2015-09-22 01:48:03 +03:00
|
|
|
var command = [].concat(TRITON).concat(args);
|
|
|
|
if (typeof (args) === 'string')
|
|
|
|
command = command.join(' ');
|
2015-11-21 22:41:16 +02:00
|
|
|
if (cb === undefined) {
|
|
|
|
cb = opts;
|
|
|
|
opts = {};
|
|
|
|
}
|
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.optionalString(opts.cwd, 'opts.cwd');
|
|
|
|
assert.func(cb, 'cb');
|
2015-09-22 01:48:03 +03:00
|
|
|
|
2015-09-01 10:31:00 +03:00
|
|
|
testcommon.execPlus({
|
2015-09-22 01:48:03 +03:00
|
|
|
command: command,
|
2015-09-01 10:31:00 +03:00
|
|
|
execOpts: {
|
|
|
|
maxBuffer: Infinity,
|
|
|
|
env: {
|
|
|
|
PATH: process.env.PATH,
|
|
|
|
HOME: process.env.HOME,
|
2015-09-22 01:48:03 +03:00
|
|
|
SSH_AUTH_SOCK: process.env.SSH_AUTH_SOCK,
|
2015-09-25 20:10:39 +03:00
|
|
|
TRITON_PROFILE: 'env',
|
2015-10-07 09:09:52 +03:00
|
|
|
TRITON_URL: CONFIG.profile.url,
|
|
|
|
TRITON_ACCOUNT: CONFIG.profile.account,
|
|
|
|
TRITON_KEY_ID: CONFIG.profile.keyId,
|
|
|
|
TRITON_TLS_INSECURE: CONFIG.profile.insecure
|
2015-11-21 22:41:16 +02:00
|
|
|
},
|
|
|
|
cwd: opts.cwd
|
2015-09-01 10:31:00 +03:00
|
|
|
},
|
|
|
|
log: LOG
|
|
|
|
}, cb);
|
|
|
|
}
|
|
|
|
|
2015-09-29 21:45:34 +03:00
|
|
|
/*
|
2016-02-09 22:23:55 +02:00
|
|
|
* `triton ...` wrapper that:
|
|
|
|
* - tests non-error exit
|
2015-09-29 21:45:34 +03:00
|
|
|
* - tests stderr is empty
|
|
|
|
*
|
|
|
|
* @param {Tape} t - tape test object
|
2016-02-09 22:23:55 +02:00
|
|
|
* @param {Object|Array} opts - options object, or just the `triton` args
|
|
|
|
* @param {Function} cb - `function (err, stdout)`
|
2015-09-29 21:45:34 +03:00
|
|
|
*/
|
|
|
|
function safeTriton(t, opts, cb) {
|
2016-02-09 22:23:55 +02:00
|
|
|
assert.object(t, 't');
|
2015-09-29 21:45:34 +03:00
|
|
|
if (Array.isArray(opts)) {
|
|
|
|
opts = {args: opts};
|
|
|
|
}
|
2016-02-09 22:23:55 +02:00
|
|
|
assert.object(opts, 'opts');
|
|
|
|
assert.arrayOfString(opts.args, 'opts.args');
|
|
|
|
assert.optionalBool(opts.json, 'opts.json');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
2016-02-15 20:03:08 +02:00
|
|
|
// t.comment(f('running: triton %s', opts.args.join(' ')));
|
2015-09-29 21:45:34 +03:00
|
|
|
triton(opts.args, function (err, stdout, stderr) {
|
2016-02-15 20:03:08 +02:00
|
|
|
t.error(err, f('ran "triton %s", err=%s', opts.args.join(' '), err));
|
|
|
|
t.equal(stderr, '', 'empty stderr');
|
2015-09-29 21:45:34 +03:00
|
|
|
if (opts.json) {
|
|
|
|
try {
|
|
|
|
stdout = JSON.parse(stdout);
|
|
|
|
} catch (e) {
|
|
|
|
t.fail('failed to parse JSON');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 22:23:55 +02:00
|
|
|
cb(err, stdout);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find and return an image that can be used for test provisions. We look
|
|
|
|
* for an available base or minimal image.
|
|
|
|
*
|
|
|
|
* @param {Tape} t - tape test object
|
|
|
|
* @param {Function} cb - `function (err, imgId)`
|
|
|
|
* where `imgId` is an image identifier (an image name, shortid, or id).
|
|
|
|
*/
|
|
|
|
function getTestImg(t, cb) {
|
|
|
|
if (CONFIG.image) {
|
|
|
|
t.ok(CONFIG.image, 'image from config: ' + CONFIG.image);
|
|
|
|
cb(null, CONFIG.image);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var candidateImageNames = {
|
|
|
|
'base-64-lts': true,
|
|
|
|
'base-64': true,
|
|
|
|
'minimal-64': true,
|
|
|
|
'base-32-lts': true,
|
|
|
|
'base-32': true,
|
|
|
|
'minimal-32': true,
|
|
|
|
'base': true
|
|
|
|
};
|
|
|
|
safeTriton(t, ['img', 'ls', '-j'], function (err, stdout) {
|
|
|
|
var imgId;
|
|
|
|
var imgs = jsonStreamParse(stdout);
|
|
|
|
// Newest images first.
|
|
|
|
tabula.sortArrayOfObjects(imgs, ['-published_at']);
|
|
|
|
var imgRepr;
|
|
|
|
for (var i = 0; i < imgs.length; i++) {
|
|
|
|
var img = imgs[i];
|
|
|
|
if (candidateImageNames[img.name]) {
|
|
|
|
imgId = img.id;
|
|
|
|
imgRepr = f('%s@%s', img.name, img.version);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-09-29 21:45:34 +03:00
|
|
|
|
2016-02-09 22:23:55 +02:00
|
|
|
t.ok(imgId, f('latest available base/minimal image: %s (%s)',
|
|
|
|
imgId, imgRepr));
|
|
|
|
cb(err, imgId);
|
2015-09-29 21:45:34 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-01 10:31:00 +03:00
|
|
|
|
2016-02-09 22:23:55 +02:00
|
|
|
/*
|
|
|
|
* Find and return an package that can be used for test provisions.
|
|
|
|
*
|
|
|
|
* @param {Tape} t - tape test object
|
|
|
|
* @param {Function} cb - `function (err, pkgId)`
|
|
|
|
* where `pkgId` is an package identifier (a name, shortid, or id).
|
|
|
|
*/
|
|
|
|
function getTestPkg(t, cb) {
|
|
|
|
if (CONFIG.package) {
|
|
|
|
t.ok(CONFIG.package, 'package from config: ' + CONFIG.package);
|
|
|
|
cb(null, CONFIG.package);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
safeTriton(t, ['pkg', 'ls', '-j'], function (err, stdout) {
|
|
|
|
var pkgs = jsonStreamParse(stdout);
|
|
|
|
// Smallest RAM first.
|
|
|
|
tabula.sortArrayOfObjects(pkgs, ['memory']);
|
|
|
|
var pkgId = pkgs[0].id;
|
|
|
|
t.ok(pkgId, f('smallest (RAM) available package: %s (%s)',
|
|
|
|
pkgId, pkgs[0].name));
|
|
|
|
cb(null, pkgId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function jsonStreamParse(s) {
|
|
|
|
var results = [];
|
|
|
|
var lines = s.trim().split('\n');
|
|
|
|
for (var i = 0; i < lines.length; i++) {
|
|
|
|
var line = lines[i].trim();
|
|
|
|
if (line) {
|
|
|
|
results.push(JSON.parse(line));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-08 21:59:45 +02:00
|
|
|
/*
|
|
|
|
* Create a TritonApi client using the CLI.
|
|
|
|
*/
|
|
|
|
function createClient() {
|
|
|
|
return mod_triton.createClient({
|
|
|
|
log: LOG,
|
|
|
|
profile: CONFIG.profile,
|
|
|
|
configDir: '~/.triton' // piggy-back on Triton CLI config dir
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-01 10:31:00 +03:00
|
|
|
// --- exports
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
CONFIG: CONFIG,
|
|
|
|
triton: triton,
|
2015-09-29 21:45:34 +03:00
|
|
|
safeTriton: safeTriton,
|
2015-12-08 21:59:45 +02:00
|
|
|
createClient: createClient,
|
2016-02-09 22:23:55 +02:00
|
|
|
getTestImg: getTestImg,
|
|
|
|
getTestPkg: getTestPkg,
|
|
|
|
jsonStreamParse: jsonStreamParse,
|
|
|
|
|
2015-09-01 10:31:00 +03:00
|
|
|
ifErr: testcommon.ifErr
|
|
|
|
};
|