| 
									
										
										
										
											2015-09-24 17:48:26 -04: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; | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  | var os = require('os'); | 
					
						
							|  |  |  | var test = require('tape'); | 
					
						
							| 
									
										
										
										
											2015-09-24 17:48:26 -04:00
										 |  |  | var vasync = require('vasync'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  | var common = require('../../lib/common'); | 
					
						
							| 
									
										
										
										
											2015-09-24 17:48:26 -04:00
										 |  |  | var h = require('./helpers'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  | // --- globals
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  | var INST_ALIAS = f('nodetritontest-managewf-%s', os.hostname()); | 
					
						
							| 
									
										
										
										
											2015-09-24 17:48:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  | var opts = { | 
					
						
							| 
									
										
										
										
											2015-10-07 12:19:26 -07:00
										 |  |  |     skip: !h.CONFIG.allowWriteActions | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-09-24 17:48:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | // global variable to hold vm instance JSON
 | 
					
						
							|  |  |  | var instance; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 12:24:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-24 17:48:26 -04:00
										 |  |  | // --- Tests
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  | if (opts.skip) { | 
					
						
							| 
									
										
										
										
											2015-10-07 12:19:26 -07:00
										 |  |  |     console.error('** skipping %s tests', __filename); | 
					
						
							|  |  |  |     console.error('** set "allowWriteActions" in test config to enable'); | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-25 12:24:37 -07:00
										 |  |  | test('triton manage workflow', opts, function (tt) { | 
					
						
							| 
									
										
										
										
											2016-02-17 00:13:29 +11:00
										 |  |  |     h.printConfig(tt); | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     tt.test('  cleanup existing inst with alias ' + INST_ALIAS, function (t) { | 
					
						
							| 
									
										
										
										
											2016-02-17 00:13:29 +11:00
										 |  |  |         h.deleteTestInst(t, INST_ALIAS, function (err) { | 
					
						
							|  |  |  |             t.ifErr(err); | 
					
						
							|  |  |  |             t.end(); | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var imgId; | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |     tt.test('  setup: find test image', function (t) { | 
					
						
							|  |  |  |         h.getTestImg(t, function (err, imgId_) { | 
					
						
							|  |  |  |             t.ifError(err, 'getTestImg' + (err ? ': ' + err : '')); | 
					
						
							|  |  |  |             imgId = imgId_; | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  |             t.end(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var pkgId; | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |     tt.test('  setup: find test package', function (t) { | 
					
						
							|  |  |  |         h.getTestPkg(t, function (err, pkgId_) { | 
					
						
							|  |  |  |             t.ifError(err, 'getTestPkg' + (err ? ': ' + err : '')); | 
					
						
							|  |  |  |             pkgId = pkgId_; | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  |             t.end(); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-09-29 14:45:34 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-09-25 12:24:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  |     // create a test machine (blocking) and output JSON
 | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |     tt.test('  setup: triton create', function (t) { | 
					
						
							| 
									
										
										
										
											2015-12-07 11:28:59 -08:00
										 |  |  |         var argv = [ | 
					
						
							|  |  |  |             'create', | 
					
						
							|  |  |  |             '-wj', | 
					
						
							|  |  |  |             '-m', 'foo=bar', | 
					
						
							|  |  |  |             '--script', __dirname + '/script-log-boot.sh', | 
					
						
							| 
									
										
										
										
											2015-12-30 16:11:54 -08:00
										 |  |  |             '--tag', 'blah=bling', | 
					
						
							| 
									
										
										
										
											2015-12-07 11:28:59 -08:00
										 |  |  |             '-n', INST_ALIAS, | 
					
						
							|  |  |  |             imgId, pkgId | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |         h.safeTriton(t, argv, function (err, stdout) { | 
					
						
							|  |  |  |             var lines = h.jsonStreamParse(stdout); | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  |             instance = lines[1]; | 
					
						
							|  |  |  |             t.equal(lines[0].id, lines[1].id, 'correct UUID given'); | 
					
						
							| 
									
										
										
										
											2015-12-07 11:28:59 -08:00
										 |  |  |             t.equal(lines[0].metadata.foo, 'bar', 'foo metadata set'); | 
					
						
							|  |  |  |             t.ok(lines[0].metadata['user-script'], 'user-script set'); | 
					
						
							| 
									
										
										
										
											2015-12-30 16:11:54 -08:00
										 |  |  |             t.equal(lines[0].tags.blah, 'bling', 'blah tag set'); | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  |             t.equal(lines[1].state, 'running', 'correct machine state'); | 
					
						
							| 
									
										
										
										
											2015-09-24 17:48:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  |             t.end(); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-09-24 17:48:26 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  |     // test `triton instance -j` with the UUID, the alias, and the short ID
 | 
					
						
							| 
									
										
										
										
											2016-01-04 10:45:13 -08:00
										 |  |  |     tt.test('  triton instance get', function (t) { | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  |         var uuid = instance.id; | 
					
						
							|  |  |  |         var shortId = common.uuidToShortId(uuid); | 
					
						
							|  |  |  |         vasync.parallel({ | 
					
						
							|  |  |  |             funcs: [ | 
					
						
							|  |  |  |                 function (cb) { | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |                     h.safeTriton(t, ['instance', 'get', '-j', INST_ALIAS], cb); | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  |                 }, | 
					
						
							|  |  |  |                 function (cb) { | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |                     h.safeTriton(t, ['instance', 'get', '-j', uuid], cb); | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  |                 }, | 
					
						
							|  |  |  |                 function (cb) { | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |                     h.safeTriton(t, ['instance', 'get', '-j', shortId], cb); | 
					
						
							| 
									
										
										
										
											2015-09-25 12:24:37 -07:00
										 |  |  |                 } | 
					
						
							|  |  |  |             ] | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04: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-24 17:48:26 -04:00
										 |  |  |                 }); | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  |             } catch (e) { | 
					
						
							|  |  |  |                 t.fail('failed to parse JSON'); | 
					
						
							|  |  |  |                 t.end(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-07 11:28:59 -08:00
										 |  |  |             t.equal(output[0].metadata.foo, 'bar', 'foo metadata set'); | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  |             output.forEach(function (res) { | 
					
						
							|  |  |  |                 t.deepEqual(output[0], res, 'same data'); | 
					
						
							| 
									
										
										
										
											2015-09-24 17:48:26 -04:00
										 |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  |             t.end(); | 
					
						
							| 
									
										
										
										
											2015-09-24 17:48:26 -04:00
										 |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 12:30:46 -08:00
										 |  |  |     // Remove instance. Add a test timeout, because '-w' on delete doesn't
 | 
					
						
							|  |  |  |     // have a way to know if the attempt failed or if it is just taking a
 | 
					
						
							|  |  |  |     // really long time.
 | 
					
						
							|  |  |  |     tt.test('  triton delete', {timeout: 10 * 60 * 1000}, function (t) { | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |         h.safeTriton(t, ['delete', '-w', instance.id], function () { | 
					
						
							| 
									
										
										
										
											2015-09-25 13:45:16 -04:00
										 |  |  |             t.end(); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-09-24 17:48:26 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-09-25 12:24:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-07 11:28:59 -08:00
										 |  |  |     // TODO: would be nice to have a `triton ssh cat /var/log/boot.log` to
 | 
					
						
							|  |  |  |     //      verify the user-script worked.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 12:53:34 -04:00
										 |  |  |     // create a test machine (non-blocking)
 | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  |     tt.test('  triton create', function (t) { | 
					
						
							|  |  |  |         h.safeTriton(t, ['create', '-jn', INST_ALIAS, imgId, pkgId], | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |             function (err, stdout) { | 
					
						
							| 
									
										
										
										
											2015-09-29 12:53:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // 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
 | 
					
						
							| 
									
										
										
										
											2016-01-04 13:08:16 -08:00
										 |  |  |     tt.test('  triton inst wait', function (t) { | 
					
						
							|  |  |  |         h.safeTriton(t, ['inst', 'wait', instance.id], | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |             function (err, stdout) { | 
					
						
							| 
									
										
										
										
											2015-09-29 12:53:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // 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
 | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  |     tt.test('  triton stop', function (t) { | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |         h.safeTriton(t, ['stop', '-w', INST_ALIAS], function (err, stdout) { | 
					
						
							| 
									
										
										
										
											2015-09-29 12:53:34 -04:00
										 |  |  |             t.ok(stdout.match(/^Stop instance/, 'correct stdout')); | 
					
						
							|  |  |  |             t.end(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // wait for the machine to stop
 | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  |     tt.test('  triton confirm stopped', function (t) { | 
					
						
							| 
									
										
										
										
											2016-01-04 10:45:13 -08:00
										 |  |  |         h.safeTriton(t, {json: true, args: ['inst', 'get', '-j', INST_ALIAS]}, | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |                 function (err, d) { | 
					
						
							| 
									
										
										
										
											2015-09-29 12:53:34 -04:00
										 |  |  |             instance = d; | 
					
						
							|  |  |  |             t.equal(d.state, 'stopped', 'machine stopped'); | 
					
						
							|  |  |  |             t.end(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // start the machine
 | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  |     tt.test('  triton start', function (t) { | 
					
						
							|  |  |  |         h.safeTriton(t, ['start', '-w', INST_ALIAS], | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |                 function (err, stdout) { | 
					
						
							| 
									
										
										
										
											2015-09-29 12:53:34 -04:00
										 |  |  |             t.ok(stdout.match(/^Start instance/, 'correct stdout')); | 
					
						
							|  |  |  |             t.end(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // wait for the machine to start
 | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  |     tt.test('  confirm running', function (t) { | 
					
						
							| 
									
										
										
										
											2016-01-04 10:45:13 -08:00
										 |  |  |         h.safeTriton(t, {json: true, args: ['inst', 'get', '-j', INST_ALIAS]}, | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |                 function (err, d) { | 
					
						
							| 
									
										
										
										
											2015-09-29 12:53:34 -04:00
										 |  |  |             instance = d; | 
					
						
							|  |  |  |             t.equal(d.state, 'running', 'machine running'); | 
					
						
							|  |  |  |             t.end(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // remove test instance
 | 
					
						
							| 
									
										
										
										
											2015-10-06 23:09:52 -07:00
										 |  |  |     tt.test('  cleanup (triton delete)', function (t) { | 
					
						
							| 
									
										
										
										
											2016-02-09 12:23:55 -08:00
										 |  |  |         h.safeTriton(t, ['delete', '-w', instance.id], function () { | 
					
						
							| 
									
										
										
										
											2015-09-29 12:53:34 -04:00
										 |  |  |             t.end(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-24 17:48:26 -04:00
										 |  |  | }); |