Browse Source

support/testing: use .assertRunOk() when possible

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>
Thomas Petazzoni 4 years ago
parent
commit
afc1ed4d51

+ 1 - 2
support/testing/tests/fs/test_f2fs.py

@@ -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)

+ 1 - 2
support/testing/tests/fs/test_jffs2.py

@@ -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)

+ 1 - 2
support/testing/tests/fs/test_squashfs.py

@@ -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)

+ 2 - 4
support/testing/tests/init/base.py

@@ -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)

+ 1 - 2
support/testing/tests/init/test_none.py

@@ -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")

+ 1 - 2
support/testing/tests/init/test_systemd.py

@@ -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")

+ 2 - 4
support/testing/tests/package/test_atop.py

@@ -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)

+ 1 - 2
support/testing/tests/package/test_bmap_tools.py

@@ -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):

+ 1 - 2
support/testing/tests/package/test_docker_compose.py

@@ -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

+ 2 - 4
support/testing/tests/package/test_dropbear.py

@@ -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)

+ 1 - 2
support/testing/tests/package/test_lua.py

@@ -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):

+ 1 - 2
support/testing/tests/package/test_netdata.py

@@ -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)

+ 2 - 4
support/testing/tests/package/test_openssh.py

@@ -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):

+ 4 - 8
support/testing/tests/package/test_opkg.py

@@ -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

+ 1 - 2
support/testing/tests/package/test_perl.py

@@ -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):

+ 4 - 8
support/testing/tests/package/test_python.py

@@ -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()

+ 1 - 2
support/testing/tests/package/test_python_pytest.py

@@ -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)

+ 1 - 2
support/testing/tests/package/test_python_pytest_asyncio.py

@@ -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)

+ 1 - 2
support/testing/tests/package/test_redis.py

@@ -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)

+ 2 - 4
support/testing/tests/package/test_rust.py

@@ -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)

+ 10 - 11
support/testing/tests/package/test_s6_rc.py

@@ -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"

+ 2 - 4
support/testing/tests/package/test_sudo.py

@@ -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"])

+ 4 - 6
support/testing/tests/package/test_syslog_ng.py

@@ -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)

+ 4 - 8
support/testing/tests/package/test_tmux.py

@@ -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)