summaryrefslogtreecommitdiffstats
path: root/apex/tests
diff options
context:
space:
mode:
authorDan Radez <dradez@redhat.com>2017-09-14 09:56:36 -0400
committerDan Radez <dradez@redhat.com>2017-09-23 20:31:10 -0400
commitc876e9d261a7c3189cc8374c995014309c5b9881 (patch)
tree01f110001bd23fa8494011c0aa37e21989fd7a23 /apex/tests
parent30a8bb049eaf1e3df040a6cb8052bafbb5dcd131 (diff)
Adding unittests for build
- covers build.py - covers build_utils.py - moved build_utils out of build module, it wasn't possible to import build.py while build_utils was in a module Change-Id: I42f08a475d9ca219a62c421d4bdd2d1d3c49691a Signed-off-by: Dan Radez <dradez@redhat.com>
Diffstat (limited to 'apex/tests')
-rw-r--r--apex/tests/test_apex_build.py308
-rw-r--r--apex/tests/test_apex_build_utils.py167
2 files changed, 475 insertions, 0 deletions
diff --git a/apex/tests/test_apex_build.py b/apex/tests/test_apex_build.py
new file mode 100644
index 00000000..a55f790e
--- /dev/null
+++ b/apex/tests/test_apex_build.py
@@ -0,0 +1,308 @@
+##############################################################################
+# Copyright (c) 2016 Dan Radez (dradez@redhat.com) (Red Hat)
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+import os
+import subprocess
+import unittest
+
+from mock import patch
+from mock import mock_open
+from argparse import ArgumentParser
+
+from apex.build import ApexBuildException
+from apex.build import build
+from apex.build import build_cache
+from apex.build import create_build_parser
+from apex.build import get_cache_file
+from apex.build import get_journal
+from apex.build import main
+from apex.build import prune_cache
+from apex.build import unpack_cache
+
+from nose.tools import (
+ assert_is_none,
+ assert_raises,
+ assert_is_instance)
+
+a_mock_open = mock_open(read_data=None)
+
+
+class TestBuild(unittest.TestCase):
+ @classmethod
+ def setup_class(cls):
+ """This method is run once for each class before any tests are run"""
+ cls.repo_name = 'test_repo'
+ cls.repo_url = 'https://gerrit.opnfv.org/gerrit/' + cls.repo_name
+ cls.change_id = 'I5c1b3ded249c4e3c558be683559e03deb27721b8'
+ cls.commit_id = '8669c687a75a00106b055add49b82fee826b8fe8'
+ cls.sys_argv = ['deploy.py', 'clone-fork', '-r', cls.repo_name]
+ cls.sys_argv_debug = ['deploy.py', '--debug']
+
+ @classmethod
+ def teardown_class(cls):
+ """This method is run once for each class _after_ all tests are run"""
+
+ def setup(self):
+ """This method is run once before _each_ test method is executed"""
+
+ def teardown(self):
+ """This method is run once after _each_ test method is executed"""
+
+ def test_create_build_parser(self):
+ assert_is_instance(create_build_parser(), ArgumentParser)
+
+ @patch('apex.build.yaml')
+ @patch('apex.build.os.path')
+ @patch('builtins.open', a_mock_open, create=True)
+ def test_get_journal_exists(self, mock_os_path, mock_yaml):
+ # setup mock
+ mock_os_path.isfile.return_value = True
+ mock_yaml.safe_load.return_value = ['a', 'list']
+ # execute
+ assert_is_instance(get_journal('test_dir'), list)
+ # assert
+ mock_os_path.isfile.assert_called_with('test_dir/cache_journal.yaml')
+ mock_yaml.safe_load.assert_called_with(a_mock_open.return_value)
+
+ @patch('apex.build.os.path')
+ def test_get_journal_notexist(self, mock_os_path):
+ # setup mock
+ mock_os_path.isfile.return_value = False
+ # execute
+ assert_is_none(get_journal('test_dir'))
+
+ @patch('apex.build.os.path')
+ @patch('apex.build.get_journal')
+ def test_get_cache_file(self, mock_get_journal, mock_os_path):
+ mock_get_journal.return_value = ['journal_contents']
+ mock_os_path.isfile.return_value = True
+ get_cache_file('test_dir')
+
+ def test_unpack_cache_no_cache_dir(self):
+ unpack_cache('dest_dir', cache_dir=None)
+
+ @patch('apex.build.os.path')
+ def test_unpack_cache_not_isdir(self, mock_os_path):
+ mock_os_path.isdir.return_value = False
+ unpack_cache('dest_dir', cache_dir='cache_dir')
+
+ @patch('apex.build.get_cache_file')
+ @patch('apex.build.os.path')
+ def test_unpack_cache_cache_file_none(self, mock_os_path, mock_cache_file):
+ mock_os_path.isdir.return_value = True
+ mock_cache_file.return_value = None
+ unpack_cache('dest_dir', cache_dir='cache_dir')
+
+ @patch('apex.build.subprocess.check_call')
+ @patch('apex.build.get_cache_file')
+ @patch('apex.build.os.path')
+ @patch('apex.build.os')
+ def test_unpack_cache_tar_failure(self, mock_os, mock_os_path,
+ mock_cache_file,
+ mock_subprocess):
+ mock_os_path.isdir.return_value = True
+ mock_cache_file.return_value = 'cache_file'
+ mock_os_path.exists.return_value = False
+ mock_subprocess.side_effect = subprocess.CalledProcessError(1, 'cmd')
+ unpack_cache('dest_dir', cache_dir='cache_dir')
+
+ @patch('apex.build.subprocess')
+ @patch('apex.build.get_cache_file')
+ @patch('apex.build.os.path')
+ @patch('apex.build.os')
+ def test_unpack_cache_cache_dest_not_exist(self, mock_os, mock_os_path,
+ mock_cache_file,
+ mock_subprocess):
+ mock_os_path.isdir.return_value = True
+ mock_cache_file.return_value = 'cache_file'
+ mock_os_path.exists.return_value = False
+ mock_os.listdir.return_value = ['listdir', 'is', 'Mocked']
+ unpack_cache('dest_dir', cache_dir='cache_dir')
+
+ @patch('apex.build.subprocess')
+ def test_build(self, mock_subprocess):
+ build('build_root', None)
+
+ @patch('apex.build.subprocess.check_call')
+ def test_build_check_call_raises(self, mock_subprocess):
+ mock_subprocess.side_effect = subprocess.CalledProcessError('cmd', 1)
+ assert_raises(subprocess.CalledProcessError, build, 'build_root', None)
+
+ @patch('apex.build.subprocess.check_output')
+ @patch('apex.build.subprocess.check_call')
+ def test_build_check_output_raises(self, mock_check_call, mock_subprocess):
+ mock_subprocess.side_effect = subprocess.CalledProcessError('cmd', 1)
+ assert_raises(subprocess.CalledProcessError, build, 'build_root', None)
+
+ @patch('apex.build.subprocess')
+ def test_build_rpms(self, mock_subprocess):
+ build('build_root', 'v123', rpms=True)
+
+ @patch('apex.build.subprocess')
+ def test_build_iso(self, mock_subprocess):
+ build('build_root', 'v123', iso=True)
+
+ def test_build_cache_none(self):
+ build_cache('cache_source', None)
+
+ @patch('apex.build.get_journal')
+ @patch('apex.build.yaml')
+ @patch('apex.build.os')
+ @patch('apex.build.subprocess')
+ @patch('builtins.open', a_mock_open, create=True)
+ def test_build_cache(self, mock_subprocess, mock_os,
+ mock_yaml, mock_get_journal):
+ build_cache('cache_source', 'cache_dir')
+ mock_yaml.safe_dump.assert_called_with(mock_get_journal.return_value,
+ a_mock_open.return_value,
+ default_flow_style=False)
+
+ @patch('apex.build.get_journal')
+ @patch('apex.build.uuid')
+ @patch('apex.build.yaml')
+ @patch('apex.build.os')
+ @patch('apex.build.subprocess')
+ @patch('builtins.open', a_mock_open, create=True)
+ def test_build_cache_get_journal_none(self, mock_subprocess, mock_os,
+ mock_yaml, mock_uuid,
+ mock_get_journal):
+ uuid = '73b18d27-8d25-4e02-a937-cb08609b6911'
+ mock_get_journal.return_value = None
+ mock_uuid.uuid4.return_value = uuid
+ build_cache('cache_source', 'cache_dir')
+ mock_yaml.safe_dump.assert_called_with(['apex-cache-{}.tgz'.format(
+ uuid)],
+ a_mock_open.return_value,
+ default_flow_style=False)
+
+ @patch('apex.build.get_journal')
+ @patch('apex.build.yaml')
+ @patch('apex.build.os.path')
+ @patch('apex.build.os')
+ @patch('apex.build.subprocess.check_call')
+ @patch('builtins.open', mock_open(read_data=None), create=True)
+ def test_build_cache_tar_fails(self, mock_subprocess, mock_os,
+ mock_os_path, mock_yaml, mock_get_journal):
+ mock_subprocess.side_effect = BaseException()
+ mock_os_path.isfile.return_value = True
+ assert_raises(BaseException, build_cache, 'cache_source', 'cache_dir')
+
+ @patch('apex.build.get_journal')
+ @patch('apex.build.yaml')
+ @patch('apex.build.os.path')
+ @patch('apex.build.os')
+ @patch('apex.build.subprocess.check_call')
+ @patch('builtins.open', mock_open(read_data=None), create=True)
+ def test_build_cache_cache_full_path_false(self, mock_subprocess, mock_os,
+ mock_os_path, mock_yaml,
+ mock_get_journal):
+ mock_os_path.isfile.return_value = False
+ build_cache('cache_source', 'cache_dir')
+ mock_yaml.safe_dump.assert_not_called()
+
+ def test_prune_cache_none(self):
+ prune_cache(None)
+
+ @patch('apex.build.get_journal')
+ def test_prune_cache_empty_journal(self, mock_get_journal):
+ mock_get_journal.return_value = []
+ prune_cache('not-none')
+
+ @patch('apex.build.get_journal')
+ @patch('apex.build.yaml')
+ @patch('apex.build.os')
+ @patch('builtins.open', mock_open(read_data=None), create=True)
+ def test_prune_cache_os_remove_error(self, mock_os, mock_yaml,
+ mock_get_journal):
+ # setup Mock
+ mock_get_journal.return_value = ['more', 'than', 'two']
+ rm = mock_os.remove
+ rm.side_effect = OSError()
+ # execute
+ prune_cache('not-none')
+
+ @patch('apex.build.get_journal')
+ @patch('apex.build.yaml')
+ @patch('apex.build.os')
+ @patch('builtins.open', a_mock_open, create=True)
+ def test_prune_cache(self, mock_os, mock_yaml, mock_get_journal):
+ # setup Mock
+ mock_get_journal.return_value = ['more', 'than', 'two']
+ # execute
+ prune_cache('not-none')
+ # assert
+ mock_yaml.safe_dump.assert_called_with(['than', 'two'],
+ a_mock_open.return_value,
+ default_flow_style=False)
+
+ @patch('apex.build.create_build_parser')
+ @patch('apex.build.subprocess.check_output')
+ @patch('apex.build.os.path')
+ @patch('apex.build.os')
+ @patch('apex.build.utils')
+ @patch('apex.build.unpack_cache')
+ @patch('apex.build.build_cache')
+ @patch('apex.build.prune_cache')
+ @patch('apex.build.build')
+ def test_main(self, mock_build, mock_prune_cache,
+ mock_build_cache, mock_unpack_cache,
+ mock_utils, mock_os, mock_os_path,
+ mock_subprocess, mock_parser):
+ # setup mock
+ mbc = mock_parser.return_value
+ args = mbc.parse_args.return_value
+ args.debug = False
+ mock_os_path.isdir.return_value = True
+ # execute
+ main()
+ # assert
+ # TODO
+
+ @patch('apex.build.create_build_parser')
+ @patch('apex.build.subprocess.check_output')
+ @patch('apex.build.os.path')
+ @patch('apex.build.os')
+ @patch('apex.build.utils')
+ @patch('apex.build.unpack_cache')
+ @patch('apex.build.build_cache')
+ @patch('apex.build.prune_cache')
+ @patch('apex.build.build')
+ def test_main_throw_build_except(self, mock_build, mock_prune_cache,
+ mock_build_cache, mock_unpack_cache,
+ mock_utils, mock_os, mock_os_path,
+ mock_subprocess, mock_parser):
+ # setup mock
+ mbc = mock_parser.return_value
+ args = mbc.parse_args.return_value
+ args.debug = True
+ mock_os_path.isdir.return_value = False
+ # execute and assert
+ assert_raises(ApexBuildException, main)
+
+ @patch('apex.build.create_build_parser')
+ @patch('apex.build.subprocess.check_output')
+ @patch('apex.build.os.path')
+ @patch('apex.build.os')
+ @patch('apex.build.utils')
+ @patch('apex.build.unpack_cache')
+ @patch('apex.build.build_cache')
+ @patch('apex.build.prune_cache')
+ @patch('apex.build.build')
+ def test_main_throw_subprocess_except(self, mock_build, mock_prune_cache,
+ mock_build_cache, mock_unpack_cache,
+ mock_utils, mock_os, mock_os_path,
+ mock_subprocess, mock_parser):
+ # setup mock
+ mbc = mock_parser.return_value
+ args = mbc.parse_args.return_value
+ args.debug = False
+ mock_os_path.isdir.return_value = True
+ mock_subprocess.side_effect = subprocess.CalledProcessError('cmd', 1)
+ # execute and assert
+ assert_raises(subprocess.CalledProcessError, main)
diff --git a/apex/tests/test_apex_build_utils.py b/apex/tests/test_apex_build_utils.py
new file mode 100644
index 00000000..d9d542d6
--- /dev/null
+++ b/apex/tests/test_apex_build_utils.py
@@ -0,0 +1,167 @@
+##############################################################################
+# Copyright (c) 2016 Dan Radez (dradez@redhat.com) (Red Hat)
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+import argparse
+import git
+
+from mock import patch
+
+from apex import build_utils
+
+from nose.tools import (
+ assert_is_instance,
+ assert_raises)
+
+
+class TestBuildUtils(object):
+ @classmethod
+ def setup_class(cls):
+ """This method is run once for each class before any tests are run"""
+ cls.repo_name = 'test_repo'
+ cls.repo_url = 'https://gerrit.opnfv.org/gerrit'
+ cls.change_id = 'I5c1b3ded249c4e3c558be683559e03deb27721b8'
+ cls.commit_id = '8669c687a75a00106b055add49b82fee826b8fe8'
+ cls.sys_argv = ['deploy.py', 'clone-fork', '-r', cls.repo_name]
+ cls.sys_argv_debug = ['deploy.py', '--debug']
+
+ @classmethod
+ def teardown_class(cls):
+ """This method is run once for each class _after_ all tests are run"""
+
+ def setup(self):
+ """This method is run once before _each_ test method is executed"""
+
+ def teardown(self):
+ """This method is run once after _each_ test method is executed"""
+
+ def test_main_wo_func_w_debug(self):
+ with patch.object(build_utils.sys, 'argv', self.sys_argv_debug):
+ # no func argument (clone-fork) throws sys exit
+ assert_raises(SystemExit, build_utils.main)
+
+ @patch('apex.build_utils.get_parser')
+ @patch('apex.build_utils.os.path')
+ @patch('apex.build_utils.os')
+ @patch('apex.build_utils.shutil')
+ @patch('apex.build_utils.GerritRestAPI')
+ @patch('apex.build_utils.git.Repo')
+ def test_clone_fork(self, mock_git_repo, mock_gerrit_api,
+ mock_shutil, mock_os, mock_path, mock_get_parser):
+ # setup mock
+ args = mock_get_parser.parse_args.return_value
+ args.repo = self.repo_name
+ args.url = self.repo_url
+ args.branch = 'master'
+ x = mock_git_repo.return_value
+ xx = x.commit.return_value
+ xx.message = '{}: {}'.format(self.repo_name, self.change_id)
+ mock_path.exists.return_value = True
+ mock_path.isdir.return_value = True
+ y = mock_gerrit_api.return_value
+ y.get.return_value = {'status': 'TEST',
+ 'current_revision': 'revision',
+ 'revisions':
+ {'revision': {'ref': self.commit_id}}}
+ z = mock_git_repo.clone_from.return_value
+ # execute
+ build_utils.clone_fork(args)
+ # check results
+ mock_path.exists.assert_called_with(self.repo_name)
+ mock_path.isdir.assert_called_with(self.repo_name)
+ mock_shutil.rmtree.assert_called_with(self.repo_name)
+ mock_git_repo.clone_from.assert_called_with('{}/{}'.
+ format(self.repo_url,
+ self.repo_name),
+ self.repo_name,
+ b='master')
+ z.git.fetch.assert_called_with('{}/{}'.format(self.repo_url,
+ self.repo_name),
+ self.commit_id)
+ z.git.checkout.assert_called_with('FETCH_HEAD')
+
+ @patch('apex.build_utils.get_parser')
+ @patch('apex.build_utils.os.path')
+ @patch('apex.build_utils.os')
+ @patch('apex.build_utils.shutil')
+ @patch('apex.build_utils.GerritRestAPI')
+ @patch('apex.build_utils.git.Repo')
+ def test_clone_fork_MERGED(self, mock_git_repo, mock_gerrit_api,
+ mock_shutil, mock_os, mock_path,
+ mock_get_parser):
+ # setup mock
+ args = mock_get_parser.parse_args.return_value
+ args.repo = self.repo_name
+ args.url = self.repo_url
+ args.branch = 'master'
+ x = mock_git_repo.return_value
+ xx = x.commit.return_value
+ xx.message = '{}: {}'.format(self.repo_name, self.change_id)
+ mock_path.exists.return_value = True
+ mock_path.isdir.return_value = False
+ y = mock_gerrit_api.return_value
+ y.get.return_value = {'status': 'MERGED',
+ 'current_revision': 'revision',
+ 'revisions':
+ {'revision': {'ref': self.commit_id}}}
+ z = mock_git_repo.clone_from.return_value
+ # execute
+ build_utils.clone_fork(args)
+ # check results
+ mock_path.exists.assert_called_with(self.repo_name)
+ mock_os.remove.assert_called_with(self.repo_name)
+ mock_git_repo.clone_from.assert_called_with('{}/{}'.
+ format(self.repo_url,
+ self.repo_name),
+ self.repo_name, b='master')
+ z.git.fetch.assert_not_called
+ z.git.checkout.assert_not_called
+
+ @patch('apex.build_utils.get_parser')
+ @patch('apex.build_utils.GerritRestAPI')
+ @patch('apex.build_utils.git.Repo')
+ def test_clone_fork_invalid_git_repo(self, mock_git_repo,
+ mock_gerrit_api, mock_get_parser):
+ # setup mock
+ args = mock_get_parser.parse_args.return_value
+ args.repo = self.repo_name
+ args.url = self.repo_url
+ args.branch = 'master'
+ mock_git_repo.side_effect = git.exc.InvalidGitRepositoryError()
+ build_utils.clone_fork(args)
+
+ @patch('apex.build_utils.get_parser')
+ @patch('apex.build_utils.GerritRestAPI')
+ @patch('apex.build_utils.git.Repo')
+ def test_clone_fork_raises_key_error(self, mock_git_repo,
+ mock_gerrit_api, mock_get_parser):
+ # setup mock
+ args = mock_get_parser.parse_args.return_value
+ args.repo = self.repo_name
+ args.url = self.repo_url
+ args.branch = 'master'
+ x = mock_git_repo.return_value
+ xx = x.commit.return_value
+ xx.message = '{}: {}'.format(self.repo_name, self.change_id)
+ y = mock_gerrit_api.return_value
+ y.get.return_value = {}
+ # execute & assert
+ assert_raises(KeyError, build_utils.clone_fork, args)
+
+ def test_get_parser(self):
+ assert_is_instance(build_utils.get_parser(), argparse.ArgumentParser)
+
+ @patch('apex.build_utils.get_parser')
+ def test_main(self, mock_get_parser):
+ with patch.object(build_utils.sys, 'argv', self.sys_argv):
+ build_utils.main()
+
+ @patch('apex.build_utils.get_parser')
+ def test_main_debug(self, mock_get_parser):
+ with patch.object(build_utils.sys, 'argv', self.sys_argv_debug):
+ build_utils.main()