Files
mu300-linux/tests/test_update.py
T
Abdurrahman Kaan DikeçandClaude Opus 5.5 ed13857c26 Tests for the scripts on every system; the buttons and an LED timeout on the U30 Air
tests/ (python3 -m unittest discover -s tests; CI on Ubuntu, macOS and Windows):
- the device scripts under dash (Ubuntu), bash and busybox ash (OpenWrt), with stub commands and a fake / :
  mu300-device, -lan-ip, -led (both devices, the timeout), -ttl, -wifi-band, -buttons, mu300-vpn (URI parsing,
  JSON, networks kept out of the tunnel, the sing-box config), mu300-update (release files, kernel choice, the
  byte helpers boot images are edited with, which bundles may go onto which device)
- tools/i18n.sh with every translation; install.ps1's T/NormalizeAnswer/Gib under PowerShell 5.1 and 7
- build-boot-image.py's generic ramdisk and the U30 Air's modules; the U30 Air's module order
- every script parses under the shells that run it; rules from past bugs (no double quotes in install.ps1's
  device commands, ASCII PowerShell, init looks for partitions after the modules)
To be testable, mu300-update and mu300-vpn define their functions only with MU300_LIB=1, and paths can point at
a fake root (MU300_SYSROOT, MU300_BIN, MU300_*_CONF).

Found by the tests: mu300-vpn's fallback for the device's own network sourced a missing lan.conf, and dash and
busybox ash leave the whole subshell when "." cannot open its file - the address came out empty.

Buttons (mu300-keys, a small static C reader of /dev/input; mu300-buttons, Ubuntu and OpenWrt):
- power: a short press wakes the LEDs, held 3 s shuts down (logind no longer powers off at a touch)
- the U30 Air's Wi-Fi key: short press switches the hotspot 2.4 <-> 5 GHz (mu300-wifi-band), held 3 s turns it
  off or on
- LEDs go dark after LED_TIMEOUT (/etc/mu300/led.conf; 60 s on the U30 Air, 0 = always on, the F50's default);
  what they should show is kept and shown on the next wake. The Wi-Fi LED shows the band: blue 5, white 2.4 GHz.
- mainline kernels: CONFIG_KEYBOARD_GPIO (there was no /dev/input at all)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeuDBrAce8rrgSJDbFJQGN
2026-09-28 10:18:37 +03:00

124 lines
5.7 KiB
Python

"""mu300-update, sourced for its functions (MU300_LIB=1) against a fake Linux partition (MU300_DISK): which files of a
release it takes, the kernel choice, the byte helpers it edits boot image headers with, and whether a kernel bundle
may go onto this device."""
import io
import struct
import tarfile
import unittest
from helpers import BIN, ShellTest
class Update(ShellTest):
def setUp(self):
super().setUp()
self.disk = self.tmp / 'disk'
(self.disk / 'ubuntu' / 'etc').mkdir(parents=True)
self.root = self.tmp / 'root'
(self.root / 'run/mu300').mkdir(parents=True)
self.device('f50')
def device(self, name):
(self.root / 'run/mu300/device').write_text(name + '\n')
def up(self, shell, code, **env):
return self.sh(shell, f'. "{BIN}/mu300-update"; {code}', MU300_LIB=1, MU300_DISK=self.disk, MU300_BIN=BIN,
MU300_SYSROOT=self.root, **env)
def test_sourcing_does_nothing(self):
for shell in self.each_shell():
r = self.up(shell, 'echo loaded')
self.assertEqual((r.returncode, r.stdout), (0, 'loaded\n'), r.stderr)
def test_rootfs_asset(self):
osr = self.disk / 'ubuntu' / 'etc' / 'os-release'
cases = [('24.04', {}, 'mu300-ubuntu-rootfs.tar.gz'),
('26.04', {}, 'mu300-ubuntu-26.04-rootfs.tar.gz'),
('26.04', {'MU300_UBUNTU': '24.04'}, 'mu300-ubuntu-rootfs.tar.gz'),
('24.04', {'MU300_UBUNTU': '26.04'}, 'mu300-ubuntu-26.04-rootfs.tar.gz'),
(None, {}, 'mu300-ubuntu-rootfs.tar.gz')]
for shell in self.each_shell():
for ver, env, want in cases:
if ver:
osr.write_text(f'NAME="Ubuntu"\nVERSION_ID="{ver}"\n')
else:
osr.unlink(missing_ok=True)
self.assertEqual(self.up(shell, 'rootfs_asset ubuntu', **env).stdout.strip(), want, (ver, env))
self.assertEqual(self.up(shell, 'rootfs_asset openwrt').stdout.strip(), 'mu300-openwrt-rootfs.tar.gz')
def test_kernel_choice(self):
boot = self.disk / 'boot'
boot.mkdir()
cases = [(None, '5.4', 'mu300-kernel.tar.gz'), ('6.18', '6.18', 'mu300-kernel-6.18.tar.gz'),
('7.2', '7.2', 'mu300-kernel-7.2.tar.gz'), ('5.4', '5.4', 'mu300-kernel.tar.gz'),
('6.1', '5.4', 'mu300-kernel.tar.gz'), ('', '5.4', 'mu300-kernel.tar.gz')]
for shell in self.each_shell():
for c, choice, asset in cases:
f = boot / 'kernel'
if c is None:
f.unlink(missing_ok=True)
else:
f.write_text(c + '\n')
self.assertEqual(self.up(shell, 'kernel_choice; kernel_asset').stdout.split(), [choice, asset], c)
def test_byte_helpers(self):
# the boot image header is edited with these: sizes little endian, the AVB footer big endian
f = self.tmp / 'bytes.bin'
for shell in self.each_shell():
for v in (0, 1, 255, 256, 0x12345678, 0xFFFFFFFF):
r = self.up(shell, f'bytes {v} 4 le > "{f}"; u32 "{f}" 0')
self.assertEqual(f.read_bytes(), struct.pack('<I', v), v)
self.assertEqual(r.stdout.strip(), str(v))
for v in (0, 0x1234, 0x0000000100000000, 0x7FFFFFFF12345678):
r = self.up(shell, f'bytes {v} 8 be > "{f}"; be64 "{f}" 0')
self.assertEqual(f.read_bytes(), struct.pack('>Q', v), v)
self.assertEqual(r.stdout.strip(), str(v))
# poke: overwrite in place, the rest untouched
f.write_bytes(bytes(16))
self.up(shell, f'bytes 3735928559 4 le | poke "{f}" 4')
self.assertEqual(f.read_bytes(), bytes(4) + struct.pack('<I', 0xDEADBEEF) + bytes(8))
def test_pad_page(self):
f = self.tmp / 'img'
for shell in self.each_shell():
for n, want in ((0, 0), (1, 4096), (4095, 4096), (4096, 4096), (4097, 8192)):
f.write_bytes(b'x' * n)
self.up(shell, f'pad_page "{f}"')
self.assertEqual(f.stat().st_size, want, n)
def test_installed_systems(self):
for shell in self.each_shell():
self.assertEqual(self.up(shell, 'installed_systems').stdout.split(), ['ubuntu'])
(self.disk / 'openwrt').mkdir(exist_ok=True)
self.assertEqual(self.up(shell, 'installed_systems').stdout.split(), ['ubuntu', 'openwrt'])
(self.disk / 'openwrt').rmdir()
def bundle(self, name, devices):
p = self.tmp / name
with tarfile.open(p, 'w:gz') as t:
for fn, data in [('./Image', b'kernel'), ('./ramdisk-generic.lz4', b'rd')] + (
[('./devices', devices.encode())] if devices is not None else []):
ti = tarfile.TarInfo(fn)
ti.size = len(data)
t.addfile(ti, io.BytesIO(data))
return p
def test_bundle_runs_here(self):
new = self.bundle('new.tar.gz', 'f50 u30air\n')
old = self.bundle('old.tar.gz', None)
f50only = self.bundle('f50.tar.gz', 'f50\n')
cases = [('f50', new, True), ('f50', old, True), ('u30air', new, True), ('u30air', old, False),
('u30air', f50only, False)]
for shell in self.each_shell():
for dev, b, ok in cases:
self.device(dev)
d = self.tmp / 'x'
d.mkdir(exist_ok=True)
r = self.up(shell, f'tar -tzf "{b}" > "{d}/list"; bundle_runs_here "{b}" "{d}/list" "{d}" && echo YES || echo NO')
self.assertEqual(r.stdout.strip(), 'YES' if ok else 'NO', (dev, b.name, r.stderr))
(d / 'devices').unlink(missing_ok=True)
if __name__ == '__main__':
unittest.main()