| # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| """Unittests for RelevantChanges.""" |
| |
| from __future__ import print_function |
| |
| from chromite.cbuildbot import chromeos_config |
| from chromite.cbuildbot import relevant_changes |
| from chromite.lib import clactions |
| from chromite.lib import constants |
| from chromite.lib import fake_cidb |
| from chromite.lib import patch_unittest |
| |
| |
| # pylint: disable=protected-access |
| class RelevantChangesTest(patch_unittest.MockPatchBase): |
| """Tests for RelevantChanges.""" |
| |
| def setUp(self): |
| self._bot_id = 'master-paladin' |
| self.site_config = chromeos_config.GetConfig() |
| self.build_config = self.site_config[self._bot_id] |
| |
| self.fake_cidb = fake_cidb.FakeCIDBConnection() |
| self.master_build_id = self.fake_cidb.InsertBuild( |
| self._bot_id, 'chromeos', '1', self._bot_id, 'bot_hostname') |
| |
| def test_GetSlaveMappingAndCLActions(self): |
| """Tests _GetSlaveMappingAndCLActions.""" |
| changes = set(self.GetPatches(how_many=4)) |
| slave_config = 'test-paladin' |
| test_build_id = self.fake_cidb.InsertBuild( |
| slave_config, 'chromeos', '2', slave_config, 'bot_hostname', |
| master_build_id=self.master_build_id, buildbucket_id='bb_id_1') |
| |
| for change in changes: |
| self.fake_cidb.InsertCLActions( |
| test_build_id, [clactions.CLAction.FromGerritPatchAndAction( |
| change, constants.CL_ACTION_PICKED_UP)]) |
| |
| config_map, action_history = ( |
| relevant_changes.RelevantChanges._GetSlaveMappingAndCLActions( |
| self.master_build_id, self.fake_cidb, self.build_config, |
| changes, ['bb_id_1'])) |
| |
| expected_config_map = { |
| self.master_build_id: self._bot_id, |
| test_build_id: slave_config |
| } |
| |
| self.assertDictEqual(config_map, expected_config_map) |
| self.assertEqual(len(action_history), 4) |
| |
| def testGetRelevantChangesForSlaves(self): |
| """Tests the logic of GetRelevantChangesForSlaves().""" |
| change_set1 = set(self.GetPatches(how_many=2)) |
| change_set2 = set(self.GetPatches(how_many=3)) |
| changes = set.union(change_set1, change_set2) |
| no_stat = ['no_stat-paladin'] |
| config_map = {'123': 'foo-paladin', |
| '124': 'bar-paladin', |
| '125': 'no_stat-paladin'} |
| changes_by_build_id = {'123': change_set1, |
| '124': change_set2} |
| # If a slave did not report status (no_stat), assume all changes |
| # are relevant. |
| expected = {'foo-paladin': change_set1, |
| 'bar-paladin': change_set2, |
| 'no_stat-paladin': changes} |
| |
| self.PatchObject(relevant_changes.RelevantChanges, |
| '_GetSlaveMappingAndCLActions', |
| return_value=(config_map, [])) |
| self.PatchObject(clactions, 'GetRelevantChangesForBuilds', |
| return_value=changes_by_build_id) |
| |
| results = relevant_changes.RelevantChanges.GetRelevantChangesForSlaves( |
| self.master_build_id, self.fake_cidb, self.build_config, changes, |
| no_stat, None) |
| self.assertEqual(results, expected) |