Skip to content
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

Add promtool test support #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ An exit code of `0` is considered a successful execution.

## Usage

Promtool GitHub Actions are a single GitHub Action that executes different promtool subcommands depending on the content of the GitHub Actions YAML file. Right now only `rules` and `config` is supported which runs `promtool check rules` and `promtool check config` for the given files.
Promtool GitHub Actions are a single GitHub Action that executes different promtool subcommands depending on the content of the GitHub Actions YAML file. Right now only `rules`, `config` or `test` is supported which runs `promtool check rules`, `promtool check config` or ` promtool test rules` for the given files.

```yaml
name: Check Prometheus Alert rules
Expand Down Expand Up @@ -49,14 +49,23 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Test Prometheus rules
uses: peimanja/promtool-github-actions@master
with:
promtool_actions_subcommand: 'test'
promtool_actions_files: 'prometheus/tests/*.yml'
promtool_actions_version: '2.14.0'
promtool_actions_comment: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

## Inputs

Inputs configure Terraform GitHub Actions to perform different actions.

* `promtool_actions_subcommand` - (Required) The Promtool subcommand to execute. Valid values are `rules` and `config`.
* `promtool_actions_files` - (Required) Path to files. Can be something like `configs/*.yml` or `alert_rules/*.yml`.
* `promtool_actions_subcommand` - (Required) The Promtool subcommand to execute. Valid values are `rules`, `config` and `test`.
* `promtool_actions_files` - (Required) Path to files. Can be something like `configs/*.yml`, `alert_rules/*.yml` or `tests/*.yml`.
* `promtool_actions_version` - (Optional) The Promtool version to install and execute (Prometheus bundle version). The default is set to `latest` and the latest stable version will be pulled down automatically.
* `promtool_actions_comment` - (Optional) Whether or not to comment on GitHub pull requests. Defaults to `true`.

Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ branding:
color: 'purple'
inputs:
promtool_actions_subcommand:
description: 'Promtool subcommand to execute (configs or rules).'
description: 'Promtool subcommand to execute (configs, rules or test).'
required: true
promtool_actions_files:
description: 'Path to files. Can be something like `configs/*.yml` or `alert_rules/*.yml`.'
description: 'Path to files. Can be something like `configs/*.yml`, `alert_rules/*.yml` or `tests/*.yml`.'
required: true
promtool_actions_version:
description: 'Promtool version to install.'
Expand Down
6 changes: 6 additions & 0 deletions src/main.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ function main {
scriptDir=$(dirname ${0})
source ${scriptDir}/promtool_check_rules.sh
source ${scriptDir}/promtool_check_config.sh
source ${scriptDir}/promtool_test_rules.sh


parseInputs
cd ${GITHUB_WORKSPACE}
Expand All @@ -77,6 +79,10 @@ function main {
installPromtool
promtoolCheckRules ${*}
;;
test)
installPromtool
promtoolTestRules ${*}
;;
*)
echo "Error: Must provide a valid value for promtool_subcommand"
exit 1
Expand Down
45 changes: 45 additions & 0 deletions src/promtool_test_rules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

function promtoolTestRules {
echo "rules: info: testing if Prometheus rule files are valid or not"
testRulesOut=$(ls ${promFiles} | xargs -I{} promtool test rules {} ${*} 2>&1)
testRulesExitCode=${?}

# Exit code of 0 indicates success. Print the output and exit.
if [ ${testRulesExitCode} -eq 0 ]; then
echo "testRules: info: Prometheus test succeeded."
echo "${testRulesOut}"
echo
testRulesCommentStatus="Success"
fi

# Exit code of !0 indicates failure.
if [ ${testRulesExitCode} -ne 0 ]; then
echo "testRules: error: Prometheus test failed."
echo "${testRulesOut}"
echo
testRulesCommentStatus="Failed"
fi

# Comment on the pull request if necessary.
if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${promtoolComment}" == "1" ]; then
testRulesCommentWrapper="#### \`promtool test rules\` ${testRulesCommentStatus}
<details><summary>Show Output</summary>

\`\`\`
${testRulesOut}
\`\`\`

</details>

*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Files: \`${promFiles}\`*"

echo "testRules: info: creating JSON"
testRulesPayload=$(echo "${testRulesCommentWrapper}" | jq -R --slurp '{body: .}')
testRulesCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
echo "testRules: info: commenting on the pull request"
echo "${testRulesPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${testRulesCommentsURL}" > /dev/null
fi

exit ${testRulesExitCode}
}