Skip to content

Commit

Permalink
chore: testing drift detection script
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderblue committed May 28, 2024
1 parent 24feefc commit 092ca5a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 29 deletions.
71 changes: 42 additions & 29 deletions .github/workflows/test_unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,60 +35,73 @@ jobs:
run: |
make test-unit cover-report
- name: Create test failure report
id: test-failure-report
if: ${{ failure() }}
run: |
go install github.com/mfridman/tparse@latest
make test-failure-report
tparse -file=coverage/unit.failures -format=markdown > coverage/unit.md
- name: Prepare multiline test report
id: payload
shell: bash
if: ${{ always() }}
run: |
cat coverage/unit.md
result=$(cat coverage/unit.md)
echo "Result: $result"
echo 'UNIT_TEST_FAILURES<<EOF'
$result
echo EOF
- name: Failure report
id: failure-report
- name: Detect drift
if: ${{ always() }}
run: |
echo "${{ join(steps.test-failure-report.outputs.*, '\n') }}"
uses: actions/github-script@v7
id: detect-state-drift
with:
script: |
const script = require('./scripts/detect-state-drift.js')
await script({core})
# - name: Report unit test coverage via Codecov
# uses: codecov/codecov-action@v3
# with:
# files: ./coverage/coverage.out

- name: Send test failures report to Slack
- name: Send report to Slack
id: slack
if: ${{ always() }}
uses: slackapi/[email protected]
with:
# Uses Slack's Block Kit to build the message
# https://app.slack.com/block-kit-builder
payload: |
{
"text": "New Relic Terraform Provider Test Failures",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":warning: *Terraform Provider | Drift Detected*"
}
},
{
"type": "divider"
},
{
"type": "context",
"elements": [
{
"type": "plain_text",
"text": ${{ toJSON(steps.test-failure-report.outputs.UNIT_TEST_FAILURES) }}
"text": ${{ toJSON(steps.detect-state-drift.outputs.failed_tests_with_drift) }}
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "More information can be viewed in the job summary."
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Job Summary",
"emoji": true
},
"value": "View Job Summary",
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
"action_id": "button-action"
}
},
{
"type": "divider"
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

54 changes: 54 additions & 0 deletions scripts/detect-state-drift.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module.exports = async ({
core
}) => {
const fs = require('fs');

// Read the text from the file
fs.readFile('coverage/unit.report', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}

// Split the text into individual lines
const lines = data.trim().split('\n');

// Parse each line and convert it to JSON
const jsonData = lines.map(line => {
try {
return JSON.parse(line);
} catch (error) {
console.error('Error parsing line:', error);
return null;
}
});

// Print the resulting JSON array
// console.log(jsonData);

const report = jsonData.filter(data => {
return data.Output ? data.Output.includes('error: After applying this test step, the plan was not empty') : false;
}).map(t => t.Output.trim());

console.log(report);

let msg = 'error: After applying this test step, the plan was not empty';
if (report.length > 0) {
msg = `'${report.join('\n')}'`;
}

core.setOutput('failed_tests_with_drift', msg);

// // Convert JSON data to string
// const jsonString = JSON.stringify(failedTests, null, 2);

// // Write the JSON data to a file
// fs.writeFile('output.json', jsonString, 'utf8', err => {
// if (err) {
// console.error('Error writing file:', err);
// return;
// }
// console.log('JSON data has been written to output.json');
// });
});
};

0 comments on commit 092ca5a

Please sign in to comment.