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 ##############################################################################
13 from mock import patch
15 from apex import build_utils
17 from nose.tools import (
22 class TestBuildUtils(object):
25 """This method is run once for each class before any tests are run"""
26 cls.repo_name = 'test_repo'
27 cls.repo_url = 'https://gerrit.opnfv.org/gerrit'
28 cls.change_id = 'I5c1b3ded249c4e3c558be683559e03deb27721b8'
29 cls.commit_id = '8669c687a75a00106b055add49b82fee826b8fe8'
30 cls.sys_argv = ['deploy.py', 'clone-fork', '-r', cls.repo_name]
31 cls.sys_argv_debug = ['deploy.py', '--debug']
34 def teardown_class(cls):
35 """This method is run once for each class _after_ all tests are run"""
38 """This method is run once before _each_ test method is executed"""
41 """This method is run once after _each_ test method is executed"""
43 def test_main_wo_func_w_debug(self):
44 with patch.object(build_utils.sys, 'argv', self.sys_argv_debug):
45 # no func argument (clone-fork) throws sys exit
46 assert_raises(SystemExit, build_utils.main)
48 @patch('apex.build_utils.get_parser')
49 @patch('apex.build_utils.os.path')
50 @patch('apex.build_utils.os')
51 @patch('apex.build_utils.shutil')
52 @patch('apex.build_utils.GerritRestAPI')
53 @patch('apex.build_utils.git.Repo')
54 def test_clone_fork(self, mock_git_repo, mock_gerrit_api,
55 mock_shutil, mock_os, mock_path, mock_get_parser):
57 args = mock_get_parser.parse_args.return_value
58 args.repo = self.repo_name
59 args.url = self.repo_url
60 args.branch = 'master'
61 x = mock_git_repo.return_value
62 xx = x.commit.return_value
63 xx.message = '{}: {}'.format(self.repo_name, self.change_id)
64 mock_path.exists.return_value = True
65 mock_path.isdir.return_value = True
66 y = mock_gerrit_api.return_value
67 y.get.return_value = {'status': 'TEST',
68 'current_revision': 'revision',
70 {'revision': {'ref': self.commit_id}}}
71 z = mock_git_repo.clone_from.return_value
73 build_utils.clone_fork(args)
75 mock_path.exists.assert_called_with(self.repo_name)
76 mock_path.isdir.assert_called_with(self.repo_name)
77 mock_shutil.rmtree.assert_called_with(self.repo_name)
78 mock_git_repo.clone_from.assert_called_with('{}/{}'.
83 z.git.fetch.assert_called_with('{}/{}'.format(self.repo_url,
86 z.git.checkout.assert_called_with('FETCH_HEAD')
88 @patch('apex.build_utils.get_parser')
89 @patch('apex.build_utils.os.path')
90 @patch('apex.build_utils.os')
91 @patch('apex.build_utils.shutil')
92 @patch('apex.build_utils.GerritRestAPI')
93 @patch('apex.build_utils.git.Repo')
94 def test_clone_fork_MERGED(self, mock_git_repo, mock_gerrit_api,
95 mock_shutil, mock_os, mock_path,
98 args = mock_get_parser.parse_args.return_value
99 args.repo = self.repo_name
100 args.url = self.repo_url
101 args.branch = 'master'
102 x = mock_git_repo.return_value
103 xx = x.commit.return_value
104 xx.message = '{}: {}'.format(self.repo_name, self.change_id)
105 mock_path.exists.return_value = True
106 mock_path.isdir.return_value = False
107 y = mock_gerrit_api.return_value
108 y.get.return_value = {'status': 'MERGED',
109 'current_revision': 'revision',
111 {'revision': {'ref': self.commit_id}}}
112 z = mock_git_repo.clone_from.return_value
114 build_utils.clone_fork(args)
116 mock_path.exists.assert_called_with(self.repo_name)
117 mock_os.remove.assert_called_with(self.repo_name)
118 mock_git_repo.clone_from.assert_called_with('{}/{}'.
119 format(self.repo_url,
121 self.repo_name, b='master')
122 z.git.fetch.assert_not_called
123 z.git.checkout.assert_not_called
125 @patch('apex.build_utils.get_parser')
126 @patch('apex.build_utils.GerritRestAPI')
127 @patch('apex.build_utils.git.Repo')
128 def test_clone_fork_invalid_git_repo(self, mock_git_repo,
129 mock_gerrit_api, mock_get_parser):
131 args = mock_get_parser.parse_args.return_value
132 args.repo = self.repo_name
133 args.url = self.repo_url
134 args.branch = 'master'
135 mock_git_repo.side_effect = git.exc.InvalidGitRepositoryError()
136 build_utils.clone_fork(args)
138 @patch('apex.build_utils.get_parser')
139 @patch('apex.build_utils.GerritRestAPI')
140 @patch('apex.build_utils.git.Repo')
141 def test_clone_fork_raises_key_error(self, mock_git_repo,
142 mock_gerrit_api, mock_get_parser):
144 args = mock_get_parser.parse_args.return_value
145 args.repo = self.repo_name
146 args.url = self.repo_url
147 args.branch = 'master'
148 x = mock_git_repo.return_value
149 xx = x.commit.return_value
150 xx.message = '{}: {}'.format(self.repo_name, self.change_id)
151 y = mock_gerrit_api.return_value
152 y.get.return_value = {}
154 assert_raises(KeyError, build_utils.clone_fork, args)
156 def test_get_parser(self):
157 assert_is_instance(build_utils.get_parser(), argparse.ArgumentParser)
159 @patch('apex.build_utils.get_parser')
160 def test_main(self, mock_get_parser):
161 with patch.object(build_utils.sys, 'argv', self.sys_argv):
164 @patch('apex.build_utils.get_parser')
165 def test_main_debug(self, mock_get_parser):
166 with patch.object(build_utils.sys, 'argv', self.sys_argv_debug):