From cfda99e6fe8b0ac79ccea4c7862b6fcfa25c71b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Wed, 2 Dec 2015 15:55:53 +0100 Subject: [PATCH] Add basic tests for buildinstall phase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test only checks that commands are created with correct arguments and that a proper number of threads is started. There is no validation for what the threads are actually doing. Signed-off-by: Lubomír Sedlář --- tests/test_buildinstall.py | 122 +++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100755 tests/test_buildinstall.py diff --git a/tests/test_buildinstall.py b/tests/test_buildinstall.py new file mode 100755 index 00000000..3b850369 --- /dev/null +++ b/tests/test_buildinstall.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + + +import unittest +import mock + +import os +import sys + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) + +from pungi.phases.buildinstall import BuildinstallPhase + + +class _DummyCompose(object): + def __init__(self, config): + self.conf = config + self.paths = mock.Mock( + compose=mock.Mock( + topdir=mock.Mock(return_value='/a/b') + ), + work=mock.Mock( + arch_repo=mock.Mock(return_value='file:///a/b/'), + buildinstall_dir=mock.Mock(side_effect=lambda x: '/buildinstall_dir/' + x), + ) + ) + self._logger = mock.Mock() + self.log_debug = mock.Mock() + self.supported = True + + def get_arches(self): + return ['x86_64', 'amd64'] + + +class TestImageChecksumPhase(unittest.TestCase): + + def test_config_skip_unless_bootable(self): + compose = _DummyCompose({}) + compose.just_phases = None + compose.skip_phases = [] + + phase = BuildinstallPhase(compose) + + self.assertTrue(phase.skip()) + + def test_does_not_skip_on_bootable(self): + compose = _DummyCompose({'bootable': True}) + compose.just_phases = None + compose.skip_phases = [] + + phase = BuildinstallPhase(compose) + + self.assertFalse(phase.skip()) + + @mock.patch('pungi.phases.buildinstall.ThreadPool') + @mock.patch('pungi.phases.buildinstall.LoraxWrapper') + @mock.patch('pungi.phases.buildinstall.get_volid') + def test_starts_threads_for_each_cmd_with_lorax(self, get_volid, loraxCls, poolCls): + compose = _DummyCompose({ + 'bootable': True, + 'release_name': 'Test', + 'release_short': 't', + 'release_version': '1', + 'release_is_layered': False, + 'buildinstall_method': 'lorax' + }) + + get_volid.return_value = 'vol_id' + + phase = BuildinstallPhase(compose) + + phase.run() + + # Two items added for processing in total. + pool = poolCls.return_value + self.assertEqual(2, len(pool.queue_put.mock_calls)) + + # Obtained correct lorax commands. + lorax = loraxCls.return_value + lorax.get_lorax_cmd.assert_has_calls( + [mock.call('Test', '1', '1', 'file:///a/b/', '/buildinstall_dir/x86_64', + buildarch='x86_64', is_final=True, nomacboot=True, noupgrade=True, volid='vol_id'), + mock.call('Test', '1', '1', 'file:///a/b/', '/buildinstall_dir/amd64', + buildarch='amd64', is_final=True, nomacboot=True, noupgrade=True, volid='vol_id')], + any_order=True) + + @mock.patch('pungi.phases.buildinstall.ThreadPool') + @mock.patch('pungi.phases.buildinstall.LoraxWrapper') + @mock.patch('pungi.phases.buildinstall.get_volid') + def test_starts_threads_for_each_cmd_with_buildinstall(self, get_volid, loraxCls, poolCls): + compose = _DummyCompose({ + 'bootable': True, + 'release_name': 'Test', + 'release_short': 't', + 'release_version': '1', + 'release_is_layered': False, + 'buildinstall_method': 'buildinstall' + }) + + get_volid.return_value = 'vol_id' + + phase = BuildinstallPhase(compose) + + phase.run() + + # Two items added for processing in total. + pool = poolCls.return_value + self.assertEqual(2, len(pool.queue_put.mock_calls)) + + # Obtained correct lorax commands. + lorax = loraxCls.return_value + lorax.get_buildinstall_cmd.assert_has_calls( + [mock.call('Test', '1', '1', 'file:///a/b/', '/buildinstall_dir/x86_64', + buildarch='x86_64', is_final=True, volid='vol_id'), + mock.call('Test', '1', '1', 'file:///a/b/', '/buildinstall_dir/amd64', + buildarch='amd64', is_final=True, volid='vol_id')], + any_order=True) + + +if __name__ == "__main__": + unittest.main()