mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-25 16:31:05 +00:00
e04e64763f
This PR cleans up testing for supervisor compose modules. It also fixes broken tests for application manager and removes a lot of dependencies for those tests on DB and other unnecessary mocks. There are probably a lot of cases that tests are missing but this should make writing new tests a lot easier. This PR also creates a new mock dockerode (mockerode) module that should make it easier to test operations that interact with the engine. All references to the old mock-dockerode have not yet been removed but that should come soon in another PR List of squashed commits: - Add tests for network create/remove - Move compose service tests to test/src/compose and reorganize test descriptions - Add support for image creation to mockerode - Add additional tests for compose volumes - Update mockerode so unimplemented fake methods throw. This is to ensure tests using mockerode fail if an unimplemented method is used - Update tests for volume-manager with mockerode - Update tests for compose/images - Simplify tests using mockerode - Clean up compose/app tests - Create application manager tests Change-type: minor
83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import * as constants from '../../src/lib/constants';
|
|
import * as db from '../../src/db';
|
|
import * as sinon from 'sinon';
|
|
|
|
import rewire = require('rewire');
|
|
|
|
// Creates a test database and returns a query builder
|
|
export async function createDB() {
|
|
const oldDatabasePath = process.env.DATABASE_PATH;
|
|
|
|
// for testing we use an in memory database
|
|
process.env.DATABASE_PATH = ':memory:';
|
|
|
|
// @ts-ignore
|
|
constants.databasePath = process.env.DATABASE_PATH;
|
|
|
|
// Cleanup the module cache in order to have it reloaded in the local context
|
|
delete require.cache[require.resolve('../../src/db')];
|
|
const testDb = rewire('../../src/db');
|
|
|
|
// Initialize the database module
|
|
await testDb.initialized;
|
|
|
|
// Get the knex instance to allow queries to the db
|
|
const knex = testDb.__get__('knex');
|
|
const { models } = testDb;
|
|
|
|
// This is hacky but haven't found another way to do it,
|
|
// stubbing the db methods here ensures the module under test
|
|
// is using the database we want
|
|
sinon.stub(db, 'models').callsFake(models);
|
|
sinon.stub(db, 'upsertModel').callsFake(testDb.upsertModel);
|
|
|
|
return {
|
|
// Returns a query builder instance for the given
|
|
// table in order perform data operations
|
|
models,
|
|
|
|
// Resets the database to initial value post
|
|
// migrations
|
|
async reset() {
|
|
// Reset the contents of the db
|
|
await testDb.transaction(async (trx: any) => {
|
|
const result = await trx.raw(`
|
|
SELECT name, sql
|
|
FROM sqlite_master
|
|
WHERE type='table'`);
|
|
for (const r of result) {
|
|
// We don't run the migrations again
|
|
if (r.name !== 'knex_migrations') {
|
|
await trx.raw(`DELETE FROM ${r.name}`);
|
|
}
|
|
}
|
|
|
|
// The supervisor expects this value to already have
|
|
// been pre-populated
|
|
await trx('deviceConfig').insert({ targetValues: '{}' });
|
|
});
|
|
|
|
// Reset stub call history
|
|
(db.models as sinon.SinonStub).resetHistory();
|
|
(db.upsertModel as sinon.SinonStub).resetHistory();
|
|
},
|
|
|
|
// Destroys the in-memory database and resets environment
|
|
async destroy() {
|
|
await knex.destroy();
|
|
|
|
// Restore the old datbase path
|
|
process.env.DATABASE_PATH = oldDatabasePath;
|
|
|
|
// Restore stubs
|
|
(db.models as sinon.SinonStub).restore();
|
|
(db.upsertModel as sinon.SinonStub).restore();
|
|
|
|
// @ts-ignore
|
|
constants.databasePath = process.env.DATABASE_PATH;
|
|
},
|
|
};
|
|
}
|
|
|
|
export type TestDatabase = UnwrappedPromise<ReturnType<typeof createDB>>;
|