Debian Codegraph @

neutron_fwaas/tests/unit/services/logapi/test_fwg_validate.py

raw symbols SCIP
1 # Copyright (c) 2018 Fujitsu Limited
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 from unittest import mock
17
18 from neutron.objects import ports
19 from neutron.services.logapi.common import exceptions as log_exc
20 from neutron.services.logapi.common import validators
21 from neutron.tests import base
22 from neutron_lib import constants as nl_const
23 from sqlalchemy.orm import exc as orm_exc
24
25 from neutron_fwaas.services.logapi import exceptions as fwg_log_exc
26 from neutron_fwaas.services.logapi import fwg_validate
27
28
29 class TestFWGLogRequestValidations(base.BaseTestCase):
30 """Test validator for a log creation request"""
31
32 def setUp(self):
33 super().setUp()
34 fwg_validate.fwg_plugin = mock.Mock()
35 fwg_validate.fwg_plugin.driver = mock.Mock()
36 fwg_validate.fwg_plugin.driver.firewall_db = mock.Mock()
37
38 def test_validate_fwg_request(self):
39 m_context = mock.Mock()
40 fake_data = {
41 'resource_type': 'firewall_group',
42 'resource_id': 'fake_fwg_id'
43 }
44 with mock.patch.object(fwg_validate, '_check_fwg'):
45 fwg_validate.validate_firewall_group_request(m_context, fake_data)
46 fwg_validate._check_fwg.\
47 assert_called_with(m_context, fake_data['resource_id'])
48 fake_data = {
49 'resource_type': 'firewall_group',
50 'resource_id': 'fake_fwg_id',
51 'target_id': 'fake_port_id'
52 }
53 with mock.patch.object(fwg_validate,
54 '_check_target_resource_bound_fwg'):
55 with mock.patch.object(fwg_validate, '_check_fwg'):
56 with mock.patch.object(fwg_validate, '_check_fwg_port'):
57 fwg_validate.validate_firewall_group_request(m_context,
58 fake_data)
59 fwg_validate._check_target_resource_bound_fwg.\
60 assert_called_with(m_context,
61 fake_data['resource_id'],
62 fake_data['target_id'])
63 fwg_validate._check_fwg. \
64 assert_called_with(m_context,
65 fake_data['resource_id'])
66 fwg_validate._check_fwg_port. \
67 assert_called_with(m_context,
68 fake_data['target_id'])
69
70 def test_validate_request_fwg_id_not_exists(self):
71
72 with mock.patch.object(fwg_validate.fwg_plugin, 'get_firewall_group',
73 side_effect=orm_exc.NoResultFound):
74 self.assertRaises(
75 log_exc.ResourceNotFound,
76 fwg_validate._check_fwg,
77 mock.ANY,
78 'fake_fwg_id')
79
80 def test_validate_request_fwg_not_active(self):
81 fake_fwg = {'id': '1234', 'status': 'PENDING'}
82 with mock.patch.object(fwg_validate.fwg_plugin, 'get_firewall_group',
83 return_value=fake_fwg):
84 self.assertRaises(
85 fwg_log_exc.FWGIsNotReadyForLogging,
86 fwg_validate._check_fwg,
87 mock.ANY,
88 'fake_fwg_id')
89
90 def test_validate_request_router_or_port_id_not_exists(self):
91 with mock.patch.object(ports.Port, 'get_object', return_value=None):
92 self.assertRaises(
93 log_exc.TargetResourceNotFound,
94 fwg_validate._check_fwg_port,
95 mock.ANY,
96 'fake_port_id')
97
98 def test_validate_request_unsupported_fwg_log_on_vm_port(self):
99
100 fake_port = {'device_owner': "compute:"}
101 with mock.patch.object(ports.Port, 'get_object',
102 return_value=fake_port):
103 with mock.patch.object(validators, 'validate_log_type_for_port',
104 return_value=False):
105 self.assertRaises(
106 log_exc.LoggingTypeNotSupported,
107 fwg_validate._check_fwg_port,
108 mock.ANY,
109 'fake_port_id')
110
111 def test_validate_request_router_port_is_not_active(self):
112
113 non_active_status = [nl_const.PORT_STATUS_DOWN,
114 nl_const.PORT_STATUS_ERROR,
115 nl_const.PORT_STATUS_NOTAPPLICABLE,
116 nl_const.PORT_STATUS_BUILD]
117 fake_port = [{'device_owner': nl_const.DEVICE_OWNER_ROUTER_INTF,
118 'status': status}
119 for status in non_active_status]
120 with mock.patch.object(ports.Port, 'get_object',
121 side_effect=fake_port):
122 for status in non_active_status:
123 self.assertRaises(
124 fwg_log_exc.PortIsNotReadyForLogging,
125 fwg_validate._check_fwg_port,
126 mock.ANY,
127 'fake_port_id')
128
129 def test_validate_request_router_port_was_not_associated_fwg(self):
130
131 fake_port = {'device_owner': nl_const.DEVICE_OWNER_ROUTER_INTF,
132 'status': nl_const.PORT_STATUS_ACTIVE}
133
134 with mock.patch.object(ports.Port, 'get_object',
135 return_value=fake_port):
136 with mock.patch.object(fwg_validate.fwg_plugin.driver.firewall_db,
137 'get_fwg_attached_to_port',
138 return_value=None):
139 self.assertRaises(
140 fwg_log_exc.TargetResourceNotAssociated,
141 fwg_validate._check_fwg_port,
142 mock.ANY,
143 'fake_port_id')
144
145 def test_validate_request_target_resource_not_bound_fwg(self):
146
147 fake_ports_in_fwg = ['fake_port_id1, fake_port_id2']
148 with mock.patch.object(
149 fwg_validate.fwg_plugin.driver.firewall_db,
150 'get_ports_in_firewall_group',
151 return_value=fake_ports_in_fwg):
152
153 self.assertRaises(
154 log_exc.InvalidResourceConstraint,
155 fwg_validate._check_target_resource_bound_fwg,
156 mock.ANY,
157 mock.ANY,
158 'fake_target_id')