Skip to content

Commit 992c7dc

Browse files
Fix integration tests.
1 parent 1646d8c commit 992c7dc

22 files changed

+225
-157
lines changed

lib/virtualbox.js

Lines changed: 103 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,56 @@ const execFile = require('child_process').execFile,
4040
},
4141
categories: { default: { appenders: ['out'], level: 'info' } },
4242
},
43+
defaultLoggerFn = () => {
44+
log4js.configure(defaultLoggingConfig);
45+
return log4js.getLogger('VirtualBox');
46+
},
47+
defaultLogger = defaultLoggerFn(),
48+
defaultvboxmanage = function (cmd, callback) {
49+
try {
50+
this._executor(this._vBoxManageBinary, cmd)
51+
.then(({ err, stdout, stderr }) => callback(err, stdout, stderr))
52+
.catch(defaultErrorHandler);
53+
} catch (err) {
54+
this._logging.error(err);
55+
}
56+
},
4357
defaultErrorHandler = (err) => {
44-
throw err;
58+
console.error(err);
4559
},
4660
allowedBinaries = ['VBoxControl'];
4761

4862
class Virtualbox {
49-
constructor(logging = defaultLoggingConfig, executor = defaultExecutor) {
50-
log4js.configure(logging);
51-
this._logging = log4js.getLogger('VirtualBox');
63+
constructor(logging = defaultLogger, executor = defaultExecutor) {
64+
this._logging = logging;
5265
this._executor = executor;
5366
this._setVboxManageBinary();
5467
allowedBinaries.push(this._vBoxManageBinary);
55-
this._logging.info(allowedBinaries);
68+
this._logging.debug(allowedBinaries);
5669
this._detectVboxVersion();
57-
this.storage = new VboxStorage(this._logging, this._executor);
58-
this.guestproperty = new VboxGuestProperty(this._logging, this._executor);
59-
this.extradata = new VboxExtraData(this._logging, this._executor);
70+
this.storage = new VboxStorage(
71+
this._logging,
72+
this._executor,
73+
this._vBoxManageBinary
74+
);
75+
this.guestproperty = new VboxGuestProperty(
76+
this._logging,
77+
this._executor,
78+
this._vBoxManageBinary
79+
);
80+
this.extradata = new VboxExtraData(
81+
this._logging,
82+
this._executor,
83+
this._vBoxManageBinary
84+
);
6085
}
6186

6287
static create(logging, executor) {
6388
return new Virtualbox({ ...defaultLoggingConfig, ...logging }, executor);
6489
}
6590

66-
vboxmanage(cmd, callback) {
67-
try {
68-
this._executor(this._vBoxManageBinary, cmd)
69-
.then(callback)
70-
.catch(defaultErrorHandler);
71-
} catch (err) {
72-
this._logging.error(err);
73-
}
74-
}
91+
vboxmanage = defaultvboxmanage;
92+
SCAN_CODES = require('./scan-codes');
7593

7694
pause(vmname, callback) {
7795
this._logging.info('Pausing VM "%s"', vmname);
@@ -105,7 +123,7 @@ class Virtualbox {
105123
};
106124

107125
this._logging.info('Listing VMs');
108-
this.vboxmanage(['list', 'runningvms'], function (_, stdout) {
126+
this.vboxmanage(['list', 'runningvms'], (_, stdout) => {
109127
var _runningvms = parse_listdata(stdout);
110128
this.vboxmanage(['list', 'vms'], function (error, fullStdout) {
111129
var _all = parse_listdata(fullStdout);
@@ -188,9 +206,7 @@ class Virtualbox {
188206

189207
acpipowerbutton(vmname, callback) {
190208
this._logging.info('ACPI power button VM "%s"', vmname);
191-
this.vboxmanage(['controlvm', vmname, 'acpipowerbutton'], function ({
192-
error,
193-
}) {
209+
this.vboxmanage(['controlvm', vmname, 'acpipowerbutton'], function (error) {
194210
callback(error);
195211
});
196212
}
@@ -330,7 +346,7 @@ class Virtualbox {
330346
}
331347

332348
isRunning(vmname, callback) {
333-
this.vboxmanage(['list', 'runningvms'], function (error, stdout) {
349+
this.vboxmanage(['list', 'runningvms'], (error, stdout) => {
334350
this._logging.info(
335351
'Checking virtual machine "%s" is running or not',
336352
vmname
@@ -354,16 +370,20 @@ class Virtualbox {
354370
return s;
355371
})
356372
.join(' ');
357-
logging.info('Sending VM "%s" keyboard scan codes "%s"', vmname, codeStr);
358-
vboxmanage(['controlvm', vmname, 'keyboardputscancode', codeStr], function (
359-
error,
360-
stdout
361-
) {
362-
callback(error, stdout);
363-
});
373+
this._logging.info(
374+
'Sending VM "%s" keyboard scan codes "%s"',
375+
vmname,
376+
codeStr
377+
);
378+
this.vboxmanage(
379+
['controlvm', vmname, 'keyboardputscancode', codeStr],
380+
function (error, stdout) {
381+
callback(error, stdout);
382+
}
383+
);
364384
}
365385

366-
vmExec(options, callback) {
386+
exec(options, callback) {
367387
var vm = options.vm || options.name || options.vmname || options.title,
368388
username = options.user || options.username || 'Guest',
369389
password = options.pass || options.passwd || options.password,
@@ -385,12 +405,10 @@ class Virtualbox {
385405
params = '';
386406
}
387407

388-
guestproperty.os(vm, getOSTypeCb);
389-
390408
const getOSTypeCb = (os_type) => {
391409
var cmd = ['guestcontrol', vm];
392-
var runcmd = vbox_version === 5 ? ['run'] : ['execute', '--image'];
393-
cmd.push(runcmd);
410+
var runcmd = this._vboxVersion > 5 ? ['run'] : ['execute', '--image'];
411+
cmd = [...cmd, ...runcmd];
394412
switch (os_type) {
395413
case known_OS_types.WINDOWS:
396414
path = path.replace(/\\/g, '\\\\');
@@ -422,9 +440,11 @@ class Virtualbox {
422440
callback(error, stdout);
423441
});
424442
};
443+
444+
this.guestproperty.os(vm, getOSTypeCb);
425445
}
426446

427-
vmKill(options, callback) {
447+
kill(options, callback) {
428448
options = options || {};
429449
var vm = options.vm || options.name || options.vmname || options.title,
430450
path =
@@ -436,10 +456,10 @@ class Virtualbox {
436456
options.run,
437457
image_name = options.image_name || path;
438458

439-
guestproperty.os(vm, function (os_type) {
459+
this.guestproperty.os(vm, function (os_type) {
440460
switch (os_type) {
441461
case known_OS_types.WINDOWS:
442-
vmExec(
462+
exec(
443463
{
444464
vm: vm,
445465
user: options.user,
@@ -452,7 +472,7 @@ class Virtualbox {
452472
break;
453473
case known_OS_types.MAC:
454474
case known_OS_types.LINUX:
455-
vmExec(
475+
exec(
456476
{
457477
vm: vm,
458478
user: options.user,
@@ -487,7 +507,6 @@ class Virtualbox {
487507
_detectVboxVersion() {
488508
this._executor(this._vBoxManageBinary, ['--version']).then(
489509
({ error, stdout }) => {
490-
this._logging.info(arguments);
491510
if (error) {
492511
throw error;
493512
} else {
@@ -503,11 +522,14 @@ class Virtualbox {
503522
}
504523

505524
class VboxStorage {
506-
constructor(logging, executor) {
525+
constructor(logging, executor, vBoxManageBinary) {
507526
this._logging = logging;
508527
this._executor = executor;
528+
this._vBoxManageBinary = vBoxManageBinary;
509529
}
510530

531+
vboxmanage = defaultvboxmanage;
532+
511533
addCtl(options, callback) {
512534
var vm = options.vm || options.name || options.vmname || options.title,
513535
device_name = options.perhiperal_name || 'IDE',
@@ -554,20 +576,25 @@ class VboxStorage {
554576
}
555577

556578
class VboxGuestProperty {
557-
constructor(logging, executor) {
579+
constructor(logging, executor, vBoxManageBinary) {
558580
this._logging = logging;
559581
this._executor = executor;
582+
this._vBoxManageBinary = vBoxManageBinary;
560583
this.os_type = null;
561584
}
585+
586+
vboxmanage = defaultvboxmanage;
587+
562588
get(options, callback) {
563589
var vm = options.vm || options.name || options.vmname || options.title,
564590
key = options.key,
565591
value = options.defaultValue || options.value;
566592

567-
guestproperty.os(vm, getOSTypeCallback);
568-
569-
function getOSTypeCallback(os_type) {
570-
vboxmanage(['guestproperty', 'get', vm, key], function (error, stdout) {
593+
this.os(vm, (os_type) => {
594+
this.vboxmanage(['guestproperty', 'get', vm, key], function (
595+
error,
596+
stdout
597+
) {
571598
if (error) {
572599
throw error;
573600
}
@@ -577,54 +604,58 @@ class VboxGuestProperty {
577604
}
578605
callback(value);
579606
});
580-
}
607+
});
581608
}
582609

583610
os(vmname, callback) {
584-
function getOSTypeCallback(error, stdout, stderr) {
585-
if (error) {
586-
throw error;
587-
}
588-
589-
// The ostype is matched against the ID attribute of 'vboxmanage list ostypes'
590-
if (stdout.indexOf('ostype="Windows') !== -1) {
591-
this.os_type = known_OS_types.WINDOWS;
592-
} else if (['ostype="MacOS', 'ostype="Mac OS machine'].includes(stdout)) {
593-
this.os_type = known_OS_types.MAC;
594-
} else {
595-
this.os_type = known_OS_types.LINUX;
596-
}
597-
logging.debug('Detected guest OS as: ' + this.os_type);
598-
callback(this.os_type);
599-
}
600-
601611
if (this.os_type) {
602612
return callback(this.os_type);
603613
}
604614

605615
try {
606-
vboxmanage(
616+
this.vboxmanage(
607617
['showvminfo', '--machinereadable', vmname],
608-
getOSTypeCallback
618+
(error, stdout, _) => {
619+
if (error) {
620+
throw error;
621+
}
622+
623+
// The ostype is matched against the ID attribute of 'vboxmanage list ostypes'
624+
if (stdout.indexOf('ostype="Windows') !== -1) {
625+
this.os_type = known_OS_types.WINDOWS;
626+
} else if (
627+
['ostype="MacOS', 'ostype="Mac OS machine'].includes(stdout)
628+
) {
629+
this.os_type = known_OS_types.MAC;
630+
} else {
631+
this.os_type = known_OS_types.LINUX;
632+
}
633+
this._logging.debug('Detected guest OS as: ' + this.os_type);
634+
callback(this.os_type);
635+
}
609636
);
610637
} catch (e) {
611-
logging.error(e);
612-
logging.info('Could not showvminfo for %s', vmname);
638+
this._logging.error(e);
639+
this._logging.info('Could not showvminfo for %s', vmname);
613640
}
614641
}
615642
}
616643

617644
class VboxExtraData {
618-
constructor(logging, executor) {
645+
constructor(logging, executor, vBoxManageBinary) {
619646
this._logging = logging;
620647
this._executor = executor;
648+
this._vBoxManageBinary = vBoxManageBinary;
621649
}
650+
651+
vboxmanage = defaultvboxmanage;
652+
622653
get(options, callback) {
623654
var vm = options.vm || options.name || options.vmname || options.title,
624655
key = options.key,
625656
value = options.defaultValue || options.value;
626657

627-
vboxmanage(['getextradata', vm, key], function (error, stdout) {
658+
this.vboxmanage(['getextradata', vm, key], function (error, stdout) {
628659
if (error) {
629660
callback(error);
630661
return;
@@ -642,8 +673,8 @@ class VboxExtraData {
642673
key = options.key,
643674
value = options.defaultValue || options.value;
644675

645-
var cmd = 'setextradata "' + vm + '" "' + key + '" "' + value + '"';
646-
vboxmanage(cmd, function (error, stdout) {
676+
var cmd = ['setextradata', vm, key, value];
677+
this.vboxmanage(cmd, function (error, stdout) {
647678
callback(error);
648679
});
649680
}
@@ -680,3 +711,4 @@ class VboxExtraData {
680711

681712
module.exports = new Virtualbox();
682713
module.exports.create = Virtualbox.create;
714+
module.exports.Virtualbox = Virtualbox;

0 commit comments

Comments
 (0)