File: test_firmware_controller.py

package info (click to toggle)
python-proliantutils 2.16.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,684 kB
  • sloc: python: 58,655; makefile: 163; sh: 2
file content (567 lines) | stat: -rw-r--r-- 25,388 bytes parent folder | download | duplicates (3)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# Copyright 2016 Hewlett Packard Enterprise Company, L.P.
# 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.
"""Test Class for Firmware controller."""
import distutils.spawn
import os
import shutil
import tempfile
import unittest
from unittest import mock

import ddt
from six.moves import builtins as __builtin__

from proliantutils import exception
from proliantutils.ilo import common
from proliantutils.ilo import firmware_controller


@ddt.ddt
class FirmwareControllerModuleTestCase(unittest.TestCase):

    def setUp(self):
        # | BEFORE_EACH |
        super(FirmwareControllerModuleTestCase, self).setUp()
        self.any_scexe_file = 'any_file.scexe'
        self.any_rpm_file = 'any_file.rpm'
        self.any_raw_fw_file = 'any_fw_file.bin'

    @ddt.data('absolute_path_to_exe', None)
    @mock.patch.object(firmware_controller, 'shutil', spec=['which'])
    @mock.patch.object(distutils.spawn, 'find_executable', autospec=True)
    def test_find_executable_returns(self, expected_ret_val,
                                     find_executable_mock, shutil_mock):
        # | GIVEN |
        find_executable_mock.return_value = expected_ret_val
        shutil_mock.which.return_value = expected_ret_val
        # | WHEN |
        actual_ret_val = firmware_controller.find_executable('executable')
        #  | THEN |
        self.assertEqual(expected_ret_val, actual_ret_val)

    @ddt.data('ilo', 'cpld', 'power_pic', 'bios', 'chassis')
    def test_check_firmware_update_component_passes_for_valid_component(
            self, component_type):
        # | GIVEN |
        ris_or_ribcl_obj_mock = mock.MagicMock()
        update_firmware_mock = mock.MagicMock()
        # Note(deray): Need to set __name__ attribute explicitly to keep
        # ``six.wraps`` happy. Passing this to the `name` argument at the time
        # creation of Mock doesn't help.
        update_firmware_mock.__name__ = 'update_firmware_mock'
        wrapped_func = (firmware_controller.
                        check_firmware_update_component(update_firmware_mock))
        # | WHEN |
        wrapped_func(
            ris_or_ribcl_obj_mock, self.any_raw_fw_file, component_type)
        #  | THEN |
        update_firmware_mock.assert_called_once_with(
            ris_or_ribcl_obj_mock, self.any_raw_fw_file, component_type)

    def test_check_firmware_update_component_throws_for_invalid_component(
            self):
        # | GIVEN |
        def func(ris_or_ribcl_obj, filename, component_type):
            pass

        wrapped_func = (firmware_controller.
                        check_firmware_update_component(func))
        ris_or_ribcl_obj_mock = mock.MagicMock()
        # | WHEN | & | THEN |
        self.assertRaises(exception.InvalidInputError,
                          wrapped_func,
                          ris_or_ribcl_obj_mock,
                          self.any_raw_fw_file,
                          'invalid_component')

    def test_get_fw_extractor_will_set_the_fw_file_attribute(self):
        # | WHEN |
        fw_img_extractor = (firmware_controller.
                            get_fw_extractor(self.any_scexe_file))
        # | THEN |
        self.assertEqual(self.any_scexe_file, fw_img_extractor.fw_file)

    @mock.patch.object(firmware_controller, '_extract_scexe_file',
                       autospec=True)
    def test__extract_scexe_file_gets_invoked_for_scexe_firmware_file(
            self, _extract_scexe_file_mock):
        # _extract_scexe_file gets invoked when fw_img_extractor is initialized
        # with scexe firmware file
        # | WHEN |
        fw_img_extractor = (firmware_controller.
                            get_fw_extractor(self.any_scexe_file))
        fw_img_extractor._do_extract('some_target_file', 'some_extract_path')
        # | THEN |
        _extract_scexe_file_mock.assert_called_once_with(
            fw_img_extractor, 'some_target_file', 'some_extract_path')

    @mock.patch.object(firmware_controller, '_extract_rpm_file', autospec=True)
    def test__extract_rpm_file_gets_invoked_for_rpm_firmware_file(
            self, _extract_rpm_file_mock):
        # _extract_rpm_file gets invoked when fw_img_extractor is initialized
        # with rpm firmware file
        # | WHEN |
        fw_img_extractor = (firmware_controller.
                            get_fw_extractor(self.any_rpm_file))
        fw_img_extractor._do_extract('some_target_file', 'some_extract_path')
        # | THEN |
        _extract_rpm_file_mock.assert_called_once_with(
            fw_img_extractor, 'some_target_file', 'some_extract_path')

    @ddt.data('any_file.hex', 'any_file.bin', 'any_file.vme', 'any_file.flash')
    def test_no_op_extract_gets_invoked_for_raw_firmware_file(self,
                                                              any_raw_file):
        # no_op extract when fw_img_extractor is initialized
        # with raw firmware file
        # | WHEN |
        fw_img_extractor = firmware_controller.get_fw_extractor(any_raw_file)
        return_result, is_extracted = fw_img_extractor.extract()
        # | THEN |
        self.assertEqual(any_raw_file, return_result)
        self.assertFalse(is_extracted)

    def test_get_fw_extractor_raises_exception_with_unknown_firmware_file(
            self):
        # | GIVEN |
        any_invalid_format_firmware_file = 'any_file.abc'
        # | WHEN | & | THEN |
        self.assertRaises(exception.InvalidInputError,
                          firmware_controller.get_fw_extractor,
                          any_invalid_format_firmware_file)

    @mock.patch.object(firmware_controller.utils, 'trycmd', autospec=True)
    def test__extract_scexe_file_issues_command_as(self, utils_trycmd_mock):
        # | GIVEN |
        any_scexe_firmware_file = 'any_file.scexe'
        any_extract_path = 'any_extract_path'
        utils_trycmd_mock.return_value = ('out', 'err')
        # | WHEN |
        firmware_controller._extract_scexe_file(
            None, any_scexe_firmware_file, any_extract_path)
        # | THEN |
        utils_trycmd_mock.assert_called_once_with(
            any_scexe_firmware_file, '--unpack=' + any_extract_path)

    @mock.patch.object(firmware_controller, 'os', autospec=True)
    @mock.patch.object(firmware_controller, 'subprocess', autospec=True)
    @mock.patch.object(
        firmware_controller, 'find_executable', lambda x: mock.ANY)
    def test__extract_rpm_file_creates_dir_if_extract_path_doesnt_exist(
            self, subprocess_mock, os_mock):
        # | GIVEN |
        any_rpm_firmware_file = 'any_file.rpm'
        any_extract_path = 'any_extract_path'

        os_mock.path.exists.return_value = False
        mock_cpio = mock.MagicMock()
        mock_cpio.communicate.return_value = ('out', 'err')
        subsequent_popen_call_returns = [mock.MagicMock(), mock_cpio]
        subprocess_mock.Popen = mock.MagicMock(
            side_effect=subsequent_popen_call_returns)
        # | WHEN |
        firmware_controller._extract_rpm_file(
            None, any_rpm_firmware_file, any_extract_path)
        # | THEN |
        os_mock.makedirs.assert_called_once_with(any_extract_path)

    @mock.patch.object(firmware_controller, 'os', autospec=True)
    @mock.patch.object(firmware_controller, 'subprocess', autospec=True)
    @mock.patch.object(
        firmware_controller, 'find_executable', lambda x: mock.ANY)
    def test__extract_rpm_file_doesnt_create_dir_if_extract_path_present(
            self, subprocess_mock, os_mock):
        # extract_rpm_file doesn't create dir if extract path
        # is already present
        # | GIVEN |
        any_rpm_firmware_file = 'any_file.rpm'
        any_extract_path = 'any_extract_path'

        os_mock.path.exists.return_value = True
        mock_cpio = mock.MagicMock()
        mock_cpio.communicate.return_value = ('out', 'err')
        subsequent_popen_call_returns = [mock.MagicMock(), mock_cpio]
        subprocess_mock.Popen = mock.MagicMock(
            side_effect=subsequent_popen_call_returns)
        # | WHEN |
        firmware_controller._extract_rpm_file(
            None, any_rpm_firmware_file, any_extract_path)
        # | THEN |
        self.assertFalse(os_mock.makedirs.called)

    @mock.patch.object(firmware_controller, 'os', autospec=True)
    @mock.patch.object(firmware_controller, 'subprocess', autospec=True)
    def test__extract_rpm_file_raises_exception_if_rpm_or_cpio_exec_not_found(
            self, subprocess_mock, os_mock):
        # | GIVEN |
        any_rpm_firmware_file = 'any_file.rpm'
        any_extract_path = 'any_extract_path'
        return_values_for_find_executable_stub = (
            [None, 'absolute_path_to_cpio'],      # `rpm2cpio` not present
            ['absolute_path_to_rpm2cpio', None]   # `cpio` not present
        )

        # | WHEN | & | THEN |
        for input in return_values_for_find_executable_stub:
            with mock.patch.object(
                    firmware_controller, 'find_executable') as find_mock:
                find_mock.side_effect = input
                self.assertRaises(exception.ImageExtractionFailed,
                                  firmware_controller._extract_rpm_file, None,
                                  any_rpm_firmware_file, any_extract_path)

    @mock.patch.object(firmware_controller, 'os', autospec=True)
    @mock.patch.object(firmware_controller, 'subprocess', autospec=True)
    @mock.patch.object(
        firmware_controller, 'find_executable', lambda x: mock.ANY)
    def test__extract_rpm_file_issues_commands_as(self,
                                                  subprocess_mock,
                                                  os_mock):
        # | GIVEN |
        any_rpm_firmware_file = 'any_file.rpm'
        any_extract_path = 'any_extract_path'

        os_mock.path.exists.return_value = True
        rpm2cpio_mock = mock.MagicMock()
        cpio_mock = mock.MagicMock()
        cpio_mock.communicate.return_value = ('out', 'err')
        subsequent_popen_call_returns = [rpm2cpio_mock, cpio_mock]
        subprocess_mock.Popen = mock.MagicMock(
            side_effect=subsequent_popen_call_returns)
        # | WHEN |
        firmware_controller._extract_rpm_file(
            None, any_rpm_firmware_file, any_extract_path)
        # | THEN |
        popen_calls_to_assert = [
            mock.call.Popen('rpm2cpio ' + any_rpm_firmware_file,
                            shell=True,
                            stdout=subprocess_mock.PIPE),
            mock.call.Popen('cpio -idm',
                            shell=True,
                            stdin=rpm2cpio_mock.stdout),
        ]
        subprocess_mock.assert_has_calls(popen_calls_to_assert)
        cpio_mock.communicate.assert_called_once_with()

    @mock.patch.object(firmware_controller, 'os', autospec=True)
    @mock.patch.object(firmware_controller, 'subprocess', autospec=True)
    @mock.patch.object(
        firmware_controller, 'find_executable', lambda x: mock.ANY)
    def test__extract_rpm_file_raises_exception_if_it_fails(self,
                                                            subprocess_mock,
                                                            os_mock):
        # | GIVEN |
        any_rpm_firmware_file = 'any_file.rpm'
        any_extract_path = 'any_extract_path'

        rpm2cpio_mock = mock.MagicMock()
        cpio_mock = mock.MagicMock()
        cpio_mock.communicate.side_effect = ValueError
        subsequent_popen_call_returns = [rpm2cpio_mock, cpio_mock]
        subprocess_mock.Popen = mock.MagicMock(
            side_effect=subsequent_popen_call_returns)
        # | WHEN | & | THEN |
        self.assertRaises(exception.ImageExtractionFailed,
                          firmware_controller._extract_rpm_file,
                          None, any_rpm_firmware_file, any_extract_path)

    def test__get_firmware_file(self):
        # | GIVEN |
        temp_dir_setup = setup_fixture_create_fw_file_extracts_for('scexe')
        # | WHEN |
        return_result = firmware_controller._get_firmware_file(temp_dir_setup)
        # | THEN |
        self.assertTrue(return_result.endswith('.bin'))
        teardown_fixture_create_fw_file_extracts_for(temp_dir_setup)

    @mock.patch.object(
        firmware_controller, '_get_firmware_file', autospec=True)
    @mock.patch.object(common, 'get_filename_and_extension_of', autospec=True)
    @mock.patch.object(firmware_controller, 'tempfile', autospec=True)
    @mock.patch.object(firmware_controller.uuid, 'uuid4', autospec=True)
    @mock.patch.object(firmware_controller.os, 'link', autospec=True)
    def test__get_firmware_file_in_new_path(
            self, os_link_mock, uuid4_mock, tempfile_mock,
            get_filename_and_extension_of_mock, _get_firmware_file_mock):
        # | GIVEN |
        _get_firmware_file_mock.return_value = 'some_raw_fw_file.bin'
        get_filename_and_extension_of_mock.return_value = ('some_raw_fw_file',
                                                           '.bin')
        tempfile_mock.gettempdir.return_value = '/tmp'
        uuid4_mock.return_value = 12345
        # | WHEN |
        new_fw_file_path = (firmware_controller.
                            _get_firmware_file_in_new_path('any_path'))
        # | THEN |
        _get_firmware_file_mock.assert_called_once_with('any_path')
        # tests the hard linking of the raw fw file so found
        os_link_mock.assert_called_once_with(
            'some_raw_fw_file.bin', '/tmp/12345_some_raw_fw_file.bin')
        self.assertEqual(new_fw_file_path, '/tmp/12345_some_raw_fw_file.bin')

    @mock.patch.object(
        firmware_controller, '_get_firmware_file', autospec=True)
    def test__get_firmware_file_in_new_path_returns_none_for_file_not_found(
            self, _get_firmware_file_mock):
        # | GIVEN |
        _get_firmware_file_mock.return_value = None
        # | WHEN |
        actual_result = (firmware_controller.
                         _get_firmware_file_in_new_path('any_path'))
        # | THEN |
        self.assertIsNone(actual_result)


def teardown_fixture_create_fw_file_extracts_for(temp_dir):
    # os.removedirs(temp_dir)
    shutil.rmtree(temp_dir)


def setup_fixture_create_fw_file_extracts_for(format):
    temp_dir = tempfile.mkdtemp()
    fw_file_exts = [
        '_ilo', '.bin', '.xml', '.TXT', '.hpsetup', '.cpq_package.inc'
    ]

    if format == 'scexe':
        fw_files_dir = temp_dir
    elif format == 'rpm':
        fw_files_dir = os.path.join(
            temp_dir
            + '/please_remove_rpm_file_extracts/usr/lib/i386-linux-gnu/'
            + 'hp-firmware-iloX-xxxx'
        )
    else:
        fw_files_dir = temp_dir

    if not os.path.exists(fw_files_dir):
        os.makedirs(fw_files_dir)

    for fw_file_ext in fw_file_exts:
        tempfile.NamedTemporaryFile(suffix=fw_file_ext,
                                    dir=fw_files_dir,
                                    delete=False)

    return temp_dir


class FirmwareImageExtractorTestCase(unittest.TestCase):

    def setUp(self):
        # | BEFORE_EACH |
        self.any_scexe_file = 'any_file.scexe'
        self.any_rpm_file = 'any_file.rpm'

    @mock.patch.object(common, 'add_exec_permission_to', autospec=True)
    @mock.patch.object(firmware_controller, 'tempfile', autospec=True)
    @mock.patch.object(firmware_controller, 'os', autospec=True)
    @mock.patch.object(firmware_controller, '_get_firmware_file_in_new_path',
                       autospec=True)
    @mock.patch.object(firmware_controller, 'shutil', autospec=True)
    def test_extract_method_calls__do_extract_in_turn(
            self, shutil_mock, _get_firmware_file_in_new_path_mock,
            os_mock, tempfile_mock, add_exec_permission_to_mock):
        # | GIVEN |
        os_mock.path.splitext.return_value = ('any_file', '.scexe')
        fw_img_extractor = (firmware_controller.
                            get_fw_extractor(self.any_scexe_file))
        # Now mock the _do_extract method of fw_img_extractor instance
        _do_extract_mock = mock.MagicMock()
        fw_img_extractor._do_extract = _do_extract_mock

        expected_return_result = 'extracted_firmware_file'
        _get_firmware_file_in_new_path_mock.return_value = (
            expected_return_result)
        # | WHEN |
        actual_return_result, is_extracted = fw_img_extractor.extract()
        # | THEN |
        _do_extract_mock.assert_called_once_with(self.any_scexe_file, mock.ANY)
        self.assertEqual(expected_return_result, actual_return_result)

    @mock.patch.object(common, 'add_exec_permission_to', autospec=True)
    @mock.patch.object(firmware_controller, 'tempfile', autospec=True)
    @mock.patch.object(firmware_controller, 'os', autospec=True)
    @mock.patch.object(firmware_controller, '_get_firmware_file_in_new_path',
                       autospec=True)
    @mock.patch.object(firmware_controller, 'shutil', autospec=True)
    def test_extract_deletes_temp_extracted_folder_before_raising_exception(
            self, shutil_mock, _get_firmware_file_in_new_path_mock,
            os_mock, tempfile_mock, add_exec_permission_to_mock):
        # | GIVEN |
        os_mock.path.splitext.return_value = ('any_file', '.rpm')

        fw_img_extractor = (firmware_controller.
                            get_fw_extractor(self.any_rpm_file))
        # Now mock the _do_extract method of fw_img_extractor instance
        exc = exception.ImageExtractionFailed(
            image_ref=self.any_rpm_file, reason='God only knows!')
        _do_extract_mock = mock.MagicMock(side_effect=exc)
        fw_img_extractor._do_extract = _do_extract_mock
        # | WHEN | & | THEN |
        self.assertRaises(exception.ImageExtractionFailed,
                          fw_img_extractor.extract)
        shutil_mock.rmtree.assert_called_once_with(
            tempfile_mock.mkdtemp.return_value, ignore_errors=True)

    @mock.patch.object(common, 'add_exec_permission_to', autospec=True)
    @mock.patch.object(firmware_controller, 'tempfile', autospec=True)
    @mock.patch.object(firmware_controller, 'os', autospec=True)
    @mock.patch.object(firmware_controller, '_extract_scexe_file',
                       autospec=True)
    @mock.patch.object(firmware_controller, '_get_firmware_file_in_new_path',
                       autospec=True)
    @mock.patch.object(firmware_controller, 'shutil', autospec=True)
    def test_extract_method_raises_exception_if_raw_fw_file_not_found(
            self, shutil_mock, _get_firmware_file_in_new_path_mock,
            _extract_scexe_mock, os_mock, tempfile_mock,
            add_exec_permission_to_mock):
        # | GIVEN |
        os_mock.path.splitext.return_value = ('any_file', '.scexe')
        _get_firmware_file_in_new_path_mock.return_value = None
        fw_img_extractor = (firmware_controller.
                            get_fw_extractor(self.any_scexe_file))
        # | WHEN | & | THEN |
        self.assertRaises(exception.InvalidInputError,
                          fw_img_extractor.extract)

    @mock.patch.object(common, 'add_exec_permission_to', autospec=True)
    @mock.patch.object(firmware_controller, 'tempfile', autospec=True)
    @mock.patch.object(firmware_controller, '_extract_scexe_file',
                       autospec=True)
    @mock.patch.object(firmware_controller, '_extract_rpm_file', autospec=True)
    # don't use autospec=True here(below one), setting side_effect
    # causes issue. refer https://bugs.python.org/issue17826
    @mock.patch.object(firmware_controller, '_get_firmware_file_in_new_path')
    @mock.patch.object(firmware_controller, 'shutil', autospec=True)
    def test_successive_calls_to_extract_method(
            self, shutil_mock, _get_firmware_file_in_new_path_mock,
            _extract_rpm_mock, _extract_scexe_mock, tempfile_mock,
            add_exec_permission_to_mock):
        """This is more of an integration test of the extract method

        """
        # | GIVEN |
        firmware_files = [
            self.any_scexe_file,
            'any_file.bin',
            self.any_rpm_file,
        ]
        actual_raw_fw_files = []
        expected_raw_fw_files = [
            ('extracted_file_from_scexe', True),
            ('any_file.bin', False),
            ('extracted_file_from_rpm', True)
        ]
        _get_firmware_file_in_new_path_mock.side_effect = [
            'extracted_file_from_scexe',
            'extracted_file_from_rpm',
        ]
        tempfile_mock.mkdtemp.side_effect = [
            '/tmp', '/tmp', '/tmp'
        ]
        # | WHEN |
        for fw_file in firmware_files:
            fw_img_extractor = firmware_controller.get_fw_extractor(fw_file)
            raw_fw_file, is_extracted = fw_img_extractor.extract()
            actual_raw_fw_files.append((raw_fw_file, is_extracted))
        # | THEN |
        self.assertSequenceEqual(actual_raw_fw_files, expected_raw_fw_files)


@ddt.ddt
class FirmwareImageUploaderTestCase(unittest.TestCase):

    def setUp(self):
        # | BEFORE_EACH |
        self.any_scexe_file = 'any_file.scexe'
        self.any_rpm_file = 'any_file.rpm'

    @mock.patch.object(firmware_controller.FirmwareImageUploader,
                       '_get_socket', autospec=True)
    @mock.patch.object(firmware_controller, 'socket')
    @mock.patch.object(__builtin__, 'open', autospec=True)
    def test_upload_file_to_returns_cookie_after_successful_upload(
            self, open_mock, socket_mock, _get_socket_mock):
        # | GIVEN |
        sock_mock = _get_socket_mock.return_value
        sock_mock.read.side_effect = [b'data returned from socket with ',
                                      b'Set-Cookie: blah_blah_cookie',
                                      b'']
        fw_img_uploader = (firmware_controller.
                           FirmwareImageUploader('any_raw_file'))
        # | WHEN |
        cookie = fw_img_uploader.upload_file_to(('host', 'port'), 60)
        # | THEN |
        self.assertEqual('blah_blah_cookie', cookie)

    @mock.patch.object(firmware_controller.FirmwareImageUploader,
                       '_get_socket', autospec=True)
    @mock.patch.object(firmware_controller, 'socket')
    @mock.patch.object(__builtin__, 'open', autospec=True)
    def test_upload_file_to_throws_exception_when_cookie_not_returned(
            self, open_mock, socket_mock, _get_socket_mock):
        # | GIVEN |
        sock_mock = _get_socket_mock.return_value
        sock_mock.read.side_effect = [b'data returned from socket with ',
                                      b'No-Cookie',
                                      b'']
        fw_img_uploader = (firmware_controller.
                           FirmwareImageUploader('any_raw_file'))
        # | WHEN | & | THEN |
        self.assertRaises(exception.IloError, fw_img_uploader.upload_file_to,
                          ('host', 'port'), 60)

    @mock.patch.object(firmware_controller, 'socket')
    @mock.patch.object(firmware_controller, 'ssl')
    def test__get_socket_returns_ssl_wrapped_socket_if_all_goes_well(
            self, ssl_mock, socket_mock):
        # | GIVEN |
        socket_mock.getaddrinfo().__iter__.return_value = [
            # (family, socktype, proto, canonname, sockaddr),
            (10, 1, 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)),
            (2, 1, 6, '', ('0.0.0.0-some-address', 80)),
        ]
        fw_img_uploader = (firmware_controller.
                           FirmwareImageUploader('any_raw_file'))
        fw_img_uploader.hostname = 'host'
        fw_img_uploader.port = 443
        fw_img_uploader.timeout = 'timeout'
        # | WHEN |
        returned_sock = fw_img_uploader._get_socket()
        # | THEN |
        socket_mock.socket.assert_has_calls([
            mock.call(10, 1, 6),
            mock.call().settimeout('timeout'),
            mock.call().connect(
                ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)),

            mock.call(2, 1, 6),
            mock.call().settimeout('timeout'),
            mock.call().connect(('0.0.0.0-some-address', 80)),
        ])
        self.assertTrue(ssl_mock.wrap_socket.called)
        self.assertEqual(returned_sock, ssl_mock.wrap_socket())

    @ddt.data(('foo.bar.blah.blah', exception.IloConnectionError),)
    @ddt.unpack
    def test__get_socket_throws_exception_in_case_of_failed_connection(
            self, input_hostname, expected_exception_type):
        # | GIVEN |
        fw_img_uploader = (firmware_controller.
                           FirmwareImageUploader('any_raw_file'))
        fw_img_uploader.hostname = input_hostname
        fw_img_uploader.port = 443
        fw_img_uploader.timeout = 1
        # | WHEN | & | THEN |
        self.assertRaises(expected_exception_type, fw_img_uploader._get_socket)