2014-01-07 16:36:59 -08:00
|
|
|
"""
|
|
|
|
Copyright 2013 Rackspace, Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import mock
|
2014-04-03 14:02:53 -07:00
|
|
|
from oslotest import base as test_base
|
2014-04-04 13:30:42 -07:00
|
|
|
import six
|
2014-01-07 16:36:59 -08:00
|
|
|
|
2014-03-19 16:19:52 -07:00
|
|
|
from ironic_python_agent import hardware
|
2014-04-10 14:12:16 -07:00
|
|
|
from ironic_python_agent import utils
|
2014-01-07 16:36:59 -08:00
|
|
|
|
2014-04-04 13:30:42 -07:00
|
|
|
if six.PY2:
|
|
|
|
OPEN_FUNCTION_NAME = '__builtin__.open'
|
|
|
|
else:
|
|
|
|
OPEN_FUNCTION_NAME = 'builtins.open'
|
|
|
|
|
2014-01-07 16:36:59 -08:00
|
|
|
|
2014-04-03 14:02:53 -07:00
|
|
|
class TestGenericHardwareManager(test_base.BaseTestCase):
|
2014-01-07 16:36:59 -08:00
|
|
|
def setUp(self):
|
2014-04-03 14:02:53 -07:00
|
|
|
super(TestGenericHardwareManager, self).setUp()
|
2014-01-22 11:09:02 -08:00
|
|
|
self.hardware = hardware.GenericHardwareManager()
|
2014-01-07 16:36:59 -08:00
|
|
|
|
2014-01-27 17:51:06 -08:00
|
|
|
@mock.patch('os.listdir')
|
2014-01-27 18:11:44 -08:00
|
|
|
@mock.patch('os.path.exists')
|
2014-04-04 13:30:42 -07:00
|
|
|
@mock.patch(OPEN_FUNCTION_NAME)
|
2014-01-27 18:11:44 -08:00
|
|
|
def test_list_network_interfaces(self,
|
|
|
|
mocked_open,
|
|
|
|
mocked_exists,
|
|
|
|
mocked_listdir):
|
2014-01-27 17:51:06 -08:00
|
|
|
mocked_listdir.return_value = ['lo', 'eth0']
|
2014-01-27 18:11:44 -08:00
|
|
|
mocked_exists.side_effect = [False, True]
|
2014-04-09 17:26:54 -07:00
|
|
|
mocked_open.return_value.__enter__ = lambda s: s
|
|
|
|
mocked_open.return_value.__exit__ = mock.Mock()
|
|
|
|
read_mock = mocked_open.return_value.read
|
|
|
|
read_mock.return_value = '00:0c:29:8c:11:b1\n'
|
2014-01-27 17:51:06 -08:00
|
|
|
interfaces = self.hardware.list_network_interfaces()
|
2014-01-27 18:11:44 -08:00
|
|
|
self.assertEqual(len(interfaces), 1)
|
|
|
|
self.assertEqual(interfaces[0].name, 'eth0')
|
|
|
|
self.assertEqual(interfaces[0].mac_address, '00:0c:29:8c:11:b1')
|
2014-01-27 17:51:06 -08:00
|
|
|
|
2014-04-10 14:12:16 -07:00
|
|
|
@mock.patch.object(utils, 'execute')
|
|
|
|
def test_get_os_install_device(self, mocked_execute):
|
|
|
|
mocked_execute.return_value = (
|
2014-01-24 14:58:07 -08:00
|
|
|
'RO RA SSZ BSZ StartSec Size Device\n'
|
|
|
|
'rw 256 512 4096 0 249578283616 /dev/sda\n'
|
|
|
|
'rw 256 512 4096 2048 8587837440 /dev/sda1\n'
|
|
|
|
'rw 256 512 4096 124967424 15728640 /dev/sda2\n'
|
2014-01-24 15:06:58 -08:00
|
|
|
'rw 256 512 4096 0 31016853504 /dev/sdb\n'
|
2014-03-05 16:29:58 -08:00
|
|
|
'rw 256 512 4096 0 249578283616 /dev/sdc\n', '')
|
2014-01-24 14:58:07 -08:00
|
|
|
|
|
|
|
self.assertEqual(self.hardware.get_os_install_device(), '/dev/sdb')
|
2014-04-10 14:12:16 -07:00
|
|
|
mocked_execute.assert_called_once_with('blockdev',
|
|
|
|
'--report',
|
|
|
|
check_exit_code=[0])
|
2014-01-28 11:25:00 -08:00
|
|
|
|
|
|
|
def test_list_hardwre_info(self):
|
|
|
|
self.hardware.list_network_interfaces = mock.Mock()
|
|
|
|
self.hardware.list_network_interfaces.return_value = [
|
|
|
|
hardware.NetworkInterface('eth0', '00:0c:29:8c:11:b1'),
|
|
|
|
hardware.NetworkInterface('eth1', '00:0c:29:8c:11:b2'),
|
|
|
|
]
|
|
|
|
|
|
|
|
hardware_info = self.hardware.list_hardware_info()
|
|
|
|
self.assertEqual(len(hardware_info), 2)
|
|
|
|
self.assertEqual(hardware_info[0].type, 'mac_address')
|
|
|
|
self.assertEqual(hardware_info[1].type, 'mac_address')
|
|
|
|
self.assertEqual(hardware_info[0].id, '00:0c:29:8c:11:b1')
|
|
|
|
self.assertEqual(hardware_info[1].id, '00:0c:29:8c:11:b2')
|