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 script to compile, build, run, call API and test response [ci skip] #5737

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 30 additions & 0 deletions script/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Scripts

This folder is intended for various scripts used during the OTP development. They are provided
"as is" and the "owner" may do whatever she/he likes with it.

If you want to submit your own scripts, you need to include:
- A header at the beginning of the script stating who the owner is.
- The script should print some usage documentation if invoked with `--help` and `-h`.

The regular pull-request approval process is required for submitting new scripts and changing
existing one. The reviewers are responsible for:
- [ ] Is this script relevant for OTP and at least one active member of the OTP community?
- [ ] Is the script harmful?
- [ ] Does the script have sufficient documentation?
- [ ] Owner section
- [ ] Print help with `-h` and `--help`

### Example
```
# Owner: J. Brown, Fun & Fast Transit Inc

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo "The purpouse of the script is .."
t2gran marked this conversation as resolved.
Show resolved Hide resolved
echo "Usage: ..."
echo "Parameters: "
:
fi

```

105 changes: 105 additions & 0 deletions script/run-and-test-otp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/bin/bash

## Owner: Thomas Gran, Entur AS

# [ EDIT HERE ] ----------------------------------------------------------

# Match or NOT Match. The script return success 0 - Good if a match is found(-l) or not found(-L)
# -l : Match
# -L : Not match
MATCH="-L"


# The HTTP URL query to call using curl"
QUERY="http://localhost:8080/otp/routers/default/plan?"
QUERY+="fromPlace=63.30959874454729%2C9.858169555664064&"
QUERY+="toPlace=63.26723697045908%2C9.811992645263674&"
QUERY+="time=14%3A50&date=03-07-2024&"
QUERY+="mode=FLEX_ACCESS%2CFLEX_EGRESS%2CTRANSIT&"
QUERY+="searchWindow=780"

ACCEPT_HEADER="accept: application/json, */*"

# The string token to search for"
SEARCH_FOR="No trip found"

# File catalog where the the OTP config files is (build-config.json & router-config.json)
DATA_DIR=../data/fix-error-access

# ----------------------------------------------------------- [ EDIT END ]

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo "This script: "
echo " 1. Compile OTP"
echo " 2. Run OTP - build a graph and start the server"
echo " 3. Send a request using curl"
echo " 4. Test the response, search for a unexpected token. If the token is"
t2gran marked this conversation as resolved.
Show resolved Hide resolved
echo " NOT present the test is GOOD, if not it is BAD"
echo ""
echo "Before using this script you should copy it to a folder which is NOT"
echo "under version control and then edit it. You must provide a query, a test and the"
echo "path to the otp data root directory with the OTP configuration files. You need to"
echo "edit the following variables in the beginning of the script:"
echo " - QUERY - the HTTP URL query to call using curl"
echo " - SEARCH_FOR - The string token to search for - if it is present the test FAILS!"
echo " - DATA_DIR - File catalog where the OTP config files is (build-config.json & router-config.json)"
echo ""
echo "This script is intended used together with 'git bisect' (binary search for good and bad"
echo "commits), but it works well with manual changes in the code as well. When you have found"
echo "the bad commit, you may manually undo it line by line to find the problem."
echo ""
echo "ARGUMENTS"
echo " --help | -h : Help"
echo " --skipCompile | -c : Skip Maven compile"
exit 0
fi

# Files used to store intermediate results - check the files if the script
# is not working as expected.
OTP_LOG=target/otp.log
RESPONSE_FILE=target/response.json


if [ "$1" != "--skipCompile" ] && [ "$1" != "-c" ]; then
echo "Build project with maven"
mvn clean package -Dps -DskipTests
fi

echo "Start OTP, output: $OTP_LOG"
mv target/otp-*-shaded.jar target/otp-shaded.jar
java -Xmx16G -jar target/otp-shaded.jar ${DATA_DIR} --build --save --serve > ${OTP_LOG} &
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be possible to use | tee "${OTP_LOG}" instead of redirection to file, keep the console output and skip the tail process below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it will work, I need the the java PID (see line 68) to terminate the java process later, so if we use tee then I think we get the tee PID instead. I tried something like it, but it did not work. Not sure if I did the exact same thing as you suggested.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice it doesn't matter that much: even if it's the tee pid that is killed, the whole pipe is still terminated.

while true ; do sleep 1 ; date ; done | tee /tmp/log.txt & ; PID=$! ; echo "PID: $PID\n" ; sleep 4 ; ps aux | grep $PID ; kill $PID

Nevermind, it doesn't matter. 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good tip, I didn't know that. I will leave it as is for now. Do not want to spend more time on it.

OTP_PID=$!

tail -F ${OTP_LOG} &
TAIL_PID=$!

while ! grep "Grizzly server running" ${OTP_LOG};do echo "#";sleep 1;done

echo "OTP Server up and running"

echo "Query: $QUERY"
curl -s -o ${RESPONSE_FILE} "$QUERY" -H "$ACCEPT_HEADER"

echo "Test results does NOT match (-L) or match (-l)"
grep ${MATCH} "${SEARCH_FOR}" ${RESPONSE_FILE}
OK=$?

echo "Shutdown..."
echo "Kill Otp Server PID: ${OTP_PID}"
kill $OTP_PID

# Allow OTP to shutdown before we kill the tail and return, this is not critical it is just
# a bit confusing if the script is done, while OTP is still writing to the console.
sleep 3
echo "Kill Tail PID: ${TAIL_PID}"
kill $TAIL_PID

echo ""

if [ "$OK" == 0 ]; then
echo "Test is OK - GOOD"
exit 0
else
echo "Test failed - BAD"
exit 1
fi
Loading