support/testing: use .assertRunOk() when possible
authorThomas Petazzoni <thomas.petazzoni@bootlin.com>
Sat, 26 Jun 2021 13:32:38 +0000 (15:32 +0200)
committerYann E. MORIN <yann.morin.1998@free.fr>
Sat, 26 Jun 2021 15:41:10 +0000 (17:41 +0200)
The BRTest() class implements an assertRunOk() method that does the
very common work of running a command inside the emulator, and
checking that it is successful.

This commit changes all locations where this .assertRunOk() method can
be used, instead of open-coding the same logic.

Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
24 files changed:
support/testing/tests/fs/test_f2fs.py
support/testing/tests/fs/test_jffs2.py
support/testing/tests/fs/test_squashfs.py
support/testing/tests/init/base.py
support/testing/tests/init/test_none.py
support/testing/tests/init/test_systemd.py
support/testing/tests/package/test_atop.py
support/testing/tests/package/test_bmap_tools.py
support/testing/tests/package/test_docker_compose.py
support/testing/tests/package/test_dropbear.py
support/testing/tests/package/test_lua.py
support/testing/tests/package/test_netdata.py
support/testing/tests/package/test_openssh.py
support/testing/tests/package/test_opkg.py
support/testing/tests/package/test_perl.py
support/testing/tests/package/test_python.py
support/testing/tests/package/test_python_pytest.py
support/testing/tests/package/test_python_pytest_asyncio.py
support/testing/tests/package/test_redis.py
support/testing/tests/package/test_rust.py
support/testing/tests/package/test_s6_rc.py
support/testing/tests/package/test_sudo.py
support/testing/tests/package/test_syslog_ng.py
support/testing/tests/package/test_tmux.py

index 950d4cf25b0cfe521c08e9450b3a14637c2ab73a..e6872f39c183d42c5537962fdc4b93c7a63cca23 100644 (file)
@@ -44,5 +44,4 @@ class TestF2FS(infra.basetest.BRTest):
                            options=options)
         self.emulator.login()
         cmd = "mount | grep '/dev/root on / type f2fs'"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
index f5066ff01f583fb4b0dd9b4f0574acabe8363e7f..f595e408d56f118ced9f14af4c3717d5dd8a335c 100644 (file)
@@ -41,5 +41,4 @@ class TestJffs2(infra.basetest.BRTest):
                            options=["-drive", "file={},if=pflash".format(img)])
         self.emulator.login()
         cmd = "mount | grep '/dev/root on / type jffs2'"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
index 234f4944be0e6e85996fcfe60365ea6df28b1fae..3e58a12ab7721d224311298227244a05afd2b95d 100644 (file)
@@ -32,5 +32,4 @@ class TestSquashfs(infra.basetest.BRTest):
         self.emulator.login()
 
         cmd = "mount | grep '/dev/root on / type squashfs'"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
index 75cfbe9c5982ce86b91ec7b468aba98e0799af3b..80f7731ba33aea62d7a307a21e41af2020d7f73a 100644 (file)
@@ -39,10 +39,8 @@ class InitSystemBase(infra.basetest.BRTest):
 
     def check_init(self, path):
         cmd = "cmp /proc/1/exe {}".format(path)
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
     def check_network(self, interface, exitCode=0):
         cmd = "ip addr show {} |grep inet".format(interface)
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, exitCode)
+        self.assertRunOk(cmd)
index 5b9b4e43f1615f88077937b6fe6e56bdf3f2c32a..fc4a46efd9cc4b338527b88814103159b0138a2d 100644 (file)
@@ -27,7 +27,6 @@ class TestInitSystemNone(InitSystemBase):
         self.assertEqual(exit_code, 0)
         self.assertEqual(out[0], "1")
 
-        _, exit_code = self.emulator.run("mount -t proc none /proc")
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk("mount -t proc none /proc")
 
         self.check_init("/bin/sh")
index c0ef5e120279d13e2e17cbd3dfa9bdd6cf537582..b553f69a48f38749af435a3a9d886bd579bfed32 100644 (file)
@@ -29,8 +29,7 @@ class InitSystemSystemdBase(InitSystemBase):
         self.assertEqual(len(output), 0)
 
         # Test we can reach the DBus daemon
-        _, exit_code = self.emulator.run("busctl --no-pager")
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk("busctl --no-pager")
 
         # Test we can read at least one line from the journal
         output, _ = self.emulator.run("journalctl --no-pager --lines 1 --quiet")
index 1da1ac7cc4f3171c7452df11abdf862a0fc40738..5a96684799e2686b7b41046160f819bb8b176016 100644 (file)
@@ -33,9 +33,7 @@ class TestAtop(infra.basetest.BRTest):
         self.emulator.login()
 
         cmd = "atop -V | grep '^Version'"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         cmd = "atop -a 1 2 | grep '% *atop *$'"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
index 5b4e5dcd3f60af000015c6fe84e294cdc3618649..baa23154f89c910fd8d2b8bf64046cdd3ba3de76 100644 (file)
@@ -42,8 +42,7 @@ class TestBmapTools(BRTest):
     def test_run(self):
         self.login()
         cmd = "/root/{}".format(os.path.basename(self.sample_script))
-        _, exit_code = self.emulator.run(cmd, timeout=20)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd, timeout=20)
 
 
 class TestPy2BmapTools(TestBmapTools):
index 737dbf393cb9a3e54bf0e02f0396b3b366894bc0..142ac95d033bc44c4d2bde309fa45f02f88ad171 100644 (file)
@@ -37,8 +37,7 @@ class TestDockerCompose(infra.basetest.BRTest):
 
     def docker_test(self):
         # will download container if not available, which may take some time
-        _, exit_code = self.emulator.run('docker run --rm -p 8888:8888 busybox:latest /bin/true', 120)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk('docker run --rm -p 8888:8888 busybox:latest /bin/true', 120)
 
     def docker_compose_test(self):
         # will download container if not available, which may take some time
index bfec8701f27c920f22db854ce163edcd147c5df3..2dd90702311c67d5751d5627b120fa43167c6c1d 100644 (file)
@@ -24,9 +24,7 @@ class TestDropbear(infra.basetest.BRTest):
                                     "-net", "user"])
         self.emulator.login(self.passwd)
         cmd = "netstat -ltn 2>/dev/null | grep 0.0.0.0:22"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         cmd = "sshpass -p {} ssh -y localhost /bin/true".format(self.passwd)
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
index 66ae85c9993251ad16c6dad530bc7c48f84dbd05..573ae010050d9b82fcf72f115b98825fdf66da32 100644 (file)
@@ -31,8 +31,7 @@ class TestLuaBase(infra.basetest.BRTest):
 
     def module_test(self, module, script="a=1"):
         cmd = "lua -l {} -e '{}'".format(module, script)
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
 
 class TestLua(TestLuaBase):
index 7418b762a4447ae6e282f94db6d0cac0513d2c3a..6b616398d3fed99dea978e5a8b09c6d69351f672 100644 (file)
@@ -19,5 +19,4 @@ class TestNetdata(infra.basetest.BRTest):
         self.emulator.login()
 
         cmd = "wget localhost:19999 -O - | grep '<title>netdata dashboard</title>'"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
index 2ae5030d5db5cacf79b5348d8f29b8483f68bcd5..490147f86f1622011abe1705d4e59b28af2e81c4 100644 (file)
@@ -25,12 +25,10 @@ class TestOpensshBase(infra.basetest.BRTest):
         self.emulator.login(self.passwd)
 
         cmd = "netstat -ltn 2>/dev/null | grep 0.0.0.0:22"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         cmd = "sshpass -p {} ssh -oStrictHostKeyChecking=no localhost /bin/true".format(self.passwd)
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
 
 class TestOpenSshuClibc(TestOpensshBase):
index aa937082237b8d41b521e8508050bd4099ee8d5e..e47879cad69d71ffd861f8f144a697aa56a1ff02 100644 (file)
@@ -33,23 +33,19 @@ class TestOpkg(infra.basetest.BRTest):
         # and prerm scripting provided in the package archive.
 
         cmd = "opkg install example-snmpd-package_1.0_arm.ipk"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         cmd = "opkg list-installed | grep example-snmpd-package"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         # Check that postinst script ran to start the services
         cmd = "ps aux | grep [s]nmpd"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         # If successful, the prerm script ran to stop the service prior to
         # the removal of the service scripting and files
         cmd = "opkg remove example-snmpd-package"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         # Verify after package removal that the services is not
         # running, but let's give it some time to really stop
index 37e7390a4b64bc0c3319e683113a2c4d683f5410..ae3f105919df44fc49c19fa68bb7f2663cbb0b22 100644 (file)
@@ -19,8 +19,7 @@ class TestPerlBase(infra.basetest.BRTest):
 
     def module_test(self, module, script="1"):
         cmd = "perl -M{} -e '{}'".format(module, script)
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
 
 class TestPerl(TestPerlBase):
index bcd363ad1eb036e0b759baefef1009fbfbb91a6b..60da8765c412f2b9406050c716382c29dda71150 100644 (file)
@@ -20,21 +20,18 @@ class TestPythonBase(infra.basetest.BRTest):
 
     def version_test(self, version, timeout=-1):
         cmd = self.interpreter + " --version 2>&1 | grep '^{}'".format(version)
-        _, exit_code = self.emulator.run(cmd, timeout)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd, timeout)
 
     def math_floor_test(self, timeout=-1):
         cmd = self.interpreter + " -c 'import math; math.floor(12.3)'"
-        _, exit_code = self.emulator.run(cmd, timeout)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd, timeout)
 
     def libc_time_test(self, timeout=-1):
         cmd = self.interpreter + " -c 'from __future__ import print_function;"
         cmd += "import ctypes;"
         cmd += "libc = ctypes.cdll.LoadLibrary(\"libc.so.1\");"
         cmd += "print(libc.time(None))'"
-        _, exit_code = self.emulator.run(cmd, timeout)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd, timeout)
 
     def zlib_test(self, timeout=-1):
         cmd = self.interpreter + " -c 'import zlib'"
@@ -116,8 +113,7 @@ class TestPythonPackageBase(TestPythonBase):
         """Run each script previously added to the image."""
         for script in self.sample_scripts:
             cmd = self.interpreter + " " + os.path.basename(script)
-            _, exit_code = self.emulator.run(cmd, timeout=self.timeout)
-            self.assertEqual(exit_code, 0)
+            self.assertRunOk(cmd, timeout=self.timeout)
 
     def test_run(self):
         self.login()
index 7fa7e440189dedc8086812ab7d49405c6d7497cc..1a631adfd3c9f0697bc18f08e9ad073e3095a89a 100644 (file)
@@ -15,5 +15,4 @@ class TestPythonPy3Pytest(TestPythonPackageBase):
     def run_sample_scripts(self):
         for script in self.sample_scripts:
             cmd = self.interpreter + " -m pytest " + os.path.basename(script)
-            _, exit_code = self.emulator.run(cmd, timeout=self.timeout)
-            self.assertEqual(exit_code, 0)
+            self.assertRunOk(cmd, timeout=self.timeout)
index caf034be7b02abcd1a1172a30827469cf3491119..e09abf2abb95c7c9cf90cbdd9c99b63e6d68109d 100644 (file)
@@ -16,5 +16,4 @@ class TestPythonPy3PytestAsyncio(TestPythonPackageBase):
     def run_sample_scripts(self):
         for script in self.sample_scripts:
             cmd = self.interpreter + " -m pytest " + os.path.basename(script)
-            _, exit_code = self.emulator.run(cmd, timeout=self.timeout)
-            self.assertEqual(exit_code, 0)
+            self.assertRunOk(cmd, timeout=self.timeout)
index d79998514e3df19c6aeb152a1ed636e1d5ddaea1..2a333f46ad313e5ecb9025debc264ca1d2ea0bc0 100644 (file)
@@ -16,8 +16,7 @@ class TestRedis(infra.basetest.BRTest):
                            options=["-initrd", cpio_file])
         self.emulator.login()
 
-        _, exit_code = self.emulator.run("redis-cli SET hello world")
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk("redis-cli SET hello world")
 
         output, exit_code = self.emulator.run("redis-cli GET hello")
         self.assertEqual(exit_code, 0)
index 15bb93dd97844cf2001d747a8e6b6844ff14c01f..cd5dccf8e11693ee6c7898f2bc20335f053b5eb1 100644 (file)
@@ -79,8 +79,7 @@ class TestRustBin(TestRustBase):
     def test_run(self):
         self.build_test_prog()
         self.login()
-        _, exit_code = self.emulator.run(self.crate)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(self.crate)
 
 
 class TestRust(TestRustBase):
@@ -108,5 +107,4 @@ class TestRust(TestRustBase):
     def test_run(self):
         self.build_test_prog()
         self.login()
-        _, exit_code = self.emulator.run(self.crate)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(self.crate)
index 8d375efe887c64360b5a910cc5cd5e7546ae1b5f..7551ba7b98beec382369d31e4cea398118dd126f 100644 (file)
@@ -22,19 +22,18 @@ class TestS6Rc(infra.basetest.BRTest):
         self.assertEqual(exit_code, 100)
 
         # Set up two service directories with a dependency
-        self.emulator.run("mkdir -p source/testsv1")
-        self.emulator.run("mkdir -p source/testsv2")
-        self.emulator.run("echo oneshot > source/testsv1/type")
-        self.emulator.run("echo oneshot > source/testsv2/type")
-        self.emulator.run("echo 'echo foo' > source/testsv1/up")
-        self.emulator.run("echo 'echo bar' > source/testsv2/up")
-        self.emulator.run("echo testsv1 > source/testsv2/dependencies")
-        self.emulator.run("chmod +x source/testsv1/up")
-        self.emulator.run("chmod +x source/testsv2/up")
+        self.assertRunOk("mkdir -p source/testsv1")
+        self.assertRunOk("mkdir -p source/testsv2")
+        self.assertRunOk("echo oneshot > source/testsv1/type")
+        self.assertRunOk("echo oneshot > source/testsv2/type")
+        self.assertRunOk("echo 'echo foo' > source/testsv1/up")
+        self.assertRunOk("echo 'echo bar' > source/testsv2/up")
+        self.assertRunOk("echo testsv1 > source/testsv2/dependencies")
+        self.assertRunOk("chmod +x source/testsv1/up")
+        self.assertRunOk("chmod +x source/testsv2/up")
 
         # Compile the service database
-        _, exit_code = self.emulator.run("s6-rc-compile compiled source")
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk("s6-rc-compile compiled source")
 
         # Inspect dependencies
         cmd = "s6-rc-db -c compiled -d dependencies testsv1"
index f9d0a6f301568ec9427a297db27d81a77653a235..daba28a1ea42acb708f4cd7326712c7a5cdda585 100644 (file)
@@ -22,11 +22,9 @@ class TestSudo(infra.basetest.BRTest):
         # -h    set home directory
         # -H    don't create home directory
         # -s    set shell
-        _, exit_code = self.emulator.run("adduser -D -h /tmp -H -s /bin/sh sudotest")
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk("adduser -D -h /tmp -H -s /bin/sh sudotest")
 
-        _, exit_code = self.emulator.run("echo 'sudotest ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers")
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk("echo 'sudotest ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers")
 
         output, exit_code = self.emulator.run("su - sudotest -c 'echo hello world'")
         self.assertEqual(output, ["hello world"])
index 3fb0be5fb88a49f36bfd29f83912e27aa2db792d..153c9f73e50eec95f379e87c0848f21785ce703b 100644 (file)
@@ -20,19 +20,17 @@ class TestSyslogNg(infra.basetest.BRTest):
         self.emulator.login()
 
         cmd = "grep 'syslog-ng starting' /var/log/messages"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         cmd = "logger my-message && "
         cmd += "sleep 1 && "
         cmd += "grep my-message /var/log/messages"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         cmd = "syslog-ng-ctl reload && "
         cmd += "sleep 1"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
+
         cmd = "grep -i 'syslog-ng.*warning' /var/log/messages"
         _, exit_code = self.emulator.run(cmd)
         self.assertEqual(exit_code, 1)
index b31854cab348d6713916b153869a5abd118c0487..7a10b7a2b044d66fa402e80b58a73738629fed3a 100644 (file)
@@ -19,20 +19,16 @@ class TestTmux(infra.basetest.BRTest):
         self.emulator.login()
 
         cmd = "tmux -V"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         cmd = "tmux -C </dev/null"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         cmd = "tmux split"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         cmd = "tmux new-window"
-        _, exit_code = self.emulator.run(cmd)
-        self.assertEqual(exit_code, 0)
+        self.assertRunOk(cmd)
 
         cmd = "tmux list-windows"
         output, exit_code = self.emulator.run(cmd)