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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
#!/usr/bin/env python3
# -*- mode: python; py-indent-offset: 4; py-continuation-offset: 4 -*-
'''
Tests for the Merge chunk of the Driver script
'''
from __future__ import print_function
import sys
sys.dont_write_bytecode = True
import os
sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import unittest
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
try:
import mock
except ImportError: # pragma nocover
import unittest.mock as mock
from argparse import Namespace
from subprocess import CalledProcessError
import PullRequestLinuxDriverMerge
class Test_header(unittest.TestCase):
'''Test that we can properly echo the header information'''
def test_writeHeader(self):
with mock.patch('sys.stdout', new_callable=StringIO) as m_stdout:
PullRequestLinuxDriverMerge.write_header()
self.assertEqual('''--------------------------------------------------------------------------------
-
- Begin: PullRequestLinuxDriver-Merge.py
-
--------------------------------------------------------------------------------
''',
m_stdout.getvalue())
class Test_EchoJenkinsVars(unittest.TestCase):
'''Test that the Jenkins environment is echoed properly'''
def setUp(self):
self.m_environ = mock.patch.dict(os.environ, {'JOB_BASE_NAME':'TEST_JOB_BASE_NAME',
'JOB_NAME':'TEST_JOB_NAME',
'WORKSPACE':os.path.join(os.sep,
'dev',
'null',
'TEST_WORKSPACE'),
'NODE_NAME':'TEST_NODE_NAME'},
clear=True)
def test_echoJenkinsVars(self):
with self.m_environ:
env_string_io = StringIO()
for key in os.environ:
print(key + ' = ' + os.environ[key],
file=env_string_io)
expected_string = '''
================================================================================
Jenkins Environment Variables:
- WORKSPACE : /dev/null/TEST_WORKSPACE
================================================================================
Environment:
pwd = {cwd}
{environ}
================================================================================
'''.format(cwd=os.getcwd(),
environ=env_string_io.getvalue())
with mock.patch('sys.stdout', new_callable=StringIO) as m_stdout, \
self.m_environ:
PullRequestLinuxDriverMerge.echoJenkinsVars(os.path.join(os.sep,
'dev',
'null',
'TEST_WORKSPACE'))
self.assertEqual(expected_string, m_stdout.getvalue())
class Test_parsing(unittest.TestCase):
'''Finally given a decent parser I want to now pass
all parameters and check them'''
def test_parseArgs(self):
test_namespace = Namespace()
setattr(test_namespace, 'sourceRepo', '/dev/null/source/Trilinos.git')
setattr(test_namespace, 'sourceBranch', 'fake_test_branch_fixing_issue_neverland')
setattr(test_namespace, 'targetRepo', '/dev/null/target/Trilinos.git')
setattr(test_namespace, 'targetBranch', 'fake_develop')
setattr(test_namespace, 'sourceSHA', '0123456789abcdef')
setattr(test_namespace, 'workspaceDir', '/dev/null/workspace')
with mock.patch.object(sys, 'argv', ['programName',
os.path.join(os.path.sep,
'dev',
'null',
'source',
'Trilinos.git'),
'fake_test_branch_fixing_issue_neverland',
os.path.join(os.path.sep,
'dev',
'null',
'target',
'Trilinos.git'),
'fake_develop',
'0123456789abcdef',
os.path.join(os.path.sep,
'dev',
'null',
'workspace')]):
args = PullRequestLinuxDriverMerge.parseArgs()
self.assertEqual(test_namespace, args)
def test_parseInsufficientArgs_fails(self):
test_namespace = Namespace()
setattr(test_namespace, 'sourceRepo', '/dev/null/source/Trilinos.git')
expected_output = '''usage: programName [-h]
sourceRepo sourceBranch targetRepo targetBranch sourceSHA
workspaceDir
programName: error: the following arguments are required: sourceRepo, \
sourceBranch, targetRepo, targetBranch, sourceSHA, workspaceDir
'''
if sys.version_info.major != 3:
expected_output = '''usage: programName [-h]
sourceRepo sourceBranch targetRepo targetBranch sourceSHA
workspaceDir\nprogramName: error: too few arguments
'''
with mock.patch.object(sys, 'argv', ['programName']), \
mock.patch('sys.stderr', new_callable=StringIO) as m_stderr:
if sys.version_info.major != 3:
with self.assertRaisesRegexp(SystemExit, '2'):
PullRequestLinuxDriverMerge.parseArgs()
else:
with self.assertRaisesRegex(SystemExit, '2'):
PullRequestLinuxDriverMerge.parseArgs()
self.assertEqual(expected_output, m_stderr.getvalue())
class Test_mergeBranch(unittest.TestCase):
'''Verify that we call the correct sequence to merge the source branch/SHA
into the target branch'''
def test_mergeBranch_without_source_remote(self):
with mock.patch('subprocess.check_output',
side_effect=['origin /dev/null/target/Trilinos',
'df324ae']) as m_check_out, \
mock.patch('subprocess.check_call') as m_check_call:
PullRequestLinuxDriverMerge.merge_branch(os.path.join(os.path.sep,
'dev',
'null',
'source',
'Trilinos.git'),
'neverland',
'fake_develop',
'df324ae')
m_check_out.assert_has_calls([mock.call(['git', 'remote', '-v']),
mock.call(['git', 'rev-parse',
'source_remote/neverland'])])
m_check_call.assert_has_calls([mock.call(['git', 'remote', 'add',
'source_remote',
'/dev/null/source/Trilinos.git']),
mock.call(['git', 'fetch', 'source_remote',
'neverland']),
mock.call(['git', 'fetch', 'origin',
'fake_develop']),
mock.call(['git', 'reset', '--hard',
'HEAD']),
mock.call(['git', 'checkout',
'-B', 'fake_develop',
'origin/fake_develop']),
mock.call(['git', 'merge',
'--no-edit',
'source_remote/neverland']),
])
def test_mergeBranch_with_source_remote(self):
with mock.patch('subprocess.check_output',
side_effect=['''origin /dev/null/target/Trilinos
source_remote /dev/null/source12/Trilinos.git''', 'df324ae']) as m_check_out, \
mock.patch('subprocess.check_call') as m_check_call, \
mock.patch('sys.stdout', new_callable=StringIO) as m_stdout:
PullRequestLinuxDriverMerge.merge_branch(os.path.join(os.path.sep,
'dev',
'null',
'source',
'Trilinos.git'),
'neverland',
'fake_develop',
'df324ae')
m_check_out.assert_has_calls([mock.call(['git', 'remote', '-v']),
mock.call(['git', 'rev-parse',
'source_remote/neverland'])])
m_check_call.assert_has_calls([mock.call(['git', 'remote', 'rm',
'source_remote']),
mock.call(['git', 'remote', 'add',
'source_remote',
'/dev/null/source/Trilinos.git']),
mock.call(['git', 'fetch', 'source_remote',
'neverland']),
mock.call(['git', 'fetch', 'origin',
'fake_develop']),
mock.call(['git', 'reset', '--hard',
'HEAD']),
mock.call(['git', 'checkout',
'-B', 'fake_develop',
'origin/fake_develop']),
mock.call(['git', 'merge',
'--no-edit',
'source_remote/neverland']),
])
self.assertEqual("git remote exists, removing it\n", m_stdout.getvalue())
def test_mergeBranch_fails_on_source_fetch(self):
with mock.patch('subprocess.check_output',
side_effect=['origin /dev/null/target/Trilinos',
'df324ae']) as m_check_out, \
mock.patch('subprocess.check_call',
side_effect=[None,
CalledProcessError(-1, 'cmd'),
CalledProcessError(-2, 'cmd'),
CalledProcessError(-3, 'cmd')]) as m_check_call:
if sys.version_info.major != 3:
with self.assertRaisesRegexp(SystemExit, '12'):
PullRequestLinuxDriverMerge.merge_branch(os.path.join(os.path.sep,
'dev',
'null',
'source',
'Trilinos.git'),
'neverland',
'fake_develop',
'df324ae')
else:
with self.assertRaisesRegex(SystemExit, '12'):
PullRequestLinuxDriverMerge.merge_branch(os.path.join(os.path.sep,
'dev',
'null',
'source',
'Trilinos.git'),
'neverland',
'fake_develop',
'df324ae')
m_check_out.assert_has_calls([mock.call(['git', 'remote', '-v'])])
m_check_call.assert_has_calls([mock.call(['git', 'remote', 'add',
'source_remote',
'/dev/null/source/Trilinos.git']),
mock.call(['git', 'fetch', 'source_remote',
'neverland']),
mock.call(['git', 'fetch', 'source_remote',
'neverland']),
mock.call(['git', 'fetch', 'source_remote',
'neverland']),
])
def test_mergeBranch_fails_on_SHA_mismatch(self):
with mock.patch('subprocess.check_output',
side_effect=['origin /dev/null/target/Trilinos',
'df324ae']) as m_check_out, \
mock.patch('subprocess.check_call') as m_check_call, \
mock.patch('sys.stdout', new_callable=StringIO) as m_stdout:
if sys.version_info.major != 3:
with self.assertRaisesRegexp(SystemExit, '-1'):
PullRequestLinuxDriverMerge.merge_branch(os.path.join(os.path.sep,
'dev',
'null',
'source',
'Trilinos.git'),
'neverland',
'fake_develop',
'foobar')
else:
with self.assertRaisesRegex(SystemExit, '-1'):
PullRequestLinuxDriverMerge.merge_branch(os.path.join(os.path.sep,
'dev',
'null',
'source',
'Trilinos.git'),
'neverland',
'fake_develop',
'foobar')
m_check_out.assert_has_calls([mock.call(['git', 'remote', '-v']),
mock.call(['git', 'rev-parse',
'source_remote/neverland'])])
m_check_call.assert_has_calls([mock.call(['git', 'remote', 'add',
'source_remote',
'/dev/null/source/Trilinos.git']),
mock.call(['git', 'fetch', 'source_remote',
'neverland']),
])
self.assertEqual('''The SHA (df324ae) for the last commit on branch neverland
in repo /dev/null/source/Trilinos.git is different than the expected SHA,
which is: foobar.\n''',
m_stdout.getvalue())
class Test_run(unittest.TestCase):
'''This is the main function that ties everything together in order'''
def test_run(self):
with mock.patch('PullRequestLinuxDriverMerge.parseArgs') as m_parser, \
mock.patch('os.chdir') as m_chdir, \
mock.patch('PullRequestLinuxDriverMerge.write_header') as m_writeHeader, \
mock.patch('PullRequestLinuxDriverMerge.echoJenkinsVars') as m_echoJenkins, \
mock.patch('PullRequestLinuxDriverMerge.merge_branch') as m_mergeBranch, \
mock.patch('os.path.join') as m_join, \
mock.patch('sys.stdout', new_callable=StringIO) as m_stdout:
self.assertTrue(PullRequestLinuxDriverMerge.run())
m_parser.assert_called_once_with()
m_chdir.assert_called_once_with(m_join(m_parser().workspaceDir, 'Trilinos'))
m_writeHeader.assert_called_once_with()
m_echoJenkins.assert_called_once_with(m_parser().workspaceDir)
m_mergeBranch.assert_called_once_with(m_parser().sourceRepo,
m_parser().sourceBranch,
m_parser().targetBranch,
m_parser().sourceSHA)
self.assertEqual('Set CWD = ' + str(m_join()) + '\n',
m_stdout.getvalue())
def test_run_fails_on_bad_parse(self):
with mock.patch('PullRequestLinuxDriverMerge.parseArgs',
side_effect=SystemExit(2)):
self.assertFalse(PullRequestLinuxDriverMerge.run())
def test_run_fails_on_bad_fetch(self):
with mock.patch('subprocess.check_output',
side_effect=['origin /dev/null/target/Trilinos',
'df324ae']), \
mock.patch('subprocess.check_call',
side_effect=[None,
CalledProcessError(-1, 'cmd'),
CalledProcessError(-2, 'cmd'),
CalledProcessError(-3, 'cmd')]), \
mock.patch('PullRequestLinuxDriverMerge.parseArgs'), \
mock.patch('sys.stdout', new_callable=StringIO), \
mock.patch('os.path.join'), \
mock.patch('os.chdir'):
self.assertFalse(PullRequestLinuxDriverMerge.run())
def test_run_fails_on_bad_remote_add(self):
expected_string = '''Recieved subprocess.CalledProcessError - returned -1
from command test_cmd
output None
stdout None
stderr None\n'''
if sys.version_info.major != 3:
expected_string = '''Recieved subprocess.CalledProcessError - returned -1
from command test_cmd
output None\n'''
with mock.patch('subprocess.check_output',
side_effect=['origin /dev/null/target/Trilinos',
'df324ae']), \
mock.patch('subprocess.check_call',
side_effect=CalledProcessError(-1, 'test_cmd')), \
mock.patch('PullRequestLinuxDriverMerge.parseArgs'), \
mock.patch('sys.stdout', new_callable=StringIO) as m_stdout, \
mock.patch('os.path.join'), \
mock.patch('os.chdir'):
self.assertFalse(PullRequestLinuxDriverMerge.run())
self.assertTrue(m_stdout.getvalue().endswith(expected_string))
if __name__ == '__main__':
unittest.main() # pragma nocover
|