Commit 7846359b authored by Marco Trevisan's avatar Marco Trevisan Committed by Bastien Nocera
Browse files

test_fprintd_utils: De-duplicate utilities launching

Avoid repeating the same operation to launch the utilities all the times,
but provide instead a function that allows to start a process and saves its
output without having to handle this in every test.

Simplify the operation when we just want the final output, still reusing
the same code.
parent 08339a06
...@@ -74,16 +74,33 @@ class TestFprintd(dbusmock.DBusTestCase): ...@@ -74,16 +74,33 @@ class TestFprintd(dbusmock.DBusTestCase):
self.device_mock = self.dbus_con.get_object('net.reactivated.Fprint', device_path) self.device_mock = self.dbus_con.get_object('net.reactivated.Fprint', device_path)
self.device_mock.SetEnrolledFingers('toto', ['left-little-finger', 'right-little-finger']) self.device_mock.SetEnrolledFingers('toto', ['left-little-finger', 'right-little-finger'])
def test_fprintd_enroll(self): def start_utility_process(self, utility_name, args=[], sleep=True):
self.setup_device()
mock_log = tempfile.NamedTemporaryFile() mock_log = tempfile.NamedTemporaryFile()
process = subprocess.Popen(self.wrapper_args + [self.tools_prefix + 'fprintd-enroll', '-f', 'right-index-finger', 'toto'], utility = [ os.path.join(self.tools_prefix, 'fprintd-{}'.format(utility_name)) ]
process = subprocess.Popen(self.wrapper_args + utility + args,
stdout=mock_log, stdout=mock_log,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
universal_newlines=True) universal_newlines=True)
self.addCleanup(process.terminate)
self.addCleanup(process.wait)
time.sleep(self.sleep_time) if sleep:
time.sleep(self.sleep_time)
return (process, mock_log)
def run_utility_process(self, utility_name, args=[], sleep=True, timeout=None):
proc, mock_log = self.start_utility_process(utility_name, args=args, sleep=sleep)
ret = proc.wait(timeout=timeout if timeout is not None else self.sleep_time * 4)
self.assertLessEqual(ret, 128)
with open(mock_log.name) as f:
return f.read()
def test_fprintd_enroll(self):
self.setup_device()
process, mock_log = self.start_utility_process('enroll', ['-f', 'right-index-finger', 'toto'])
with open(mock_log.name) as f: with open(mock_log.name) as f:
out = f.read() out = f.read()
...@@ -99,13 +116,7 @@ class TestFprintd(dbusmock.DBusTestCase): ...@@ -99,13 +116,7 @@ class TestFprintd(dbusmock.DBusTestCase):
def test_fprintd_verify(self): def test_fprintd_verify(self):
self.setup_device() self.setup_device()
mock_log = tempfile.NamedTemporaryFile() process, mock_log = self.start_utility_process('verify', ['toto'])
process = subprocess.Popen(self.wrapper_args + [self.tools_prefix + 'fprintd-verify', 'toto'],
stdout=mock_log,
stderr=subprocess.STDOUT,
universal_newlines=True)
time.sleep(self.sleep_time)
with open(mock_log.name) as f: with open(mock_log.name) as f:
out = f.read() out = f.read()
...@@ -126,13 +137,7 @@ class TestFprintd(dbusmock.DBusTestCase): ...@@ -126,13 +137,7 @@ class TestFprintd(dbusmock.DBusTestCase):
] ]
self.device_mock.SetVerifyScript(script) self.device_mock.SetVerifyScript(script)
mock_log = tempfile.NamedTemporaryFile() process, mock_log = self.start_utility_process('verify', ['toto'])
process = subprocess.Popen(self.wrapper_args + [self.tools_prefix + 'fprintd-verify', 'toto'],
stdout=mock_log,
stderr=subprocess.STDOUT,
universal_newlines=True)
time.sleep(self.sleep_time)
with open(mock_log.name) as f: with open(mock_log.name) as f:
out = f.read() out = f.read()
...@@ -149,37 +154,27 @@ class TestFprintd(dbusmock.DBusTestCase): ...@@ -149,37 +154,27 @@ class TestFprintd(dbusmock.DBusTestCase):
self.setup_device() self.setup_device()
# Rick has no fingerprints enrolled # Rick has no fingerprints enrolled
out = subprocess.check_output(self.wrapper_args + [self.tools_prefix + 'fprintd-list', 'rick'], out = self.run_utility_process('list', ['rick'])
stderr=subprocess.STDOUT,
universal_newlines=True)
self.assertRegex(out, r'has no fingers enrolled for') self.assertRegex(out, r'has no fingers enrolled for')
# Toto does # Toto does
out = subprocess.check_output(self.wrapper_args + [self.tools_prefix + 'fprintd-list', 'toto'], out = self.run_utility_process('list', ['toto'])
universal_newlines=True)
self.assertRegex(out, r'left-little-finger')
self.assertRegex(out, r'right-little-finger') self.assertRegex(out, r'right-little-finger')
def test_fprintd_delete(self): def test_fprintd_delete(self):
self.setup_device() self.setup_device()
# Has fingerprints enrolled # Has fingerprints enrolled
out = subprocess.check_output(self.wrapper_args + [self.tools_prefix + 'fprintd-list', 'toto'], out = self.run_utility_process('list', ['toto'])
stderr=subprocess.STDOUT,
universal_newlines=True)
self.assertRegex(out, r'left-little-finger') self.assertRegex(out, r'left-little-finger')
self.assertRegex(out, r'right-little-finger') self.assertRegex(out, r'right-little-finger')
# Delete fingerprints # Delete fingerprints
out = subprocess.check_output(self.wrapper_args + [self.tools_prefix + 'fprintd-delete', 'toto'], out = self.run_utility_process('delete', ['toto'])
stderr=subprocess.STDOUT,
universal_newlines=True)
self.assertRegex(out, r'Fingerprints deleted') self.assertRegex(out, r'Fingerprints deleted')
# Doesn't have fingerprints # Doesn't have fingerprints
out = subprocess.check_output(self.wrapper_args + [self.tools_prefix + 'fprintd-list', 'toto'], out = self.run_utility_process('list', ['toto'])
stderr=subprocess.STDOUT,
universal_newlines=True)
self.assertRegex(out, r'has no fingers enrolled for') self.assertRegex(out, r'has no fingers enrolled for')
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment