Skip to content

Commit

Permalink
Merge pull request #8 from OpenSemanticWorld/master
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
simontaurus committed Dec 16, 2023
2 parents 51314e0 + c96592f commit c17f7e2
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 30 deletions.
64 changes: 60 additions & 4 deletions .github/workflows/redirect_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,77 @@ on:
workflow_dispatch:
inputs:
search_path:
description: "Subpath to test, e.g. '/osw' (empty = all subpaths)"
description: "Subpath(s) to test, e.g. '/osw;/emmo;/pmd' ('/*' = all subpaths)"
default: '/*'
pull_request:

jobs:
build:
dispatch_test:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'workflow_dispatch' }}
steps:
- name: Checkout Project
uses: actions/checkout@v1
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Check if at least one test exists
id: precheck
continue-on-error: true # if 'false' the whole workflow will fail if no test was defined
run: find $(IFS=';' read -ra paths <<< "${{ github.event.inputs.search_path }}"; for path in "${paths[@]}"; do echo ".$path "; done) -type f -name ".htaccess" -print0 | xargs -0 grep "##TEST"

- name: Run apache docker
if: steps.precheck.outcome == 'success'
run: docker run -dit --name apache -e SEARCH_PATH="${{ github.event.inputs.search_path }}" -v "$PWD":/usr/local/apache2/htdocs/ -v "$PWD"/.test/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf httpd:2.4

- name: Install curl
if: steps.precheck.outcome == 'success'
run: docker exec -i apache sh -c "apt-get update && apt-get install curl"

- name: Run tests
if: steps.precheck.outcome == 'success'
run: docker exec -i apache bash < .test/test.sh

pull_request_test:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'pull_request' }}
steps:
- name: Checkout Project
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Get changed dirs
id: changed-dirs
uses: tj-actions/changed-files@v40
with:
dir_names: true
dir_names_max_depth: '1'
dir_names_exclude_current_dir: false

- name: List all changed dirs
run: |
paths=""
for dir in ${{ steps.changed-dirs.outputs.all_changed_files }}; do
echo "$dir was changed"
paths="$paths/$dir;"
done
echo "SEARCH_PATH=$paths" >> $GITHUB_ENV
echo "set SEARCH_PATH to $SEARCH_PATH"
- name: Check if at least one test exists
id: precheck
continue-on-error: true # if 'false' the whole workflow will fail if no test was defined
run: find $(IFS=';' read -ra paths <<< "$SEARCH_PATH"; for path in "${paths[@]}"; do echo ".$path "; done) -type f -name ".htaccess" -print0 | xargs -0 grep "##TEST"

- name: Run apache docker
run: docker run -dit --name apache -e SEARCH_PATH=${{ github.event.inputs.search_path }} -v "$PWD":/usr/local/apache2/htdocs/ -v "$PWD"/.test/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf httpd:2.4
if: steps.precheck.outcome == 'success'
run: docker run -dit --name apache -e SEARCH_PATH="$SEARCH_PATH" -v "$PWD":/usr/local/apache2/htdocs/ -v "$PWD"/.test/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf httpd:2.4

- name: Install curl
if: steps.precheck.outcome == 'success'
run: docker exec -i apache sh -c "apt-get update && apt-get install curl"

- name: Run tests
if: steps.precheck.outcome == 'success'
run: docker exec -i apache bash < .test/test.sh
17 changes: 14 additions & 3 deletions .test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
Tests running directly on the source .htaccess files. It does only test the redirect, not the existance of the resource redirected to.
This is done by mounting the w3id.org repo in an apache docker and test the redirects with curl.

Test specifications are currently extracted from .htaccess file comments, e. g.
Test specifications are currently extracted from .htaccess file comments, e.g.
```
##TESTv1 '/emmo/domain-test --header "Accept: text/html"' "https://emmo-repo.github.io/domain-test/test.html"
##TESTv1 '/mypath --header "Accept: text/html"' "https://my-target-domain.com/test.html"
```

## Run the test as Github Action
1. Navigate to `https://github.com/<your fork>/w3id.org/actions/workflows/redirect_tests.yml` (example see [here](https://github.com/OpenSemanticWorld/w3id.org/actions/workflows/redirect_tests.yml))
1. Select 'Run workflow'
1. Accept the master branch, or select your custom branch
1. Change the subpath to your subpath, e.g., `/mypath`
1. Click 'Run workflow'
1. After completion, click on the run entry to view details (example see [here](https://github.com/OpenSemanticWorld/w3id.org/actions/runs/7224639650/job/19686349546))

## Run the test locally
Note: replace `/*` with your own trusted subpath, e.g. `/mypath`, otherwise all subpaths are tested which may create security issues
```bash
docker run -dit --name apache -p 8080:80 -v "$PWD":/usr/local/apache2/htdocs/ -v "$PWD"/.test/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf httpd:2.4
docker stop apache && docker rm apache
docker run -dit --name apache -e SEARCH_PATH='/*' -v "$PWD":/usr/local/apache2/htdocs/ -v "$PWD"/.test/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf httpd:2.4
docker exec -i apache sh -c "apt-get update && apt-get install curl"
docker exec -i apache bash < .test/test.sh
```
Expand All @@ -19,3 +29,4 @@ docker exec -i apache bash < .test/test.sh
```bash
docker exec -i apache sh -c 'curl -sL -o /dev/null -w "%{url_effective}\n" --resolve www.w3id.org:80:127.0.0.1 www.w3id.org:80/subpath --header "Accept: application/rdf+xml"'
```

71 changes: 48 additions & 23 deletions .test/test.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,56 @@
#!/bin/bash

# Script to run redirct tests directly on the source .htaccess files

# default: no error
return_code=0
for f in $(find /usr/local/apache2/htdocs$SEARCH_PATH -name '.htaccess')
do
echo "Processing file $f"
while IFS= read -r line

# SEARCH_PATH can provide a single ("/mypath") or multiple values ("/mypath1;/mypath2")
IFS=';' read -ra paths <<< "$SEARCH_PATH"
for path in "${paths[@]}"; do

# search path can be restricted with env var $SEARCH_PATH
for f in $(find /usr/local/apache2/htdocs$path -name '.htaccess')
do
#eval "parts=($line)" # not safe to eval user input
# ecape most special chars, better but not completly safe
declare -a "parts=($( echo "$line" | sed 's/[][`~!@#$%^&*();<>,?\|{}=+]/\\&/g' ))"

curl="curl -sL -o /dev/null -w "%{url_effective}" --resolve www.w3id.org:80:127.0.0.1 www.w3id.org:80"
curl_cmd="${parts[1]}"
expected_result="${parts[2]}"
echo "TEST '$curl_cmd' => '$expected_result'"

cmd="$curl$curl_cmd"
result=$(bash -c "$cmd")
if [ "$result" == "$expected_result" ]; then
echo "SUCCESS: Result is '$result'"
else
echo "ERROR : Result is '$result'"
return_code=1
echo "Processing file $f"

tests_found=0

while IFS= read -r line
do
tests_found=$((tests_found + 1))

# Security note: In gerneral it is not secure at all to eval user input
# (here and later in the curl command).
# Make sure to run the test within a encapsulated environment
# and / or run only the test in your subpath.
declare -a "parts=($( echo "$line" | sed 's/##TESTv1//g' ))"

# note: we request only the effective url and create an alias with --resolve
# the subpath specified in the test gets appended to the request url
curl="curl -sL -o /dev/null -w "%{url_effective}" --resolve www.w3id.org:80:127.0.0.1 www.w3id.org:80"
curl_cmd="${parts[0]}"
expected_result="${parts[1]}"

echo "TEST '$curl_cmd' => '$expected_result'"

cmd="$curl$curl_cmd"
result=$(bash -c "$cmd")

# compare the result url with the expected url
if [ "$result" == "$expected_result" ]; then
echo "SUCCESS: Result is '$result'"
else
echo "ERROR : Result is '$result'"
return_code=1
fi
done < <(grep "##TESTv1" "$f")

if [ $tests_found == 0 ]; then
echo "No tests found"
fi
done < <(grep "##TESTv1" "$f")

done
exit $return_code


done
exit $return_code

0 comments on commit c17f7e2

Please sign in to comment.