1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (dradez@redhat.com) (Red Hat)
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
15 from mock import patch
17 from apex import build_utils
18 from apex.tests import constants as con
20 from nose.tools import (
25 class TestBuildUtils(unittest.TestCase):
28 """This method is run once for each class before any tests are run"""
29 cls.repo_name = 'test_repo'
30 cls.repo_url = 'https://gerrit.opnfv.org/gerrit'
31 cls.change_id = 'I5c1b3ded249c4e3c558be683559e03deb27721b8'
32 cls.commit_id = '8669c687a75a00106b055add49b82fee826b8fe8'
33 cls.sys_argv = ['deploy.py', 'clone-fork', '-r', cls.repo_name]
34 cls.sys_argv_debug = ['deploy.py', '--debug']
37 def teardown_class(cls):
38 """This method is run once for each class _after_ all tests are run"""
41 """This method is run once before _each_ test method is executed"""
44 """This method is run once after _each_ test method is executed"""
46 def test_main_wo_func_w_debug(self):
47 with patch.object(build_utils.sys, 'argv', self.sys_argv_debug):
48 # no func argument (clone-fork) throws sys exit
49 assert_raises(SystemExit, build_utils.main)
51 @patch('apex.build_utils.get_parser')
52 @patch('apex.build_utils.os.path')
53 @patch('apex.build_utils.os')
54 @patch('apex.build_utils.shutil')
55 @patch('apex.build_utils.GerritRestAPI')
56 @patch('apex.build_utils.git.Repo')
57 def test_clone_fork(self, mock_git_repo, mock_gerrit_api,
58 mock_shutil, mock_os, mock_path, mock_get_parser):
60 args = mock_get_parser.parse_args.return_value
61 args.repo = self.repo_name
62 args.url = self.repo_url
63 args.branch = 'master'
64 x = mock_git_repo.return_value
65 xx = x.commit.return_value
66 xx.message = '{}: {}'.format(self.repo_name, self.change_id)
67 mock_path.exists.return_value = True
68 mock_path.isdir.return_value = True
69 y = mock_gerrit_api.return_value
70 y.get.return_value = {'status': 'TEST',
71 'current_revision': 'revision',
73 {'revision': {'ref': self.commit_id}}}
74 z = mock_git_repo.clone_from.return_value
76 build_utils.clone_fork(args)
78 mock_path.exists.assert_called_with(self.repo_name)
79 mock_path.isdir.assert_called_with(self.repo_name)
80 mock_shutil.rmtree.assert_called_with(self.repo_name)
81 mock_git_repo.clone_from.assert_called_with('{}/{}'.
86 z.git.fetch.assert_called_with('{}/{}'.format(self.repo_url,
89 z.git.checkout.assert_called_with('FETCH_HEAD')
91 @patch('apex.build_utils.get_parser')
92 @patch('apex.build_utils.os.path')
93 @patch('apex.build_utils.os')
94 @patch('apex.build_utils.shutil')
95 @patch('apex.build_utils.GerritRestAPI')
96 @patch('apex.build_utils.git.Repo')
97 def test_clone_fork_MERGED(self, mock_git_repo, mock_gerrit_api,
98 mock_shutil, mock_os, mock_path,
101 args = mock_get_parser.parse_args.return_value
102 args.repo = self.repo_name
103 args.url = self.repo_url
104 args.branch = 'master'
105 x = mock_git_repo.return_value
106 xx = x.commit.return_value
107 xx.message = '{}: {}'.format(self.repo_name, self.change_id)
108 mock_path.exists.return_value = True
109 mock_path.isdir.return_value = False
110 y = mock_gerrit_api.return_value
111 y.get.return_value = {'status': 'MERGED',
112 'current_revision': 'revision',
114 {'revision': {'ref': self.commit_id}}}
115 z = mock_git_repo.clone_from.return_value
117 build_utils.clone_fork(args)
119 mock_path.exists.assert_called_with(self.repo_name)
120 mock_os.remove.assert_called_with(self.repo_name)
121 mock_git_repo.clone_from.assert_called_with('{}/{}'.
122 format(self.repo_url,
124 self.repo_name, b='master')
125 z.git.fetch.assert_not_called
126 z.git.checkout.assert_not_called
128 @patch('apex.build_utils.get_parser')
129 @patch('apex.build_utils.GerritRestAPI')
130 @patch('apex.build_utils.git.Repo')
131 def test_clone_fork_invalid_git_repo(self, mock_git_repo,
132 mock_gerrit_api, mock_get_parser):
134 args = mock_get_parser.parse_args.return_value
135 args.repo = self.repo_name
136 args.url = self.repo_url
137 args.branch = 'master'
138 mock_git_repo.side_effect = git.exc.InvalidGitRepositoryError()
139 build_utils.clone_fork(args)
141 @patch('apex.build_utils.get_parser')
142 @patch('apex.build_utils.GerritRestAPI')
143 @patch('apex.build_utils.git.Repo')
144 def test_clone_fork_raises_key_error(self, mock_git_repo,
145 mock_gerrit_api, mock_get_parser):
147 args = mock_get_parser.parse_args.return_value
148 args.repo = self.repo_name
149 args.url = self.repo_url
150 args.branch = 'master'
151 x = mock_git_repo.return_value
152 xx = x.commit.return_value
153 xx.message = '{}: {}'.format(self.repo_name, self.change_id)
154 y = mock_gerrit_api.return_value
155 y.get.return_value = {}
157 assert_raises(KeyError, build_utils.clone_fork, args)
159 def test_get_parser(self):
160 assert_is_instance(build_utils.get_parser(), argparse.ArgumentParser)
162 @patch('apex.build_utils.get_parser')
163 def test_main(self, mock_get_parser):
164 with patch.object(build_utils.sys, 'argv', self.sys_argv):
167 @patch('apex.build_utils.get_parser')
168 def test_main_debug(self, mock_get_parser):
169 with patch.object(build_utils.sys, 'argv', self.sys_argv_debug):
172 def test_strip_patch_sections(self):
173 with open(os.path.join(con.TEST_DUMMY_CONFIG, '98faaca.diff')) as fh:
174 dummy_patch = fh.read()
175 tmp_patch = build_utils.strip_patch_sections(dummy_patch)
176 self.assertNotRegex(tmp_patch, 'releasenotes')
177 self.assertNotRegex(tmp_patch, 'Minor update ODL steps')
178 self.assertNotRegex(tmp_patch, 'Steps of upgrade are as follows')
179 self.assertNotRegex(tmp_patch, 'Steps invlolved in level 2 update')
181 def test_is_path_in_patch(self):
182 with open(os.path.join(con.TEST_DUMMY_CONFIG, '98faaca.diff')) as fh:
183 dummy_patch = fh.read()
184 self.assertTrue(build_utils.is_path_in_patch(dummy_patch,
187 def test_strip_no_patch_sections(self):
188 with open(os.path.join(con.TEST_DUMMY_CONFIG, '98faaca.diff')) as fh:
189 dummy_patch = fh.read()
190 tmp_patch = build_utils.strip_patch_sections(dummy_patch,
192 self.assertEqual(dummy_patch, tmp_patch)