Changeset View
Changeset View
Standalone View
Standalone View
contrib/buildbot/test/test_endpoint_triggerCI.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# | # | ||||
# Copyright (c) 2017-2020 The Bitcoin ABC developers | # Copyright (c) 2017-2020 The Bitcoin ABC developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
import itertools | import itertools | ||||
import json | |||||
import test.mocks.phabricator | import test.mocks.phabricator | ||||
import test.mocks.teamcity | import test.mocks.teamcity | ||||
import unittest | import unittest | ||||
from test.abcbot_fixture import ABCBotFixture | from test.abcbot_fixture import ABCBotFixture | ||||
from unittest.mock import call | from unittest.mock import call | ||||
import mock | import mock | ||||
from phabricator_wrapper import BITCOIN_ABC_PROJECT_PHID | from phabricator_wrapper import BITCOIN_ABC_PROJECT_PHID | ||||
Show All 22 Lines | def setUp(self): | ||||
{ | { | ||||
"phid": self.user_PHID | "phid": self.user_PHID | ||||
}, | }, | ||||
] | ] | ||||
} | } | ||||
} | } | ||||
}]) | }]) | ||||
self.phab.user.search.return_value = test.mocks.phabricator.Result([ | |||||
{ | |||||
"id": 1, | |||||
"type": "USER", | |||||
"phid": "PHID-AUTHORIZED-USER", | |||||
"fields": { | |||||
"roles": [ | |||||
"verified", | |||||
"approved", | |||||
"activated", | |||||
], | |||||
}, | |||||
}, | |||||
]) | |||||
# Phabricator returns the default diff ID | # Phabricator returns the default diff ID | ||||
self.phab.differential.diff.search.return_value = test.mocks.phabricator.Result([{ | self.phab.differential.diff.search.return_value = test.mocks.phabricator.Result([{ | ||||
"id": self.diff_id, | "id": self.diff_id, | ||||
}]) | }]) | ||||
config = { | |||||
"builds": { | |||||
"build-1": {}, | |||||
"build-11": {}, | |||||
"build-12": {}, | |||||
"build-13": {}, | |||||
"build-2": {}, | |||||
"build-21": {}, | |||||
"build-22": {}, | |||||
"build-23": {}, | |||||
"build-3": {}, | |||||
"build-31": {}, | |||||
"build-32": {}, | |||||
"build-33": {}, | |||||
"build-docker": {"docker": {}}, | |||||
}, | |||||
} | |||||
self.phab.get_file_content_from_master = mock.Mock() | |||||
self.phab.get_file_content_from_master.return_value = json.dumps( | |||||
config) | |||||
# Transaction webhook on diff update | # Transaction webhook on diff update | ||||
def call_endpoint(self): | def call_endpoint(self): | ||||
webhook_transaction = { | webhook_transaction = { | ||||
"object": { | "object": { | ||||
"phid": self.revision_PHID, | "phid": self.revision_PHID, | ||||
"type": "DREV", | "type": "DREV", | ||||
}, | }, | ||||
"transactions": [ | "transactions": [ | ||||
▲ Show 20 Lines • Show All 189 Lines • ▼ Show 20 Lines | def test_triggerCI_no_build_queued(self): | ||||
"@bot build-31 build-32 build-33", | "@bot build-31 build-32 build-33", | ||||
], | ], | ||||
"PHID-USER-nonabc" | "PHID-USER-nonabc" | ||||
) | ) | ||||
response = self.call_endpoint() | response = self.call_endpoint() | ||||
self.teamcity.session.send.assert_not_called() | self.teamcity.session.send.assert_not_called() | ||||
self.assertEqual(response.status_code, 200) | self.assertEqual(response.status_code, 200) | ||||
# Authorized but non-ABC user, running at least one non-existent build | |||||
self.set_transaction_return_value( | |||||
[ | |||||
# Build 4 doesn't exist | |||||
"@bot build-4", | |||||
"@bot build-4 build-11 build-12 build-13 build-2 build-3", | |||||
"@bot build-11 build-12 build-13 build-2 build-3 build-4", | |||||
], | |||||
"PHID-AUTHORIZED-USER" | |||||
) | |||||
response = self.call_endpoint() | |||||
self.teamcity.session.send.assert_not_called() | |||||
self.assertEqual(response.status_code, 200) | |||||
# Authorized but non-ABC user, running at least one docker build | |||||
self.set_transaction_return_value( | |||||
[ | |||||
"@bot build-docker", | |||||
"@bot build-docker build-11 build-12 build-13 build-2 build-3", | |||||
"@bot build-11 build-12 build-13 build-2 build-3 build-docker", | |||||
], | |||||
"PHID-AUTHORIZED-USER" | |||||
) | |||||
response = self.call_endpoint() | |||||
self.teamcity.session.send.assert_not_called() | |||||
self.assertEqual(response.status_code, 200) | |||||
def test_triggerCI_some_build_queued(self): | def test_triggerCI_some_build_queued(self): | ||||
def assert_teamcity_queued_builds(comments, queued_builds): | def assert_teamcity_queued_builds(comments, queued_builds): | ||||
# Default user is an ABC member in set_transaction_return_value | |||||
self.set_transaction_return_value(comments) | self.set_transaction_return_value(comments) | ||||
response = self.call_endpoint() | response = self.call_endpoint() | ||||
expected_calls = [ | expected_calls = [ | ||||
call( | call( | ||||
"BitcoinABC_BitcoinAbcStaging", | "BitcoinABC_BitcoinAbcStaging", | ||||
f"refs/tags/phabricator/diff/{self.diff_id}", | f"refs/tags/phabricator/diff/{self.diff_id}", | ||||
properties=[{ | properties=[{ | ||||
'name': 'env.ABC_BUILD_NAME', | 'name': 'env.ABC_BUILD_NAME', | ||||
'value': build_id, | 'value': build_id, | ||||
}] | }] | ||||
) | ) | ||||
for build_id in queued_builds | for build_id in queued_builds | ||||
] | ] | ||||
print(expected_calls) | |||||
self.teamcity.trigger_build.assert_has_calls( | self.teamcity.trigger_build.assert_has_calls( | ||||
expected_calls, any_order=True) | expected_calls, any_order=True) | ||||
self.assertEqual(response.status_code, 200) | self.assertEqual(response.status_code, 200) | ||||
# Authorized user, 1 comment targeting the bot with 1 build | # ABC user, 1 comment targeting the bot with 1 build | ||||
assert_teamcity_queued_builds( | assert_teamcity_queued_builds( | ||||
[ | [ | ||||
"@bot build-1", | "@bot build-1", | ||||
], | ], | ||||
[ | [ | ||||
"build-1", | "build-1", | ||||
] | ] | ||||
) | ) | ||||
# Authorized user, 1 comment targeting the bot with 3 builds | # ABC user, 1 comment targeting the bot with 3 builds | ||||
assert_teamcity_queued_builds( | assert_teamcity_queued_builds( | ||||
[ | [ | ||||
"@bot build-1 build-2 build-3", | "@bot build-1 build-2 build-3", | ||||
], | ], | ||||
[ | [ | ||||
"build-1", | "build-1", | ||||
"build-2", | "build-2", | ||||
"build-3", | "build-3", | ||||
] | ] | ||||
) | ) | ||||
# Authorized user, 3 comments targeting the bot with 3 builds each | # ABC user, 3 comments targeting the bot with 3 builds each | ||||
assert_teamcity_queued_builds( | assert_teamcity_queued_builds( | ||||
[ | [ | ||||
"@bot build-11 build-12 build-13", | "@bot build-11 build-12 build-13", | ||||
"@bot build-21 build-22 build-23", | "@bot build-21 build-22 build-23", | ||||
"@bot build-31 build-32 build-33", | "@bot build-31 build-32 build-33", | ||||
], | ], | ||||
[ | [ | ||||
"build-11", "build-12", "build-13", | "build-11", "build-12", "build-13", | ||||
"build-21", "build-22", "build-23", | "build-21", "build-22", "build-23", | ||||
"build-31", "build-32", "build-33", | "build-31", "build-32", "build-33", | ||||
] | ] | ||||
) | ) | ||||
# Authorized user, 1 comment targeting the bot with duplicated builds | # ABC user, 1 comment targeting the bot with duplicated builds | ||||
assert_teamcity_queued_builds( | assert_teamcity_queued_builds( | ||||
[ | [ | ||||
"@bot build-1 build-2 build-1 build-3 build-2", | "@bot build-1 build-2 build-1 build-3 build-2", | ||||
], | ], | ||||
[ | [ | ||||
"build-1", | "build-1", | ||||
"build-2", | "build-2", | ||||
"build-3", | "build-3", | ||||
] | ] | ||||
) | ) | ||||
# ABC user, some comments targeting the bot with 3 builds involving docker | |||||
assert_teamcity_queued_builds( | |||||
[ | |||||
"@bot build-docker build-1 build-2 build-3", | |||||
], | |||||
[ | |||||
"build-docker", "build-1", "build-2", "build-3", | |||||
] | |||||
) | |||||
assert_teamcity_queued_builds( | |||||
[ | |||||
"@bot build-1 build-2 build-docker build-3", | |||||
], | |||||
[ | |||||
"build-1", "build-2", "build-docker", "build-3", | |||||
] | |||||
) | |||||
assert_teamcity_queued_builds( | |||||
[ | |||||
"@bot build-1 build-2 build-3 build-docker", | |||||
], | |||||
[ | |||||
"build-1", "build-2", "build-3", "build-docker", | |||||
] | |||||
) | |||||
assert_teamcity_queued_builds( | |||||
[ | |||||
"@bot build-docker build-1 build-docker build-2 build-docker build-3 build-docker", | |||||
], | |||||
[ | |||||
"build-docker", "build-1", "build-2", "build-3", | |||||
] | |||||
) | |||||
def test_triggerCI_check_user_roles(self): | def test_triggerCI_check_user_roles(self): | ||||
user_PHID = "PHID-USER-notabc" | user_PHID = "PHID-USER-notabc" | ||||
# No build triggered, exit status OK | # No build triggered, exit status OK | ||||
def check_build_triggered(expect_trigger): | def check_build_triggered(expect_trigger): | ||||
self.teamcity.trigger_build.reset_mock() | self.teamcity.trigger_build.reset_mock() | ||||
self.set_transaction_return_value(["@bot build-1"], user_PHID) | self.set_transaction_return_value(["@bot build-1"], user_PHID) | ||||
response = self.call_endpoint() | response = self.call_endpoint() | ||||
▲ Show 20 Lines • Show All 107 Lines • Show Last 20 Lines |