Skip to content

Commit

Permalink
Add web and websecure entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronMcHale committed Jan 19, 2025
1 parent 41b2a7a commit ab361f9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
9 changes: 9 additions & 0 deletions services/traefik/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ services:
# Compose file, which is only enabled if the `TRAEFIK_API_DASHBOARD` env
# variable, so we ensure API and Dashboard are disabled by default.
TRAEFIK_API_DASHBOARD: false
# HTTP and HTTPS entrypoints
# We use `asDefault` to setup HTTPS by default on all routes, and redirect
# all HTTP to HTTPS. This means routes do not need to specify entrypoints,
# it just works.
TRAEFIK_ENTRYPOINTS_WEB_ADDRESS: ':80'
TRAEFIK_ENTRYPOINTS_WEB_HTTP_REDIRECTIONS_ENTRYPOINT_TO: 'websecure'
TRAEFIK_ENTRYPOINTS_WEBSECURE_ADDRESS: ':443'
TRAEFIK_ENTRYPOINTS_WEBSECURE_ASDEFAULT: 'true'
TRAEFIK_ENTRYPOINTS_WEBSECURE_HTTP_TLS: 'true'
# Traefik log level settings, defaults to ERROR if the TRAEFIK_LOG_LEVEL
# env variable is not set.
TRAEFIK_LOG_LEVEL: "${TRAEFIK_LOG_LEVEL:-ERROR}"
Expand Down
32 changes: 32 additions & 0 deletions tests/asserts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function assert() {
'not: compose_container_up') assert_compose_service_up_not "$1" ;;
'http_status_code') assert_http_status_code "$1" "$2" ;;
'not: http_status_code') assert_http_status_code_not "$1" "$2" ;;
'http_response_headers_contain') assert_http_response_headers_contain "$1" "$2" ;;
'not: http_response_headers_contain') assert_http_response_headers_contain_not "$1" "$2" ;;
*) echo 'assert: '"$assert"': test does not exist.'; exit 1 ;;
esac
}
Expand Down Expand Up @@ -173,3 +175,33 @@ function assert_http_status_code_not() {
exit 1
fi
}

function assert_http_response_headers_contain() {
tmp_file='/tmp/web-router-test-curl-'"$RANDOM"
curl --insecure --no-progress-meter --head "$1" > "$tmp_file"
if [ $(grep --count --fixed-strings "$2" "$tmp_file") -eq 0 ]; then
print_assert_msg 'Expected response header to contain string, response headers do not contain expected string.'
echo 'Expected string: '"$2"
echo 'URL: '"$1"
echo 'Response headers:'
cat "$tmp_file"
rm -f "$tmp_file"
exit 1
fi
rm -f "$tmp_file"
}

function assert_http_response_headers_contain_not() {
tmp_file='/tmp/web-router-test-curl-'"$RANDOM"
curl --insecure --no-progress-meter --head "$1" > "$tmp_file"
if [ $(grep --count --fixed-strings "$2" "$tmp_file") -gt 0 ]; then
print_assert_msg 'Expected response header not to contain string, found string in response headers.'
echo 'String: '"$2"
echo 'URL: '"$1"
echo 'Response headers:'
cat "$tmp_file"
rm -f "$tmp_file"
exit 1
fi
rm -f "$tmp_file"
}
4 changes: 2 additions & 2 deletions tests/traefik/test-traefik-api-dashboard.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ echo 'Setup tests for TRAEFIK_API_DASHBOARD=0...'
echo 'TRAEFIK_API_DASHBOARD=0' > test-traefik-api-dashboard.env
export ENV_FILE='test-traefik-api-dashboard.env'
. env.sh
traefik_url='http://'"$DEFAULT_DOMAIN"
traefik_url='https://'"$DEFAULT_DOMAIN"
docker compose up -d
sleep 2 # give a little time for the route to be registered
echo -e "OK\n"
Expand All @@ -32,7 +32,7 @@ echo 'Setup tests for TRAEFIK_API_DASHBOARD=1...'
echo 'TRAEFIK_API_DASHBOARD=1' > test-traefik-api-dashboard.env
export ENV_FILE='test-traefik-api-dashboard.env'
. env.sh
traefik_url='http://'"$DEFAULT_DOMAIN"
traefik_url='https://'"$DEFAULT_DOMAIN"
docker compose up -d
sleep 2 # give a little time for the route to be registered
echo -e "OK\n"
Expand Down
12 changes: 11 additions & 1 deletion tests/traefik/test-traefik.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ echo "Test setup..."
cd ..
. env.sh
docker compose up -d
sleep 2 # give a little time for routes to be registered
echo -e "OK\n"

echo "Test Traefik is set to start by default..."
Expand All @@ -32,11 +33,20 @@ set +e; output="$(docker compose exec traefik echo 'test'>/tmp/test && cat /tmp/
assert_msg='attempt to write to /tmp failed or the file was written but its contents could not be read, expected `cat /tmp/test` to output: test'
assert contain 'test' "$output"
echo -e "OK\n"
unset assert_msg

echo "Test Traefik redirects HTTP to HTTPS by default..."
echo " Testing status code..."
assert http_status_code 'http://localhost/' '301'
echo " Testing response headers..."
assert http_response_headers_contain "http://localhost/" "https://localhost/"
echo -e "OK\n"
exit

echo "Test Traefik can discover Docker containers..."
export COMPOSE_FILE="$COMPOSE_FILE"":tests/traefik/docker-compose.test-traefik-docker-discovery.yml"
docker compose up -d
sleep 2 # give a little time for the route to be registered
url="http://""$DEFAULT_DOMAIN""/api/http/routers/test-traefik-docker-discovery@docker"
url="https://""$DEFAULT_DOMAIN""/api/http/routers/test-traefik-docker-discovery@docker"
assert http_status_code $url '200'
echo -e "OK\n"

0 comments on commit ab361f9

Please sign in to comment.