def setUp(self):
        super(ConntrackHelperExtensionBaseTestCase, self).setUp()

        self.cth_ext = cth.ConntrackHelperAgentExtension()

        self.context = context.get_admin_context()
        self.connection = mock.Mock()

        self.router_id = uuidutils.generate_uuid()
        self.conntrack_helper1 = cth_obj.ConntrackHelper(
            context=None,
            id=uuidutils.generate_uuid(),
            protocol='udp',
            port=69,
            helper='tftp',
            router_id=self.router_id)

        self.agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
        self.router = {'id': self.router_id, 'ha': False, 'distributed': False}
        self.router_info = l3router.RouterInfo(self.agent, self.router_id,
                                               self.router, **self.ri_kwargs)
        self.agent.router_info[self.router['id']] = self.router_info

        self.get_router_info = mock.patch(
            'neutron.agent.l3.l3_agent_extension_api.'
            'L3AgentExtensionAPI.get_router_info').start()
        self.get_router_info.return_value = self.router_info

        self.agent_api = l3_ext_api.L3AgentExtensionAPI(None, None)
        self.cth_ext.consume_api(self.agent_api)

        self.conntrack_helpers = [self.conntrack_helper1]
 def test_check_conntrack_helper_changes_no_change(self):
     self._set_cth()
     new_cth = cth_obj.ConntrackHelper(context=None,
                                       id=self.conntrack_helper1.id,
                                       protocol='udp',
                                       port=69,
                                       helper='tftp',
                                       router_id=self.router1)
     self.assertFalse(self.mapping.check_conntrack_helper_changes(new_cth))
 def setUp(self):
     super(ConntrackHelperMappingTestCase, self).setUp()
     self.mapping = cth.ConntrackHelperMapping()
     self.router1 = uuidutils.generate_uuid()
     self.router2 = uuidutils.generate_uuid()
     self.conntrack_helper1 = cth_obj.ConntrackHelper(
         context=None,
         id=uuidutils.generate_uuid(),
         protocol='udp',
         port=69,
         helper='tftp',
         router_id=self.router1)
     self.conntrack_helper2 = cth_obj.ConntrackHelper(
         context=None,
         id=uuidutils.generate_uuid(),
         protocol='udp',
         port=69,
         helper='tftp',
         router_id=self.router2)
     self.conntrack_helper3 = cth_obj.ConntrackHelper(
         context=None,
         id=uuidutils.generate_uuid(),
         protocol='udp',
         port=21,
         helper='ftp',
         router_id=self.router1)
     self.conntrack_helper4 = cth_obj.ConntrackHelper(
         context=None,
         id=uuidutils.generate_uuid(),
         protocol='udp',
         port=21,
         helper='ftp',
         router_id=self.router2)
     self.conntrack_helper_dict = {
         self.conntrack_helper1.id: self.conntrack_helper1,
         self.conntrack_helper2.id: self.conntrack_helper2,
         self.conntrack_helper3.id: self.conntrack_helper3,
         self.conntrack_helper4.id: self.conntrack_helper4
     }
    def test_add_conntrack_helper_update_router(self, mock_add_chain,
                                                mock_add_rule):
        self.cth_ext.add_router(self.context, self.router)
        # Create another conntrack helper with the same router_id
        mock_add_chain.reset_mock()
        mock_add_rule.reset_mock()

        test_conntrackhelper = cth_obj.ConntrackHelper(
            context=None,
            id=uuidutils.generate_uuid(),
            protocol='tcp',
            port=21,
            helper='ftp',
            router_id=self.conntrack_helper1.router_id)
        self.conntrack_helpers.append(test_conntrackhelper)
        self.cth_ext.update_router(self.context, self.router)

        chain_name = (
            cth.CONNTRACK_HELPER_CHAIN_PREFIX +
            test_conntrackhelper.id)[:constants.MAX_IPTABLES_CHAIN_LEN_WRAP]
        chain_rule = ('-p %(protocol)s --dport %(dport)s -j CT --helper '
                      '%(helper)s' % {
                          'protocol': test_conntrackhelper.protocol,
                          'dport': test_conntrackhelper.port,
                          'helper': test_conntrackhelper.helper
                      })
        tag = cth.CONNTRACK_HELPER_PREFIX + test_conntrackhelper.id

        self.assertEqual(mock_add_chain.call_count, 6)
        self.assertEqual(mock_add_rule.call_count, 6)

        mock_add_chain.assert_has_calls([
            mock.call(cth.DEFAULT_CONNTRACK_HELPER_CHAIN),
            mock.call(cth.DEFAULT_CONNTRACK_HELPER_CHAIN),
            mock.call(cth.DEFAULT_CONNTRACK_HELPER_CHAIN),
            mock.call(chain_name),
            mock.call(chain_name)
        ])

        mock_add_rule.assert_has_calls([
            mock.call(DEFAULT_RULE[0], DEFAULT_RULE[1]),
            mock.call(DEFAULT_RULE[0], DEFAULT_RULE[1]),
            mock.call(cth.DEFAULT_CONNTRACK_HELPER_CHAIN,
                      '-j %s-' % BINARY_NAME + chain_name,
                      tag=tag),
            mock.call(cth.DEFAULT_CONNTRACK_HELPER_CHAIN,
                      '-j %s-' % BINARY_NAME + chain_name,
                      tag=tag),
            mock.call(chain_name, chain_rule, tag=tag),
            mock.call(chain_name, chain_rule, tag=tag)
        ])
Esempio n. 5
0
    def setUp(self):
        super(L3AgentConntrackHelperExtensionTestFramework, self).setUp()
        self.conf.set_override('extensions', ['conntrack_helper'], 'agent')
        self.agent = neutron_l3_agent.L3NATAgentWithStateReport(
            'agent1', self.conf)

        self.cth_ext = conntrack_helper.ConntrackHelperAgentExtension()

        self.router_id1 = uuidutils.generate_uuid()
        self.router_id2 = uuidutils.generate_uuid()
        self.conntrackhelper1 = cth_obj.ConntrackHelper(
            context=None,
            id=uuidutils.generate_uuid(),
            protocol='udp',
            port=69,
            helper='tftp',
            router_id=self.router_id1)
        self.conntrackhelper2 = cth_obj.ConntrackHelper(
            context=None,
            id=uuidutils.generate_uuid(),
            protocol='tcp',
            port=21,
            helper='ftp',
            router_id=self.router_id2)

        self.conntrack_helpers = [self.conntrackhelper1, self.conntrackhelper2]

        self.managed_cths = {}
        self.managed_cths[self.conntrackhelper1.id] = self.conntrackhelper1
        self.managed_cths[self.conntrackhelper2.id] = self.conntrackhelper2

        self.router_cth_map = collections.defaultdict(set)
        self.router_cth_map[self.router_id1].add(self.conntrackhelper1.id)
        self.router_cth_map[self.router_id2].add(self.conntrackhelper2.id)

        self._set_bulk_poll_mock()
 def test_update_conntrack_helper(self):
     self._set_cth()
     new_conntrack_helper1 = cth_obj.ConntrackHelper(
         context=None,
         id=self.conntrack_helper1.id,
         protocol='udp',
         port=6969,
         helper='tftp',
         router_id=self.router1)
     self.mapping.update_conntrack_helpers([new_conntrack_helper1])
     managed_cths = self.mapping.get_managed_conntrack_helpers()
     self.assertEqual(new_conntrack_helper1,
                      managed_cths[self.conntrack_helper1.id])
     for router_id in self.mapping._router_conntrack_helper_mapping.keys():
         self.assertIn(router_id, [self.router1, self.router2])
     self.assertEqual(
         len([self.router1, self.router2]),
         len(self.mapping._router_conntrack_helper_mapping.keys()))
Esempio n. 7
0
 def create_router_conntrack_helper(self, context, router_id,
                                    conntrack_helper):
     conntrack_helper = conntrack_helper.get(apidef.RESOURCE_NAME)
     conntrack_helper['router_id'] = router_id
     cth_obj = cth.ConntrackHelper(context, **conntrack_helper)
     self._check_conntrack_helper_constraints(cth_obj)
     try:
         with db_api.CONTEXT_WRITER.using(context):
             # If this get_router does not raise an exception, a router
             # with router_id exists.
             self.get_router(context, router_id)
             cth_obj.create()
     except obj_exc.NeutronDbObjectDuplicateEntry:
         (__, conflict_params) = self._find_existing_conntrack_helper(
             context, router_id, cth_obj.to_dict())
         message = _("A duplicate conntrack helper entry with same "
                     "attributes already exists, conflicting values "
                     "are %s") % conflict_params
         raise lib_exc.BadRequest(resource=apidef.RESOURCE_NAME,
                                  msg=message)
     self.push_api.push(context, [cth_obj], rpc_events.CREATED)
     return cth_obj