|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +from unittest import mock |
| 14 | + |
| 15 | +from nova import exception |
| 16 | +from nova import test |
| 17 | +from nova.tests import fixtures |
| 18 | +from nova.virt.libvirt.cpu import core |
| 19 | + |
| 20 | + |
| 21 | +class TestCore(test.NoDBTestCase): |
| 22 | + |
| 23 | + @mock.patch.object(core.filesystem, 'read_sys') |
| 24 | + @mock.patch.object(core.hardware, 'parse_cpu_spec') |
| 25 | + def test_get_available_cores(self, mock_parse_cpu_spec, mock_read_sys): |
| 26 | + mock_read_sys.return_value = '1-2' |
| 27 | + mock_parse_cpu_spec.return_value = set([1, 2]) |
| 28 | + self.assertEqual(set([1, 2]), core.get_available_cores()) |
| 29 | + mock_read_sys.assert_called_once_with(core.AVAILABLE_PATH) |
| 30 | + mock_parse_cpu_spec.assert_called_once_with('1-2') |
| 31 | + |
| 32 | + @mock.patch.object(core.filesystem, 'read_sys') |
| 33 | + @mock.patch.object(core.hardware, 'parse_cpu_spec') |
| 34 | + def test_get_available_cores_none( |
| 35 | + self, mock_parse_cpu_spec, mock_read_sys): |
| 36 | + mock_read_sys.return_value = '' |
| 37 | + self.assertEqual(set(), core.get_available_cores()) |
| 38 | + mock_parse_cpu_spec.assert_not_called() |
| 39 | + |
| 40 | + @mock.patch.object(core, 'get_available_cores') |
| 41 | + def test_exists(self, mock_get_available_cores): |
| 42 | + mock_get_available_cores.return_value = set([1]) |
| 43 | + self.assertTrue(core.exists(1)) |
| 44 | + mock_get_available_cores.assert_called_once_with() |
| 45 | + self.assertFalse(core.exists(2)) |
| 46 | + |
| 47 | + @mock.patch.object( |
| 48 | + core, 'CPU_PATH_TEMPLATE', |
| 49 | + new_callable=mock.PropertyMock(return_value='/sys/blah%(core)s')) |
| 50 | + @mock.patch.object(core, 'exists') |
| 51 | + def test_gen_cpu_path(self, mock_exists, mock_cpu_path): |
| 52 | + mock_exists.return_value = True |
| 53 | + self.assertEqual('/sys/blah1', core.gen_cpu_path(1)) |
| 54 | + mock_exists.assert_called_once_with(1) |
| 55 | + |
| 56 | + @mock.patch.object(core, 'exists') |
| 57 | + def test_gen_cpu_path_raises(self, mock_exists): |
| 58 | + mock_exists.return_value = False |
| 59 | + self.assertRaises(ValueError, core.gen_cpu_path, 1) |
| 60 | + self.assertIn('Unable to access CPU: 1', self.stdlog.logger.output) |
| 61 | + |
| 62 | + |
| 63 | +class TestCoreHelpers(test.NoDBTestCase): |
| 64 | + |
| 65 | + def setUp(self): |
| 66 | + super(TestCoreHelpers, self).setUp() |
| 67 | + self.useFixture(fixtures.PrivsepFixture()) |
| 68 | + _p1 = mock.patch.object(core, 'exists', return_value=True) |
| 69 | + self.mock_exists = _p1.start() |
| 70 | + self.addCleanup(_p1.stop) |
| 71 | + |
| 72 | + _p2 = mock.patch.object(core, 'gen_cpu_path', |
| 73 | + side_effect=lambda x: '/fakesys/blah%s' % x) |
| 74 | + self.mock_gen_cpu_path = _p2.start() |
| 75 | + self.addCleanup(_p2.stop) |
| 76 | + |
| 77 | + @mock.patch.object(core.filesystem, 'read_sys') |
| 78 | + def test_get_online(self, mock_read_sys): |
| 79 | + mock_read_sys.return_value = '1' |
| 80 | + self.assertTrue(core.get_online(1)) |
| 81 | + mock_read_sys.assert_called_once_with('/fakesys/blah1/online') |
| 82 | + |
| 83 | + @mock.patch.object(core.filesystem, 'read_sys') |
| 84 | + def test_get_online_not_exists(self, mock_read_sys): |
| 85 | + mock_read_sys.side_effect = exception.FileNotFound(file_path='foo') |
| 86 | + self.assertTrue(core.get_online(1)) |
| 87 | + mock_read_sys.assert_called_once_with('/fakesys/blah1/online') |
| 88 | + |
| 89 | + @mock.patch.object(core.filesystem, 'write_sys') |
| 90 | + @mock.patch.object(core, 'get_online') |
| 91 | + def test_set_online(self, mock_get_online, mock_write_sys): |
| 92 | + mock_get_online.return_value = True |
| 93 | + self.assertTrue(core.set_online(1)) |
| 94 | + mock_write_sys.assert_called_once_with('/fakesys/blah1/online', |
| 95 | + data='1') |
| 96 | + mock_get_online.assert_called_once_with(1) |
| 97 | + |
| 98 | + @mock.patch.object(core.filesystem, 'write_sys') |
| 99 | + @mock.patch.object(core, 'get_online') |
| 100 | + def test_set_offline(self, mock_get_online, mock_write_sys): |
| 101 | + mock_get_online.return_value = False |
| 102 | + self.assertTrue(core.set_offline(1)) |
| 103 | + mock_write_sys.assert_called_once_with('/fakesys/blah1/online', |
| 104 | + data='0') |
| 105 | + mock_get_online.assert_called_once_with(1) |
| 106 | + |
| 107 | + @mock.patch.object(core.filesystem, 'read_sys') |
| 108 | + def test_get_governor(self, mock_read_sys): |
| 109 | + mock_read_sys.return_value = 'fake_gov' |
| 110 | + self.assertEqual('fake_gov', core.get_governor(1)) |
| 111 | + mock_read_sys.assert_called_once_with( |
| 112 | + '/fakesys/blah1/cpufreq/scaling_governor') |
| 113 | + |
| 114 | + @mock.patch.object(core, 'get_governor') |
| 115 | + @mock.patch.object(core.filesystem, 'write_sys') |
| 116 | + def test_set_governor(self, mock_write_sys, mock_get_governor): |
| 117 | + mock_get_governor.return_value = 'fake_gov' |
| 118 | + self.assertEqual('fake_gov', |
| 119 | + core.set_governor(1, 'fake_gov')) |
| 120 | + mock_write_sys.assert_called_once_with( |
| 121 | + '/fakesys/blah1/cpufreq/scaling_governor', data='fake_gov') |
| 122 | + mock_get_governor.assert_called_once_with(1) |
0 commit comments