def _test_get_cfg_agents_for_hosting_devices(self, assignable_agents,
                                                 schedule=False,
                                                 id_disable_cfg_agent=None):
        def faked_choice(seq):
            res = seq[self._current_index]
            self._current_index += 1
            return res

        if id_disable_cfg_agent is not None:
            self._update('agents', id_disable_cfg_agent,
                         {'agent': {'admin_state_up': False}})
        with self.hosting_device_template(
                host_category=self.host_category) as hosting_device_template:
            hdt = hosting_device_template['hosting_device_template']
            with self.hosting_device(template_id=hdt['id']) as (
                    hosting_device1),\
                    self.hosting_device(template_id=hdt['id']) as (
                        hosting_device2),\
                    self.hosting_device(template_id=hdt['id']) as (
                        hosting_device3):
                hds = [hd['hosting_device'] for hd in (
                    hosting_device1, hosting_device2, hosting_device3)]
                hd_ids = [hd['id'] for hd in hds]
                # hosting devices initially not assigned to any cfg agent
                for hd in hds:
                    self.assertIsNone(hd['cfg_agent_id'])
                # hosting device with index 1 is to be left unassigned
                ids_hd_to_assign = [hds[0]['id'], hds[2]['id']]
                self._current_index = 0
                with mock.patch(
                    'networking_cisco.plugins.cisco.device_manager.scheduler'
                    '.hosting_device_cfg_agent_scheduler'
                        '.random.choice') as random_mock:
                    random_mock.side_effect = faked_choice
                    agents = self.plugin.get_cfg_agents_for_hosting_devices(
                        self.adminContext, ids_hd_to_assign, schedule=schedule)
                    self .assertEqual(len(assignable_agents), len(agents))
                    for agent in agents:
                        self.assertIn(agent['id'], assignable_agents)
                    hds_after_dict = {hd['id']: hd for hd in self._list(
                        'hosting_devices')['hosting_devices']}
                    hds_after = [hds_after_dict[i] for i in hd_ids]
                    if len(assignable_agents) == 0:
                        # with no assignable cfg agents none of the hosting
                        # devices should have been assigned a cfg agent
                        for hd_after in hds_after:
                            self.assertIsNone(hd_after['cfg_agent_id'])
                    else:
                        # hosting devices with index 0 and 2 should have been
                        # assigned a config agent (each one different)
                        self.assertIn(hds_after[0]['cfg_agent_id'],
                                      assignable_agents)
                        # remove agent from set to ensure the next assert
                        # can only match the other agent
                        assignable_agents.discard(hds_after[0]['cfg_agent_id'])
                        self.assertIn(hds_after[2]['cfg_agent_id'],
                                      assignable_agents)
                        # hosting device with index should still not have a
                        # cfg agent assigned
                        self.assertIsNone(hds_after[1]['cfg_agent_id'])
dataset/ETHPy150Open openstack/networking-cisco/networking_cisco/tests/unit/cisco/device_manager/test_hosting_device_cfg_agent_scheduler.py/HostingDeviceConfigAgentSchedulerTestCase._test_get_cfg_agents_for_hosting_devices/original