File: test_hosts.py

package info (click to toggle)
python-blazarclient 4.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 468 kB
  • sloc: python: 2,742; makefile: 18; sh: 2
file content (244 lines) | stat: -rw-r--r-- 8,026 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
# Copyright (c) 2018 NTT
#
# 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 argparse
from unittest import mock

from blazarclient import shell
from blazarclient import tests
from blazarclient.v1.shell_commands import hosts


class CreateHostTest(tests.TestCase):

    def setUp(self):
        super(CreateHostTest, self).setUp()
        self.create_host = hosts.CreateHost(shell.BlazarShell(), mock.Mock())

    def test_args2body(self):
        args = argparse.Namespace(
            name='test-host',
            extra_capabilities=[
                'extra_key1=extra_value1',
                'extra_key2=extra_value2',
            ]
        )

        expected = {
            'name': 'test-host',
            'extra_key1': 'extra_value1',
            'extra_key2': 'extra_value2',
        }

        ret = self.create_host.args2body(args)
        self.assertDictEqual(ret, expected)


class UpdateHostTest(tests.TestCase):

    def create_update_command(self, list_value):
        mock_host_manager = mock.Mock()
        mock_host_manager.list.return_value = list_value

        mock_client = mock.Mock()
        mock_client.host = mock_host_manager

        blazar_shell = shell.BlazarShell()
        blazar_shell.client = mock_client
        return hosts.UpdateHost(blazar_shell, mock.Mock()), mock_host_manager

    def test_update_host(self):
        list_value = [
            {'id': '101', 'hypervisor_hostname': 'host-1'},
            {'id': '201', 'hypervisor_hostname': 'host-2'},
        ]
        update_host, host_manager = self.create_update_command(list_value)
        args = argparse.Namespace(
            id='101',
            extra_capabilities=[
                'key1=value1',
                'key2=value2'
            ])
        expected = {
            'values': {
                'key1': 'value1',
                'key2': 'value2'
            }
        }
        update_host.run(args)
        host_manager.update.assert_called_once_with('101', **expected)

    def test_update_host_with_name(self):
        list_value = [
            {'id': '101', 'hypervisor_hostname': 'host-1'},
            {'id': '201', 'hypervisor_hostname': 'host-2'},
        ]
        update_host, host_manager = self.create_update_command(list_value)
        args = argparse.Namespace(
            id='host-1',
            extra_capabilities=[
                'key1=value1',
                'key2=value2'
            ])
        expected = {
            'values': {
                'key1': 'value1',
                'key2': 'value2'
            }
        }
        update_host.run(args)
        host_manager.update.assert_called_once_with('101', **expected)

    def test_update_host_with_name_startwith_number(self):
        list_value = [
            {'id': '101', 'hypervisor_hostname': '1-host'},
            {'id': '201', 'hypervisor_hostname': '2-host'},
        ]
        update_host, host_manager = self.create_update_command(list_value)
        args = argparse.Namespace(
            id='1-host',
            extra_capabilities=[
                'key1=value1',
                'key2=value2'
            ])
        expected = {
            'values': {
                'key1': 'value1',
                'key2': 'value2'
            }
        }
        update_host.run(args)
        host_manager.update.assert_called_once_with('101', **expected)


class ShowHostTest(tests.TestCase):

    def create_show_command(self, list_value, get_value):
        mock_host_manager = mock.Mock()
        mock_host_manager.list.return_value = list_value
        mock_host_manager.get.return_value = get_value

        mock_client = mock.Mock()
        mock_client.host = mock_host_manager

        blazar_shell = shell.BlazarShell()
        blazar_shell.client = mock_client
        return hosts.ShowHost(blazar_shell, mock.Mock()), mock_host_manager

    def test_show_host(self):
        list_value = [
            {'id': '101', 'hypervisor_hostname': 'host-1'},
            {'id': '201', 'hypervisor_hostname': 'host-2'},
        ]
        get_value = {
            'id': '101', 'hypervisor_hostname': 'host-1'}

        show_host, host_manager = self.create_show_command(list_value,
                                                           get_value)

        args = argparse.Namespace(id='101')
        expected = [('hypervisor_hostname', 'id'), ('host-1', '101')]

        ret = show_host.get_data(args)
        self.assertEqual(ret, expected)

        host_manager.get.assert_called_once_with('101')

    def test_show_host_with_name(self):
        list_value = [
            {'id': '101', 'hypervisor_hostname': 'host-1'},
            {'id': '201', 'hypervisor_hostname': 'host-2'},
        ]
        get_value = {
            'id': '101', 'hypervisor_hostname': 'host-1'}

        show_host, host_manager = self.create_show_command(list_value,
                                                           get_value)

        args = argparse.Namespace(id='host-1')
        expected = [('hypervisor_hostname', 'id'), ('host-1', '101')]

        ret = show_host.get_data(args)
        self.assertEqual(ret, expected)

        host_manager.get.assert_called_once_with('101')

    def test_show_host_with_name_startwith_number(self):
        list_value = [
            {'id': '101', 'hypervisor_hostname': '1-host'},
            {'id': '201', 'hypervisor_hostname': '2-host'},
        ]
        get_value = {
            'id': '101', 'hypervisor_hostname': '1-host'}

        show_host, host_manager = self.create_show_command(list_value,
                                                           get_value)
        args = argparse.Namespace(id='1-host')
        expected = [('hypervisor_hostname', 'id'), ('1-host', '101')]

        ret = show_host.get_data(args)
        self.assertEqual(ret, expected)

        host_manager.get.assert_called_once_with('101')


class DeleteHostTest(tests.TestCase):

    def create_delete_command(self, list_value):
        mock_host_manager = mock.Mock()
        mock_host_manager.list.return_value = list_value

        mock_client = mock.Mock()
        mock_client.host = mock_host_manager

        blazar_shell = shell.BlazarShell()
        blazar_shell.client = mock_client
        return hosts.DeleteHost(blazar_shell, mock.Mock()), mock_host_manager

    def test_delete_host(self):
        list_value = [
            {'id': '101', 'hypervisor_hostname': 'host-1'},
            {'id': '201', 'hypervisor_hostname': 'host-2'},
        ]
        delete_host, host_manager = self.create_delete_command(list_value)

        args = argparse.Namespace(id='101')
        delete_host.run(args)

        host_manager.delete.assert_called_once_with('101')

    def test_delete_host_with_name(self):
        list_value = [
            {'id': '101', 'hypervisor_hostname': 'host-1'},
            {'id': '201', 'hypervisor_hostname': 'host-2'},
        ]
        delete_host, host_manager = self.create_delete_command(list_value)

        args = argparse.Namespace(id='host-1')
        delete_host.run(args)

        host_manager.delete.assert_called_once_with('101')

    def test_delete_host_with_name_startwith_number(self):
        list_value = [
            {'id': '101', 'hypervisor_hostname': '1-host'},
            {'id': '201', 'hypervisor_hostname': '2-host'},
        ]
        delete_host, host_manager = self.create_delete_command(list_value)

        args = argparse.Namespace(id='1-host')
        delete_host.run(args)

        host_manager.delete.assert_called_once_with('101')