def test_partition_vifs(self): with self._stubs(self.interfaces, self.sg_states, self.vif_recs)\ as (interfaces, sg_states, vif_recs): xapi_client = xapi.XapiClient() added, updated, removed = agent.partition_vifs( xapi_client, interfaces, sg_states) self.assertEqual(added, [interfaces[0], interfaces[4]]) self.assertEqual(updated, [interfaces[1]]) self.assertEqual(removed, [interfaces[2]])
def run(): """Fetches changes and applies them to VIFs periodically Process as of RM11449: * Get all groups from redis * Fetch ALL VIFs from Xen * Walk ALL VIFs and partition them into added, updated and removed * Walk the final "modified" VIFs list and apply flows to each """ groups_client = sg_cli.SecurityGroupsClient() xapi_client = xapi.XapiClient() interfaces = set() while True: try: interfaces = xapi_client.get_interfaces() except Exception: LOG.exception("Unable to get instances/interfaces from xapi") _sleep() continue try: sg_states = groups_client.get_security_group_states(interfaces) new_sg, updated_sg, removed_sg = partition_vifs(xapi_client, interfaces, sg_states) xapi_client.update_interfaces(new_sg, updated_sg, removed_sg) groups_to_ack = [v for v in new_sg + updated_sg if v.success] # NOTE(quade): This solves a race condition where a security group # rule may have changed between the time the sg_states were called # and when they were officially ack'd. It functions as a compare # and set. This is a fix until we get onto a proper messaging # queue. NCP-2287 sg_sts_curr = groups_client.get_security_group_states(interfaces) groups_to_ack = get_groups_to_ack(groups_to_ack, sg_states, sg_sts_curr) # This list will contain all the security group rules that do not # match ack_groups(groups_client, groups_to_ack) except Exception: LOG.exception("Unable to get security groups from registry and " "apply them to xapi") _sleep() continue _sleep()
def run(): redis_client = redis.RedisClient() xapi_client = xapi.XapiClient() vc = version_control.VersionControl() instances = set() interfaces = set() while True: try: new_instances = xapi_client.get_instances() new_interfaces = xapi_client.get_interfaces(new_instances) except Exception: LOG.exception("Unable to get instances/interfaces from xapi") _sleep() continue new_instances = set(new_instances.values()) added_instances = new_instances - instances removed_instances = instances - new_instances if added_instances or removed_instances: LOG.debug("instances: added %s removed %s", added_instances, removed_instances) added_interfaces = new_interfaces - interfaces removed_interfaces = interfaces - new_interfaces if added_interfaces or removed_interfaces: LOG.debug("interfaces: added %s removed %s", added_interfaces, removed_interfaces) try: new_security_groups = redis_client.get_security_groups( new_interfaces) added_sg, updated_sg, removed_sg = vc.diff(new_security_groups) xapi.update_interfaces(new_instances, added_sg, updated_sg, removed_sg) except Exception: LOG.exception("Unable to get security groups from Redis and apply" " them to xapi") _sleep() continue vc.commit(new_security_groups) instances = new_instances interfaces = new_interfaces _sleep()
def test_partition_vifs(self, sess): def _vif_rec(mac, tagged): rec = {"MAC": mac, "other_config": {}} if tagged: rec["other_config"] = {"security_groups": "enabled"} return rec vif_recs = [ _vif_rec(1, False), _vif_rec(2, True), _vif_rec(3, True), _vif_rec(4, False), _vif_rec(5, False) ] interfaces = [ xapi.VIF("added", vif_recs[0], "added_ref"), xapi.VIF("updated", vif_recs[1], "updated_ref"), xapi.VIF("removed", vif_recs[2], "removed_ref"), xapi.VIF("no groups", vif_recs[3], "no groups ref"), xapi.VIF("self heal", vif_recs[4], "self heal ref") ] sg_states = { interfaces[0]: False, interfaces[1]: False, interfaces[4]: True } xapi_client = xapi.XapiClient() added, updated, removed = agent.partition_vifs(xapi_client, interfaces, sg_states) self.assertEqual(added, [interfaces[0], interfaces[4]]) self.assertEqual(updated, [interfaces[1]]) self.assertEqual(removed, [interfaces[2]])
def run(): """Fetches changes and applies them to VIFs periodically Process as of RM11449: * Get all groups from redis * Fetch ALL VIFs from Xen * Walk ALL VIFs and partition them into added, updated and removed * Walk the final "modified" VIFs list and apply flows to each """ groups_client = sg_cli.SecurityGroupsClient() xapi_client = xapi.XapiClient() interfaces = set() while True: try: interfaces = xapi_client.get_interfaces() except Exception: LOG.exception("Unable to get instances/interfaces from xapi") _sleep() continue try: sg_states = groups_client.get_security_group_states(interfaces) new_sg, updated_sg, removed_sg = partition_vifs(xapi_client, interfaces, sg_states) xapi_client.update_interfaces(new_sg, updated_sg, removed_sg) groups_to_ack = [v for v in new_sg + updated_sg if v.success] ack_groups(groups_client, groups_to_ack) except Exception: LOG.exception("Unable to get security groups from registry and " "apply them to xapi") _sleep() continue _sleep()
def setUp(self): patcher = mock.patch("quark.agent.xapi.XenAPI.Session") self.addCleanup(patcher.stop) self.session = patcher.start().return_value self.xclient = xapi.XapiClient()
def test_sessioned_exception_handling(self, xapi_session): xapi_session.side_effect = XenAPI.Failure("HANDLE_INVALID") with self.assertRaises(XenAPI.Failure): xapi.XapiClient() self.session.logout.assert_called_once()