Set lock_path correctly.
[openstack-build/neutron-build.git] / neutron / tests / api / admin / test_l3_agent_scheduler.py
1 # Copyright 2013 IBM Corp.
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 from tempest_lib.common.utils import data_utils
16
17 from neutron.tests.api import base
18 from neutron.tests.tempest import exceptions
19 from neutron.tests.tempest import test
20
21 AGENT_TYPE = 'L3 agent'
22 AGENT_MODES = (
23     'legacy',
24     'dvr_snat'
25 )
26
27
28 class L3AgentSchedulerTestJSON(base.BaseAdminNetworkTest):
29     _agent_mode = 'legacy'
30
31     """
32     Tests the following operations in the Neutron API using the REST client for
33     Neutron:
34
35         List routers that the given L3 agent is hosting.
36         List L3 agents hosting the given router.
37         Add and Remove Router to L3 agent
38
39     v2.0 of the Neutron API is assumed.
40
41     The l3_agent_scheduler extension is required for these tests.
42     """
43
44     @classmethod
45     def skip_checks(cls):
46         super(L3AgentSchedulerTestJSON, cls).skip_checks()
47         if not test.is_extension_enabled('l3_agent_scheduler', 'network'):
48             msg = "L3 Agent Scheduler Extension not enabled."
49             raise cls.skipException(msg)
50
51     @classmethod
52     def resource_setup(cls):
53         super(L3AgentSchedulerTestJSON, cls).resource_setup()
54         body = cls.admin_client.list_agents()
55         agents = body['agents']
56         for agent in agents:
57             # TODO(armax): falling back on default _agent_mode can be
58             # dropped as soon as Icehouse is dropped.
59             agent_mode = (
60                 agent['configurations'].get('agent_mode', cls._agent_mode))
61             if agent['agent_type'] == AGENT_TYPE and agent_mode in AGENT_MODES:
62                 cls.agent = agent
63                 break
64         else:
65             msg = "L3 Agent Scheduler enabled in conf, but L3 Agent not found"
66             raise exceptions.InvalidConfiguration(msg)
67         cls.router = cls.create_router(data_utils.rand_name('router'))
68         # NOTE(armax): If DVR is an available extension, and the created router
69         # is indeed a distributed one, more resources need to be provisioned
70         # in order to bind the router to the L3 agent.
71         # That said, let's preserve the existing test logic, where the extra
72         # query and setup steps are only required if the extension is available
73         # and only if the router's default type is distributed.
74         if test.is_extension_enabled('dvr', 'network'):
75             is_dvr_router = cls.admin_client.show_router(
76                 cls.router['id'])['router'].get('distributed', False)
77             if is_dvr_router:
78                 cls.network = cls.create_network()
79                 cls.create_subnet(cls.network)
80                 cls.port = cls.create_port(cls.network)
81                 cls.client.add_router_interface_with_port_id(
82                     cls.router['id'], cls.port['id'])
83
84     @test.attr(type='smoke')
85     @test.idempotent_id('b7ce6e89-e837-4ded-9b78-9ed3c9c6a45a')
86     def test_list_routers_on_l3_agent(self):
87         self.admin_client.list_routers_on_l3_agent(self.agent['id'])
88
89     @test.attr(type='smoke')
90     @test.idempotent_id('9464e5e7-8625-49c3-8fd1-89c52be59d66')
91     def test_add_list_remove_router_on_l3_agent(self):
92         l3_agent_ids = list()
93         self.admin_client.add_router_to_l3_agent(
94             self.agent['id'],
95             self.router['id'])
96         body = (
97             self.admin_client.list_l3_agents_hosting_router(self.router['id']))
98         for agent in body['agents']:
99             l3_agent_ids.append(agent['id'])
100             self.assertIn('agent_type', agent)
101             self.assertEqual('L3 agent', agent['agent_type'])
102         self.assertIn(self.agent['id'], l3_agent_ids)
103         body = self.admin_client.remove_router_from_l3_agent(
104             self.agent['id'],
105             self.router['id'])
106         # NOTE(afazekas): The deletion not asserted, because neutron
107         # is not forbidden to reschedule the router to the same agent