summaryrefslogtreecommitdiff
path: root/ironic/tests/unit/drivers/modules/xclarity/test_power.py
blob: e12ee837a85f00fb2e144699dee938138ae2f68a (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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 importlib
import sys
from unittest import mock

from oslo_utils import importutils

from ironic.common import exception
from ironic.common import states
from ironic.conductor import task_manager
from ironic.drivers.modules.xclarity import common
from ironic.drivers.modules.xclarity import power
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

STATE_POWER_ON = "power on"
STATE_POWER_OFF = "power off"
STATE_POWERING_ON = "power on"
STATE_POWERING_OFF = "power on"

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


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

    def setUp(self):
        super(XClarityPowerDriverTestCase, self).setUp()
        self.config(enabled_hardware_types=['xclarity'],
                    enabled_power_interfaces=['xclarity'],
                    enabled_management_interfaces=['xclarity'])
        self.node = obj_utils.create_test_node(
            self.context,
            driver='xclarity',
            driver_info=db_utils.get_test_xclarity_driver_info())

    def test_get_properties(self, mock_get_xc_client):
        expected = common.COMMON_PROPERTIES
        driver = power.XClarityPower()
        self.assertEqual(expected, driver.get_properties())

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

    @mock.patch.object(power.XClarityPower, 'get_power_state',
                       return_value=STATE_POWER_ON, autospec=True)
    def test_get_power_state(self, mock_get_power_state, mock_get_xc_client):
        with task_manager.acquire(self.context, self.node.uuid) as task:
            result = power.XClarityPower.get_power_state(self, task)
        self.assertEqual(STATE_POWER_ON, result)

    @mock.patch.object(common, 'translate_xclarity_power_state',
                       spec_set=True, autospec=True)
    def test_get_power_state_fail(self, mock_translate_state, 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:
                importlib.reload(
                    sys.modules['ironic.drivers.modules.xclarity'])
            ex = exception.XClarityError('E')
            mock_xc_client.return_value.get_node_power_status.side_effect = ex
            self.assertRaises(exception.XClarityError,
                              task.driver.power.get_power_state,
                              task)
            self.assertFalse(mock_translate_state.called)

    @mock.patch.object(power.LOG, 'warning', autospec=True)
    @mock.patch.object(power.XClarityPower, 'get_power_state',
                       return_value=states.POWER_ON, autospec=True)
    def test_set_power(self, mock_set_power_state, mock_log,
                       mock_get_xc_client):
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.driver.power.set_power_state(task, states.POWER_ON)
            expected = task.driver.power.get_power_state(task)
        self.assertEqual(expected, states.POWER_ON)
        self.assertFalse(mock_log.called)

    @mock.patch.object(power.LOG, 'warning', autospec=True)
    @mock.patch.object(power.XClarityPower, 'get_power_state',
                       return_value=states.POWER_ON, autospec=True)
    def test_set_power_timeout(self, mock_set_power_state, mock_log,
                               mock_get_xc_client):
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.driver.power.set_power_state(task, states.POWER_ON,
                                              timeout=21)
            expected = task.driver.power.get_power_state(task)
        self.assertEqual(expected, states.POWER_ON)
        self.assertTrue(mock_log.called)

    def test_set_power_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:
                importlib.reload(
                    sys.modules['ironic.drivers.modules.xclarity'])
            ex = exception.XClarityError('E')
            mock_xc_client.return_value.set_node_power_status.side_effect = ex
            self.assertRaises(exception.XClarityError,
                              task.driver.power.set_power_state,
                              task, states.POWER_OFF)

    @mock.patch.object(power.LOG, 'warning', autospec=True)
    @mock.patch.object(power.XClarityPower, 'set_power_state', autospec=True)
    def test_reboot(self, mock_set_power_state, mock_log, mock_get_xc_client):
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.driver.power.reboot(task)
            mock_set_power_state.assert_called_with(
                mock.ANY, task, states.REBOOT)
            self.assertFalse(mock_log.called)

    @mock.patch.object(power.LOG, 'warning', autospec=True)
    @mock.patch.object(power.XClarityPower, 'set_power_state', autospec=True)
    def test_reboot_timeout(self, mock_set_power_state, mock_log,
                            mock_get_xc_client):
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.driver.power.reboot(task, timeout=55)
            mock_set_power_state.assert_called_with(
                mock.ANY, task, states.REBOOT)
            self.assertTrue(mock_log.called)