-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #319 from bcgov/ssoteam-396
Fix: kc-cron-job hanging
- Loading branch information
Showing
8 changed files
with
2,242 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: Run unit tests | ||
|
||
on: | ||
push: | ||
paths: | ||
- "docker/kc-cron-job/**" | ||
- ".github/workflows/unit-test.yml" | ||
|
||
jobs: | ||
unit_test: | ||
runs-on: ubuntu-20.04 | ||
defaults: | ||
run: | ||
working-directory: ./docker/kc-cron-job | ||
steps: | ||
# Matching docker version | ||
- uses: actions/checkout@v3 | ||
- name: Use Node.js 14 | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 14 | ||
- name: unit test | ||
run: | | ||
ls | ||
yarn | ||
yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
node_modules | ||
npm-debug.log | ||
__tests__ | ||
jest.config.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# About | ||
|
||
This is the codebase for the [related helm chart](../../helm/kc-cron-job/). | ||
|
||
## Zip Logs | ||
|
||
The [zip logs](./zip-logs.js) job is designed to create zip files of the logs as the buildup in keycloak, and insert a copy into our database. Keycloak's [configuration](/docker/keycloak/configuration/standalone-openshift-7.6.xml#L68) periodically aggregates log files into dated files. The workflow for this job uses that to: | ||
|
||
- zip logs files from the same date together into one folder | ||
- delete zipped logs older than the EXPIRY_LENGTH_DAYS variable | ||
- insert logs into a database for longer storage | ||
|
||
### Testing | ||
|
||
Tests for this job can be run with `yarn test`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const { saveFilesToDatabase, getDate, getClient } = require('../event-logs'); | ||
const fsPromises = require('fs').promises; | ||
const path = require('path'); | ||
const { Client } = require('pg'); | ||
|
||
/** | ||
* First log is an empty line | ||
* Second log is missing expected "sequence" parameter | ||
* Third line contains all expected fields | ||
*/ | ||
const unexpectedLogs = ` | ||
{"@timestamp":"a","loggerClassName":"a","loggerName":"a","level":"a","message":"type=USER_INFO_REQUEST","threadName":"a","threadId":1,"mdc":{},"ndc":"","hostName":"a","processName":"a","processId":1,"@version":"1"} | ||
{"@timestamp":"a","sequence":1001,"loggerClassName":"a","loggerName":"a","level":"a","message":"type=USER_INFO_REQUEST","threadName":"a","threadId":1,"mdc":{},"ndc":"","hostName":"a","processName":"a","processId":1,"@version":"1"}`; | ||
|
||
const fileDir = getDate(2); | ||
const dir = path.join(__dirname, `fixtures/${fileDir}`); | ||
|
||
jest.mock('pg', () => { | ||
const mockClient = { | ||
connect: jest.fn(), | ||
query: jest.fn(), | ||
end: jest.fn(), | ||
}; | ||
return { Client: jest.fn(() => mockClient) }; | ||
}); | ||
|
||
/** | ||
* Create some fixture data with usable dates | ||
*/ | ||
const setupFixtures = async (fileData) => { | ||
await fsPromises.mkdir(dir, { recursive: true }); | ||
await fsPromises.writeFile(path.join(`${dir}/test`), fileData); | ||
}; | ||
|
||
const clearFiles = () => fsPromises.rmdir(dir, { force: true, recursive: true }); | ||
|
||
describe('Save Files to Database', () => { | ||
let client; | ||
beforeEach(() => { | ||
client = new Client(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await clearFiles(); | ||
}); | ||
|
||
it('Logs an info message if could not parse a line, and continues on to remaining logs', async () => { | ||
console.info = jest.fn(); | ||
|
||
await setupFixtures(unexpectedLogs); | ||
await saveFilesToDatabase('__tests__/fixtures'); | ||
|
||
// Empty line in logs should log a message indicating could not be parsed | ||
const jsonParseError = console.info.mock.calls.find((call) => | ||
call[0].startsWith('Error trying to JSON parse line'), | ||
); | ||
expect(jsonParseError).not.toBe(undefined); | ||
|
||
// Log with missing sequence should log a message indicating could not be uploaded | ||
const unexpectedFormatError = console.info.mock.calls.find((call) => | ||
call[0].startsWith('Log does not have expected format'), | ||
); | ||
expect(unexpectedFormatError).not.toBe(undefined); | ||
|
||
// FInal valid log is still sent to DB insert command (log with sequence number 1001) | ||
const queries = client.query.mock.calls; | ||
const insertionQuery = queries.find((query) => query[0].startsWith('INSERT INTO sso_logs')); | ||
expect(insertionQuery[0].includes('1001')).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
rootDir: '.', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ | |
"author": "First Last <[email protected]>", | ||
"main": "server.js", | ||
"scripts": { | ||
"start": "node server.js" | ||
"start": "node server.js", | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"archiver": "^5.3.0", | ||
|
@@ -20,6 +21,7 @@ | |
"xml2js": "^0.6.0" | ||
}, | ||
"devDependencies": { | ||
"dotenv": "^16.3.1" | ||
"dotenv": "^16.3.1", | ||
"jest": "^29.7.0" | ||
} | ||
} |
Oops, something went wrong.