Skip to content

Instantly share code, notes, and snippets.

@imrehg
Last active April 10, 2018 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imrehg/e7781e360b373916f4b4ed7086d23f33 to your computer and use it in GitHub Desktop.
Save imrehg/e7781e360b373916f4b4ed7086d23f33 to your computer and use it in GitHub Desktop.
Testing stuff

Supervisor update test through the API

Test

This test checks if the update-resin-supervisor script correctly gets the supervisor information from the API.

  • connect to the host OS
  • copy and paste the below script into the shell to run it - it will take the device's credentials, calls the API for the supervisor for cross-check, update the API entry for this device, and calls the supervisor updater to check if it finds the updated info.
source /etc/resin-supervisor/supervisor.conf
if [ -f "/mnt/boot/config.json" ]; then
  CONFIGJSON=/mnt/boot/config.json
elif [ -f "/mnt/conf/config.json" ]; then
  CONFIGJSON=/mnt/conf/config.json
fi
if [ -z "$SUPERVISOR_TAG" ]; then
  echo "Couldn't extract SUPERVISOR_TAG from the supervisor.conf"
elif [ -z "$CONFIGJSON" ]; then
  echo "Couldn't find config.json, cannot continue".
else
  APIKEY="$(jq -r '.apiKey // .deviceApiKey' "${CONFIGJSON}")"
  DEVICEID="$(jq -r '.deviceId' "${CONFIGJSON}")"
  API_ENDPOINT="$(jq -r '.apiEndpoint' "${CONFIGJSON}")"
  SLUG="$(jq -r '.deviceType' "${CONFIGJSON}")"
  echo "SUPERVISOR_TAG: $SUPERVISOR_TAG"
  SUPERVISOR_ID=$(curl -s "${API_ENDPOINT}/v3/supervisor_release?\$select=id,image_name&\$filter=((device_type%20eq%20'$SLUG')%20and%20(supervisor_version%20eq%20'$SUPERVISOR_TAG'))&apikey=${APIKEY}" | jq -e -r '.d[0].id')
  echo "Extracted supervisor ID: $SUPERVISOR_ID"
  API_REPLY=$(curl -s "${API_ENDPOINT}/v2/device($DEVICEID)?apikey=$APIKEY" -X PATCH -H 'Content-Type: application/json;charset=UTF-8' --data-binary "{\"supervisor_release\": \"$SUPERVISOR_ID\"}")
  if [ "$API_REPLY" = "OK" ]; then
    if update-resin-supervisor | grep "Supervisor configuration found from API." ; then
      TEST_OK=yes
    fi
  else
    echo "Couldn't set Supervisor release in the API"
  fi
fi
if [ "$TEST_OK" = "yes" ]; then
  echo "Test SUCCESSFUL."
else
  echo "Test FAILED."
fi

Result

Setting the supervisor should output the supervisor release ID and an OK for the set:

Extracted supervisor ID: 968

If you see Extracted supervisor ID: null, then check https://api.resin.io/v4/supervisor_release whether there's a supervisor version equal to SUPERVISOR_TAG released for the given device type. If there isn't, ping @team in i/supervisor, and retry the test when the supervisor is released properly.

The update-resin-supervisor run should have Supervisor configuration found from API., this signals that the script has gotten the supervisor ID and information correctly from the API.

If the test successful, you should see a Test SUCCESSFUL. in the end, if any of the steps have failed, then you'll see a Test FAILED.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment