summaryrefslogtreecommitdiff
path: root/ironic/tests/unit/drivers/cimc/test_common.py
blob: 0924f81dea27ded23be7e4059266bb257c093a9a (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 2015, Cisco Systems.
#
# 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

from oslo_config import cfg
from oslo_utils import importutils

from ironic.common import exception
from ironic.conductor import task_manager
from ironic.drivers.modules.cimc import common as cimc_common
from ironic.tests.unit.conductor import utils as 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

imcsdk = importutils.try_import('ImcSdk')

CONF = cfg.CONF


class CIMCBaseTestCase(db_base.DbTestCase):

    def setUp(self):
        super(CIMCBaseTestCase, self).setUp()
        mgr_utils.mock_the_extension_manager(driver="fake_cimc")
        self.node = obj_utils.create_test_node(
            self.context,
            driver='fake_cimc',
            driver_info=db_utils.get_test_cimc_info(),
            instance_uuid="fake_uuid")
        CONF.set_override('max_retry', 2, 'cimc')
        CONF.set_override('action_interval', 0, 'cimc')


class ParseDriverInfoTestCase(CIMCBaseTestCase):

    def test_parse_driver_info(self):
        info = cimc_common.parse_driver_info(self.node)

        self.assertIsNotNone(info.get('cimc_address'))
        self.assertIsNotNone(info.get('cimc_username'))
        self.assertIsNotNone(info.get('cimc_password'))

    def test_parse_driver_info_missing_address(self):
        del self.node.driver_info['cimc_address']
        self.assertRaises(exception.MissingParameterValue,
                          cimc_common.parse_driver_info, self.node)

    def test_parse_driver_info_missing_username(self):
        del self.node.driver_info['cimc_username']
        self.assertRaises(exception.MissingParameterValue,
                          cimc_common.parse_driver_info, self.node)

    def test_parse_driver_info_missing_password(self):
        del self.node.driver_info['cimc_password']
        self.assertRaises(exception.MissingParameterValue,
                          cimc_common.parse_driver_info, self.node)


@mock.patch.object(cimc_common, 'cimc_handle', autospec=True)
class CIMCHandleLogin(CIMCBaseTestCase):

    def test_cimc_handle_login(self, mock_handle):
        info = cimc_common.parse_driver_info(self.node)

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            with mock_handle(task) as handle:
                cimc_common.handle_login(task, handle, info)

                handle.login.assert_called_once_with(
                    self.node.driver_info['cimc_address'],
                    self.node.driver_info['cimc_username'],
                    self.node.driver_info['cimc_password'])

    def test_cimc_handle_login_exception(self, mock_handle):
        info = cimc_common.parse_driver_info(self.node)

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            with mock_handle(task) as handle:
                handle.login.side_effect = imcsdk.ImcException('Boom')

                self.assertRaises(exception.CIMCException,
                                  cimc_common.handle_login,
                                  task, handle, info)

                handle.login.assert_called_once_with(
                    self.node.driver_info['cimc_address'],
                    self.node.driver_info['cimc_username'],
                    self.node.driver_info['cimc_password'])


class CIMCHandleTestCase(CIMCBaseTestCase):

    @mock.patch.object(imcsdk, 'ImcHandle', autospec=True)
    @mock.patch.object(cimc_common, 'handle_login', autospec=True)
    def test_cimc_handle(self, mock_login, mock_handle):
        mo_hand = mock.MagicMock()
        mo_hand.username = self.node.driver_info.get('cimc_username')
        mo_hand.password = self.node.driver_info.get('cimc_password')
        mo_hand.name = self.node.driver_info.get('cimc_address')
        mock_handle.return_value = mo_hand
        info = cimc_common.parse_driver_info(self.node)

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            with cimc_common.cimc_handle(task) as handle:
                self.assertEqual(handle, mock_handle.return_value)

        mock_login.assert_called_once_with(task, mock_handle.return_value,
                                           info)
        mock_handle.return_value.logout.assert_called_once_with()