Skip to content

Commit

Permalink
added more tests for healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
zotya committed Sep 26, 2023
1 parent 3254d57 commit bb38530
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/config/healthcheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function executeQuery(q, appConfig, params = {}) {
return new Promise(async (resolve, reject) => {
params['index_name'] = 'status_' + appConfig['index_name'];
const query = buildQuery(q, params);
// console.log(JSON.stringify(query));
console.log(JSON.stringify(query));
const resp = await runRequest(query, appConfig);
// console.log(JSON.stringify(resp.body));
resolve(resp.body);
Expand All @@ -44,7 +44,7 @@ export async function getlastandnext_started_execution(appConfig) {
});
}

async function getlastfailed_execution(appConfig, params) {
export async function getlastfailed_execution(appConfig, params) {
return new Promise(async (resolve, reject) => {
const body = await executeQuery(
failed_scheduled_atempts_since_last_started,
Expand All @@ -62,7 +62,7 @@ async function getlastfailed_execution(appConfig, params) {
});
}

async function getlastsynctaskssincestarted(appConfig, params) {
export async function getlastsynctaskssincestarted(appConfig, params) {
return new Promise(async (resolve, reject) => {
const body = await executeQuery(
last_sync_task_since_last_start,
Expand All @@ -79,7 +79,7 @@ async function getlastsynctaskssincestarted(appConfig, params) {
});
}

async function getlastsuccessfultasks_for_site(appConfig, params) {
export async function getlastsuccessfultasks_for_site(appConfig, params) {
return new Promise(async (resolve, reject) => {
const body = await executeQuery(
started_or_finished_site_since_last_started,
Expand All @@ -94,7 +94,7 @@ async function getlastsuccessfultasks_for_site(appConfig, params) {
});
}

async function getlatesttasks_for_site(appConfig, params) {
export async function getlatesttasks_for_site(appConfig, params) {
return new Promise(async (resolve, reject) => {
const body = await executeQuery(latest_tasks_for_site, appConfig, params);
if (body.hits.total.value > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { getlastfailed_execution } from '../healthcheck';
//import last_scheduled_started_indexing_RESP from './last_scheduled_started_indexing_RESP.json';

const SLOTS = [
'aboveSearchInput',
'belowSearchInput',
'aboveResults',
'belowResults',
];

jest.mock('@eeacms/search', () => ({
SLOTS: SLOTS,
}));

jest.mock('@eeacms/search/lib/runRequest', () => ({
runRequest: jest.fn().mockResolvedValue({
body: {
took: 963,
timed_out: false,
_shards: {
total: 1,
successful: 1,
skipped: 0,
failed: 0,
},
hits: {
total: {
value: 624,
relation: 'eq',
},
max_score: null,
hits: [
{
_index: 'status_test_index',
_id: 'main_task_2023_09_26_12_50_13',
_score: null,
_source: {
'@version': '1',
cluster: 'main_task',
start_time_ts: 1695732613000,
docs_cnt: 92190,
sites: ['test_site'],
next_execution_date: '2023_09_26_12_55_00',
next_execution_date_ts: 1695732900000,
start_time: '2023_09_26_12_50_13',
task_name: 'scheduled',
msg: '',
index_name: 'status_test_index',
id: 'main_task_2023_09_26_12_50_13',
status: 'Started',
'@timestamp': '2023-09-26T12:50:13.450Z',
},
sort: [1695732613000],
},
],
},
},
}),
}));

describe('test_healthcheck', () => {
it('should return last_started and next_execution_date', async () => {
const appConfig = { index_name: 'test_index' };

const resp = await getlastfailed_execution(appConfig, {});
expect(resp).toEqual({
last_started: 1695732613000,
next_execution_date: 1695732900000,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ jest.mock('@eeacms/search/lib/runRequest', () => ({
}));

describe('test_healthcheck', () => {
it('should return OK', async () => {
//console.log(healthcheck);
it('should return last_started and next_execution_date', async () => {
const appConfig = { index_name: 'test_index' };

const resp = await getlastandnext_started_execution(appConfig, {});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { getlastsynctaskssincestarted } from '../healthcheck';
//import last_scheduled_started_indexing_RESP from './last_scheduled_started_indexing_RESP.json';

const SLOTS = [
'aboveSearchInput',
'belowSearchInput',
'aboveResults',
'belowResults',
];

jest.mock('@eeacms/search', () => ({
SLOTS: SLOTS,
}));

jest.mock('@eeacms/search/lib/runRequest', () => ({
runRequest: jest.fn().mockResolvedValue({
body: {
took: 28,
timed_out: false,
_shards: {
total: 1,
successful: 1,
skipped: 0,
failed: 0,
},
hits: {
total: {
value: 288,
relation: 'eq',
},
max_score: null,
hits: [
{
_index: 'status_test_index',
_id: 'main_task_2023_09_26_13_01_12',
_score: null,
_source: {
'@version': '1',
cluster: 'main_task',
start_time_ts: 1695733272000,
docs_cnt: 242,
sites: ['test_site1', 'test_site2'],
next_execution_date: '2023_09_26_14_00_00',
next_execution_date_ts: 1695736800000,
start_time: '2023_09_26_13_01_12',
task_name: '',
msg: '',
index_name: 'status_test_index',
id: 'main_task_2023_09_26_13_01_12',
status: 'Started',
'@timestamp': '2023-09-26T13:01:12.601Z',
},
sort: [1695733272000],
},
{
_index: 'status_test_index',
_id: 'main_task_2023_09_26_12_00_24',
_score: null,
_source: {
'@version': '1',
cluster: 'main_task',
start_time_ts: 1695729624000,
docs_cnt: 242,
sites: ['test_site1', 'test_site2'],
next_execution_date: '2023_09_26_13_00_00',
next_execution_date_ts: 1695733200000,
start_time: '2023_09_26_12_00_24',
task_name: '',
msg: '',
index_name: 'status_test_index',
id: 'main_task_2023_09_26_12_00_24',
status: 'Started',
'@timestamp': '2023-09-26T12:00:24.187Z',
},
sort: [1695729624000],
},
],
},
},
}),
}));

describe('test_healthcheck', () => {
it('should return list of clusters', async () => {
const appConfig = { index_name: 'test_index' };

const resp = await getlastsynctaskssincestarted(appConfig, {});
expect(resp).toEqual({ sites: ['test_site1', 'test_site2'] });
});
});
97 changes: 97 additions & 0 deletions src/config/healthcheck_queries/latest_tasks_for_site.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { getlatesttasks_for_site } from '../healthcheck';
//import last_scheduled_started_indexing_RESP from './last_scheduled_started_indexing_RESP.json';

const SLOTS = [
'aboveSearchInput',
'belowSearchInput',
'aboveResults',
'belowResults',
];

jest.mock('@eeacms/search', () => ({
SLOTS: SLOTS,
}));

jest.mock('@eeacms/search/lib/runRequest', () => ({
runRequest: jest.fn().mockResolvedValue({
body: {
took: 63,
timed_out: false,
_shards: {
total: 1,
successful: 1,
skipped: 0,
failed: 0,
},
hits: {
total: {
value: 288,
relation: 'eq',
},
max_score: null,
hits: [
{
_index: 'status_test_index',
_id: 'test_site_2023_09_26_13_02_28',
_score: null,
_source: {
'@version': '1',
'@timestamp': '2023-09-26T13:04:34.337Z',
cluster: 'test_site',
start_time_ts: 1695733348000,
msg: '',
index_name: 'status_test_index',
id: 'test_site_2023_09_26_13_02_28',
start_time: '2023_09_26_13_02_28',
status: 'Finished',
},
sort: [1695733348000],
},
{
_index: 'status_test_index',
_id: 'test_site_2023_09_26_12_02_32',
_score: null,
_source: {
'@version': '1',
'@timestamp': '2023-09-26T12:04:57.781Z',
cluster: 'test_site',
start_time_ts: 1695729752000,
msg: '',
index_name: 'test_site',
id: 'test_site_2023_09_26_12_02_32',
start_time: '2023_09_26_12_02_32',
status: 'Finished',
},
sort: [1695729752000],
},
{
_index: 'status_test_index',
_id: 'test_site_2023_09_26_11_02_41',
_score: null,
_source: {
'@version': '1',
'@timestamp': '2023-09-26T11:04:21.093Z',
cluster: 'test_site',
start_time_ts: 1695726161000,
msg: '',
index_name: 'status_test_index',
id: 'test_site_2023_09_26_11_02_41',
start_time: '2023_09_26_11_02_41',
status: 'Finished',
},
sort: [1695726161000],
},
],
},
},
}),
}));

describe('test_healthcheck', () => {
it('should return "OK"', async () => {
const appConfig = { index_name: 'test_index' };

const resp = await getlatesttasks_for_site(appConfig, {});
expect(resp).toEqual('OK');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { getlastsuccessfultasks_for_site } from '../healthcheck';
//import last_scheduled_started_indexing_RESP from './last_scheduled_started_indexing_RESP.json';

const SLOTS = [
'aboveSearchInput',
'belowSearchInput',
'aboveResults',
'belowResults',
];

jest.mock('@eeacms/search', () => ({
SLOTS: SLOTS,
}));

jest.mock('@eeacms/search/lib/runRequest', () => ({
runRequest: jest.fn().mockResolvedValue({
body: {
took: 18,
timed_out: false,
_shards: {
total: 1,
successful: 1,
skipped: 0,
failed: 0,
},
hits: {
total: {
value: 271,
relation: 'eq',
},
max_score: null,
hits: [
{
_index: 'status_test_index',
_id: 'test_site_2023_09_26_13_02_28',
_score: null,
_source: {
'@version': '1',
'@timestamp': '2023-09-26T13:04:34.337Z',
cluster: 'test_site',
start_time_ts: 1695733348000,
msg: '',
index_name: 'status_test_index',
id: 'test_site_2023_09_26_13_02_28',
start_time: '2023_09_26_13_02_28',
status: 'Finished',
},
sort: [1695733348000],
},
],
},
},
}),
}));

describe('test_healthcheck', () => {
it('should return true', async () => {
const appConfig = { index_name: 'test_index' };

const resp = await getlastsuccessfultasks_for_site(appConfig, {});
expect(resp).toEqual(true);
});
});

0 comments on commit bb38530

Please sign in to comment.