From fb398637918595be84407a31804d0ef8921e150d Mon Sep 17 00:00:00 2001 From: Pedro Dias Date: Sun, 5 Apr 2020 23:35:32 +0100 Subject: [PATCH] cleaning --- examples/startvm.js | 6 ++-- lib/firecracker.js | 21 +++++++----- test/firefracker.js | 80 +++++++++++++++++++++++++++++---------------- test/helper.js | 5 +-- 4 files changed, 67 insertions(+), 45 deletions(-) diff --git a/examples/startvm.js b/examples/startvm.js index 04abf3d..55cd514 100644 --- a/examples/startvm.js +++ b/examples/startvm.js @@ -6,16 +6,16 @@ var firecracker = new Firecracker({ socketPath: '/tmp/firecracker.socket' }); firecracker.bootSource({ 'kernel_image_path': os.tmpdir() + '/hello-vmlinux.bin', 'boot_args': 'console=ttyS0 reboot=k panic=1 pci=off' -}).then(function (data) { +}).then(function () { var drive = firecracker.drive('rootfs'); return drive.updatePreboot({ 'path_on_host': os.tmpdir() + '/hello-rootfs.ext4', 'is_root_device': true, 'is_read_only': false }); -}).then(function (data) { +}).then(function () { return firecracker.action('InstanceStart'); -}).then(function (data) { +}).then(function () { console.log('MicroVM booted!'); }).catch(function (err) { console.log(err); diff --git a/lib/firecracker.js b/lib/firecracker.js index 3bc1138..17c34d9 100644 --- a/lib/firecracker.js +++ b/lib/firecracker.js @@ -276,18 +276,21 @@ Firecracker.prototype.spawn = function (binPath) { var self = this; binPath = binPath || '/usr/bin/firecracker'; - this.child = child_process.spawn(binPath, ['--api-sock', this.options.socketPath]); + return new Promise(function (resolve, reject) { - this.child.on('exit', function (code, signal) { - fs.unlink(self.options.socketPath, () => { }); - self.child = undefined; - }); + self.child = child_process.spawn(binPath, ['--api-sock', self.options.socketPath]); - this.child.on('error', function (err) { - fs.unlink(self.options.socketPath, () => { }); - }); + self.child.on('exit', function (code, signal) { + fs.unlink(self.options.socketPath, () => { }); + self.child = undefined; + }); - return this.child; + self.child.on('error', function (err) { + fs.unlink(self.options.socketPath, () => { }); + }); + + resolve(self.child); + }); }; Firecracker.prototype.kill = function () { diff --git a/test/firefracker.js b/test/firefracker.js index ab6e48a..6cb9ddf 100644 --- a/test/firefracker.js +++ b/test/firefracker.js @@ -2,58 +2,80 @@ const os = require('os'); const expect = require('chai').expect; var firecracker = require('./helper').firecracker; +before(async () => { + try { + let process = await firecracker.spawn(); + console.log('Firefracker started! ' + process.pid); + } catch (err) { + expect(err).to.be.null; + } +}); + describe('#firecracker', function () { describe('#images', function () { - it('should download kernel & filesystem images', function () { + it('should download kernel & filesystem images', async function () { var kernelImg = 'https://s3.amazonaws.com/spec.ccfc.min/img/hello/kernel/hello-vmlinux.bin'; var rootImg = 'https://s3.amazonaws.com/spec.ccfc.min/img/hello/fsfiles/hello-rootfs.ext4'; - return firecracker.downloadImage(kernelImg, os.tmpdir() + '/hello-vmlinux.bin').then(function () { - return firecracker.downloadImage(rootImg, os.tmpdir() + '/hello-rootfs.ext4'); - }).then(function () { - }).catch(function (err) { + try { + await firecracker.downloadImage(kernelImg, os.tmpdir() + '/hello-vmlinux.bin'); + await firecracker.downloadImage(rootImg, os.tmpdir() + '/hello-rootfs.ext4'); + } + catch (err) { expect(err).to.be.null; - }); + } }); - it('should use load the kernel image', function () { - return firecracker.bootSource({ - 'kernel_image_path': os.tmpdir() + '/hello-vmlinux.bin', - 'boot_args': 'console=ttyS0 reboot=k panic=1 pci=off' - }).then(function () { - }).catch(function (err) { + it('should use load the kernel image', async function () { + try { + await firecracker.bootSource({ + 'kernel_image_path': os.tmpdir() + '/hello-vmlinux.bin', + 'boot_args': 'console=ttyS0 reboot=k panic=1 pci=off' + }); + } + catch (err) { expect(err).to.be.null; - }); + } }); - it('should use load the filesystem image', function () { + it('should use load the filesystem image', async function () { var drive = firecracker.drive('rootfs'); - return drive.updatePreboot({ - 'path_on_host': os.tmpdir() + '/hello-rootfs.ext4', - 'is_root_device': true, - 'is_read_only': false - }).then(function () { - }).catch(function (err) { + try { + await drive.updatePreboot({ + 'path_on_host': os.tmpdir() + '/hello-rootfs.ext4', + 'is_root_device': true, + 'is_read_only': false + }); + } + catch (err) { expect(err).to.be.null; - }); + } }); }); describe('#firestarter', function () { - it('should get info', function () { - return firecracker.info().then(function (data) { + it('should get info', async function () { + try { + const data = await firecracker.info(); expect(data).to.be.ok; - }).catch(function (err) { + expect(data.state).to.equal('Uninitialized'); + } + catch (err) { expect(err).to.be.null; - }); + } }); - it('should start microvm', function () { - return firecracker.action('InstanceStart').then(function () { - }).catch(function (err) { + it('should start microvm', async function () { + try { + await firecracker.action('InstanceStart'); + const data = await firecracker.info(); + expect(data).to.be.ok; + expect(data.state).to.equal('Running'); + } + catch (err) { expect(err).to.be.null; - }); + } }); }); diff --git a/test/helper.js b/test/helper.js index d6857d7..7d73e01 100644 --- a/test/helper.js +++ b/test/helper.js @@ -1,8 +1,5 @@ var Firecracker = require('../lib/firecracker'); - -var firecracker = new Firecracker({ socketPath: '/tmp/firecracker.socket' }); -//var process = firecracker.spawn(); -//console.log('Firefracker started! ' + process.pid); +firecracker = new Firecracker({ socketPath: '/tmp/firecracker.socket' }); module.exports = { 'firecracker': firecracker