2020-05-20 04:11:06 +00:00
|
|
|
import { stripIndent } from 'common-tags';
|
2021-04-26 19:54:04 +00:00
|
|
|
import { promises as fs } from 'fs';
|
2019-09-12 15:26:21 +00:00
|
|
|
import { Server } from 'net';
|
2020-05-28 17:15:33 +00:00
|
|
|
import { SinonSpy, SinonStub, spy, stub } from 'sinon';
|
2021-05-07 01:47:10 +00:00
|
|
|
import { expect } from 'chai';
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2020-06-02 13:29:05 +00:00
|
|
|
import prepare = require('./lib/prepare');
|
|
|
|
import * as config from '../src/config';
|
2020-07-21 15:25:47 +00:00
|
|
|
import * as deviceState from '../src/device-state';
|
2020-05-28 17:15:33 +00:00
|
|
|
import Log from '../src/lib/supervisor-console';
|
|
|
|
import balenaAPI = require('./lib/mocked-balena-api');
|
2020-06-02 13:29:05 +00:00
|
|
|
import { schema } from '../src/config/schema';
|
|
|
|
import ConfigJsonConfigBackend from '../src/config/configJson';
|
2020-05-16 13:08:14 +00:00
|
|
|
import * as TargetState from '../src/device-state/target-state';
|
2020-07-21 09:45:37 +00:00
|
|
|
import * as ApiHelper from '../src/lib/api-helper';
|
2021-04-29 13:14:18 +00:00
|
|
|
import supervisorVersion = require('../src/lib/supervisor-version');
|
2021-05-07 01:47:10 +00:00
|
|
|
import * as eventTracker from '../src/event-tracker';
|
2020-07-21 09:45:37 +00:00
|
|
|
|
|
|
|
import { TypedError } from 'typed-error';
|
|
|
|
import { DeviceNotFoundError } from '../src/lib/errors';
|
|
|
|
|
|
|
|
let ApiBinder: typeof import('../src/api-binder');
|
|
|
|
|
|
|
|
class ExpectedError extends TypedError {}
|
2020-05-19 14:03:00 +00:00
|
|
|
|
2019-09-12 15:26:21 +00:00
|
|
|
const initModels = async (obj: Dictionary<any>, filename: string) => {
|
2020-05-28 17:15:33 +00:00
|
|
|
await prepare();
|
2020-07-21 09:45:37 +00:00
|
|
|
|
2020-06-08 17:16:53 +00:00
|
|
|
// @ts-expect-error setting read-only property
|
2020-06-02 13:29:05 +00:00
|
|
|
config.configJsonBackend = new ConfigJsonConfigBackend(schema, filename);
|
2020-07-21 09:45:37 +00:00
|
|
|
await config.generateRequiredFields();
|
|
|
|
|
2020-06-08 17:16:53 +00:00
|
|
|
// @ts-expect-error using private properties
|
|
|
|
config.configJsonBackend.cache = await config.configJsonBackend.read();
|
2020-06-02 13:29:05 +00:00
|
|
|
await config.generateRequiredFields();
|
2019-09-12 15:26:21 +00:00
|
|
|
|
|
|
|
obj.logger = {
|
|
|
|
clearOutOfDateDBLogs: () => {
|
|
|
|
/* noop */
|
|
|
|
},
|
|
|
|
} as any;
|
|
|
|
|
2020-07-21 09:45:37 +00:00
|
|
|
ApiBinder = await import('../src/api-binder');
|
|
|
|
await ApiBinder.initialized;
|
|
|
|
obj.apiBinder = ApiBinder;
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2020-07-21 15:25:47 +00:00
|
|
|
await deviceState.initialized;
|
|
|
|
obj.deviceState = deviceState;
|
2019-09-12 15:26:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const mockProvisioningOpts = {
|
|
|
|
apiEndpoint: 'http://0.0.0.0:3000',
|
|
|
|
uuid: 'abcd',
|
|
|
|
deviceApiKey: 'averyvalidkey',
|
|
|
|
provisioningApiKey: 'anotherveryvalidkey',
|
|
|
|
apiTimeout: 30000,
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('ApiBinder', () => {
|
2020-07-21 09:45:37 +00:00
|
|
|
const defaultConfigBackend = config.configJsonBackend;
|
2019-09-12 15:26:21 +00:00
|
|
|
let server: Server;
|
|
|
|
|
2020-07-21 09:45:37 +00:00
|
|
|
before(async () => {
|
|
|
|
delete require.cache[require.resolve('../src/api-binder')];
|
2020-06-04 09:21:39 +00:00
|
|
|
|
2019-09-12 15:26:21 +00:00
|
|
|
spy(balenaAPI.balenaBackend!, 'registerHandler');
|
|
|
|
server = balenaAPI.listen(3000);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
2020-06-08 17:16:53 +00:00
|
|
|
// @ts-expect-error setting read-only property
|
2019-09-12 15:26:21 +00:00
|
|
|
balenaAPI.balenaBackend!.registerHandler.restore();
|
|
|
|
try {
|
|
|
|
server.close();
|
|
|
|
} catch (error) {
|
|
|
|
/* noop */
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// We do not support older OS versions anymore, so we only test this case
|
|
|
|
describe('on an OS with deviceApiKey support', () => {
|
|
|
|
const components: Dictionary<any> = {};
|
2021-05-07 01:47:10 +00:00
|
|
|
let eventTrackSpy: SinonSpy;
|
2020-07-21 09:45:37 +00:00
|
|
|
|
|
|
|
before(async () => {
|
2021-05-07 01:47:10 +00:00
|
|
|
eventTrackSpy = spy(eventTracker, 'track');
|
2020-07-21 09:45:37 +00:00
|
|
|
await initModels(components, '/config-apibinder.json');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
eventTrackSpy.resetHistory();
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
2020-06-02 13:29:05 +00:00
|
|
|
after(async () => {
|
2020-07-21 09:45:37 +00:00
|
|
|
eventTrackSpy.restore();
|
|
|
|
|
2020-06-08 17:16:53 +00:00
|
|
|
// @ts-expect-error setting read-only property
|
2020-06-02 13:29:05 +00:00
|
|
|
config.configJsonBackend = defaultConfigBackend;
|
|
|
|
await config.generateRequiredFields();
|
|
|
|
});
|
|
|
|
|
2020-07-21 09:45:37 +00:00
|
|
|
it('provisions a device', async () => {
|
|
|
|
const opts = await config.get('provisioningOptions');
|
|
|
|
await ApiHelper.provision(components.apiBinder.balenaApi, opts);
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2020-07-21 09:45:37 +00:00
|
|
|
expect(balenaAPI.balenaBackend!.registerHandler).to.be.calledOnce;
|
|
|
|
expect(eventTrackSpy).to.be.called;
|
|
|
|
expect(eventTrackSpy).to.be.calledWith('Device bootstrap success');
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2020-07-21 09:45:37 +00:00
|
|
|
// @ts-expect-error function does not exist on type
|
|
|
|
balenaAPI.balenaBackend!.registerHandler.resetHistory();
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
2020-05-19 14:03:00 +00:00
|
|
|
it('exchanges keys if resource conflict when provisioning', async () => {
|
|
|
|
// Get current config to extend
|
2020-06-02 13:29:05 +00:00
|
|
|
const currentConfig = await config.get('provisioningOptions');
|
2020-07-21 09:45:37 +00:00
|
|
|
|
2020-05-19 14:03:00 +00:00
|
|
|
// Stub config values so we have correct conditions
|
2020-06-02 13:29:05 +00:00
|
|
|
const configStub = stub(config, 'get').resolves({
|
2020-05-19 14:03:00 +00:00
|
|
|
...currentConfig,
|
|
|
|
registered_at: null,
|
|
|
|
provisioningApiKey: '123', // Previous test case deleted the provisioningApiKey so add one
|
|
|
|
uuid: 'not-unique', // This UUID is used in mocked-balena-api as an existing registered UUID
|
|
|
|
});
|
2020-07-21 09:45:37 +00:00
|
|
|
|
2020-05-19 14:03:00 +00:00
|
|
|
// If api-binder reaches this function then tests pass
|
2020-07-21 09:45:37 +00:00
|
|
|
// We throw an error so we don't have to keep stubbing
|
2020-05-19 14:03:00 +00:00
|
|
|
const functionToReach = stub(
|
2020-07-21 09:45:37 +00:00
|
|
|
ApiHelper,
|
2020-05-19 14:03:00 +00:00
|
|
|
'exchangeKeyAndGetDeviceOrRegenerate',
|
2020-07-21 09:45:37 +00:00
|
|
|
).throws(new ExpectedError());
|
|
|
|
|
2020-05-19 14:03:00 +00:00
|
|
|
spy(Log, 'debug');
|
|
|
|
|
|
|
|
try {
|
2020-07-21 09:45:37 +00:00
|
|
|
const opts = await config.get('provisioningOptions');
|
|
|
|
await ApiHelper.provision(components.apiBinder.balenaApi, opts);
|
2020-05-19 14:03:00 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Check that the error thrown is from this test
|
2020-07-21 09:45:37 +00:00
|
|
|
expect(e).to.be.instanceOf(ExpectedError);
|
2020-05-19 14:03:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 09:45:37 +00:00
|
|
|
expect(functionToReach.called).to.be.true;
|
2020-05-19 14:03:00 +00:00
|
|
|
expect((Log.debug as SinonSpy).lastCall.lastArg).to.equal(
|
|
|
|
'UUID already registered, trying a key exchange',
|
|
|
|
);
|
|
|
|
|
|
|
|
// Restore stubs
|
|
|
|
configStub.restore();
|
2020-07-21 09:45:37 +00:00
|
|
|
functionToReach.restore();
|
2020-05-19 14:03:00 +00:00
|
|
|
(Log.debug as SinonStub).restore();
|
|
|
|
});
|
|
|
|
|
2019-09-12 15:26:21 +00:00
|
|
|
it('deletes the provisioning key', async () => {
|
2020-06-02 13:29:05 +00:00
|
|
|
expect(await config.get('apiKey')).to.be.undefined;
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('sends the correct parameters when provisioning', async () => {
|
|
|
|
const conf = JSON.parse(
|
|
|
|
await fs.readFile('./test/data/config-apibinder.json', 'utf8'),
|
|
|
|
);
|
|
|
|
expect(balenaAPI.balenaBackend!.devices).to.deep.equal({
|
|
|
|
'1': {
|
|
|
|
id: 1,
|
|
|
|
application: conf.applicationId,
|
|
|
|
uuid: conf.uuid,
|
|
|
|
device_type: conf.deviceType,
|
|
|
|
api_key: conf.deviceApiKey,
|
2021-04-29 13:14:18 +00:00
|
|
|
mac_address: '00:11:22:33:44:55 66:77:88:99:AA:BB',
|
|
|
|
os_variant: 'dev',
|
|
|
|
os_version: 'balenaOS 2.0.6+rev1',
|
|
|
|
supervisor_version: supervisorVersion,
|
2019-09-12 15:26:21 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchDevice', () => {
|
|
|
|
const components: Dictionary<any> = {};
|
|
|
|
before(() => {
|
|
|
|
return initModels(components, '/config-apibinder.json');
|
|
|
|
});
|
2020-06-02 13:29:05 +00:00
|
|
|
after(async () => {
|
2020-06-08 17:16:53 +00:00
|
|
|
// @ts-expect-error setting read-only property
|
2020-06-02 13:29:05 +00:00
|
|
|
config.configJsonBackend = defaultConfigBackend;
|
|
|
|
await config.generateRequiredFields();
|
|
|
|
});
|
2019-09-12 15:26:21 +00:00
|
|
|
|
|
|
|
it('gets a device by its uuid from the balena API', async () => {
|
|
|
|
// Manually add a device to the mocked API
|
|
|
|
balenaAPI.balenaBackend!.devices[3] = {
|
|
|
|
id: 3,
|
|
|
|
user: 'foo',
|
|
|
|
application: 1337,
|
|
|
|
uuid: 'abcd',
|
|
|
|
device_type: 'intel-nuc',
|
|
|
|
api_key: 'verysecure',
|
|
|
|
};
|
|
|
|
|
2020-07-21 09:45:37 +00:00
|
|
|
const device = await ApiHelper.fetchDevice(
|
|
|
|
components.apiBinder.balenaApi,
|
2019-09-12 15:26:21 +00:00
|
|
|
'abcd',
|
|
|
|
'someApiKey',
|
|
|
|
30000,
|
|
|
|
);
|
|
|
|
expect(device).to.deep.equal(balenaAPI.balenaBackend!.devices[3]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('exchangeKeyAndGetDevice', () => {
|
|
|
|
const components: Dictionary<any> = {};
|
|
|
|
before(() => {
|
|
|
|
return initModels(components, '/config-apibinder.json');
|
|
|
|
});
|
2020-06-02 13:29:05 +00:00
|
|
|
after(async () => {
|
2020-06-08 17:16:53 +00:00
|
|
|
// @ts-expect-error setting read-only property
|
2020-06-02 13:29:05 +00:00
|
|
|
config.configJsonBackend = defaultConfigBackend;
|
|
|
|
await config.generateRequiredFields();
|
|
|
|
});
|
2019-09-12 15:26:21 +00:00
|
|
|
|
|
|
|
it('returns the device if it can fetch it with the deviceApiKey', async () => {
|
|
|
|
spy(balenaAPI.balenaBackend!, 'deviceKeyHandler');
|
|
|
|
|
2020-07-21 09:45:37 +00:00
|
|
|
const fetchDeviceStub = stub(ApiHelper, 'fetchDevice');
|
2019-09-12 15:26:21 +00:00
|
|
|
fetchDeviceStub.onCall(0).resolves({ id: 1 });
|
|
|
|
|
2020-07-21 09:45:37 +00:00
|
|
|
const device = await ApiHelper.exchangeKeyAndGetDevice(
|
|
|
|
components.apiBinder.balenaApi,
|
2019-09-12 15:26:21 +00:00
|
|
|
mockProvisioningOpts,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(balenaAPI.balenaBackend!.deviceKeyHandler).to.not.be.called;
|
|
|
|
expect(device).to.deep.equal({ id: 1 });
|
2020-07-21 09:45:37 +00:00
|
|
|
expect(fetchDeviceStub).to.be.calledOnce;
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2020-06-08 17:16:53 +00:00
|
|
|
// @ts-expect-error function does not exist on type
|
2019-09-12 15:26:21 +00:00
|
|
|
balenaAPI.balenaBackend.deviceKeyHandler.restore();
|
2020-07-21 09:45:37 +00:00
|
|
|
fetchDeviceStub.restore();
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('throws if it cannot get the device with any of the keys', () => {
|
|
|
|
spy(balenaAPI.balenaBackend!, 'deviceKeyHandler');
|
2020-07-21 09:45:37 +00:00
|
|
|
const fetchDeviceStub = stub(ApiHelper, 'fetchDevice').throws(
|
|
|
|
new DeviceNotFoundError(),
|
|
|
|
);
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2020-07-21 09:45:37 +00:00
|
|
|
const promise = ApiHelper.exchangeKeyAndGetDevice(
|
|
|
|
components.apiBinder.balenaApi,
|
2019-09-12 15:26:21 +00:00
|
|
|
mockProvisioningOpts,
|
|
|
|
);
|
|
|
|
promise.catch(() => {
|
|
|
|
/* noop */
|
|
|
|
});
|
|
|
|
|
|
|
|
return expect(promise).to.be.rejected.then(() => {
|
|
|
|
expect(balenaAPI.balenaBackend!.deviceKeyHandler).to.not.be.called;
|
2020-07-21 09:45:37 +00:00
|
|
|
expect(fetchDeviceStub).to.be.calledTwice;
|
|
|
|
fetchDeviceStub.restore();
|
2020-06-08 17:16:53 +00:00
|
|
|
// @ts-expect-error function does not exist on type
|
2019-09-12 15:26:21 +00:00
|
|
|
balenaAPI.balenaBackend.deviceKeyHandler.restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('exchanges the key and returns the device if the provisioning key is valid', async () => {
|
|
|
|
spy(balenaAPI.balenaBackend!, 'deviceKeyHandler');
|
2020-07-21 09:45:37 +00:00
|
|
|
const fetchDeviceStub = stub(ApiHelper, 'fetchDevice');
|
|
|
|
fetchDeviceStub.onCall(0).throws(new DeviceNotFoundError());
|
2019-09-12 15:26:21 +00:00
|
|
|
fetchDeviceStub.onCall(1).returns(Promise.resolve({ id: 1 }));
|
|
|
|
|
2020-07-21 09:45:37 +00:00
|
|
|
const device = await ApiHelper.exchangeKeyAndGetDevice(
|
|
|
|
components.apiBinder.balenaApi,
|
2019-09-12 15:26:21 +00:00
|
|
|
mockProvisioningOpts as any,
|
|
|
|
);
|
|
|
|
expect(balenaAPI.balenaBackend!.deviceKeyHandler).to.be.calledOnce;
|
|
|
|
expect(device).to.deep.equal({ id: 1 });
|
2020-07-21 09:45:37 +00:00
|
|
|
expect(fetchDeviceStub).to.be.calledTwice;
|
|
|
|
fetchDeviceStub.restore();
|
2020-06-08 17:16:53 +00:00
|
|
|
// @ts-expect-error function does not exist on type
|
2019-09-12 15:26:21 +00:00
|
|
|
balenaAPI.balenaBackend.deviceKeyHandler.restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('unmanaged mode', () => {
|
|
|
|
const components: Dictionary<any> = {};
|
|
|
|
before(() => {
|
|
|
|
return initModels(components, '/config-apibinder-offline.json');
|
|
|
|
});
|
2020-06-02 13:29:05 +00:00
|
|
|
after(async () => {
|
2020-06-08 17:16:53 +00:00
|
|
|
// @ts-expect-error setting read-only property
|
2020-06-02 13:29:05 +00:00
|
|
|
config.configJsonBackend = defaultConfigBackend;
|
|
|
|
await config.generateRequiredFields();
|
|
|
|
});
|
2019-09-12 15:26:21 +00:00
|
|
|
|
|
|
|
it('does not generate a key if the device is in unmanaged mode', async () => {
|
2020-06-02 13:29:05 +00:00
|
|
|
const mode = await config.get('unmanaged');
|
2019-09-12 15:26:21 +00:00
|
|
|
// Ensure offline mode is set
|
|
|
|
expect(mode).to.equal(true);
|
|
|
|
// Check that there is no deviceApiKey
|
2020-06-02 13:29:05 +00:00
|
|
|
const conf = await config.getMany(['deviceApiKey', 'uuid']);
|
2019-09-12 15:26:21 +00:00
|
|
|
expect(conf['deviceApiKey']).to.be.empty;
|
|
|
|
expect(conf['uuid']).to.not.be.undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Minimal config unmanaged mode', () => {
|
|
|
|
const components2: Dictionary<any> = {};
|
|
|
|
before(() => {
|
|
|
|
return initModels(components2, '/config-apibinder-offline2.json');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not generate a key with the minimal config', async () => {
|
2020-06-02 13:29:05 +00:00
|
|
|
const mode = await config.get('unmanaged');
|
2019-09-12 15:26:21 +00:00
|
|
|
expect(mode).to.equal(true);
|
2020-06-02 13:29:05 +00:00
|
|
|
const conf = await config.getMany(['deviceApiKey', 'uuid']);
|
2019-09-12 15:26:21 +00:00
|
|
|
expect(conf['deviceApiKey']).to.be.empty;
|
|
|
|
return expect(conf['uuid']).to.not.be.undefined;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-05-20 04:11:06 +00:00
|
|
|
|
|
|
|
describe('healthchecks', () => {
|
|
|
|
const components: Dictionary<any> = {};
|
|
|
|
let configStub: SinonStub;
|
|
|
|
let infoLobSpy: SinonSpy;
|
2020-05-16 13:08:14 +00:00
|
|
|
let previousLastFetch: ReturnType<typeof process.hrtime>;
|
2020-05-20 04:11:06 +00:00
|
|
|
|
2020-05-20 05:03:53 +00:00
|
|
|
before(async () => {
|
|
|
|
await initModels(components, '/config-apibinder.json');
|
2020-05-16 13:08:14 +00:00
|
|
|
previousLastFetch = TargetState.lastFetch;
|
2020-05-20 04:11:06 +00:00
|
|
|
});
|
2020-05-16 13:08:14 +00:00
|
|
|
|
2020-06-02 13:29:05 +00:00
|
|
|
after(async () => {
|
2020-06-08 17:16:53 +00:00
|
|
|
// @ts-expect-error setting read-only property
|
2020-06-02 13:29:05 +00:00
|
|
|
config.configJsonBackend = defaultConfigBackend;
|
|
|
|
await config.generateRequiredFields();
|
|
|
|
});
|
2020-05-20 04:11:06 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// This configStub will be modified in each test case so we can
|
|
|
|
// create the exact conditions we want to for testing healthchecks
|
2020-06-02 13:29:05 +00:00
|
|
|
configStub = stub(config, 'getMany');
|
2020-05-20 04:11:06 +00:00
|
|
|
infoLobSpy = spy(Log, 'info');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
configStub.restore();
|
|
|
|
infoLobSpy.restore();
|
2020-05-16 13:08:14 +00:00
|
|
|
(TargetState as any).lastFetch = previousLastFetch;
|
2020-05-20 04:11:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('passes with correct conditions', async () => {
|
|
|
|
// Set unmanaged to false so we check all values
|
|
|
|
// The other values are stubbed to make it pass
|
|
|
|
configStub.resolves({
|
|
|
|
unmanaged: false,
|
|
|
|
appUpdatePollInterval: 1000,
|
|
|
|
connectivityCheckEnabled: false,
|
|
|
|
});
|
2020-05-16 13:08:14 +00:00
|
|
|
// Set lastFetch to now so it is within appUpdatePollInterval
|
|
|
|
(TargetState as any).lastFetch = process.hrtime();
|
2020-05-20 04:11:06 +00:00
|
|
|
expect(await components.apiBinder.healthcheck()).to.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes if unmanaged is true and exit early', async () => {
|
|
|
|
// Setup failing conditions
|
|
|
|
configStub.resolves({
|
|
|
|
unmanaged: false,
|
|
|
|
appUpdatePollInterval: null,
|
|
|
|
connectivityCheckEnabled: false,
|
|
|
|
});
|
|
|
|
// Verify this causes healthcheck to fail
|
|
|
|
expect(await components.apiBinder.healthcheck()).to.equal(false);
|
|
|
|
// Do it again but set unmanaged to true
|
|
|
|
configStub.resolves({
|
|
|
|
unmanaged: true,
|
|
|
|
appUpdatePollInterval: null,
|
|
|
|
connectivityCheckEnabled: false,
|
|
|
|
});
|
|
|
|
expect(await components.apiBinder.healthcheck()).to.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails if appUpdatePollInterval not set in config and exit early', async () => {
|
|
|
|
configStub.resolves({
|
|
|
|
unmanaged: false,
|
|
|
|
appUpdatePollInterval: null,
|
|
|
|
connectivityCheckEnabled: false,
|
|
|
|
});
|
|
|
|
expect(await components.apiBinder.healthcheck()).to.equal(false);
|
|
|
|
expect(Log.info).to.be.calledOnce;
|
|
|
|
expect((Log.info as SinonSpy).lastCall?.lastArg).to.equal(
|
|
|
|
'Healthcheck failure - Config value `appUpdatePollInterval` cannot be null',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails when hasn't checked target state within poll interval", async () => {
|
|
|
|
configStub.resolves({
|
|
|
|
unmanaged: false,
|
|
|
|
appUpdatePollInterval: 1,
|
|
|
|
connectivityCheckEnabled: false,
|
|
|
|
});
|
|
|
|
expect(await components.apiBinder.healthcheck()).to.equal(false);
|
|
|
|
expect(Log.info).to.be.calledOnce;
|
|
|
|
expect((Log.info as SinonSpy).lastCall?.lastArg).to.equal(
|
|
|
|
'Healthcheck failure - Device has not fetched target state within appUpdatePollInterval limit',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails when stateReportHealthy is false', async () => {
|
2021-09-06 19:47:05 +00:00
|
|
|
const currentState = await import('../src/api-binder/report');
|
2020-10-12 10:32:07 +00:00
|
|
|
|
2020-05-20 04:11:06 +00:00
|
|
|
configStub.resolves({
|
|
|
|
unmanaged: false,
|
|
|
|
appUpdatePollInterval: 1000,
|
|
|
|
connectivityCheckEnabled: true,
|
|
|
|
});
|
2020-07-21 09:45:37 +00:00
|
|
|
|
2020-05-16 13:08:14 +00:00
|
|
|
// Set lastFetch to now so it is within appUpdatePollInterval
|
|
|
|
(TargetState as any).lastFetch = process.hrtime();
|
2020-07-21 09:45:37 +00:00
|
|
|
|
2020-05-20 04:11:06 +00:00
|
|
|
// Copy previous values to restore later
|
2020-10-12 10:32:07 +00:00
|
|
|
const previousStateReportErrors = currentState.stateReportErrors;
|
2020-05-20 04:11:06 +00:00
|
|
|
const previousDeviceStateConnected =
|
2020-07-21 15:25:47 +00:00
|
|
|
// @ts-ignore
|
|
|
|
components.deviceState.connected;
|
2020-07-21 09:45:37 +00:00
|
|
|
|
2020-05-20 04:11:06 +00:00
|
|
|
// Set additional conditions not in configStub to cause a fail
|
2020-10-12 10:32:07 +00:00
|
|
|
try {
|
|
|
|
currentState.stateReportErrors = 4;
|
|
|
|
components.deviceState.connected = true;
|
|
|
|
|
|
|
|
expect(await components.apiBinder.healthcheck()).to.equal(false);
|
|
|
|
|
|
|
|
expect(Log.info).to.be.calledOnce;
|
|
|
|
expect((Log.info as SinonSpy).lastCall?.lastArg).to.equal(
|
|
|
|
stripIndent`
|
|
|
|
Healthcheck failure - At least ONE of the following conditions must be true:
|
|
|
|
- No connectivityCheckEnabled ? false
|
|
|
|
- device state is disconnected ? false
|
|
|
|
- stateReportErrors less then 3 ? false`,
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
// Restore previous values
|
|
|
|
currentState.stateReportErrors = previousStateReportErrors;
|
|
|
|
components.deviceState.connected = previousDeviceStateConnected;
|
|
|
|
}
|
2020-05-20 04:11:06 +00:00
|
|
|
});
|
|
|
|
});
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|