2021-05-07 01:47:10 +00:00
|
|
|
import { expect } from 'chai';
|
2021-08-03 23:12:47 +00:00
|
|
|
import * as sinon from 'sinon';
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2022-08-17 23:35:08 +00:00
|
|
|
import { StatusCodeError, UpdatesLockedError } from '~/lib/errors';
|
|
|
|
import * as dockerUtils from '~/lib/docker-utils';
|
|
|
|
import * as config from '~/src/config';
|
|
|
|
import * as imageManager from '~/src/compose/images';
|
|
|
|
import { ConfigTxt } from '~/src/config/backends/config-txt';
|
|
|
|
import * as deviceState from '~/src/device-state';
|
|
|
|
import * as deviceConfig from '~/src/device-config';
|
|
|
|
import { loadTargetFromFile, appsJsonBackup } from '~/src/device-state/preload';
|
|
|
|
import Service from '~/src/compose/service';
|
|
|
|
import { intialiseContractRequirements } from '~/lib/contracts';
|
|
|
|
import * as updateLock from '~/lib/update-lock';
|
|
|
|
import * as fsUtils from '~/lib/fs-utils';
|
|
|
|
import { TargetState } from '~/src/types';
|
2021-08-03 23:12:47 +00:00
|
|
|
|
2022-08-17 23:35:08 +00:00
|
|
|
import * as dbHelper from '~/test-lib/db-helper';
|
|
|
|
import log from '~/lib/supervisor-console';
|
2019-09-12 15:26:21 +00:00
|
|
|
|
|
|
|
const mockedInitialConfig = {
|
|
|
|
RESIN_SUPERVISOR_CONNECTIVITY_CHECK: 'true',
|
|
|
|
RESIN_SUPERVISOR_DELTA: 'false',
|
|
|
|
RESIN_SUPERVISOR_DELTA_APPLY_TIMEOUT: '0',
|
|
|
|
RESIN_SUPERVISOR_DELTA_REQUEST_TIMEOUT: '30000',
|
|
|
|
RESIN_SUPERVISOR_DELTA_RETRY_COUNT: '30',
|
|
|
|
RESIN_SUPERVISOR_DELTA_RETRY_INTERVAL: '10000',
|
|
|
|
RESIN_SUPERVISOR_DELTA_VERSION: '2',
|
|
|
|
RESIN_SUPERVISOR_INSTANT_UPDATE_TRIGGER: 'true',
|
|
|
|
RESIN_SUPERVISOR_LOCAL_MODE: 'false',
|
|
|
|
RESIN_SUPERVISOR_LOG_CONTROL: 'true',
|
|
|
|
RESIN_SUPERVISOR_OVERRIDE_LOCK: 'false',
|
|
|
|
RESIN_SUPERVISOR_POLL_INTERVAL: '60000',
|
|
|
|
RESIN_SUPERVISOR_VPN_CONTROL: 'true',
|
|
|
|
};
|
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
describe('device-state', () => {
|
|
|
|
const originalImagesSave = imageManager.save;
|
|
|
|
const originalImagesInspect = imageManager.inspectByName;
|
2020-07-08 09:50:52 +00:00
|
|
|
const originalGetCurrent = deviceConfig.getCurrent;
|
2020-07-21 15:25:47 +00:00
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
let testDb: dbHelper.TestDatabase;
|
|
|
|
|
2019-09-12 15:26:21 +00:00
|
|
|
before(async () => {
|
2021-08-03 23:12:47 +00:00
|
|
|
testDb = await dbHelper.createDB();
|
|
|
|
|
2022-09-06 18:03:23 +00:00
|
|
|
await config.initialized();
|
2020-07-21 15:25:47 +00:00
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
// Prevent side effects from changes in config
|
|
|
|
sinon.stub(config, 'on');
|
|
|
|
|
|
|
|
// Set the device uuid
|
|
|
|
await config.set({ uuid: 'local' });
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2022-09-06 18:03:23 +00:00
|
|
|
await deviceState.initialized();
|
2021-08-03 23:12:47 +00:00
|
|
|
|
|
|
|
// disable log output during testing
|
|
|
|
sinon.stub(log, 'debug');
|
|
|
|
sinon.stub(log, 'warn');
|
|
|
|
sinon.stub(log, 'info');
|
|
|
|
sinon.stub(log, 'event');
|
|
|
|
sinon.stub(log, 'success');
|
|
|
|
|
|
|
|
// TODO: all these stubs are internal implementation details of
|
|
|
|
// deviceState, we should refactor deviceState to use dependency
|
|
|
|
// injection instead of initializing everything in memory
|
2021-09-21 20:33:32 +00:00
|
|
|
sinon.stub(Service as any, 'extendEnvVars').callsFake((env: any) => {
|
2019-09-12 15:26:21 +00:00
|
|
|
env['ADDITIONAL_ENV_VAR'] = 'foo';
|
|
|
|
return env;
|
|
|
|
});
|
|
|
|
|
2020-04-07 11:22:56 +00:00
|
|
|
intialiseContractRequirements({
|
|
|
|
supervisorVersion: '11.0.0',
|
|
|
|
deviceType: 'intel-nuc',
|
|
|
|
});
|
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
sinon
|
|
|
|
.stub(dockerUtils, 'getNetworkGateway')
|
|
|
|
.returns(Promise.resolve('172.17.0.1'));
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2020-08-13 12:25:39 +00:00
|
|
|
// @ts-expect-error Assigning to a RO property
|
2021-08-03 23:12:47 +00:00
|
|
|
imageManager.cleanImageData = () => {
|
2020-08-13 12:25:39 +00:00
|
|
|
console.log('Cleanup database called');
|
|
|
|
};
|
|
|
|
|
2020-06-09 13:43:45 +00:00
|
|
|
// @ts-expect-error Assigning to a RO property
|
2021-08-03 23:12:47 +00:00
|
|
|
imageManager.save = () => Promise.resolve();
|
2020-06-09 13:43:45 +00:00
|
|
|
|
|
|
|
// @ts-expect-error Assigning to a RO property
|
2021-08-03 23:12:47 +00:00
|
|
|
imageManager.inspectByName = () => {
|
2020-06-09 13:43:45 +00:00
|
|
|
const err: StatusCodeError = new Error();
|
2019-09-12 15:26:21 +00:00
|
|
|
err.statusCode = 404;
|
|
|
|
return Promise.reject(err);
|
2020-06-09 13:43:45 +00:00
|
|
|
};
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2020-07-08 09:50:52 +00:00
|
|
|
// @ts-expect-error Assigning to a RO property
|
2020-08-07 17:34:32 +00:00
|
|
|
deviceConfig.configBackend = new ConfigTxt();
|
2020-07-08 09:50:52 +00:00
|
|
|
|
|
|
|
// @ts-expect-error Assigning to a RO property
|
|
|
|
deviceConfig.getCurrent = async () => mockedInitialConfig;
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
after(async () => {
|
2019-09-12 15:26:21 +00:00
|
|
|
(Service as any).extendEnvVars.restore();
|
2020-06-02 16:56:58 +00:00
|
|
|
(dockerUtils.getNetworkGateway as sinon.SinonStub).restore();
|
2020-06-09 13:43:45 +00:00
|
|
|
|
|
|
|
// @ts-expect-error Assigning to a RO property
|
2021-08-03 23:12:47 +00:00
|
|
|
imageManager.save = originalImagesSave;
|
2020-06-09 13:43:45 +00:00
|
|
|
// @ts-expect-error Assigning to a RO property
|
2021-08-03 23:12:47 +00:00
|
|
|
imageManager.inspectByName = originalImagesInspect;
|
2020-07-08 09:50:52 +00:00
|
|
|
// @ts-expect-error Assigning to a RO property
|
|
|
|
deviceConfig.getCurrent = originalGetCurrent;
|
2021-08-03 23:12:47 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await testDb.destroy();
|
|
|
|
} catch (e) {
|
|
|
|
/* noop */
|
|
|
|
}
|
|
|
|
sinon.restore();
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
afterEach(async () => {
|
|
|
|
await testDb.reset();
|
2020-06-08 09:33:19 +00:00
|
|
|
});
|
|
|
|
|
2019-09-12 15:26:21 +00:00
|
|
|
it('loads a target state from an apps.json file and saves it as target state, then returns it', async () => {
|
2021-12-10 15:15:59 +00:00
|
|
|
const appsJson = process.env.ROOT_MOUNTPOINT + '/apps.json';
|
|
|
|
await loadTargetFromFile(appsJson);
|
2020-07-08 09:50:52 +00:00
|
|
|
const targetState = await deviceState.getTarget();
|
2021-12-10 15:15:59 +00:00
|
|
|
expect(await fsUtils.exists(appsJsonBackup(appsJson))).to.be.true;
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
expect(targetState)
|
|
|
|
.to.have.property('local')
|
|
|
|
.that.has.property('config')
|
|
|
|
.that.has.property('HOST_CONFIG_gpu_mem')
|
|
|
|
.that.equals('256');
|
2020-08-13 12:25:39 +00:00
|
|
|
expect(targetState)
|
|
|
|
.to.have.property('local')
|
|
|
|
.that.has.property('apps')
|
|
|
|
.that.has.property('1234')
|
|
|
|
.that.is.an('object');
|
|
|
|
const app = targetState.local.apps[1234];
|
|
|
|
expect(app).to.have.property('appName').that.equals('superapp');
|
|
|
|
expect(app).to.have.property('services').that.is.an('array').with.length(1);
|
|
|
|
expect(app.services[0])
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('image')
|
|
|
|
.that.equals('registry2.resin.io/superapp/abcdef:latest');
|
|
|
|
expect(app.services[0].config)
|
|
|
|
.to.have.property('labels')
|
|
|
|
.that.has.property('io.balena.something')
|
|
|
|
.that.equals('bar');
|
|
|
|
expect(app).to.have.property('appName').that.equals('superapp');
|
|
|
|
expect(app).to.have.property('services').that.is.an('array').with.length(1);
|
|
|
|
expect(app.services[0])
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('image')
|
|
|
|
.that.equals('registry2.resin.io/superapp/abcdef:latest');
|
|
|
|
expect(app.services[0].config)
|
|
|
|
.to.have.property('labels')
|
|
|
|
.that.has.property('io.balena.something')
|
|
|
|
.that.equals('bar');
|
|
|
|
expect(app).to.have.property('appName').that.equals('superapp');
|
|
|
|
expect(app).to.have.property('services').that.is.an('array').with.length(1);
|
|
|
|
expect(app.services[0])
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('image')
|
|
|
|
.that.equals('registry2.resin.io/superapp/abcdef:latest');
|
|
|
|
expect(app.services[0].config)
|
|
|
|
.to.have.property('labels')
|
|
|
|
.that.has.property('io.balena.something')
|
|
|
|
.that.equals('bar');
|
|
|
|
expect(app).to.have.property('appName').that.equals('superapp');
|
|
|
|
expect(app).to.have.property('services').that.is.an('array').with.length(1);
|
|
|
|
expect(app.services[0])
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('image')
|
|
|
|
.that.equals('registry2.resin.io/superapp/abcdef:latest');
|
|
|
|
expect(app.services[0].config)
|
|
|
|
.to.have.property('labels')
|
|
|
|
.that.has.property('io.balena.something')
|
|
|
|
.that.equals('bar');
|
|
|
|
expect(app).to.have.property('appName').that.equals('superapp');
|
|
|
|
expect(app).to.have.property('services').that.is.an('array').with.length(1);
|
|
|
|
expect(app.services[0])
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('image')
|
|
|
|
.that.equals('registry2.resin.io/superapp/abcdef:latest');
|
|
|
|
expect(app.services[0].config)
|
|
|
|
.to.have.property('labels')
|
|
|
|
.that.has.property('io.balena.something')
|
|
|
|
.that.equals('bar');
|
2021-12-10 15:15:59 +00:00
|
|
|
|
|
|
|
// Restore renamed apps.json
|
|
|
|
await fsUtils.safeRename(appsJsonBackup(appsJson), appsJson);
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
2020-03-23 19:02:17 +00:00
|
|
|
it('stores info for pinning a device after loading an apps.json with a pinDevice field', async () => {
|
2021-12-10 15:15:59 +00:00
|
|
|
const appsJson = process.env.ROOT_MOUNTPOINT + '/apps-pin.json';
|
|
|
|
await loadTargetFromFile(appsJson);
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2020-03-23 19:02:17 +00:00
|
|
|
const pinned = await config.get('pinDevice');
|
2020-05-15 11:01:51 +00:00
|
|
|
expect(pinned).to.have.property('app').that.equals(1234);
|
|
|
|
expect(pinned).to.have.property('commit').that.equals('abcdef');
|
2021-12-10 15:15:59 +00:00
|
|
|
expect(await fsUtils.exists(appsJsonBackup(appsJson))).to.be.true;
|
|
|
|
|
|
|
|
// Restore renamed apps.json
|
|
|
|
await fsUtils.safeRename(appsJsonBackup(appsJson), appsJson);
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
2020-07-21 15:25:47 +00:00
|
|
|
it('emits a change event when a new state is reported', (done) => {
|
2021-08-03 23:12:47 +00:00
|
|
|
// TODO: where is the test on this test?
|
2020-07-21 15:25:47 +00:00
|
|
|
deviceState.once('change', done);
|
2019-12-18 15:58:21 +00:00
|
|
|
deviceState.reportCurrentState({ someStateDiff: 'someValue' } as any);
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
it('writes the target state to the db with some extra defaults', async () => {
|
|
|
|
await deviceState.setTarget({
|
|
|
|
local: {
|
|
|
|
name: 'aDeviceWithDifferentName',
|
|
|
|
config: {
|
|
|
|
RESIN_HOST_CONFIG_gpu_mem: '512',
|
|
|
|
},
|
|
|
|
apps: {
|
|
|
|
myapp: {
|
|
|
|
id: 1234,
|
|
|
|
name: 'superapp',
|
|
|
|
class: 'fleet',
|
|
|
|
releases: {
|
|
|
|
afafafa: {
|
|
|
|
id: 2,
|
|
|
|
services: {
|
|
|
|
aservice: {
|
|
|
|
id: 23,
|
|
|
|
image_id: 12345,
|
|
|
|
image: 'registry2.resin.io/superapp/edfabc',
|
|
|
|
environment: {
|
|
|
|
FOO: 'bar',
|
|
|
|
},
|
|
|
|
labels: {},
|
|
|
|
},
|
|
|
|
anotherService: {
|
|
|
|
id: 24,
|
|
|
|
image_id: 12346,
|
|
|
|
image: 'registry2.resin.io/superapp/afaff',
|
|
|
|
environment: {
|
|
|
|
FOO: 'bro',
|
|
|
|
},
|
|
|
|
labels: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
volumes: {},
|
|
|
|
networks: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as TargetState);
|
|
|
|
const targetState = await deviceState.getTarget();
|
2019-09-12 15:26:21 +00:00
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
expect(targetState)
|
|
|
|
.to.have.property('local')
|
|
|
|
.that.has.property('config')
|
|
|
|
.that.has.property('HOST_CONFIG_gpu_mem')
|
|
|
|
.that.equals('512');
|
|
|
|
|
|
|
|
expect(targetState)
|
|
|
|
.to.have.property('local')
|
|
|
|
.that.has.property('apps')
|
|
|
|
.that.has.property('1234')
|
|
|
|
.that.is.an('object');
|
|
|
|
|
|
|
|
const app = targetState.local.apps[1234];
|
|
|
|
expect(app).to.have.property('appName').that.equals('superapp');
|
|
|
|
expect(app).to.have.property('commit').that.equals('afafafa');
|
|
|
|
expect(app).to.have.property('services').that.is.an('array').with.length(2);
|
|
|
|
expect(app.services[0])
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('image')
|
|
|
|
.that.equals('registry2.resin.io/superapp/edfabc:latest');
|
|
|
|
expect(app.services[0].config)
|
|
|
|
.to.have.property('environment')
|
|
|
|
.that.has.property('FOO')
|
|
|
|
.that.equals('bar');
|
|
|
|
expect(app.services[1])
|
|
|
|
.to.have.property('config')
|
|
|
|
.that.has.property('image')
|
|
|
|
.that.equals('registry2.resin.io/superapp/afaff:latest');
|
|
|
|
expect(app.services[1].config)
|
|
|
|
.to.have.property('environment')
|
|
|
|
.that.has.property('FOO')
|
|
|
|
.that.equals('bro');
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not allow setting an invalid target state', () => {
|
2021-08-03 23:12:47 +00:00
|
|
|
// v2 state should be rejected
|
|
|
|
expect(
|
|
|
|
deviceState.setTarget({
|
|
|
|
local: {
|
|
|
|
name: 'aDeviceWithDifferentName',
|
|
|
|
config: {
|
|
|
|
RESIN_HOST_CONFIG_gpu_mem: '512',
|
|
|
|
},
|
|
|
|
apps: {
|
|
|
|
1234: {
|
|
|
|
appId: '1234',
|
|
|
|
name: 'superapp',
|
|
|
|
commit: 'afafafa',
|
|
|
|
releaseId: '2',
|
|
|
|
config: {},
|
|
|
|
services: {
|
|
|
|
23: {
|
|
|
|
serviceId: '23',
|
|
|
|
serviceName: 'aservice',
|
|
|
|
imageId: '12345',
|
|
|
|
image: 'registry2.resin.io/superapp/edfabc',
|
|
|
|
environment: {
|
|
|
|
' FOO': 'bar',
|
|
|
|
},
|
|
|
|
labels: {},
|
|
|
|
},
|
|
|
|
24: {
|
|
|
|
serviceId: '24',
|
|
|
|
serviceName: 'anotherService',
|
|
|
|
imageId: '12346',
|
|
|
|
image: 'registry2.resin.io/superapp/afaff',
|
|
|
|
environment: {
|
|
|
|
FOO: 'bro',
|
|
|
|
},
|
|
|
|
labels: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
dependent: { apps: {}, devices: {} },
|
|
|
|
} as any),
|
|
|
|
).to.be.rejected;
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
2020-05-15 11:01:51 +00:00
|
|
|
it('allows triggering applying the target state', (done) => {
|
2021-08-03 23:12:47 +00:00
|
|
|
const applyTargetStub = sinon
|
|
|
|
.stub(deviceState, 'applyTarget')
|
|
|
|
.returns(Promise.resolve());
|
2019-09-12 15:26:21 +00:00
|
|
|
|
|
|
|
deviceState.triggerApplyTarget({ force: true });
|
2020-07-21 15:25:47 +00:00
|
|
|
expect(applyTargetStub).to.not.be.called;
|
2019-09-12 15:26:21 +00:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2020-07-21 15:25:47 +00:00
|
|
|
expect(applyTargetStub).to.be.calledWith({
|
2019-09-12 15:26:21 +00:00
|
|
|
force: true,
|
|
|
|
initial: false,
|
|
|
|
});
|
2020-07-21 15:25:47 +00:00
|
|
|
applyTargetStub.restore();
|
2019-09-12 15:26:21 +00:00
|
|
|
done();
|
2020-07-21 15:25:47 +00:00
|
|
|
}, 1000);
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
it('accepts a target state with an valid contract', async () => {
|
|
|
|
await deviceState.setTarget({
|
|
|
|
local: {
|
|
|
|
name: 'aDeviceWithDifferentName',
|
|
|
|
config: {},
|
|
|
|
apps: {
|
|
|
|
myapp: {
|
|
|
|
id: 1234,
|
|
|
|
name: 'superapp',
|
|
|
|
class: 'fleet',
|
|
|
|
releases: {
|
|
|
|
one: {
|
|
|
|
id: 2,
|
|
|
|
services: {
|
|
|
|
valid: {
|
|
|
|
id: 23,
|
|
|
|
image_id: 12345,
|
|
|
|
image: 'registry2.resin.io/superapp/valid',
|
|
|
|
environment: {},
|
|
|
|
labels: {},
|
|
|
|
},
|
|
|
|
alsoValid: {
|
|
|
|
id: 24,
|
|
|
|
image_id: 12346,
|
|
|
|
image: 'registry2.resin.io/superapp/afaff',
|
|
|
|
contract: {
|
|
|
|
type: 'sw.container',
|
|
|
|
slug: 'supervisor-version',
|
|
|
|
name: 'Enforce supervisor version',
|
|
|
|
requires: [
|
|
|
|
{
|
|
|
|
type: 'sw.supervisor',
|
|
|
|
version: '>=11.0.0',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
environment: {},
|
|
|
|
labels: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
volumes: {},
|
|
|
|
networks: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as TargetState);
|
|
|
|
const targetState = await deviceState.getTarget();
|
|
|
|
const app = targetState.local.apps[1234];
|
|
|
|
expect(app).to.have.property('appName').that.equals('superapp');
|
|
|
|
expect(app).to.have.property('commit').that.equals('one');
|
|
|
|
// Only a single service should be on the target state
|
|
|
|
expect(app).to.have.property('services').that.is.an('array').with.length(2);
|
|
|
|
expect(app.services[1])
|
|
|
|
.that.has.property('serviceName')
|
|
|
|
.that.equals('alsoValid');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('accepts a target state with an invalid contract for an optional container', async () => {
|
|
|
|
await deviceState.setTarget({
|
|
|
|
local: {
|
|
|
|
name: 'aDeviceWithDifferentName',
|
|
|
|
config: {},
|
|
|
|
apps: {
|
|
|
|
myapp: {
|
|
|
|
id: 1234,
|
|
|
|
name: 'superapp',
|
|
|
|
class: 'fleet',
|
|
|
|
releases: {
|
|
|
|
one: {
|
|
|
|
id: 2,
|
|
|
|
services: {
|
|
|
|
valid: {
|
|
|
|
id: 23,
|
|
|
|
image_id: 12345,
|
|
|
|
image: 'registry2.resin.io/superapp/valid',
|
|
|
|
environment: {},
|
|
|
|
labels: {},
|
|
|
|
},
|
|
|
|
invalidButOptional: {
|
|
|
|
id: 24,
|
|
|
|
image_id: 12346,
|
|
|
|
image: 'registry2.resin.io/superapp/afaff',
|
|
|
|
contract: {
|
|
|
|
type: 'sw.container',
|
|
|
|
slug: 'supervisor-version',
|
|
|
|
name: 'Enforce supervisor version',
|
|
|
|
requires: [
|
|
|
|
{
|
|
|
|
type: 'sw.supervisor',
|
|
|
|
version: '>=12.0.0',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
environment: {},
|
|
|
|
labels: {
|
|
|
|
'io.balena.features.optional': 'true',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
volumes: {},
|
|
|
|
networks: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as TargetState);
|
|
|
|
const targetState = await deviceState.getTarget();
|
|
|
|
const app = targetState.local.apps[1234];
|
|
|
|
expect(app).to.have.property('appName').that.equals('superapp');
|
|
|
|
expect(app).to.have.property('commit').that.equals('one');
|
|
|
|
// Only a single service should be on the target state
|
|
|
|
expect(app).to.have.property('services').that.is.an('array').with.length(1);
|
|
|
|
expect(app.services[0])
|
|
|
|
.that.has.property('serviceName')
|
|
|
|
.that.equals('valid');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects a target state with invalid contract and non optional service', async () => {
|
|
|
|
await expect(
|
|
|
|
deviceState.setTarget({
|
|
|
|
local: {
|
|
|
|
name: 'aDeviceWithDifferentName',
|
|
|
|
config: {},
|
|
|
|
apps: {
|
|
|
|
myapp: {
|
|
|
|
id: 1234,
|
|
|
|
name: 'superapp',
|
|
|
|
class: 'fleet',
|
|
|
|
releases: {
|
|
|
|
one: {
|
|
|
|
id: 2,
|
|
|
|
services: {
|
|
|
|
valid: {
|
|
|
|
id: 23,
|
|
|
|
image_id: 12345,
|
|
|
|
image: 'registry2.resin.io/superapp/valid',
|
|
|
|
environment: {},
|
|
|
|
labels: {},
|
|
|
|
},
|
|
|
|
invalid: {
|
|
|
|
id: 24,
|
|
|
|
image_id: 12346,
|
|
|
|
image: 'registry2.resin.io/superapp/afaff',
|
|
|
|
contract: {
|
|
|
|
type: 'sw.container',
|
|
|
|
slug: 'supervisor-version',
|
|
|
|
name: 'Enforce supervisor version',
|
|
|
|
requires: [
|
|
|
|
{
|
|
|
|
type: 'sw.supervisor',
|
|
|
|
version: '>=12.0.0',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
environment: {},
|
|
|
|
labels: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
volumes: {},
|
|
|
|
networks: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as TargetState),
|
|
|
|
).to.be.rejected;
|
|
|
|
});
|
|
|
|
|
2020-07-21 15:25:47 +00:00
|
|
|
// TODO: There is no easy way to test this behaviour with the current
|
|
|
|
// interface of device-state. We should really think about the device-state
|
|
|
|
// interface to allow this flexibility (and to avoid having to change module
|
|
|
|
// internal variables)
|
|
|
|
it.skip('cancels current promise applying the target state');
|
2020-05-20 05:03:53 +00:00
|
|
|
|
2020-07-21 15:25:47 +00:00
|
|
|
it.skip('applies the target state for device config');
|
2020-05-20 05:03:53 +00:00
|
|
|
|
2020-07-21 15:25:47 +00:00
|
|
|
it.skip('applies the target state for applications');
|
2021-06-14 10:10:16 +00:00
|
|
|
|
|
|
|
it('prevents reboot or shutdown when HUP rollback breadcrumbs are present', async () => {
|
|
|
|
const testErrMsg = 'Waiting for Host OS updates to finish';
|
2021-08-03 23:12:47 +00:00
|
|
|
sinon
|
|
|
|
.stub(updateLock, 'abortIfHUPInProgress')
|
|
|
|
.throws(new UpdatesLockedError(testErrMsg));
|
2021-06-14 10:10:16 +00:00
|
|
|
|
2022-05-26 21:23:08 +00:00
|
|
|
await expect(deviceState.shutdown({ reboot: true }))
|
2021-06-14 10:10:16 +00:00
|
|
|
.to.eventually.be.rejectedWith(testErrMsg)
|
|
|
|
.and.be.an.instanceOf(UpdatesLockedError);
|
|
|
|
await expect(deviceState.shutdown())
|
|
|
|
.to.eventually.be.rejectedWith(testErrMsg)
|
|
|
|
.and.be.an.instanceOf(UpdatesLockedError);
|
|
|
|
|
2021-08-03 23:12:47 +00:00
|
|
|
(updateLock.abortIfHUPInProgress as sinon.SinonStub).restore();
|
2021-06-14 10:10:16 +00:00
|
|
|
});
|
2019-09-12 15:26:21 +00:00
|
|
|
});
|