1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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()
|