Enables containerized overcloud deployments
[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 import os
13 import unittest
14
15 from mock import patch
16
17 from apex import build_utils
18 from apex.tests import constants as con
19
20 from nose.tools import (
21     assert_is_instance,
22     assert_raises)
23
24
25 class TestBuildUtils(unittest.TestCase):
26     @classmethod
27     def setup_class(cls):
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']
35
36     @classmethod
37     def teardown_class(cls):
38         """This method is run once for each class _after_ all tests are run"""
39
40     def setup(self):
41         """This method is run once before _each_ test method is executed"""
42
43     def teardown(self):
44         """This method is run once after _each_ test method is executed"""
45
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)
50
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):
59         # setup mock
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',
72                               'revisions':
73                                   {'revision': {'ref': self.commit_id}}}
74         z = mock_git_repo.clone_from.return_value
75         # execute
76         build_utils.clone_fork(args)
77         # check results
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('{}/{}'.
82                                                     format(self.repo_url,
83                                                            self.repo_name),
84                                                     self.repo_name,
85                                                     b='master')
86         z.git.fetch.assert_called_with('{}/{}'.format(self.repo_url,
87                                                       self.repo_name),
88                                        self.commit_id)
89         z.git.checkout.assert_called_with('FETCH_HEAD')
90
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,
99                                mock_get_parser):
100         # setup mock
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',
113                               'revisions':
114                                   {'revision': {'ref': self.commit_id}}}
115         z = mock_git_repo.clone_from.return_value
116         # execute
117         build_utils.clone_fork(args)
118         # check results
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,
123                                                            self.repo_name),
124                                                     self.repo_name, b='master')
125         z.git.fetch.assert_not_called
126         z.git.checkout.assert_not_called
127
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):
133         # setup mock
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)
140
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):
146         # setup mock
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 = {}
156         # execute & assert
157         assert_raises(KeyError, build_utils.clone_fork, args)
158
159     def test_get_parser(self):
160         assert_is_instance(build_utils.get_parser(), argparse.ArgumentParser)
161
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):
165             build_utils.main()
166
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):
170             build_utils.main()
171
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')
180
181     def test_strip_no_patch_sections(self):
182         with open(os.path.join(con.TEST_DUMMY_CONFIG, '98faaca.diff')) as fh:
183             dummy_patch = fh.read()
184         tmp_patch = build_utils.strip_patch_sections(dummy_patch,
185                                                      sections=[])
186         self.assertEqual(dummy_patch, tmp_patch)