-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: log workflowGroup from the exit-handler TDE-1230 #703
Closed
Closed
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
087a144
feat: log workflowGroup from the exit-handler TDE-1230
paulfouquet bd5f99a
docs: update exit handler documentation
paulfouquet 239c19b
refactor: simplify
paulfouquet b10afe8
test: add tests for exit handler script
paulfouquet 3ea2db3
Merge branch 'master' into feat/exit-handler-log-workflowgroup-tde-1230
paulfouquet 4ce7b4b
refactor: example using mock
blacha 5232e66
refactor: mock logs and use JSON stringify to pass string json
paulfouquet 80bda46
fix: avoid startsWith to error if source is not a string
paulfouquet bc2fbed
Merge branch 'master' into feat/exit-handler-log-workflowgroup-tde-1230
paulfouquet 32f6166
test: update expected logs
paulfouquet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,112 @@ | ||
import assert from 'node:assert'; | ||
import fs from 'node:fs'; | ||
import { describe, it } from 'node:test'; | ||
|
||
/** | ||
* Read the workflow YAML file and create a function from the script inside. | ||
* replacing {{ inputs.* }} with ctx | ||
* | ||
* @param {*} ctx | ||
* @returns Function that requires a `context` and `core` parameter | ||
*/ | ||
function createTestFunction(ctx) { | ||
const func = fs.readFileSync('./templates/common/exit.handler.yml', 'utf-8').split('source: |')[1]; | ||
|
||
const newFunc = func | ||
// Replace inputs | ||
.replace('{{= inputs.parameters.workflow_parameters }}', `${ctx.workflowParameters ?? '[]'}`) | ||
.replace('{{inputs.parameters.workflow_status}}', `${ctx.workflowStatus ?? 'Failed'}`) | ||
.split('\n') | ||
.join('\n'); | ||
//console.log(newFunc); | ||
return new Function('context', newFunc); | ||
} | ||
|
||
function runScript(ctx) { | ||
const action = createTestFunction(ctx); | ||
|
||
const output = {}; | ||
action(); | ||
|
||
return output; | ||
} | ||
|
||
describe('exit handler script template', () => { | ||
it('should log workflow status and parameters', () => { | ||
const originalLog = console.log; | ||
let logOutput = []; | ||
console.log = (...args) => logOutput.push(args.join(' ')); | ||
|
||
runScript({ | ||
workflowParameters: `[ | ||
{ name: 'source', value: 's3://linz-topographic-upload/abc/', description: 'Source bucket' }, | ||
{ name: 'ticket', value: 'GDE-123', description: 'JIRA Ticket' }, | ||
]`, | ||
workflowStatus: `Succeeded`, | ||
}); | ||
|
||
console.log = originalLog; | ||
let logOutputDict = JSON.parse(logOutput); | ||
// override time | ||
logOutputDict.time = 1724037007216; | ||
|
||
assert.deepEqual(logOutputDict, { | ||
time: 1724037007216, | ||
level: 20, | ||
pid: 1, | ||
msg: 'Workflow:Succeeded', | ||
workflowGroup: 'land', | ||
parameters: { source: 's3://linz-topographic-upload/abc/', ticket: 'GDE-123' }, | ||
}); | ||
}); | ||
|
||
it('should log workflowGroup as land', () => { | ||
const originalLog = console.log; | ||
let logOutput = []; | ||
console.log = (...args) => logOutput.push(args.join(' ')); | ||
|
||
runScript({ | ||
workflowParameters: `[ | ||
{ name: 'source', value: 's3://linz-topographic-upload/abc/'}, | ||
]`, | ||
}); | ||
console.log = originalLog; | ||
const logOutputDict = JSON.parse(logOutput); | ||
|
||
assert.equal(logOutputDict.workflowGroup, 'land'); | ||
}); | ||
|
||
it('should log workflowGroup as sea', () => { | ||
const originalLog = console.log; | ||
let logOutput = []; | ||
console.log = (...args) => logOutput.push(args.join(' ')); | ||
|
||
runScript({ | ||
workflowParameters: `[ | ||
{ name: 'source', value: 's3://linz-hydrographic-upload/abc/'}, | ||
]`, | ||
}); | ||
|
||
console.log = originalLog; | ||
const logOutputDict = JSON.parse(logOutput); | ||
|
||
assert.equal(logOutputDict.workflowGroup, 'sea'); | ||
}); | ||
|
||
it('should log workflowGroup as unknown', () => { | ||
const originalLog = console.log; | ||
let logOutput = []; | ||
console.log = (...args) => logOutput.push(args.join(' ')); | ||
|
||
runScript({ | ||
workflowParameters: `[ | ||
{ name: 'source', value: 's3://linz-bucket/abc/'}, | ||
]`, | ||
}); | ||
|
||
console.log = originalLog; | ||
const logOutputDict = JSON.parse(logOutput); | ||
|
||
assert.equal(logOutputDict.workflowGroup, 'unknown'); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -29,12 +29,27 @@ spec: | |
let parameters = {}; | ||
{{= inputs.parameters.workflow_parameters }}.forEach((pair) => (parameters[pair.name] = pair.value)); | ||
|
||
function guessWorkflowGroup() { | ||
const source = parameters['source']; | ||
if (!source) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. safer to be |
||
return 'unknown'; | ||
} | ||
if (source.startsWith('s3://linz-topographic-upload')) { | ||
return 'land'; | ||
} | ||
if (source.startsWith('s3://linz-hydrographic-upload')) { | ||
return 'sea'; | ||
} | ||
return 'unknown'; | ||
} | ||
|
||
console.log( | ||
JSON.stringify({ | ||
time: Date.now(), | ||
level: 20, | ||
pid: 1, | ||
msg: 'Workflow:{{inputs.parameters.workflow_status}}', | ||
workflowGroup: guessWorkflowGroup(), | ||
parameters, | ||
}), | ||
); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be good to use the built in test stubber her rather than overwriting the logger