Set lock_path correctly.
[openstack-build/neutron-build.git] / neutron / tests / unit / agent / l3 / test_rt_tables.py
1 # Copyright (c) 2015 Hewlett-Packard Enterprise Development Company, L.P.
2 #
3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #    not use this file except in compliance with the License. You may obtain
5 #    a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #    Unless required by applicable law or agreed to in writing, software
10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #    License for the specific language governing permissions and limitations
13 #    under the License.
14
15 import fixtures
16 import mock
17
18 from neutron.agent.common import utils as common_utils
19 from neutron.agent.l3 import rt_tables
20 from neutron.tests import base
21
22
23 def mock_netnamespace_directory(function):
24     """Decorator to test RoutingTablesManager with temp dir
25
26     Allows direct testing of RoutingTablesManager by changing the directory
27     where it finds the rt_tables to one in /tmp where root privileges are not
28     required and it won't mess with any real routing tables.
29     """
30     orig_execute = common_utils.execute
31
32     def execute_no_root(*args, **kwargs):
33         kwargs['run_as_root'] = False
34         orig_execute(*args, **kwargs)
35
36     def inner(*args, **kwargs):
37         with fixtures.TempDir() as tmpdir:
38             cls = rt_tables.NamespaceEtcDir
39             with mock.patch.object(common_utils, 'execute') as execute,\
40                     mock.patch.object(cls, 'BASE_DIR', tmpdir.path):
41                 execute.side_effect = execute_no_root
42                 function(*args, **kwargs)
43     return inner
44
45
46 class TestRoutingTablesManager(base.BaseTestCase):
47     def setUp(self):
48         super(TestRoutingTablesManager, self).setUp()
49         self.ns_name = "fakens"
50
51     @mock_netnamespace_directory
52     def test_default_tables(self):
53         rtm = rt_tables.RoutingTablesManager(self.ns_name)
54         self.assertEqual(253, rtm.get("default").table_id)
55         self.assertEqual(254, rtm.get("main").table_id)
56         self.assertEqual(255, rtm.get("local").table_id)
57         self.assertEqual(0, rtm.get("unspec").table_id)
58
59     @mock_netnamespace_directory
60     def test_get_all(self):
61         rtm = rt_tables.RoutingTablesManager(self.ns_name)
62         table_names = set(rt.name for rt in rtm.get_all())
63         self.assertEqual({"main", "default", "local", "unspec"}, table_names)
64
65         new_table = rtm.add("faketable")
66         self.assertIn(new_table, rtm.get_all())
67
68     @mock_netnamespace_directory
69     def test_add(self):
70         rtm = rt_tables.RoutingTablesManager(self.ns_name)
71         added_table = rtm.add("faketable")
72         self.assertGreaterEqual(added_table.table_id, 1024)
73
74         table = rtm.get("faketable")
75         self.assertEqual(added_table, table)
76
77         # Be sure that adding it twice gets the same result
78         added_again = rtm.add("faketable")
79         self.assertEqual(added_table, added_again)
80
81     @mock_netnamespace_directory
82     def test_delete(self):
83         rtm = rt_tables.RoutingTablesManager(self.ns_name)
84         rtm.add("faketable")
85         rtm.delete("faketable")
86
87         table = rtm.get("faketable")
88         self.assertIsNone(table)