From 859cd30d7ada3e969f86e16c6adcf726499fea8e Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 24 Jul 2023 14:17:27 +0300 Subject: [PATCH] misc: Add micropython test. --- .github/workflows/client-test.yml | 27 ++++++++++-- tests/ci.py | 10 ++--- tests/ci.sh | 69 ++++++++++++++++++++----------- 3 files changed, 75 insertions(+), 31 deletions(-) diff --git a/.github/workflows/client-test.yml b/.github/workflows/client-test.yml index e33d3bb..e838988 100644 --- a/.github/workflows/client-test.yml +++ b/.github/workflows/client-test.yml @@ -31,12 +31,23 @@ jobs: - name: '⏳ Checkout repository' uses: actions/checkout@v3 + - name: '♻ Caching dependencies' + uses: actions/cache@v3.3.1 + id: cache + with: + path: ~/cache/bin/ + key: 'micropython' + - name: '🐍 Set up Python' uses: actions/setup-python@v4 with: cache: 'pip' python-version: "3.10" + - name: '🐍 Set up MicroPython' + if: steps.cache.outputs.cache-hit != 'true' + run: source tests/ci.sh && ci_install_micropython + - name: '🛠 Install dependencies' run: | python -m pip install --upgrade pip @@ -49,7 +60,8 @@ jobs: - name: '🛠 Install package' run: | python3 -m build - pip install dist/arduino_iot_cloud-*.whl + pip install --user dist/arduino_iot_cloud-*.whl + pip install --target=${HOME}/.micropython/lib dist/arduino_iot_cloud-*.whl - name: '🔑 Configure soft crypto device' env: @@ -59,17 +71,26 @@ jobs: run: | source tests/ci.sh && ci_configure_softhsm - - name: '☁️ Connect to IoT cloud (basic auth)' + - name: '☁️ Connect to IoT cloud (CPython / Basic Auth)' env: DEVICE_ID: ${{ secrets.DEVICE_ID1 }} SECRET_KEY: ${{ secrets.SECRET_KEY }} run: | python tests/ci.py - - name: '☁️ Connect to IoT cloud (using crypto device)' + - name: '☁️ Connect to IoT cloud (CPython / Crypto Auth)' env: DEVICE_ID: ${{ secrets.DEVICE_ID2 }} SECRET_KEY: ${{ secrets.SECRET_KEY }} run: | export SOFTHSM2_CONF="${HOME}/softhsm/tokens/softhsm2.conf" python tests/ci.py --crypto-device + + - name: '☁️ Connect to IoT cloud (MicroPython / Basic Auth)' + env: + DEVICE_ID: ${{ secrets.DEVICE_ID1 }} + SECRET_KEY: ${{ secrets.SECRET_KEY }} + run: | + export PATH="${HOME}/cache/bin:${PATH}" + micropython -c "import sys; print(sys.path)" + micropython tests/ci.py diff --git a/tests/ci.py b/tests/ci.py index 7fd5c23..45c277e 100644 --- a/tests/ci.py +++ b/tests/ci.py @@ -7,7 +7,6 @@ import asyncio from arduino_iot_cloud import ArduinoCloudClient import argparse -import arduino_iot_cloud.ussl as ssl def exception_handler(loop, context): @@ -47,8 +46,9 @@ def on_value_changed(client, value): # be used to authenticate, for example: # client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY) if args.crypto_device: + import arduino_iot_cloud.ussl as ssl client = ArduinoCloudClient( - device_id=os.environ["DEVICE_ID"], + device_id=os.getenv("DEVICE_ID"), ssl_params={ "pin": "1234", "keyfile": "pkcs11:token=arduino", @@ -61,9 +61,9 @@ def on_value_changed(client, value): ) else: client = ArduinoCloudClient( - device_id=os.environ["DEVICE_ID"], - username=os.environ["DEVICE_ID"], - password=os.environ["SECRET_KEY"], + device_id=os.getenv("DEVICE_ID"), + username=os.getenv("DEVICE_ID"), + password=os.getenv("SECRET_KEY"), ) # Register cloud objects. diff --git a/tests/ci.sh b/tests/ci.sh index 2b577b5..b300947 100755 --- a/tests/ci.sh +++ b/tests/ci.sh @@ -1,35 +1,58 @@ #!/bin/bash +ci_install_micropython() { + CACHE_DIR=${HOME}/cache/bin + mkdir -p ${CACHE_DIR} + + sudo apt-get install gcc-arm-none-eabi libnewlib-arm-none-eabi + + git clone --depth=1 https://github.com/micropython/micropython.git + + cat > micropython/ports/unix/manifest.py <<-EOF + include("\$(PORT_DIR)/variants/manifest.py") + include("\$(MPY_DIR)/extmod/asyncio") + require("bundle-networking") + require("time") + require("senml") + require("logging") + EOF + + make -C micropython/mpy-cross/ + make -C micropython/ports/unix/ submodules + make -C micropython/ports/unix/ FROZEN_MANIFEST=manifest.py CFLAGS_EXTRA="-DMICROPY_PY_SELECT=1" + cp micropython/ports/unix/build-standard/micropython ${CACHE_DIR} +} + ci_configure_softhsm() { -TOKEN_DIR=${HOME}/softhsm/tokens/ -TOKEN_URI="pkcs11:token=arduino" -PROVIDER=/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so + TOKEN_DIR=${HOME}/softhsm/tokens/ + TOKEN_URI="pkcs11:token=arduino" + PROVIDER=/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so -mkdir -p ${TOKEN_DIR} -cat > ${TOKEN_DIR}/softhsm2.conf << EOF -directories.tokendir = ${TOKEN_DIR} -objectstore.backend = file + mkdir -p ${TOKEN_DIR} + cat > ${TOKEN_DIR}/softhsm2.conf <<-EOF + directories.tokendir = ${TOKEN_DIR} + objectstore.backend = file -# ERROR, WARNING, INFO, DEBUG -log.level = ERROR + # ERROR, WARNING, INFO, DEBUG + log.level = ERROR -# If CKF_REMOVABLE_DEVICE flag should be set -slots.removable = false + # If CKF_REMOVABLE_DEVICE flag should be set + slots.removable = false -# Enable and disable PKCS#11 mechanisms using slots.mechanisms. -slots.mechanisms = ALL + # Enable and disable PKCS#11 mechanisms using slots.mechanisms. + slots.mechanisms = ALL -# If the library should reset the state on fork -library.reset_on_fork = false -EOF + # If the library should reset the state on fork + library.reset_on_fork = false + EOF -export SOFTHSM2_CONF=${TOKEN_DIR}/softhsm2.conf + export SOFTHSM2_CONF=${TOKEN_DIR}/softhsm2.conf -echo "$KEY_PEM" >> key.pem -echo "$CERT_PEM" >> cert.pem -echo "$CA_PEM" >> ca-root.pem + echo "$KEY_PEM" >> key.pem + echo "$CERT_PEM" >> cert.pem + echo "$CA_PEM" >> ca-root.pem -softhsm2-util --init-token --slot 0 --label "arduino" --pin 1234 --so-pin 1234 -p11tool --provider=${PROVIDER} --login --set-pin=1234 --write ${TOKEN_URI} --load-privkey key.pem --label "mykey" -p11tool --provider=${PROVIDER} --login --set-pin=1234 --write ${TOKEN_URI} --load-certificate cert.pem --label "mycert" + softhsm2-util --init-token --slot 0 --label "arduino" --pin 1234 --so-pin 1234 + p11tool --provider=${PROVIDER} --login --set-pin=1234 --write ${TOKEN_URI} --load-privkey key.pem --label "mykey" + p11tool --provider=${PROVIDER} --login --set-pin=1234 --write ${TOKEN_URI} --load-certificate cert.pem --label "mycert" }