test: add test from PluginTaskSchedulerJanitor

Signed-off-by: Ana Luiza Ramalho dos Santos <analuiza@pop-os.localdomain>
This commit is contained in:
Ana Luiza Ramalho dos Santos
2023-02-14 10:21:22 -03:00
parent 3e28855c02
commit 3b630ee2cd
2 changed files with 93 additions and 1 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
---
'@backstage/backend-tasks': minor
'@backstage/backend-tasks': patch
---
Added the adapted query to mysql and sqlite3 databases to not returning warning on logs
@@ -0,0 +1,92 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getVoidLogger } from '@backstage/backend-common';
import { TestDatabases } from '@backstage/backend-test-utils';
import { Knex } from 'knex';
import { Duration } from 'luxon';
import waitForExpect from 'wait-for-expect';
import { migrateBackendTasks } from '../database/migrateBackendTasks';
import { DbTasksRow, DB_TASKS_TABLE } from '../database/tables';
import { PluginTaskSchedulerJanitor } from './PluginTaskSchedulerJanitor';
const insertTask = async (knex: Knex, task: DbTasksRow) => {
return knex<DbTasksRow>(DB_TASKS_TABLE)
.insert(task)
.onConflict('id')
.merge(['settings_json']);
};
const getTask = async (knex: Knex): Promise<DbTasksRow> => {
return (await knex<DbTasksRow>(DB_TASKS_TABLE))[0];
};
describe('PluginTaskSchedulerJanitor', () => {
const logger = getVoidLogger();
const databases = TestDatabases.create({
ids: ['MYSQL_8', 'POSTGRES_13', 'POSTGRES_9', 'SQLITE_3'],
});
jest.setTimeout(300_000);
beforeEach(() => {
jest.resetAllMocks();
});
it.each(databases.eachSupportedId())(
'Should update date if current_run_expires_at expires , %p',
async databaseId => {
const knex = await databases.init(databaseId);
await migrateBackendTasks(knex);
const dateYesterday = new Date(
new Date().setDate(new Date().getDate() - 1),
);
await insertTask(knex, {
id: 'task1',
settings_json: '',
next_run_start_at: new Date('2023-03-01 00:00:00'),
current_run_ticket: 'ticket',
current_run_started_at: dateYesterday,
current_run_expires_at: dateYesterday,
});
const worker = new PluginTaskSchedulerJanitor({
knex,
waitBetweenRuns: Duration.fromObject({ minutes: 1 }),
logger,
});
const abortController = new AbortController();
await worker.start(abortController.signal);
const row1 = await getTask(knex);
await waitForExpect(async () => {
expect(row1).toEqual(
expect.objectContaining({
id: 'task1',
current_run_ticket: null,
current_run_started_at: null,
current_run_expires_at: null,
}),
);
});
},
);
});