Skip to content

Commit

Permalink
Add automated testing workflow (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
locnnil authored Feb 20, 2024
1 parent 64145bb commit 250875c
Show file tree
Hide file tree
Showing 12 changed files with 428 additions and 39 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/build-and-test-snap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Snap Build and Test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allow manual trigger
workflow_dispatch:

jobs:
build-arm64:
runs-on: ubuntu-latest
strategy:
matrix:
platform:
- arm64
steps:

- name: Checkout code
uses: actions/checkout@v4

- name: Setup QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: ${{ matrix.platform }}

- name: Build snap for arm64
uses: diddlesnaps/snapcraft-multiarch-action@v1
id: build
with:
architecture: ${{ matrix.platform }}

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: matter-pi-gpio-commander_${{ github.run_number}}_${{ matrix.platform }}.snap
path: ${{ steps.build.outputs.snap }}

build-and-test-amd64:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Inject amd64 build
run: tests/inject-amd64-arch.sh

- name: Build Snap
uses: snapcore/action-build@v1
id: snapcraft

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.*'
cache: false

- name: Run tests
working-directory: tests
env:
MOCK_GPIO: true
SKIP_TEARDOWN_REMOVAL: true
LOCAL_SERVICE_SNAP: ../${{ steps.snapcraft.outputs.snap }}
run: go test -failfast -p 1 -timeout 20m -v

- name: Upload snap logs
if: always()
uses: actions/upload-artifact@v4
with:
name: snap-logs
path: tests/*.log
38 changes: 0 additions & 38 deletions .github/workflows/build-snap.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
squashfs-root
matter-pi-gpio-commander_*.txt
test-blink

# Ignore log files
*.log
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ sudo snap connect matter-pi-gpio-commander:avahi-control
> ```
#### GPIO
The gpio access is granted using the [`custom-device`](https://snapcraft.io/docs/custom-device-interface), which declares a slot to expose the `/dev/gpiochip*` device and also a plug to self connect.
This interface is auto connected when installing the snap from the Snap Store.
Expand Down
3 changes: 2 additions & 1 deletion snap/hooks/configure
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ if [[ -n $GPIO ]] && ! [[ $GPIO =~ ^[1-9][0-9]*$ ]]; then
fi

GPIOCHIP=$(snapctl get gpiochip)
if (( GPIOCHIP != 0 && GPIOCHIP != 4 )); then
VALIDATION=$(snapctl get gpiochip-validation)
if [[ "$VALIDATION" == "true" ]] && (( GPIOCHIP != 0 && GPIOCHIP != 4 )); then
logger -t $TAG --stderr "gpiochip: '$GPIOCHIP' is not supported; set to 0 or 4"
exit 1
fi
1 change: 1 addition & 0 deletions snap/hooks/install
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

# Set defaults
snapctl set gpiochip=0
snapctl set gpiochip-validation=true
27 changes: 27 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Run Tests

This section provides documentation on running tests and various scenarios.

## Environment Variables

To run the tests, you must set the following environment variables:

- `SERVICE_CHANNEL`: The channel from which the snap will be downloaded if not using a local snap file. The default option is `latest/edge`.
- `LOCAL_SERVICE_SNAP`: Path to the local service snap to be tested instead of downloading from a channel.
- `SKIP_TEARDOWN_REMOVAL`: Skip the removal of snaps during teardown (useful when running on CI machines). The default option is `false`.
- `MOCK_GPIO`: This is used to determine whether you wish to use gpio-mock to test the application instead of a physical gpiochip. Possible values are `true` or `false`. Important note: the usage of gpio-mock will only work if the snap is being installed from a local file; in other words, `LOCAL_SERVICE_SNAP` must be defined.
- `GPIO_CHIP`: The GPIO chip number; accepted values are `0` (for legacy Raspberry Pis) or `4` for the Raspberry Pi 5. This doesn't need to be specified if using `USE_GPIO_MOCK`.
- `GPIO_LINE`: This is the line offset to be used to test the selected gpiochip. The number of available lines can be checked with the `gpiodetect` and `gpioinfo` commands from the Debian package `gpiod`. This doesn't need to be specified if using `USE_GPIO_MOCK`.

**Note:** The `USE_GPIO_MOCK` takes precedence over the specific `GPIO_CHIP` and `GPIO_LINE` settings if the former is set to "1"; thus, the other two are ignored.

## Running tests

```bash
go test -v -failfast -count 1
```

where:
- `-v` is to enable verbose output
- `-failfast` makes the test stop after first failure
- `-count 1` is to avoid Go test caching for example when testing a rebuilt snap
15 changes: 15 additions & 0 deletions tests/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module matter-pi-gpio-commander-tests

go 1.21.6

require (
github.com/canonical/matter-snap-testing v1.0.0-beta.1
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 18 additions & 0 deletions tests/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/canonical/matter-snap-testing v1.0.0-beta.1 h1:obxh5jYRWgCw6QNax4+aY571h+q/zcPZBOP64gwd56E=
github.com/canonical/matter-snap-testing v1.0.0-beta.1/go.mod h1:800wP9+ux8DW9Xr9QvlIdxFHlu85mqlhvlA2JO20aeQ=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
44 changes: 44 additions & 0 deletions tests/gpio-mock.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

if [ "$1" = "teardown" ]; then
sudo rmmod gpio_mockup
rm -rf gpio-mockup
exit 0
fi

mkdir gpio-mockup
cd gpio-mockup

# Update and install dependencies
sudo apt-get update
sudo apt-get install -y linux-headers-$(uname -r)
sudo apt-get install -y build-essential flex bison make

kernel_major_minor=$(uname -r | cut -d'.' -f1-2)

echo "Kernel major minor version: $kernel_major_minor"

# Get GPIO Mockup driver
wget https://raw.githubusercontent.com/torvalds/linux/v$kernel_major_minor/drivers/gpio/gpio-mockup.c
wget https://raw.githubusercontent.com/torvalds/linux/v$kernel_major_minor/drivers/gpio/gpiolib.h

# Create Makefile
echo "
obj-m = gpio-mockup.o
KVERSION = \$(shell uname -r)
all:
make -C /lib/modules/\$(KVERSION)/build M=\$(PWD) modules
clean:
make -C /lib/modules/\$(KVERSION)/build M=\$(PWD) clean
" > Makefile


make -j$(nproc)

sudo insmod gpio-mockup.ko gpio_mockup_ranges=-1,16 gpio_mockup_named_lines

gpio_mock_chip=$(ls /dev/gpiochip* | sort -n | head -n 1)

echo "GPIO Mockup chip: $gpio_mock_chip"

cd ..
1 change: 1 addition & 0 deletions tests/inject-amd64-arch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sed -i '/architectures:/a \ \ - build-on: amd64' snap/snapcraft.yaml
Loading

0 comments on commit 250875c

Please sign in to comment.