Skip to content

Instantly share code, notes, and snippets.

@misterdjules
Created February 21, 2018 18:25
Show Gist options
  • Save misterdjules/c868cdd75ec870e7c24bfcc7aaa6a862 to your computer and use it in GitHub Desktop.
Save misterdjules/c868cdd75ec870e7c24bfcc7aaa6a862 to your computer and use it in GitHub Desktop.
diff --git a/test/cnapi.test.js b/test/cnapi.test.js
index 6c67a44..169b7bc 100644
--- a/test/cnapi.test.js
+++ b/test/cnapi.test.js
@@ -8,10 +8,12 @@
* Copyright (c) 2018, Joyent, Inc.
*/
+var assert = require('assert-plus');
var bunyan = require('bunyan');
var test = require('tape');
var util = require('util');
var uuid = require('uuid');
+var vasync = require('vasync');
var CNAPI = require('../lib/index').CNAPI;
@@ -27,6 +29,43 @@ var testVmAlias = 'nodesdcclientstest-cnapi-' + testVmUuid.split('-')[0];
var TASK = null;
var CUSTOMER = process.env.UFDS_ADMIN_UUID;
+// --- Helpers
+
+function waitForTaskAndCheckVmState(options, cb) {
+ assert.object(options, 'options');
+ assert.object(options.cnapiClient, 'options.cnapiClient');
+ assert.uuid(options.taskId, 'options.taskId');
+ assert.string(options.vmState, 'options.vmState');
+ assert.uuid(options.vmUuid, 'options.vmUuid');
+ assert.func(cb, 'cb');
+
+ var cnapi = options.cnapiClient;
+ var taskId = options.taskId;
+ var vmState = options.vmState;
+ var vmUuid = options.vmUuid;
+
+ vasync.pipeline({funcs: [
+ function waitForTask(_, next) {
+ cnapi.waitTask(taskId, {}, next);
+ },
+ function checkVmState(_, next) {
+ cnapi.getVm(SERVER, vmUuid, function (getVmErr, vm) {
+ if (getVmErr) {
+ next(getVmErr);
+ return;
+ }
+
+ if (!vm || vm.state !== vmState) {
+ next(new Error('Expected state: ' + vmState + ', got: ' +
+ (vm ? vm.state : undefined)));
+ } else {
+ next();
+ }
+ });
+ }
+ ]}, cb);
+}
+
// --- Tests
test('cnapi', function (tt) {
@@ -131,16 +170,14 @@ test('cnapi', function (tt) {
});
tt.test(' wait for running', function (t) {
- cnapi.waitTask(TASK, {}, function onTaskDone(taskErr, task) {
- t.ifError(taskErr);
- if (!taskErr) {
- cnapi.getVm(SERVER, testVmUuid, function (getVmErr, vm) {
- t.ok(vm && vm.state === 'running', 'VM is running');
- t.end();
- });
- } else {
- t.end();
- }
+ waitForTaskAndCheckVmState({
+ taskId: TASK,
+ vmUuid: testVmUuid,
+ vmState: 'running',
+ cnapiClient: cnapi
+ }, function waitDone(waitErr) {
+ t.ifError(waitErr);
+ t.end();
});
});
@@ -154,16 +191,14 @@ test('cnapi', function (tt) {
});
tt.test(' wait for stopped', function (t) {
- cnapi.waitTask(TASK, {}, function onTaskDone(taskErr, task) {
- t.ifError(taskErr);
- if (!taskErr) {
- cnapi.getVm(SERVER, testVmUuid, function (getVmErr, vm) {
- t.ok(vm && vm.state === 'stopped', 'VM is stopped');
- t.end();
- });
- } else {
- t.end();
- }
+ waitForTaskAndCheckVmState({
+ taskId: TASK,
+ vmUuid: testVmUuid,
+ vmState: 'stopped',
+ cnapiClient: cnapi
+ }, function waitDone(waitErr) {
+ t.ifError(waitErr);
+ t.end();
});
});
@@ -177,16 +212,14 @@ test('cnapi', function (tt) {
});
tt.test(' wait for running', function (t) {
- cnapi.waitTask(TASK, {}, function onTaskDone(taskErr, task) {
- t.ifError(taskErr);
- if (!taskErr) {
- cnapi.getVm(SERVER, testVmUuid, function (getVmErr, vm) {
- t.ok(vm && vm.state === 'running', 'VM is running');
- t.end();
- });
- } else {
- t.end();
- }
+ waitForTaskAndCheckVmState({
+ taskId: TASK,
+ vmUuid: testVmUuid,
+ vmState: 'running',
+ cnapiClient: cnapi
+ }, function waitDone(waitErr) {
+ t.ifError(waitErr);
+ t.end();
});
});
@@ -200,16 +233,14 @@ test('cnapi', function (tt) {
});
tt.test(' wait for running', function (t) {
- cnapi.waitTask(TASK, {}, function onTaskDone(taskErr, task) {
- t.ifError(taskErr);
- if (!taskErr) {
- cnapi.getVm(SERVER, testVmUuid, function (getVmErr, vm) {
- t.ok(vm && vm.state === 'running', 'VM is running');
- t.end();
- });
- } else {
- t.end();
- }
+ waitForTaskAndCheckVmState({
+ taskId: TASK,
+ vmUuid: testVmUuid,
+ vmState: 'running',
+ cnapiClient: cnapi
+ }, function waitDone(waitErr) {
+ t.ifError(waitErr);
+ t.end();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment