Skip to content

Commit

Permalink
test client-cli in GH actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Habbie committed Mar 6, 2024
1 parent df474a9 commit c131fc4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ jobs:
run: meson setup ${{github.workspace}}/build
- name: Build
run: meson compile -C ${{github.workspace}}/build
- name: start HA in the background
run: scripts/background-ha.sh
- name: test client-cli
run: LD_LIBRARY_PATH=./build/subprojects/curl-8.5.0/build/lib/.libs/ HA_WS_URL=ws://localhost:8123/api/websocket HA_API_TOKEN=$(cat scripts/docker/longtoken.txt) build/client-cli list-entities
- uses: actions/upload-artifact@v4
with:
name: ubuntu-build
Expand Down
12 changes: 12 additions & 0 deletions scripts/background-ha.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh -x

# FIXME: this started as a copy of tmux.sh, and we should see if we can unify them

PWD=`pwd`
ROOT=`dirname $0`/..

docker pull ghcr.io/home-assistant/home-assistant:stable

cp -r ${ROOT}/scripts/docker/config ${ROOT}/scripts/docker/ha-config
( cd ${ROOT}/scripts/docker && docker-compose up -d )
( cd ${ROOT}/scripts/docker && ./register.sh )
4 changes: 3 additions & 1 deletion scripts/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ services:
- /etc/localtime:/etc/localtime:ro
# restart: unless-stopped
privileged: true
network_mode: host
# network_mode: host
ports:
- 8123:8123
26 changes: 13 additions & 13 deletions scripts/docker/register.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/sh

CHECK_COUNT=0
curl -s 'http://localhost:8123/' 1>/dev/null
curl -s 'http://0.0.0.0:8123/' 1>/dev/null
while [ $? -ne 0 ]
do
if [ $CHECK_COUNT -gt 10 ]; then
if [ $CHECK_COUNT -gt 15 ]; then
echo "HA doesn't seem to be started after a while, giving up."
exit 1
fi
Expand All @@ -14,41 +14,41 @@ do
CHECK_COUNT=$((CHECK_COUNT+1))

# Keep on the last line because of exit status check
curl -s 'http://localhost:8123' 1>/dev/null
curl -s 'http://0.0.0.0:8123' 1>/dev/null
done


# Create voorkant user
curl 'http://localhost:8123/api/onboarding/users' -s -X POST --data-raw '{"client_id":"http://localhost:8123/","name":"voorkant","username":"voorkant","password":"v00rk4nt","language":"en-GB"}' > auth_code.json
curl 'http://0.0.0.0:8123/api/onboarding/users' -s -X POST --data-raw '{"client_id":"http://0.0.0.0:8123/","name":"voorkant","username":"voorkant","password":"v00rk4nt","language":"en-GB"}' > auth_code.json
MSG=`jq -r '.message' auth_code.json`
if [ "${MSG}" != "User step already done" ]; then
CODE=`jq -r '.auth_code' auth_code.json`
curl 'http://localhost:8123/auth/token' -s -X POST -F "grant_type=authorization_code" -F "code=${CODE}" -F "client_id=http://localhost:8123/" > token.json
curl 'http://0.0.0.0:8123/auth/token' -s -X POST -F "grant_type=authorization_code" -F "code=${CODE}" -F "client_id=http://0.0.0.0:8123/" > token.json
TOKEN=`jq -r '.access_token' token.json`
# To complete setup, you have to set some other stuff. For whatever reason (might be because demo mode) these requests are empty in our capture.
curl 'http://localhost:8123/api/onboarding/core_config' -s -X POST -H "authorization: Bearer ${TOKEN}" > /dev/null
curl 'http://localhost:8123/api/onboarding/analytics' -s -X POST -H "authorization: Bearer ${TOKEN}" > /dev/null
curl 'http://localhost:8123/api/onboarding/integration' -s -X POST -H "authorization: Bearer ${TOKEN}" -d '{"client_id":"http://localhost:8123/","redirect_uri":"http://localhost:8123/?auth_callback=1"}' > /dev/null
curl 'http://0.0.0.0:8123/api/onboarding/core_config' -s -X POST -H "authorization: Bearer ${TOKEN}" > /dev/null
curl 'http://0.0.0.0:8123/api/onboarding/analytics' -s -X POST -H "authorization: Bearer ${TOKEN}" > /dev/null
curl 'http://0.0.0.0:8123/api/onboarding/integration' -s -X POST -H "authorization: Bearer ${TOKEN}" -d '{"client_id":"http://0.0.0.0:8123/","redirect_uri":"http://0.0.0.0:8123/?auth_callback=1"}' > /dev/null

rm auth_code.json
rm token.json

if [ -x "../../build/client-cli" ]
then
HA_WS_URL=ws://localhost:8123/api/websocket HA_API_TOKEN="${TOKEN}" LD_LIBRARY_PATH=../../build/subprojects/curl-8.5.0/build/lib/.libs/ ../../build/client-cli ha-get-token 2>register.log 1>longtoken.txt
HA_WS_URL=ws://0.0.0.0:8123/api/websocket HA_API_TOKEN="${TOKEN}" LD_LIBRARY_PATH=../../build/subprojects/curl-8.5.0/build/lib/.libs/ ../../build/client-cli ha-get-token 2>register.log 1>longtoken.txt
echo "Providing long lived token"
echo HA_WS_URL=ws://localhost:8123/api/websocket HA_API_TOKEN="`cat longtoken.txt`"
echo HA_WS_URL=ws://0.0.0.0:8123/api/websocket HA_API_TOKEN="`cat longtoken.txt`"
else
echo "WARNING - could not find build/client-cli. Providing SHORT lived token"
echo HA_WS_URL=ws://localhost:8123/api/websocket HA_API_TOKEN="${TOKEN}"
echo HA_WS_URL=ws://0.0.0.0:8123/api/websocket HA_API_TOKEN="${TOKEN}"
fi
else
if [ -e "longtoken.txt" ]
then
echo "Registration in HA was already completed, but found old long lived token."
echo HA_WS_URL=ws://localhost:8123/api/websocket HA_API_TOKEN="`cat longtoken.txt`"
echo HA_WS_URL=ws://0.0.0.0:8123/api/websocket HA_API_TOKEN="`cat longtoken.txt`"
else
echo "Registration in HA has already been completed, can't provide a token."
echo HA_WS_URL=ws://localhost:8123/api/websocket
echo HA_WS_URL=ws://0.0.0.0:8123/api/websocket
fi
fi

0 comments on commit c131fc4

Please sign in to comment.