d9d542d64e7f9447d909ddc135b96c9e48cf317f
[apex.git] / apex / tests / test_apex_build_utils.py
1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (dradez@redhat.com) (Red Hat)
3 #
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 ##############################################################################
9
10 import argparse
11 import git
12
13 from mock import patch
14
15 from apex import build_utils
16
17 from nose.tools import (
18     assert_is_instance,
19     assert_raises)
20
21
22 class TestBuildUtils(object):
23     @classmethod
24     def setup_class(cls):
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']
32
33     @classmethod
34     def teardown_class(cls):
35         """This method is run once for each class _after_ all tests are run"""
36
37     def setup(self):
38         """This method is run once before _each_ test method is executed"""
39
40     def teardown(self):
41         """This method is run once after _each_ test method is executed"""
42
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)
47
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):
56         # setup mock
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',
69                               'revisions':
70                                   {'revision': {'ref': self.commit_id}}}
71         z = mock_git_repo.clone_from.return_value
72         # execute
73         build_utils.clone_fork(args)
74         # check results
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('{}/{}'.
79                                                     format(self.repo_url,
80                                                            self.repo_name),
81                                                     self.repo_name,
82                                                     b='master')
83         z.git.fetch.assert_called_with('{}/{}'.format(self.repo_url,
84                                                       self.repo_name),
85                                        self.commit_id)
86         z.git.checkout.assert_called_with('FETCH_HEAD')
87
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,
96                                mock_get_parser):
97         # setup mock
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',
110                               'revisions':
111                                   {'revision': {'ref': self.commit_id}}}
112         z = mock_git_repo.clone_from.return_value
113         # execute
114         build_utils.clone_fork(args)
115         # check results
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,
120                                                            self.repo_name),
121                                                     self.repo_name, b='master')
122         z.git.fetch.assert_not_called
123         z.git.checkout.assert_not_called
124
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):
130         # setup mock
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)
137
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):
143         # setup mock
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 = {}
153         # execute & assert
154         assert_raises(KeyError, build_utils.clone_fork, args)
155
156     def test_get_parser(self):
157         assert_is_instance(build_utils.get_parser(), argparse.ArgumentParser)
158
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):
162             build_utils.main()
163
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):
167             build_utils.main()