[virt-tools-list] [virt-bootstrap] [PATCH 7/9] tests: Add unit tests for VirtBuilderSource
Radostin Stoyanov
rstoyanov1 at gmail.com
Fri Jul 28 09:21:45 UTC 2017
---
tests/test_utils.py | 35 +++++++++
tests/test_virtbuilder_source.py | 162 +++++++++++++++++++++++++++++++++++++++
2 files changed, 197 insertions(+)
create mode 100644 tests/test_virtbuilder_source.py
diff --git a/tests/test_utils.py b/tests/test_utils.py
index dacfbd1..540aa41 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -726,6 +726,41 @@ class TestUtils(unittest.TestCase):
default_terminal_width + 1)
mocked['sys'].stdout.write.assert_called_once()
+ ###################################
+ # Tests for: show_error_if_file_exits()
+ ###################################
+ def test_show_error_if_file_exits_does_not_show_error(self):
+ """
+ Ensures that show_error_if_file_exits() does not show error and
+ exit if the file does not exists.
+ """
+ path = 'foo'
+ with mock.patch('sys.exit') as m_exit:
+ with mock.patch('os.path.exists') as m_path_exists:
+ m_path_exists.return_value = False
+ with mock.patch('virtBootstrap.utils.logger') as m_logger:
+ utils.show_error_if_file_exits(path)
+
+ m_path_exists.assert_called_once_with(path)
+ m_logger.error.assert_not_called()
+ m_exit.assert_not_called()
+
+ def test_show_error_if_file_exits(self):
+ """
+ Ensures that show_error_if_file_exits() does not show error and
+ exit if the file does not exists.
+ """
+ path = 'foo'
+ with mock.patch('sys.exit') as m_exit:
+ with mock.patch('os.path.exists') as m_path_exists:
+ m_path_exists.return_value = True
+ with mock.patch('virtBootstrap.utils.logger') as m_logger:
+ utils.show_error_if_file_exits(path)
+
+ m_path_exists.assert_called_once_with(path)
+ m_logger.error.assert_called_once_with("File already exist '%s'", path)
+ m_exit.assert_called_once_with(1)
+
if __name__ == '__main__':
unittest.main(exit=False)
diff --git a/tests/test_virtbuilder_source.py b/tests/test_virtbuilder_source.py
new file mode 100644
index 0000000..7289147
--- /dev/null
+++ b/tests/test_virtbuilder_source.py
@@ -0,0 +1,162 @@
+# Authors: Radostin Stoyanov <rstoyanov1 at gmail.com>
+#
+# Copyright (C) 2017 Radostin Stoyanov
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+"""
+Unit tests for methods defined in virtBootstrap.sources.VirtBuilderSource
+"""
+
+from tests import unittest
+from tests import mock
+from tests import sources
+
+
+# pylint: disable=invalid-name
+class TestVirtBuilderSource(unittest.TestCase):
+ """
+ Test cases for VirtBuilderSource
+ """
+
+ ###################################
+ # Tests for: __init__()
+ ###################################
+ def test_argument_assignment(self):
+ """
+ Ensures that __init__() assigns the arguments' values to instance
+ variables.
+ """
+ kwargs = {'uri': mock.Mock(),
+ 'fmt': 'dir',
+ 'progress': mock.Mock()}
+
+ src_instance = sources.VirtBuilderSource(**kwargs)
+
+ test_values = {
+ src_instance.template: kwargs['uri'].netloc,
+ src_instance.output_format: kwargs['fmt'],
+ src_instance.progress: kwargs['progress'].update_progress
+ }
+ for value in test_values:
+ self.assertIs(value, test_values[value])
+
+ ###################################
+ # Tests for: unpack()
+ ###################################
+ def test_unpack_for_dir_format(self):
+ """
+ Ensures that unpack() creates calls build_image() with
+ tmp_image_file and execute() with virt-copy-out command
+ when self.output_format='dir'.
+ """
+ m_self = mock.Mock(spec=sources.VirtBuilderSource)
+ m_self.template = 'bar'
+ m_self.output_format = 'dir'
+ m_self.progress = mock.Mock()
+ m_self.build_image = mock.Mock()
+ tmp_dir = '/foo'
+ tmp_image_file = '/foo/bar.img'
+ dest = 'destination_path'
+
+ with mock.patch('shutil.rmtree'):
+ with mock.patch.multiple('virtBootstrap.utils',
+ get_image_dir=mock.DEFAULT,
+ execute=mock.DEFAULT) as mocked:
+ mocked['get_image_dir'].return_value = tmp_dir
+ sources.VirtBuilderSource.unpack(m_self, dest)
+
+ mocked['get_image_dir'].assert_called_once_with(no_cache=True)
+ m_self.build_image.assert_called_once_with(tmp_image_file)
+ mocked['execute'].assert_called_once_with(['virt-copy-out', '-a',
+ tmp_image_file, '/', dest])
+
+ def test_unpack_for_qcow2_format(self):
+ """
+ Ensures that unpack() creates calls build_image() with
+ tmp_image_file and execute() with virt-tar-out command
+ when self.output_format='qcow2'.
+ """
+ m_self = mock.Mock(spec=sources.VirtBuilderSource)
+ m_self.template = 'bar'
+ m_self.output_format = 'qcow2'
+ m_self.progress = mock.Mock()
+ m_self.build_image = mock.Mock()
+ tmp_dir = '/tmp'
+ tmp_image_file = tmp_dir + '/' + m_self.template + '.img'
+ tmp_tar_file = tmp_dir + '/filesystem.tar'
+ dest = 'destination_path'
+ image_file = dest + '/' + m_self.template + '.qcow2'
+
+ executed_calls = [
+ mock.call(['virt-tar-out', '-a', tmp_image_file,
+ '/', tmp_tar_file]),
+ mock.call(['virt-make-fs', '--type=ext3', '--format=qcow2',
+ '--size=+200M', tmp_tar_file, image_file])
+ ]
+
+ with mock.patch('shutil.rmtree'):
+ with mock.patch.multiple('virtBootstrap.utils',
+ get_image_dir=mock.DEFAULT,
+ show_error_if_file_exits=mock.DEFAULT,
+ execute=mock.DEFAULT) as mocked:
+ mocked['get_image_dir'].return_value = tmp_dir
+ sources.VirtBuilderSource.unpack(m_self, dest)
+
+ mocked['get_image_dir'].assert_called_once_with(no_cache=True)
+ m_self.build_image.assert_called_once_with(tmp_image_file)
+ mocked['execute'].assert_has_calls(executed_calls)
+
+ def test_unpack_raise_error_for_unknown_format(self):
+ """
+ Ensures that unpack() throws an Exception when called with
+ invalid output format.
+ """
+ m_self = mock.Mock(spec=sources.VirtBuilderSource)
+ m_self.template = 'foo'
+ m_self.output_format = 'bar'
+ tmp_dir = '/tmp'
+
+ with self.assertRaises(Exception) as err:
+ with mock.patch('shutil.rmtree') as m_rmtree:
+ with mock.patch('virtBootstrap.utils.get_image_dir') as mocked:
+ mocked.return_value = tmp_dir
+ sources.VirtBuilderSource.unpack(m_self, 'dest')
+
+ m_rmtree.assert_called_once_with(tmp_dir)
+ self.assertEqual("Unknown format:" + m_self.output_format,
+ str(err.exception))
+
+ ###################################
+ # Tests for: build_image()
+ ###################################
+ def test_build_image(self):
+ """
+ Ensures that build_image() calls virt-builder with correct
+ command.
+ """
+ m_self = mock.Mock(spec=sources.VirtBuilderSource)
+ m_self.template = 'foo'
+ output_file = 'bar'
+
+ cmd = ['virt-builder', m_self.template,
+ '-o', output_file,
+ '--no-network', '--delete', '/dev/*', '--delete', '/boot/*',
+ '--edit', '/etc/fstab:s/^/#/']
+
+ with mock.patch('virtBootstrap.sources.check_call') as m_call:
+ sources.VirtBuilderSource.build_image(m_self, output_file)
+
+ m_call.assert_called_once_with(cmd)
--
2.9.4
More information about the virt-tools-list
mailing list