Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: use mock-gpio.py in enable-disable-units-gpio test #6153

Merged
merged 46 commits into from Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
bb52e33
tests: use fake-gpio.py in enable-disable-units-gpio
mvo5 Nov 14, 2018
ebc24a1
Update tests/lib/systemd.sh
zyga Nov 15, 2018
56b2ff3
interfaces/builtin: add gpio-keys interface for accessing events
Oct 2, 2018
92b9f51
interfaces/builtin: rename gpio-keys to device-buttons
Oct 3, 2018
6e0331f
interfaces/builtin: recognize device buttons based on ID_INPUT_KEY
Oct 3, 2018
e64d4c1
tests: add device-buttons test to interfaces-many
Oct 3, 2018
b873173
interfaces/builtin: update documentation for device_buttons
Oct 5, 2018
887a98c
tests: make sure keyboards cannot be accessed via device-buttons
Oct 5, 2018
3761b82
tests: add tests for device-buttons interface
Oct 5, 2018
e1f4a2c
interfaces/builtin: do not trigger all input on desktop for input keys
Oct 24, 2018
7a78d3d
interface/builtin: update the documentation for test
Oct 24, 2018
4e89e7c
interfaces/builtin: add TestAppArmorSpec for device buttons interface
Oct 24, 2018
974c28f
store,daemon: make UserInfo,LoginUser part of the store interface
mvo5 Oct 30, 2018
72a802a
Set hotplug-key of the connection when connecting hotplug slots. Hand…
stolowski Oct 31, 2018
219bf60
interfaces/builtin/device-buttons: update tests
bboozzoo Nov 5, 2018
3833bf5
tests/main/security-dev-input-event-denied: improve testing of device…
bboozzoo Nov 5, 2018
7059849
MockHotplugSlot test helper.
stolowski Nov 6, 2018
450f18f
store: add new TestCreateUserNoSSHKeys test
mvo5 Nov 9, 2018
f77767e
tests/main/security-dev-input-event-denied: disconnect joystick, fix …
bboozzoo Nov 9, 2018
f0397c8
interfaces/builtin/device_buttons: make sure that device cgroup is al…
bboozzoo Nov 9, 2018
fe8919b
Fix how pinentry is prepared for new gpg v 2.1 and 2.2
sergiocazzolato Nov 13, 2018
3a36250
packaging/arch: fix bash completions path
bboozzoo Nov 13, 2018
735d0b0
interfaces/builtin/device_buttons: use input/key subsystem trigger
bboozzoo Nov 13, 2018
62221a7
tests/main/security-dev-input-event-denied: fix typo
bboozzoo Nov 13, 2018
9b77c6a
interfaces/builtin/device_buttons: update unit tests
bboozzoo Nov 13, 2018
780017e
Rename ensureSystemSnapIsPresent to checkSystemSnapIsPresent and use …
stolowski Nov 13, 2018
a020053
snap, store, overlord/snapshotstate: drop epoch pointers
chipaca Nov 13, 2018
f09177f
Added a comment.
stolowski Nov 13, 2018
6194ca9
tests/lib/fakestore: send snap type
bboozzoo Nov 13, 2018
b679fd4
tests/main/refresh: extend to cover parallel instances
bboozzoo Nov 13, 2018
898377b
tests/main/auto-refresh: extend the test to cover parallel instances
bboozzoo Nov 13, 2018
85425cb
tests/main/refresh-all: extend the test to verify parallel installed …
bboozzoo Nov 13, 2018
d74e3ff
Unifying the method to prepare the pinentry
sergiocazzolato Nov 13, 2018
21a72d6
tests/main/interfaces-device-buttons: shellchecks, fix device path
bboozzoo Nov 13, 2018
72392a5
tests/lib/files: make ensure_file_*, wait_for_file work with non-regu…
bboozzoo Nov 13, 2018
16fc551
Added tests. Return bool instead of err.
stolowski Nov 13, 2018
d471616
cmd: handle tumbleweed and leap in autogen.sh
zyga Nov 13, 2018
6d22368
Added SystemSnapName mapper helper.
stolowski Nov 14, 2018
5999b1e
Added SystemSnapInfo helper.
stolowski Nov 14, 2018
5f3a016
SystemSnapName of IdentityMapper needed for tests only.
stolowski Nov 14, 2018
c527031
Use SystemSnapName() in remap methods.
stolowski Nov 14, 2018
fe2a88d
snapd: allow snap-update-ns to read /proc/version
zyga Nov 14, 2018
b2de03e
ifacestate: rename setupPhasedSecurity -> setupSecurityByBackend
mvo5 Nov 14, 2018
06d2977
Merge remote-tracking branch 'upstream/master' into gpio-updates
mvo5 Nov 15, 2018
add2cda
tests: remove leftover in systemd_create_and_start_unit
mvo5 Nov 15, 2018
5f733f0
tests: improve robustness of enable-disable-units-gpio
mvo5 Nov 16, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -6,6 +6,7 @@
import os
import selectors
import shutil
import socket
import subprocess
import sys
import tempfile
Expand Down Expand Up @@ -59,6 +60,15 @@ def dispatch(sel):
return True


def maybe_sd_notify(s: str) -> None:
addr = os.getenv('NOTIFY_SOCKET')
if not addr:
return
soc = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
soc.connect(addr)
soc.sendall(s.encode())


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, double newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added those because pep8 wants double newlines between functions that are not part of a class.

def main():
"""the main method"""
if os.getuid() != 0:
Expand All @@ -83,6 +93,8 @@ def main():
ufd = os.open("unexport", os.O_RDWR | os.O_NONBLOCK)
sel.register(efd, selectors.EVENT_READ, export_ready)
sel.register(ufd, selectors.EVENT_READ, unexport_ready)
# notify
maybe_sd_notify("READY=1")
while True:
if not dispatch(sel):
break
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/systemd.sh
Expand Up @@ -10,6 +10,31 @@ systemd_create_and_start_unit() {
systemctl start "$1"
}

# Create and start a persistent systemd unit that survives reboots. Use as:
# systemd_create_and_start_persistent_unit "name" "my-service --args"
# The third arg supports "overrides" which allow to customize the service
# as needed, e.g.:
# systemd_create_and_start_persistent_unit "name" "start" "[Unit]\nAfter=foo"
systemd_create_and_start_persistent_unit() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

printf '[Unit]\nDescription=For testing purposes\n[Service]\nType=simple\nExecStart=%s\n[Install]\nWantedBy=multi-user.target\n' "$2" > "/etc/systemd/system/$1.service"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we could automatically insert the name of current test into the description, that would be a nice touch, perahps spread exposes it somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this a lot, will do a followup with this idea.

if [ -n "${3:-}" ]; then
mkdir -p "/etc/systemd/system/$1.service.d"
# shellcheck disable=SC2059
printf "$3" >> "/etc/systemd/system/$1.service.d/override.conf"
mvo5 marked this conversation as resolved.
Show resolved Hide resolved
fi
systemctl daemon-reload
systemctl enable "$1"
systemctl start "$1"
wait_for_service "$1"
}

system_stop_and_remove_persistent_unit() {
systemctl stop "$1" || true
systemctl disable "$1" || true
rm -f "/etc/systemd/system/$1.service"
rm -rf "/etc/systemd/system/$1.service.d"
}

# Use like systemd_stop_and_destroy_unit(fakestore)
systemd_stop_and_destroy_unit() {
if systemctl is-active "$1"; then
Expand Down
71 changes: 16 additions & 55 deletions tests/main/enable-disable-units-gpio/task.yaml
Expand Up @@ -12,80 +12,41 @@ details: |

systems: [ubuntu-core-16-64]

environment:
GPIO_MOCK_DIR: /home/test/gpio-mock

prepare: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
fi
# shellcheck source=tests/lib/systemd.sh
. "$TESTSLIB/systemd.sh"
echo "Create/enable fake gpio"
systemd_create_and_start_persistent_unit fake-gpio "$TESTSLIB/fakegpio/fake-gpio.py" "[Unit]\\nBefore=snap.core.interface.gpio-100.service\\n[Service]\\nType=notify"

echo "Given a snap declaring a plug on gpio is installed"
#shellcheck source=tests/lib/snaps.sh
. "$TESTSLIB"/snaps.sh
install_local gpio-consumer

echo "And a mocked gpio device is in place"
cat > /home/test/gpio-mock.sh <<-EOF
#!/bin/sh
if [ ! -d "$GPIO_MOCK_DIR" ]; then
# the service has just been created
mkdir -p "$GPIO_MOCK_DIR"
touch "$GPIO_MOCK_DIR/gpio100" "$GPIO_MOCK_DIR/export" "$GPIO_MOCK_DIR/unexport"
else
# after reboot, remove device node to test export
rm "$GPIO_MOCK_DIR/gpio100"
truncate -s 0 "$GPIO_MOCK_DIR/export"
fi
mount --bind "$GPIO_MOCK_DIR" /sys/class/gpio
EOF
chmod a+x /home/test/gpio-mock.sh

cat > /etc/systemd/system/gpio-mock.service <<-EOF
[Unit]
Description=Set up mock for gpio test
Before=snap.core.interface.gpio-100.service

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/home/test/gpio-mock.sh
ExecStop=

[Install]
WantedBy=multi-user.target
EOF
systemctl enable --now gpio-mock.service

echo "And the gpio plug is connected"
snap connect gpio-consumer:gpio :gpio-pin

restore: |
if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
fi
# shellcheck source=tests/lib/systemd.sh
. "$TESTSLIB/systemd.sh"
system_stop_and_remove_persistent_unit fake-gpio
umount /sys/class/gpio || true
systemctl disable gpio-mock.service
rm -rf "$GPIO_MOCK_DIR" /etc/systemd/system/gpio-mock.service /home/test/gpio-mock.sh

execute: |
# shellcheck source=tests/lib/journalctl.sh
. "$TESTSLIB/journalctl.sh"

if [ "$TRUST_TEST_KEYS" = "false" ]; then
echo "This test needs test keys to be trusted"
exit
fi

echo "Then the snap service units concerning the gpio device must be run before and after a reboot"
expected="Unit snap.core.interface.gpio-100.service has finished starting up"
get_journalctl_log -xe --no-pager | MATCH "$expected"

if [ "$SPREAD_REBOOT" = "1" ]; then
MATCH "^100$" < "$GPIO_MOCK_DIR/export"
fi
for i in $(seq 120); do
get_journalctl_log -xe --no-pager | MATCH "$expected"
sleep 0.5
done

if [ "$SPREAD_REBOOT" = "0" ]; then
REBOOT
fi

if [ "$SPREAD_REBOOT" = "1" ]; then
test -e /sys/class/gpio/gpio100
fi
15 changes: 8 additions & 7 deletions tests/regression/lp-1802581/task.yaml
Expand Up @@ -20,19 +20,20 @@ description: |
systems: [ubuntu-core-16-64]

prepare: |
echo "Run mock gpio daemon"
systemd-run --unit mock-gpio -- "$(pwd)/mock-gpio.py"
# shellcheck source=tests/lib/systemd.sh
. "$TESTSLIB/systemd.sh"
wait_for_service mock-gpio
echo "Create/enable fake gpio"
systemd_create_and_start_persistent_unit fake-gpio "$TESTSLIB/fakegpio/fake-gpio.py" "[Unit]\nBefore=snap.core.interface.gpio-100.service\n[Service]\nType=notify"

restore: |
systemctl stop mock-gpio || true
# for good measure, mock-gpio.py does this umount already on exit
# shellcheck source=tests/lib/systemd.sh
. "$TESTSLIB/systemd.sh"
system_stop_and_remove_persistent_unit fake-gpio
# for good measure, fake-gpio.py does this umount already on exit
umount /sys/class/gpio || true

debug: |
journalctl -u mock-gpio.service
journalctl -u fake-gpio.service

execute: |
echo "Install a snap that uses the gpio consumer"
Expand All @@ -53,4 +54,4 @@ execute: |
snap interfaces | MATCH ":gpio-pin.*gpio-consumer:gpio"

echo "Ensure that our mock service is full functional"
systemctl status mock-gpio.service | MATCH "Active: active"
systemctl status fake-gpio.service | MATCH "Active: active"