Skip to content

Commit

Permalink
Separate routes & actions, add tests for GET /v2/version
Browse files Browse the repository at this point in the history
Signed-off-by: Christina Ying Wang <[email protected]>
  • Loading branch information
cywang117 committed Dec 12, 2023
1 parent 231fd22 commit 9443281
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 22 deletions.
3 changes: 1 addition & 2 deletions src/config/functions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import * as _ from 'lodash';
import * as memoizee from 'memoizee';

import supervisorVersion = require('../lib/supervisor-version');

import * as config from '.';
import * as constants from '../lib/constants';
import * as osRelease from '../lib/os-release';
import * as macAddress from '../lib/mac-address';
import * as hostUtils from '../lib/host-utils';
import log from '../lib/supervisor-console';
import { supervisorVersion } from '../lib/supervisor-version';

export const fnSchema = {
version: () => {
Expand Down
10 changes: 10 additions & 0 deletions src/device-api/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
BadRequestError,
} from '../lib/errors';
import { JournalctlOpts, spawnJournalctl } from '../lib/journald';
import { supervisorVersion } from '../lib/supervisor-version';

/**
* Run an array of healthchecks, outputting whether all passed or not
Expand Down Expand Up @@ -474,3 +475,12 @@ export const cleanupVolumes = async (
export const getLogStream = (opts: JournalctlOpts) => {
return spawnJournalctl(opts);
};

/**
* Get version of running Supervisor
* Used by:
* - GET /v2/version
*/
export const getSupervisorVersion = () => {
return supervisorVersion;
};
16 changes: 10 additions & 6 deletions src/device-api/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import * as logger from '../logger';
import * as images from '../compose/images';
import * as serviceManager from '../compose/service-manager';
import log from '../lib/supervisor-console';
import supervisorVersion = require('../lib/supervisor-version');
import { checkInt, checkString, checkTruthy } from '../lib/validation';
import {
isNotFoundError,
Expand Down Expand Up @@ -378,11 +377,16 @@ router.get('/v2/local/logs', async (_req, res) => {
listenStream.pipe(res);
});

router.get('/v2/version', (_req, res) => {
res.status(200).json({
status: 'success',
version: supervisorVersion,
});
router.get('/v2/version', (_req, res, next) => {
try {
const supervisorVersion = actions.getSupervisorVersion();
return res.status(200).json({
status: 'success',
version: supervisorVersion,
});
} catch (e: unknown) {
next(e);
}
});

router.get('/v2/containerId', async (req: AuthorizedRequest, res) => {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import * as resumableRequestLib from 'resumable-request';

import * as constants from './constants';
import * as osRelease from './os-release';

import supervisorVersion = require('./supervisor-version');
import { supervisorVersion } from './supervisor-version';

export { requestLib };

Expand Down
13 changes: 6 additions & 7 deletions src/lib/supervisor-version.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as _ from 'lodash';

import * as packageJson from '../../package.json';
let version = packageJson.version;
import { version as packageJsonVersion } from '../../package.json';
let supervisorVersion = packageJsonVersion;

const tagExtra = process.env.SUPERVISOR_TAG_EXTRA;
if (!_.isEmpty(tagExtra)) {
version += '+' + tagExtra;
if (tagExtra != null) {
supervisorVersion += '+' + tagExtra;
}
export = version;

export { supervisorVersion };
6 changes: 3 additions & 3 deletions src/supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { initializeContractRequirements } from './lib/contracts';
import { normaliseLegacyDatabase } from './lib/legacy';
import * as osRelease from './lib/os-release';
import log from './lib/supervisor-console';
import version = require('./lib/supervisor-version');
import { supervisorVersion } from './lib/supervisor-version';
import * as avahi from './lib/avahi';
import * as firewall from './lib/firewall';

Expand All @@ -32,7 +32,7 @@ export class Supervisor {
private api: SupervisorAPI;

public async init() {
log.info(`Supervisor v${version} starting up...`);
log.info(`Supervisor v${supervisorVersion} starting up...`);

await db.initialized();
await config.initialized();
Expand All @@ -43,7 +43,7 @@ export class Supervisor {
const conf = await config.getMany(startupConfigFields);

initializeContractRequirements({
supervisorVersion: version,
supervisorVersion,
deviceType: await config.get('deviceType'),
deviceArch: await config.get('deviceArch'),
l4tVersion: await osRelease.getL4tVersion(),
Expand Down
27 changes: 27 additions & 0 deletions test/integration/device-api/v2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
NotFoundError,
BadRequestError,
} from '~/lib/errors';
import { supervisorVersion } from '~/src/lib/supervisor-version';

// All routes that require Authorization are integration tests due to
// the api-key module relying on the database.
Expand Down Expand Up @@ -802,4 +803,30 @@ describe('device-api/v2', () => {
.expect(503);
});
});

describe('GET /v2/version', () => {
let getSupervisorVersionStub: SinonStub;
before(() => {
getSupervisorVersionStub = stub(actions, 'getSupervisorVersion');
});
after(() => {
getSupervisorVersionStub.restore();
});

it('responds with 200 and Supervisor version', async () => {
getSupervisorVersionStub.callThrough();
await request(api)
.get('/v2/version')
.set('Authorization', `Bearer ${await deviceApi.getGlobalApiKey()}`)
.expect(200, { status: 'success', version: supervisorVersion });
});

it('responds with 503 if an error occurred', async () => {
getSupervisorVersionStub.throws(new Error());
await request(api)
.get('/v2/version')
.set('Authorization', `Bearer ${await deviceApi.getGlobalApiKey()}`)
.expect(503);
});
});
});
2 changes: 1 addition & 1 deletion test/legacy/10-api-binder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { schema } from '~/src/config/schema';
import ConfigJsonConfigBackend from '~/src/config/configJson';
import * as TargetState from '~/src/device-state/target-state';
import * as ApiHelper from '~/lib/api-helper';
import supervisorVersion = require('~/lib/supervisor-version');
import { supervisorVersion } from '~/lib/supervisor-version';
import * as eventTracker from '~/src/event-tracker';
import * as constants from '~/lib/constants';

Expand Down
7 changes: 7 additions & 0 deletions test/unit/device-api/actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { spy, useFakeTimers, stub, SinonStub } from 'sinon';

import * as hostConfig from '~/src/host-config';
import * as actions from '~/src/device-api/actions';
import { supervisorVersion } from '~/lib/supervisor-version';
import blink = require('~/lib/blink');

describe('device-api/actions', () => {
Expand Down Expand Up @@ -67,4 +68,10 @@ describe('device-api/actions', () => {
expect(await actions.getHostConfig()).to.deep.equal(conf);
});
});

describe('gets Supervisor version', () => {
it('gets Supervisor version from package.json', () => {
expect(actions.getSupervisorVersion()).to.equal(supervisorVersion);
});
});
});
2 changes: 1 addition & 1 deletion test/unit/lib/contracts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as semver from 'semver';
import { SinonStub, stub } from 'sinon';

import * as osRelease from '~/lib/os-release';
import supervisorVersion = require('~/lib/supervisor-version');
import { supervisorVersion } from '~/lib/supervisor-version';
import * as fsUtils from '~/lib/fs-utils';

describe('lib/contracts', () => {
Expand Down
9 changes: 9 additions & 0 deletions test/unit/lib/supervisor-version.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect } from 'chai';
import { version as packageJsonVersion } from '~/src/../package.json';
import { supervisorVersion } from '~/lib/supervisor-version';

describe('lib/supervisor-version', () => {
it('should return the version from package.json', () => {
expect(supervisorVersion).to.equal(packageJsonVersion);
});
});

0 comments on commit 9443281

Please sign in to comment.