Skip to content

Commit

Permalink
cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
apocas committed Apr 5, 2020
1 parent 8216b94 commit fb39863
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 45 deletions.
6 changes: 3 additions & 3 deletions examples/startvm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 12 additions & 9 deletions lib/firecracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
80 changes: 51 additions & 29 deletions test/firefracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
});
});

Expand Down
5 changes: 1 addition & 4 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit fb39863

Please sign in to comment.