Masayuki Igawa | ba7ce93 | 2014-03-17 13:24:26 +0900 | [diff] [blame] | 1 | # Copyright 2014 NEC Corporation. |
| 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 | import mock |
| 17 | |
| 18 | from tempest.common import debug |
| 19 | from tempest import config |
| 20 | from tempest.openstack.common.fixture import mockpatch |
| 21 | from tempest import test |
| 22 | from tempest.tests import base |
| 23 | from tempest.tests import fake_config |
| 24 | |
| 25 | |
| 26 | class TestDebug(base.TestCase): |
| 27 | |
| 28 | def setUp(self): |
| 29 | super(TestDebug, self).setUp() |
| 30 | self.useFixture(fake_config.ConfigFixture()) |
| 31 | self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) |
| 32 | |
| 33 | common_pre = 'tempest.common.commands' |
| 34 | self.ip_addr_raw_mock = self.patch(common_pre + '.ip_addr_raw') |
| 35 | self.ip_route_raw_mock = self.patch(common_pre + '.ip_route_raw') |
| 36 | self.iptables_raw_mock = self.patch(common_pre + '.iptables_raw') |
| 37 | self.ip_ns_list_mock = self.patch(common_pre + '.ip_ns_list') |
| 38 | self.ip_ns_addr_mock = self.patch(common_pre + '.ip_ns_addr') |
| 39 | self.ip_ns_route_mock = self.patch(common_pre + '.ip_ns_route') |
| 40 | self.iptables_ns_mock = self.patch(common_pre + '.iptables_ns') |
| 41 | self.ovs_db_dump_mock = self.patch(common_pre + '.ovs_db_dump') |
| 42 | |
| 43 | self.log_mock = self.patch('tempest.common.debug.LOG') |
| 44 | |
| 45 | def test_log_ip_ns_debug_disabled(self): |
| 46 | self.useFixture(mockpatch.PatchObject(test.CONF.debug, |
| 47 | 'enable', False)) |
| 48 | debug.log_ip_ns() |
| 49 | self.assertFalse(self.ip_addr_raw_mock.called) |
| 50 | self.assertFalse(self.log_mock.info.called) |
| 51 | |
| 52 | def test_log_ip_ns_debug_enabled(self): |
| 53 | self.useFixture(mockpatch.PatchObject(test.CONF.debug, |
| 54 | 'enable', True)) |
| 55 | |
| 56 | tables = ['filter', 'nat', 'mangle'] |
| 57 | self.ip_ns_list_mock.return_value = [1, 2] |
| 58 | |
| 59 | debug.log_ip_ns() |
| 60 | self.ip_addr_raw_mock.assert_called_with() |
| 61 | self.assertTrue(self.log_mock.info.called) |
| 62 | self.ip_route_raw_mock.assert_called_with() |
| 63 | self.assertEqual(len(tables), self.iptables_raw_mock.call_count) |
| 64 | for table in tables: |
| 65 | self.assertIn(mock.call(table), |
| 66 | self.iptables_raw_mock.call_args_list) |
| 67 | |
| 68 | self.ip_ns_list_mock.assert_called_with() |
| 69 | self.assertEqual(len(self.ip_ns_list_mock.return_value), |
| 70 | self.ip_ns_addr_mock.call_count) |
| 71 | self.assertEqual(len(self.ip_ns_list_mock.return_value), |
| 72 | self.ip_ns_route_mock.call_count) |
| 73 | for ns in self.ip_ns_list_mock.return_value: |
| 74 | self.assertIn(mock.call(ns), |
| 75 | self.ip_ns_addr_mock.call_args_list) |
| 76 | self.assertIn(mock.call(ns), |
| 77 | self.ip_ns_route_mock.call_args_list) |
| 78 | |
| 79 | self.assertEqual(len(tables) * len(self.ip_ns_list_mock.return_value), |
| 80 | self.iptables_ns_mock.call_count) |
| 81 | for ns in self.ip_ns_list_mock.return_value: |
| 82 | for table in tables: |
| 83 | self.assertIn(mock.call(ns, table), |
| 84 | self.iptables_ns_mock.call_args_list) |
| 85 | |
| 86 | def test_log_ovs_db_debug_disabled(self): |
| 87 | self.useFixture(mockpatch.PatchObject(test.CONF.debug, |
| 88 | 'enable', False)) |
| 89 | self.useFixture(mockpatch.PatchObject(test.CONF.service_available, |
| 90 | 'neutron', False)) |
| 91 | debug.log_ovs_db() |
| 92 | self.assertFalse(self.ovs_db_dump_mock.called) |
| 93 | |
| 94 | self.useFixture(mockpatch.PatchObject(test.CONF.debug, |
| 95 | 'enable', True)) |
| 96 | self.useFixture(mockpatch.PatchObject(test.CONF.service_available, |
| 97 | 'neutron', False)) |
| 98 | debug.log_ovs_db() |
| 99 | self.assertFalse(self.ovs_db_dump_mock.called) |
| 100 | |
| 101 | self.useFixture(mockpatch.PatchObject(test.CONF.debug, |
| 102 | 'enable', False)) |
| 103 | self.useFixture(mockpatch.PatchObject(test.CONF.service_available, |
| 104 | 'neutron', True)) |
| 105 | debug.log_ovs_db() |
| 106 | self.assertFalse(self.ovs_db_dump_mock.called) |
| 107 | |
| 108 | def test_log_ovs_db_debug_enabled(self): |
| 109 | self.useFixture(mockpatch.PatchObject(test.CONF.debug, |
| 110 | 'enable', True)) |
| 111 | self.useFixture(mockpatch.PatchObject(test.CONF.service_available, |
| 112 | 'neutron', True)) |
| 113 | debug.log_ovs_db() |
| 114 | self.ovs_db_dump_mock.assert_called_with() |
| 115 | |
| 116 | def test_log_net_debug(self): |
| 117 | self.log_ip_ns_mock = self.patch('tempest.common.debug.log_ip_ns') |
| 118 | self.log_ovs_db_mock = self.patch('tempest.common.debug.log_ovs_db') |
| 119 | |
| 120 | debug.log_net_debug() |
| 121 | self.log_ip_ns_mock.assert_called_with() |
| 122 | self.log_ovs_db_mock.assert_called_with() |