Changeset View
Changeset View
Standalone View
Standalone View
contrib/buildbot/test/test_endpoint_buildDiff.py
Show All 26 Lines | def __str__(self): | ||||
return "?{}".format("&".join("{}={}".format(key, value) | return "?{}".format("&".join("{}={}".format(key, value) | ||||
for key, value in self.__dict__.items())) | for key, value in self.__dict__.items())) | ||||
class EndpointBuildDiffTestCase(ABCBotFixture): | class EndpointBuildDiffTestCase(ABCBotFixture): | ||||
def test_buildDiff(self): | def test_buildDiff(self): | ||||
data = buildDiffRequestQuery() | data = buildDiffRequestQuery() | ||||
def set_build_configuration(builds): | def set_build_configuration(buildConfig): | ||||
# add some build configs that we expect to always be skipped | |||||
mergedConfig = dict() | |||||
mergedConfig.update({ | |||||
"build-skip-1": { | |||||
"runOnDiff": False, | |||||
}, | |||||
"build-skip-2": {}, | |||||
}) | |||||
mergedConfig.update(buildConfig) | |||||
config = { | config = { | ||||
"builds": { | "builds": mergedConfig, | ||||
} | |||||
} | |||||
for build in builds: | |||||
config["builds"][build.name] = { | |||||
"runOnDiff": True | |||||
} | } | ||||
self.phab.get_file_content_from_master = mock.Mock() | self.phab.get_file_content_from_master = mock.Mock() | ||||
self.phab.get_file_content_from_master.return_value = json.dumps( | self.phab.get_file_content_from_master.return_value = json.dumps( | ||||
config) | config) | ||||
def call_buildDiff(builds): | def call_buildDiff(expectedBuilds): | ||||
self.teamcity.session.send.side_effect = [ | self.teamcity.session.send.side_effect = [ | ||||
test.mocks.teamcity.buildInfo(build_id=build.build_id, buildqueue=True) for build in builds | test.mocks.teamcity.buildInfo(build_id=build.build_id, buildqueue=True) for build in expectedBuilds | ||||
] | ] | ||||
self.phab.differential.getcommitpaths = mock.Mock() | |||||
self.phab.differential.getcommitpaths.return_value = { | |||||
"0": "dir/subdir/file.h", | |||||
"1": "dir/subdir/file.cpp", | |||||
"2": "someotherdir/file2.txt", | |||||
} | |||||
response = self.app.post( | response = self.app.post( | ||||
'/buildDiff{}'.format(data), | '/buildDiff{}'.format(data), | ||||
headers=self.headers) | headers=self.headers) | ||||
self.assertEqual(response.status_code, 200) | self.assertEqual(response.status_code, 200) | ||||
self.phab.differential.getcommitpaths.assert_called() | |||||
self.phab.get_file_content_from_master.assert_called() | self.phab.get_file_content_from_master.assert_called() | ||||
expected_calls = [ | expected_calls = [ | ||||
call(AnyWith(requests.PreparedRequest, { | call(AnyWith(requests.PreparedRequest, { | ||||
"url": "https://teamcity.test/app/rest/buildQueue", | "url": "https://teamcity.test/app/rest/buildQueue", | ||||
"body": json.dumps({ | "body": json.dumps({ | ||||
"branchName": data.stagingRef, | "branchName": data.stagingRef, | ||||
"buildType": { | "buildType": { | ||||
Show All 12 Lines | def test_buildDiff(self): | ||||
{ | { | ||||
'name': 'env.harborMasterTargetPHID', | 'name': 'env.harborMasterTargetPHID', | ||||
'value': data.targetPHID, | 'value': data.targetPHID, | ||||
}, | }, | ||||
], | ], | ||||
}, | }, | ||||
}), | }), | ||||
})) | })) | ||||
for build in builds | for build in expectedBuilds | ||||
] | ] | ||||
self.teamcity.session.send.assert_has_calls( | self.teamcity.session.send.assert_has_calls( | ||||
expected_calls, any_order=True) | expected_calls, any_order=True) | ||||
self.teamcity.session.send.reset_mock() | self.teamcity.session.send.reset_mock() | ||||
# No diff to run | # No diff to run | ||||
builds = [] | builds = [] | ||||
set_build_configuration(builds) | set_build_configuration({}) | ||||
call_buildDiff(builds) | call_buildDiff(builds) | ||||
self.teamcity.session.send.assert_not_called() | self.teamcity.session.send.assert_not_called() | ||||
# Single diff | # Single diff build | ||||
builds.append(Build(1, BuildStatus.Queued, "build-1")) | builds.append(Build(1, BuildStatus.Queued, "build-1")) | ||||
set_build_configuration(builds) | set_build_configuration({ | ||||
"build-1": { | |||||
"runOnDiff": True, | |||||
}, | |||||
}) | |||||
call_buildDiff(builds) | |||||
# With matching file regex | |||||
set_build_configuration({ | |||||
"build-1": { | |||||
"runOnDiffRegex": "dir/subdir/.*", | |||||
}, | |||||
}) | |||||
call_buildDiff(builds) | call_buildDiff(builds) | ||||
# With non-matching file regex | |||||
set_build_configuration({ | |||||
"build-1": { | |||||
"runOnDiffRegex": "dir/nonmatching/.*", | |||||
}, | |||||
}) | |||||
call_buildDiff([]) | |||||
# Some builds match the file regex | |||||
builds.append(Build(1, BuildStatus.Queued, "build-2")) | |||||
set_build_configuration({ | |||||
"build-1": { | |||||
"runOnDiffRegex": "dir/nonmatching/.*", | |||||
}, | |||||
"build-2": { | |||||
"runOnDiffRegex": "someotherdir/file2.txt", | |||||
}, | |||||
}) | |||||
call_buildDiff([builds[1]]) | |||||
# Lot of builds | # Lot of builds | ||||
builds = [Build(i, BuildStatus.Queued, "build-{}".format(i)) | builds = [Build(i, BuildStatus.Queued, "build-{}".format(i)) | ||||
for i in range(10)] | for i in range(10)] | ||||
set_build_configuration(builds) | buildConfig = {} | ||||
for build in builds: | |||||
buildConfig[build.name] = { | |||||
"runOnDiff": True, | |||||
} | |||||
set_build_configuration(buildConfig) | |||||
call_buildDiff(builds) | call_buildDiff(builds) | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
unittest.main() | unittest.main() |