File: test_main.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 (102 lines) | stat: -rw-r--r-- 4,584 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
# Copyright 2017 Hewlett Packard Enterprise Development LP
# 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 json
from unittest import mock

from sushy import auth as sushy_auth
import testtools

from proliantutils import exception
from proliantutils.redfish import connector
from proliantutils.redfish import main
from proliantutils.redfish.resources.account_service import account_service
from proliantutils.redfish.resources.manager import manager
from proliantutils.redfish.resources.system import system
from proliantutils.redfish.resources import update_service


class HPESushyTestCase(testtools.TestCase):

    @mock.patch.object(sushy_auth, 'SessionOrBasicAuth', autospec=True)
    @mock.patch.object(connector, 'HPEConnector', autospec=True)
    def setUp(self, connector_mock, mock_auth):
        super(HPESushyTestCase, self).setUp()
        with open('proliantutils/tests/redfish/'
                  'json_samples/root.json', 'r') as f:
            root_json = json.loads(f.read())
        connector_mock.return_value.get.return_value.json.return_value = (
            root_json)
        self.hpe_sushy = main.HPESushy('https://1.2.3.4',
                                       username='foo',
                                       password='bar')

    def test_get_system_collection_path(self):
        self.assertEqual('/redfish/v1/Systems/',
                         self.hpe_sushy.get_system_collection_path())

    def test_get_system_collection_path_missing_systems_attr(self):
        self.hpe_sushy.json.pop('Systems')
        self.assertRaisesRegex(
            exception.MissingAttributeError,
            'The attribute Systems is missing',
            self.hpe_sushy.get_system_collection_path)

    @mock.patch.object(system, 'HPESystem', autospec=True)
    def test_get_system(self, mock_system):
        sys_inst = self.hpe_sushy.get_system('1234')
        self.assertIsInstance(sys_inst,
                              system.HPESystem.__class__)
        mock_system.assert_called_once_with(self.hpe_sushy._conn,
                                            '1234',
                                            self.hpe_sushy.redfish_version)

    def test_get_manager_collection_path(self):
        self.assertEqual('/redfish/v1/Managers/',
                         self.hpe_sushy.get_manager_collection_path())

    def test_get_manager_collection_path_missing_systems_attr(self):
        self.hpe_sushy.json.pop('Managers')
        self.assertRaisesRegex(
            exception.MissingAttributeError,
            'The attribute Managers is missing',
            self.hpe_sushy.get_manager_collection_path)

    @mock.patch.object(manager, 'HPEManager', autospec=True)
    def test_get_manager(self, mock_manager):
        sys_inst = self.hpe_sushy.get_manager('1234')
        self.assertIsInstance(sys_inst,
                              manager.HPEManager.__class__)
        mock_manager.assert_called_once_with(self.hpe_sushy._conn,
                                             '1234',
                                             self.hpe_sushy.redfish_version)

    @mock.patch.object(update_service, 'HPEUpdateService', autospec=True)
    def test_get_update_service(self, mock_update_service):
        us_inst = self.hpe_sushy.get_update_service()
        self.assertIsInstance(us_inst,
                              update_service.HPEUpdateService.__class__)
        mock_update_service.assert_called_once_with(
            self.hpe_sushy._conn, "/redfish/v1/UpdateService/",
            self.hpe_sushy.redfish_version)

    @mock.patch.object(account_service, 'HPEAccountService', autospec=True)
    def test_get_account_service(self, mock_account_service):
        acc_inst = self.hpe_sushy.get_account_service()
        self.assertIsInstance(acc_inst,
                              account_service.HPEAccountService.__class__)
        mock_account_service.assert_called_once_with(
            self.hpe_sushy._conn, "/redfish/v1/AccountService/",
            self.hpe_sushy.redfish_version)