Skip to content

Commit

Permalink
Merge pull request #65 from arduino/workflow_micropython
Browse files Browse the repository at this point in the history
misc: Add micropython test.
  • Loading branch information
iabdalkader authored Jul 24, 2023
2 parents 8e2f518 + 859cd30 commit 9ac4199
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 31 deletions.
27 changes: 24 additions & 3 deletions .github/workflows/client-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,23 @@ jobs:
- name: '⏳ Checkout repository'
uses: actions/checkout@v3

- name: '♻ Caching dependencies'
uses: actions/[email protected]
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
Expand All @@ -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:
Expand All @@ -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
10 changes: 5 additions & 5 deletions tests/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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",
Expand All @@ -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.
Expand Down
69 changes: 46 additions & 23 deletions tests/ci.sh
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 9ac4199

Please sign in to comment.