Fixed stubs for test suite

Closes: #1280
Change-type: patch
Signed-off-by: Miguel Casqueira <miguel@balena.io>
This commit is contained in:
Miguel Casqueira 2020-05-10 21:03:16 -04:00
parent 2fa9e638f1
commit f6aa4dd015
2 changed files with 65 additions and 22 deletions

View File

@ -6,30 +6,31 @@ process.env.DATABASE_PATH_2 = './test/data/database2.sqlite';
process.env.DATABASE_PATH_3 = './test/data/database3.sqlite';
process.env.LED_FILE = './test/data/led_file';
import * as dbus from 'dbus';
import { DBusError, DBusInterface } from 'dbus';
import { stub } from 'sinon';
import * as dbus from 'dbus';
// Stub the dbus objects to dynamically generate the methods
// on request
stub(dbus, 'getBus').returns({
getInterface: (
_obj: unknown,
cb: (err: Error | undefined, iface: dbus.DBusInterface) => void,
_serviceName: string,
_objectPath: string,
_interfaceName: string,
interfaceCb: (err: null | DBusError, iface: DBusInterface) => void,
) => {
return cb(
undefined,
new Proxy(
{},
{
get(_target, name) {
console.log(`Dbus method ${String(name)} requested`);
return () => {
/* noop */
};
},
},
) as any,
);
interfaceCb(null, {
Get: (
_unitName: string,
_property: string,
getCb: (err: null | Error, value: unknown) => void,
) => {
getCb(null, 'this is the value');
},
GetUnit: (
_unitName: string,
getUnitCb: (err: null | Error, unitPath: string) => void,
) => {
getUnitCb(null, 'this is the unit path');
},
} as any);
},
} as any);

View File

@ -1,10 +1,52 @@
import { SinonStub, stub } from 'sinon';
import APIBinder from '../src/api-binder';
import { ApplicationManager } from '../src/application-manager';
import DeviceState from '../src/device-state';
import * as constants from '../src/lib/constants';
import { DockerUtils as Docker } from '../src/lib/docker-utils';
import { Supervisor } from '../src/supervisor';
import { expect } from './lib/chai-config';
describe('Startup', () => {
it('should startup correctly', function() {
let initClientStub: SinonStub;
let startStub: SinonStub;
let vpnStatusPathStub: SinonStub;
let appManagerStub: SinonStub;
let deviceStateStub: SinonStub;
let dockerStub: SinonStub;
before(() => {
initClientStub = stub(APIBinder.prototype as any, 'initClient').returns(
Promise.resolve(),
);
startStub = stub(APIBinder.prototype as any, 'start').returns(
Promise.resolve(),
);
appManagerStub = stub(ApplicationManager.prototype, 'init').returns(
Promise.resolve(),
);
vpnStatusPathStub = stub(constants, 'vpnStatusPath').returns('');
deviceStateStub = stub(DeviceState.prototype as any, 'applyTarget').returns(
Promise.resolve(),
);
dockerStub = stub(Docker.prototype, 'listContainers').returns(
Promise.resolve([]),
);
});
after(() => {
initClientStub.restore();
startStub.restore();
appManagerStub.restore();
vpnStatusPathStub.restore();
deviceStateStub.restore();
dockerStub.restore();
});
it('should startup correctly', async () => {
const supervisor = new Supervisor();
expect(supervisor.init()).to.not.throw;
expect(await supervisor.init()).to.not.throw;
// Cast as any to access private properties
const anySupervisor = supervisor as any;
expect(anySupervisor.db).to.not.be.null;