File: test_shard.py

package info (click to toggle)
python-ironicclient 5.10.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,632 kB
  • sloc: python: 25,269; makefile: 22; sh: 8
file content (115 lines) | stat: -rw-r--r-- 4,068 bytes parent folder | download
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
# 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 unittest
from unittest.mock import patch

from ironicclient.v1.shard import ShardManager


class TestShardManager(unittest.TestCase):

    @patch('ironicclient.common.base.Manager._list')
    def test_list_shards(self, mock_list):
        # Mock response for the list of shards
        mock_response = [
            {'name': 'example_shard1', 'count': 47},
            {'name': 'example_shard2', 'count': 46},
            {'name': None, 'count': 3}  # Nodes with no shard assigned
        ]

        # Configure mock to return the mocked response
        mock_list.return_value = mock_response

        # Initialize the ShardManager
        shard_manager = ShardManager(api=None)  # `api=None` for simplicity

        # Perform the test call
        result = shard_manager.list(os_ironic_api_version="1.82")

        # Assertions
        mock_list.assert_called_once_with(
            shard_manager._path(None),
            "shards",
            os_ironic_api_version="1.82",
            global_request_id=None
        )
        self.assertEqual(len(result), 3)
        self.assertEqual(result[0]['name'], 'example_shard1')
        self.assertEqual(result[0]['count'], 47)
        self.assertEqual(result[1]['name'], 'example_shard2')
        self.assertEqual(result[1]['count'], 46)
        self.assertIsNone(result[2]['name'])
        self.assertEqual(result[2]['count'], 3)

    @patch('ironicclient.common.base.Manager._list')
    def test_list_shards_empty(self, mock_list):
        # Test when the shards list is empty
        mock_list.return_value = []

        # Initialize the ShardManager
        shard_manager = ShardManager(api=None)

        # Perform the test call
        result = shard_manager.list(os_ironic_api_version="1.82")

        # Assertions
        mock_list.assert_called_once_with(
            shard_manager._path(None),
            "shards",
            os_ironic_api_version="1.82",
            global_request_id=None
        )
        self.assertEqual(result, [])

    @patch('ironicclient.common.base.Manager._list')
    def test_list_shards_with_global_request_id(self, mock_list):
        # Test with global request ID
        mock_response = [
            {'name': 'example_shard1', 'count': 47},
            {'name': 'example_shard2', 'count': 46}
        ]
        mock_list.return_value = mock_response

        # Initialize the ShardManager
        shard_manager = ShardManager(api=None)

        # Perform the test call with global_request_id
        result = shard_manager.list(
            os_ironic_api_version="1.82",
            global_request_id="req-12345"
        )

        # Assertions
        mock_list.assert_called_once_with(
            shard_manager._path(None),
            "shards",
            os_ironic_api_version="1.82",
            global_request_id="req-12345"
        )
        self.assertEqual(result, mock_response)

    @patch('ironicclient.common.base.Manager._list')
    def test_list_shards_api_version_mismatch(self, mock_list):
        # Simulate a 404 error for an unsupported API version
        mock_list.side_effect = ValueError(
            "404 Not Found: The requested version is not supported"
        )

        # Initialize the ShardManager
        shard_manager = ShardManager(api=None)

        # Perform the test call and assert exception is raised
        with self.assertRaises(ValueError) as context:
            shard_manager.list(os_ironic_api_version="1.50")

        self.assertIn("404 Not Found", str(context.exception))