Add tests for build class

Functions:
* rsync_inject
* dump_runtime_uuid
* propagate_host_resolv_conf
* restore_resolv_conf
* make_targz
* run_script_in_chroot
* recompress_initramfs
* get_installed_packages
* copy_kernel_initramfs
* run_mksquashfs

Partially implements: blueprint dynamically-build-bootstrap
Change-Id: I01c6620a95bc61baaf5257521917583b9e91d62f
This commit is contained in:
Valyavskiy Viacheslav 2015-11-27 20:01:34 +03:00
parent f0eddf7873
commit a7f4f28307

View File

@ -558,3 +558,196 @@ class BuildUtilsTestCase(unittest2.TestCase):
self.assertEqual(
[mock.call(filename, '/dev/loop0')] * 3,
mock_attach_file.call_args_list)
@mock.patch.object(utils, 'makedirs_if_not_exists')
@mock.patch.object(utils, 'execute')
def test_rsync_inject(self, mock_exec, mock_makedirs):
src = 'host1:/folder1'
dst = 'host2:/folder2'
bu.rsync_inject(src, dst)
mock_exec.assert_called_once_with('rsync', '-rlptDKv', src + '/',
dst + '/', logged=True)
@mock.patch('fuel_agent.utils.build.open',
create=True, new_callable=mock.mock_open)
@mock.patch.object(utils, 'makedirs_if_not_exists')
@mock.patch.object(os, 'path', return_value=True)
@mock.patch('fuel_agent.utils.build.yaml.load', return_value={'test': 22})
@mock.patch('fuel_agent.utils.build.yaml.safe_dump')
def test_dump_runtime_uuid(self, mock_open, mock_makedirs_if_not_exists,
mock_os, mock_load_yaml, mock_safe_dump_yaml):
uuid = "8"
config = "/tmp/test.conf"
bu.dump_runtime_uuid(uuid, config)
mock_open.assert_called_with({'runtime_uuid': '8', 'test': 22},
stream=mock.ANY,
encoding='utf-8')
@mock.patch.object(utils, 'makedirs_if_not_exists')
@mock.patch.object(os.path, 'isfile',
side_effect=[True, True, True, True])
@mock.patch.object(shutil, 'copy')
def test_propagate_host_resolv_conf(self, mock_copy, mock_path,
mock_makedirs):
bu.propagate_host_resolv_conf('/test/path')
expected_args = [mock.call('/test/path/etc/resolv.conf',
'/test/path/etc/resolv.conf.bak'),
mock.call('/etc/resolv.conf',
'/test/path/etc/resolv.conf'),
mock.call('/test/path/etc/hosts',
'/test/path/etc/hosts.bak'),
mock.call('/etc/hosts',
'/test/path/etc/hosts')]
self.assertEqual(mock_copy.call_args_list, expected_args)
@mock.patch.object(utils, 'makedirs_if_not_exists')
@mock.patch.object(os.path, 'isfile', side_effect=[True, True])
@mock.patch.object(shutil, 'move')
def test_restore_host_resolv_conf(self, mock_move, mock_path,
mock_makedirs):
bu.restore_resolv_conf('/test/path')
expected_args = [mock.call('/test/path/etc/resolv.conf.bak',
'/test/path/etc/resolv.conf'),
mock.call('/test/path/etc/hosts.bak',
'/test/path/etc/hosts')]
self.assertEqual(mock_move.call_args_list, expected_args)
@mock.patch.object(utils, 'makedirs_if_not_exists')
@mock.patch.object(utils, 'execute')
@mock.patch('fuel_agent.utils.build.uuid.uuid4', return_value='fake_uuid')
def test_make_targz(self, mock_uuid, mock_exec, mock_makedirs):
self.assertEqual(bu.make_targz('/test/path'), 'fake_uuid.tar.gz')
mock_exec.assert_called_with('tar', '-czf', 'fake_uuid.tar.gz',
'--directory', '/test/path', '.',
logged=True)
@mock.patch.object(utils, 'makedirs_if_not_exists')
@mock.patch.object(utils, 'execute')
def test_make_targz_with_name(self, mock_exec, mock_makedirs):
self.assertEqual(bu.make_targz('/test/path', 'testname'), 'testname')
mock_exec.assert_called_with('tar', '-czf', 'testname', '--directory',
'/test/path', '.', logged=True)
@mock.patch.object(utils, 'makedirs_if_not_exists')
@mock.patch.object(utils, 'execute')
@mock.patch.object(os.path, 'isdir', return_value=True)
@mock.patch.object(shutil, 'copy')
@mock.patch.object(os, 'chmod')
def test_run_script_in_chroot(self, mock_chmod, mock_copy, mock_isdir,
mock_exec, mock_makedirs):
bu.run_script_in_chroot('/test/path', 'script_name')
mock_exec.assert_called_with('chroot', '/test/path', '/bin/bash',
'-c', '/script_name', logged=True)
@mock.patch.object(utils, 'execute')
@mock.patch.object(bu, 'remove_files')
@mock.patch('fuel_agent.utils.build.glob.glob', return_value=[])
@mock.patch('fuel_agent.utils.build.os', environ={})
def test_recompress_initramfs(self, mock_os, mock_glob, mock_rm_files,
mock_exec):
bu.recompress_initramfs('/test/path')
mock_rm_files.assert_called_with('/', [])
mock_exec.assert_called_with(
'chroot', '/test/path',
'update-initramfs -v -c -k all',
logged=True,
env_variables={'TMP': '/tmp', 'TMPDIR': '/tmp'}
)
@mock.patch.object(utils, 'execute')
def test_get_installed_packages(self, mock_exec):
mock_exec.return_value = 'virt-what 1.11-1;;vlan 1.9-3ubuntu6;;'\
'watchdog 5.11-1', None
expected_packages = {'watchdog': '5.11-1',
'vlan': '1.9-3ubuntu6',
'virt-what': '1.11-1'}
packages = bu.get_installed_packages('/test/path')
self.assertEqual(expected_packages, packages)
@mock.patch.object(utils, 'makedirs_if_not_exists')
@mock.patch.object(utils, 'execute')
@mock.patch('fuel_agent.utils.build.glob.glob')
@mock.patch.object(shutil, 'copy')
def test_copy_kernel_initramfs(self, mock_copy, mock_glob, mock_exec,
mock_makedirs):
global_return_hash = {'/test/path/boot/vmlinuz*': ['testfile1'],
'/test/path/boot/initrd*': ['testfile2']}
mock_glob.side_effect = lambda x: global_return_hash[x]
bu.copy_kernel_initramfs('/test/path', '/test/dst/dir')
expected_copy_calls = [
mock.call('testfile1', '/test/dst/dir/vmlinuz'),
mock.call('testfile2', '/test/dst/dir/initrd.img')
]
self.assertItemsEqual(expected_copy_calls, mock_copy.call_args_list)
@mock.patch.object(utils, 'makedirs_if_not_exists')
@mock.patch.object(utils, 'execute')
@mock.patch('fuel_agent.utils.build.glob.glob')
@mock.patch.object(shutil, 'copy')
@mock.patch.object(bu, 'remove_files')
def test_copy_kernel_initramfs_with_remove(self, mock_rm, mock_copy,
mock_glob, mock_exec,
mock_makedirs):
global_return_hash = {'/test/path/boot/vmlinuz*': ['testfile1'],
'/test/path/boot/initrd*': ['testfile2']}
mock_glob.side_effect = lambda x: global_return_hash[x]
bu.copy_kernel_initramfs('/test/path', '/test/dst/dir', True)
expected_copy_calls = [
mock.call('testfile1', '/test/dst/dir/vmlinuz'),
mock.call('testfile2', '/test/dst/dir/initrd.img')
]
expected_rm_calls = [
mock.call('/', ['testfile1']),
mock.call('/', ['testfile2'])
]
self.assertItemsEqual(expected_copy_calls, mock_copy.call_args_list)
self.assertItemsEqual(expected_rm_calls, mock_rm.call_args_list)
@mock.patch.object(utils, 'makedirs_if_not_exists')
@mock.patch.object(utils, 'execute')
@mock.patch.object(shutil, 'move')
@mock.patch('fuel_agent.utils.build.fu.mount_fs')
@mock.patch('fuel_agent.utils.build.fu.umount_fs')
@mock.patch.object(bu, 'stop_chrooted_processes')
@mock.patch('fuel_agent.utils.build.uuid.uuid4', return_value='fake_uuid')
def test_run_mksquashfs(self, mock_uuid, mock_stop_proc, mock_umount,
mock_mount, mock_move, mock_exec, mock_makedirs):
bu.run_mksquashfs('/test/dst/dir')
expected_mount_args = [
mock.call('tmpfs', 'mnt_.mksquashfs.tmp.fake_uuid',
'/test/dst/dir/mnt',
'rw,nodev,nosuid,noatime,mode=0755,size=4M'),
mock.call(None, '/test/dst/dir', '/test/dst/dir/mnt/src',
opts='bind'),
mock.call(None, None, '/test/dst/dir/mnt/src', 'remount,bind,ro'),
mock.call(None, '', '/test/dst/dir/mnt/dst', opts='bind')
]
self.assertEqual(expected_mount_args, mock_mount.call_args_list)
expected_umount_args = [
mock.call('/test/dst/dir/mnt/dst'),
mock.call('/test/dst/dir/mnt/src'),
mock.call('/test/dst/dir/mnt')
]
self.assertEqual(expected_umount_args, mock_umount.call_args_list)
expected_exec_args = [
mock.call('chroot', '/test/dst/dir', 'mksquashfs', '/mnt/src',
'/mnt/dst/.mksquashfs.tmp.fake_uuid', '-comp', 'xz',
'-no-progress', '-noappend', logged=True)
]
self.assertEqual(expected_exec_args, mock_exec.call_args_list)
@mock.patch.object(utils, 'makedirs_if_not_exists')
@mock.patch.object(utils, 'execute')
@mock.patch.object(shutil, 'move')
@mock.patch('fuel_agent.utils.build.fu.mount_fs')
@mock.patch('fuel_agent.utils.build.fu.umount_fs')
@mock.patch.object(bu, 'stop_chrooted_processes')
@mock.patch('fuel_agent.utils.build.uuid.uuid4', return_value='fake_uuid')
def test_run_mksquashfs_with_name(self, mock_uuid, mock_stop_proc,
mock_umount, mock_mount, mock_move,
mock_exec, mock_makedirs):
bu.run_mksquashfs('/test/dst/dir', output_name='myname')
mock_move.assert_called_with(
'/test/dst/dir/mnt/dst/.mksquashfs.tmp.fake_uuid',
'myname'
)