]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Add test_handle_router_snat_rules_add_rules
authorJian Wen <jian.wen@canonical.com>
Fri, 6 Sep 2013 09:08:00 +0000 (17:08 +0800)
committerJian Wen <jian.wen@canonical.com>
Wed, 27 Nov 2013 06:39:57 +0000 (14:39 +0800)
Add a unit test to verify that snat rules are added and added in order.
So that it could be refactored without fear next time.

Closes-Bug: #1222660
Change-Id: I07e820cc28c9a6c139a8eed0917aef2cfe62638a

neutron/tests/unit/test_l3_agent.py

index f0f5068a8133efc75253e87d3eb567ff7a7ffad5..aea6a61a261598d21dd8b9940587321d87df05aa 100644 (file)
@@ -19,6 +19,7 @@ import copy
 
 import mock
 from oslo.config import cfg
+from testtools import matchers
 
 from neutron.agent.common import config as agent_config
 from neutron.agent import l3_agent
@@ -590,6 +591,30 @@ class TestBasicRouterOperations(base.BaseTestCase):
                 self.assertEqual(kwargs, {})
                 break
 
+    def test_handle_router_snat_rules_add_rules(self):
+        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
+        ri = l3_agent.RouterInfo(_uuid(), self.conf.root_helper,
+                                 self.conf.use_namespaces, None)
+        ex_gw_port = {'fixed_ips': [{'ip_address': '192.168.1.4'}]}
+        internal_cidrs = ['10.0.0.0/24']
+        agent._handle_router_snat_rules(ri, ex_gw_port, internal_cidrs,
+                                        "iface", "add_rules")
+
+        nat_rules = map(str, ri.iptables_manager.ipv4['nat'].rules)
+        wrap_name = ri.iptables_manager.wrap_name
+
+        jump_float_rule = "-A %s-snat -j %s-float-snat" % (wrap_name,
+                                                           wrap_name)
+        internal_net_rule = ("-A %s-snat -s %s -j SNAT --to-source %s") % (
+            wrap_name, internal_cidrs[0],
+            ex_gw_port['fixed_ips'][0]['ip_address'])
+
+        self.assertIn(jump_float_rule, nat_rules)
+
+        self.assertIn(internal_net_rule, nat_rules)
+        self.assertThat(nat_rules.index(jump_float_rule),
+                        matchers.LessThan(nat_rules.index(internal_net_rule)))
+
     def test_routers_with_admin_state_down(self):
         agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
         self.plugin_api.get_external_network_id.return_value = None