summaryrefslogtreecommitdiff
path: root/ironic_python_agent/tests/unit/hardware_managers/test_cna.py
blob: 3253ad4cf79e8177cab08cf3f16385b6979bab5e (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
147
148
149
150
151
152
153
# Copyright (C) 2016 Intel Corporation
#
# 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 os

import mock
from oslo_concurrency import processutils
from oslo_config import cfg

from ironic_python_agent import hardware
from ironic_python_agent.hardware_managers import cna
from ironic_python_agent.tests.unit import base
from ironic_python_agent import utils

CONF = cfg.CONF


class TestIntelCnaHardwareManager(base.IronicAgentTest):
    def setUp(self):
        super(TestIntelCnaHardwareManager, self).setUp()
        self.hardware = cna.IntelCnaHardwareManager()
        self.node = {'uuid': 'dda135fb-732d-4742-8e72-df8f3199d244',
                     'driver_internal_info': {}}
        CONF.clear_override('disk_wait_attempts')
        CONF.clear_override('disk_wait_delay')

    @mock.patch.object(os, 'listdir', autospec=True)
    @mock.patch.object(utils, 'execute', autospec=True)
    def test_detect_cna_card(self, mock_execute, mock_listdir):
        def mock_return_execute(*args, **kwargs):
            if 'eth0' in args[2]:
                return '/foo/bar/fake', ''
            if 'eth1' in args[2]:
                return '/foo/bar/i40e', ''

        mock_listdir.return_value = ['eth0', 'eth1']
        mock_execute.side_effect = mock_return_execute
        self.assertTrue(cna._detect_cna_card())

    @mock.patch.object(os, 'listdir', autospec=True)
    @mock.patch.object(utils, 'execute', autospec=True)
    def test_detect_cna_card_execute_error(self, mock_execute, mock_listdir):
        def mock_return_execute(*args, **kwargs):
            if 'eth0' in args[2]:
                return '/foo/bar/fake', ''
            if 'eth1' in args[2]:
                raise processutils.ProcessExecutionError('fake')
            if 'eth2' in args[2]:
                raise OSError('fake')

        mock_listdir.return_value = ['eth0', 'eth1', 'eth2']
        mock_execute.side_effect = mock_return_execute
        self.assertFalse(cna._detect_cna_card())

    @mock.patch.object(os, 'listdir', autospec=True)
    @mock.patch.object(utils, 'execute', autospec=True)
    def test_detect_cna_card_no_i40e_driver(self, mock_execute, mock_listdir):
        def mock_return_execute(*args, **kwargs):
            if 'eth0' in args[2]:
                return '/foo/bar/fake1', ''
            if 'eth1' in args[2]:
                return '/foo/bar/fake2', ''

        mock_listdir.return_value = ['eth0', 'eth1']
        mock_execute.side_effect = mock_return_execute
        self.assertFalse(cna._detect_cna_card())

    @mock.patch.object(cna, 'LOG', autospec=True)
    @mock.patch.object(os, 'listdir', autospec=True)
    @mock.patch.object(os.path, 'exists', autospec=True)
    def test_disable_embedded_lldp_agent_in_cna_card(self, mock_exists,
                                                     mock_listdir, mock_log):
        mock_exists.return_value = True
        mock_listdir.return_value = ['foo', 'bar']
        write_mock = mock.mock_open()
        with mock.patch('six.moves.builtins.open', write_mock, create=True):
            cna._disable_embedded_lldp_agent_in_cna_card()
            write_mock().write.assert_called_with('lldp stop')
            self.assertFalse(mock_log.warning.called)

    @mock.patch.object(cna, 'LOG', autospec=True)
    @mock.patch.object(os.path, 'exists', autospec=True)
    def test_disable_embedded_lldp_agent_wrong_dir_path(self, mock_exists,
                                                        mock_log):
        mock_exists.return_value = False
        cna._disable_embedded_lldp_agent_in_cna_card()
        expected_log_message = 'Driver i40e was not loaded properly'
        mock_log.warning.assert_called_once_with(expected_log_message)

    @mock.patch.object(cna, 'LOG', autospec=True)
    @mock.patch.object(os, 'listdir', autospec=True)
    @mock.patch.object(os.path, 'exists', autospec=True)
    def test_disable_embedded_lldp_agent_write_error(self, mock_exists,
                                                     mock_listdir, mock_log):
        mock_exists.return_value = True
        listdir_dict = ['foo', 'bar']
        mock_listdir.return_value = listdir_dict
        write_mock = mock.mock_open()
        with mock.patch('six.moves.builtins.open', write_mock, create=True):
            write_mock.side_effect = IOError('fake error')
            cna._disable_embedded_lldp_agent_in_cna_card()
            expected_log_message = ('Failed to disable the embedded LLDP on '
                                    'Intel CNA network card. Addresses of '
                                    'failed pci devices: {}'
                                    .format(str(listdir_dict).strip('[]')))
            mock_log.warning.assert_called_once_with(expected_log_message)

    @mock.patch.object(cna, 'LOG', autospec=True)
    @mock.patch.object(cna, '_detect_cna_card', autospec=True)
    def test_evaluate_hardware_support(self, mock_detect_card, mock_log):
        mock_detect_card.return_value = True
        expected_support = hardware.HardwareSupport.MAINLINE
        actual_support = self.hardware.evaluate_hardware_support()
        self.assertEqual(expected_support, actual_support)
        mock_log.debug.assert_called_once()

    @mock.patch.object(cna, 'LOG', autospec=True)
    @mock.patch.object(cna, '_detect_cna_card', autospec=True)
    def test_evaluate_hardware_support_no_cna_card_detected(self,
                                                            mock_detect_card,
                                                            mock_log):
        mock_detect_card.return_value = False
        expected_support = hardware.HardwareSupport.NONE
        actual_support = self.hardware.evaluate_hardware_support()
        self.assertEqual(expected_support, actual_support)
        mock_log.debug.assert_called_once()

    @mock.patch.object(hardware.GenericHardwareManager, 'collect_lldp_data',
                       autospec=True)
    def test_collect_lldp_data(self, mock_super_collect):
        iface_names = ['eth0', 'eth1']
        returned_lldp_data = [
            (0, 'foo'),
            (1, 'bar'),
        ]
        mock_super_collect.return_value = returned_lldp_data
        with mock.patch.object(cna, '_disable_embedded_lldp_agent_in_cna_card',
                               autospec=True):
            result = self.hardware.collect_lldp_data(iface_names)
            mock_super_collect.assert_called_once_with(self.hardware,
                                                       iface_names)
            self.assertEqual(returned_lldp_data, result)