Skip to content

Commit

Permalink
add more test to ensure 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lonegunmanb committed Feb 14, 2025
1 parent ea072ed commit 1580456
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ jobs:
- name: Run utils tests
run: |
echo `opa version`
target="./policy/common/common.utils.rego"
target="./policy/common"
echo $target
set -o pipefail
opa test $target -v || exit 1
coverage_values=`opa test --coverage $target -v | grep -iE '"coverage": [ 0-9 ]+'`
coverage=`printf "${coverage_values}%%\n" | tail -n 1 | xargs`
echo $coverage
coverage_json=$(opa test --coverage --format=json $target -v)
coverage=$(echo $coverage_json | jq '.coverage')
echo "Coverage: $coverage"
if [ "$coverage" != "coverage: 100%" ]; then
opa test --coverage $target -v
if [ "$coverage" != "100" ]; then
echo $coverage_json | jq
printf "\n\e[0;31mRule test coverage must be 100%%\e[0m\n"
exit 1
fi;
Expand Down
16 changes: 16 additions & 0 deletions policy/common/common.utils_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,20 @@ test_is_resource_create_or_update if {
data.utils.is_resource_create_or_update({"change": {"actions": ["delete", "create"]}})
not data.utils.is_resource_create_or_update({"change": {"actions": ["create", "delete"]}})
data.utils.is_resource_create_or_update({"change": {"actions": ["update"]}})
}

test_is_azure_type if {
# Test case: resource type matches the specified Azure type
utils.is_azure_type({"type": "Microsoft.DocumentDB/databaseAccounts@2024-12-01-preview"}, "Microsoft.DocumentDB/databaseAccounts")

# Test case: resource type does not match the specified Azure type
r := {"type": "Microsoft.Network/loadBalancers@2024-12-01-preview"}
azure_type := "Microsoft.DocumentDB/databaseAccounts"
not utils.is_azure_type({"type": "Microsoft.Network/loadBalancers@2024-12-01-preview"}, "Microsoft.DocumentDB/databaseAccounts")

# Test case: resource type matches a different Azure type
utils.is_azure_type({"type": "Microsoft.Network/loadBalancers@2024-12-01-preview"}, "Microsoft.Network/loadBalancers")

# Test case: resource type does not match any Azure type
not utils.is_azure_type({"type": "Custom.ResourceType@2024-12-01-preview"}, "Microsoft.DocumentDB/databaseAccounts")
}

0 comments on commit 1580456

Please sign in to comment.