summaryrefslogtreecommitdiff
path: root/ironic/tests/unit/drivers/modules/xclarity/test_management.py
blob: 1d4fc92095291eb125b5f1db594534dfd1c4adec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Copyright 2017 Lenovo, Inc.
# All Rights Reserved.
#
#    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 sys

import six

import mock

from oslo_utils import importutils

from ironic.common import boot_devices
from ironic.conductor import task_manager
from ironic.drivers.modules.xclarity import common
from ironic.drivers.modules.xclarity import management
from ironic.tests.unit.conductor import mgr_utils
from ironic.tests.unit.db import base as db_base
from ironic.tests.unit.db import utils as db_utils
from ironic.tests.unit.objects import utils as obj_utils


xclarity_client_exceptions = importutils.try_import(
    'xclarity_client.exceptions')


@mock.patch.object(common, 'get_xclarity_client', spect_set=True,
                   autospec=True)
class XClarityManagementDriverTestCase(db_base.DbTestCase):

    def setUp(self):
        super(XClarityManagementDriverTestCase, self).setUp()
        self.config(enabled_hardware_types=['xclarity'],
                    enabled_power_interfaces=['xclarity'],
                    enabled_management_interfaces=['xclarity'])
        mgr_utils.mock_the_extension_manager(
            driver='xclarity', namespace='ironic.hardware.types')
        self.node = obj_utils.create_test_node(
            self.context,
            driver='xclarity',
            driver_info=db_utils.get_test_xclarity_driver_info())

    @mock.patch.object(common, 'get_server_hardware_id',
                       spect_set=True, autospec=True)
    def test_validate(self, mock_validate, mock_get_xc_client):
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.driver.management.validate(task)
        common.get_server_hardware_id(task.node)
        mock_validate.assert_called_with(task.node)

    def test_get_properties(self, mock_get_xc_client):

        expected = common.REQUIRED_ON_DRIVER_INFO
        self.assertItemsEqual(expected,
                              self.node.driver_info)

    @mock.patch.object(management.XClarityManagement, 'get_boot_device',
                       return_value='pxe')
    def test_set_boot_device(self, mock_get_boot_device,
                             mock_get_xc_client):
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.driver.management.set_boot_device(task, 'pxe')
            result = task.driver.management.get_boot_device(task)
        self.assertEqual(result, 'pxe')

    def test_set_boot_device_fail(self, mock_get_xc_client):
        with task_manager.acquire(self.context, self.node.uuid) as task:
            xclarity_client_exceptions.XClarityError = Exception
            sys.modules['xclarity_client.exceptions'] = (
                xclarity_client_exceptions)
            if 'ironic.drivers.modules.xclarity' in sys.modules:
                six.moves.reload_module(
                    sys.modules['ironic.drivers.modules.xclarity'])
            ex = common.XClarityError('E')
            mock_get_xc_client.return_value.set_node_boot_info.side_effect = ex
            self.assertRaises(common.XClarityError,
                              task.driver.management.set_boot_device,
                              task,
                              "pxe")

    def test_get_supported_boot_devices(self, mock_get_xc_client):
        with task_manager.acquire(self.context, self.node.uuid) as task:
            expected = [boot_devices.PXE, boot_devices.BIOS,
                        boot_devices.DISK, boot_devices.CDROM]
            self.assertItemsEqual(
                expected,
                task.driver.management.get_supported_boot_devices(task))

    @mock.patch.object(
        management.XClarityManagement,
        'get_boot_device',
        return_value={'boot_device': 'pxe', 'persistent': False})
    def test_get_boot_device(self, mock_get_boot_device, mock_get_xc_client):
        reference = {'boot_device': 'pxe', 'persistent': False}
        with task_manager.acquire(self.context, self.node.uuid) as task:
            expected_boot_device = task.driver.management.get_boot_device(
                task=task)

        self.assertEqual(reference, expected_boot_device)

    def test_get_boot_device_fail(self, mock_xc_client):
        with task_manager.acquire(self.context, self.node.uuid) as task:
            xclarity_client_exceptions.XClarityError = Exception
            sys.modules['xclarity_client.exceptions'] = (
                xclarity_client_exceptions)
            if 'ironic.drivers.modules.xclarity' in sys.modules:
                six.moves.reload_module(
                    sys.modules['ironic.drivers.modules.xclarity'])
            ex = common.XClarityError('E')
            mock_xc_client.return_value.get_node_all_boot_info.side_effect = ex
            self.assertRaises(
                common.XClarityError,
                task.driver.management.get_boot_device,
                task)