summaryrefslogtreecommitdiff
path: root/nova/tests/functional/api_sample_tests/test_hypervisors.py
blob: f402f9ebdebf39f8afaa55a9b00c2ce746f5c1dd (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
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
# Copyright 2012 Nebula, Inc.
# Copyright 2013 IBM Corp.
#
#    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 mock

from nova.tests.functional.api_sample_tests import api_sample_base


class HypervisorsSampleJsonTests(api_sample_base.ApiSampleTestBaseV21):
    ADMIN_API = True
    sample_dir = "os-hypervisors"

    def test_hypervisors_list(self):
        response = self._do_get('os-hypervisors')
        self._verify_response('hypervisors-list-resp', {}, response, 200)

    def test_hypervisors_search(self):
        response = self._do_get('os-hypervisors/fake/search')
        self._verify_response('hypervisors-search-resp', {}, response, 200)

    def test_hypervisors_without_servers(self):
        response = self._do_get('os-hypervisors/fake/servers')
        self._verify_response('hypervisors-without-servers-resp',
                              {}, response, 200)

    @mock.patch("nova.compute.api.HostAPI.instance_get_all_by_host")
    def test_hypervisors_with_servers(self, mock_instance_get):
        instance = [
            {
                "deleted": None,
                "name": "test_server1",
                "uuid": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
            },
            {
                "deleted": None,
                "name": "test_server2",
                "uuid": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
            }]

        mock_instance_get.return_value = instance
        response = self._do_get('os-hypervisors/fake/servers')
        self._verify_response('hypervisors-with-servers-resp', {},
                              response, 200)

    def test_hypervisors_detail(self):
        hypervisor_id = '1'
        subs = {
            'hypervisor_id': hypervisor_id,
            'service_id': '[0-9]+',
        }
        response = self._do_get('os-hypervisors/detail')
        self._verify_response('hypervisors-detail-resp', subs, response, 200)

    def test_hypervisors_show(self):
        hypervisor_id = '1'
        subs = {
            'hypervisor_id': hypervisor_id,
            'service_id': '[0-9]+',
        }
        response = self._do_get('os-hypervisors/%s' % hypervisor_id)
        self._verify_response('hypervisors-show-resp', subs, response, 200)

    def test_hypervisors_statistics(self):
        response = self._do_get('os-hypervisors/statistics')
        self._verify_response('hypervisors-statistics-resp', {}, response, 200)

    def test_hypervisors_uptime(self):
        def fake_get_host_uptime(self, context, hyp):
            return (" 08:32:11 up 93 days, 18:25, 12 users,  load average:"
                    " 0.20, 0.12, 0.14")

        self.stub_out('nova.compute.api.HostAPI.get_host_uptime',
                      fake_get_host_uptime)
        hypervisor_id = '1'
        response = self._do_get('os-hypervisors/%s/uptime' % hypervisor_id)
        subs = {
            'hypervisor_id': hypervisor_id,
        }
        self._verify_response('hypervisors-uptime-resp', subs, response, 200)


class HypervisorsSampleJson228Tests(HypervisorsSampleJsonTests):
    microversion = '2.28'
    scenarios = [('v2_28', {'api_major_version': 'v2.1'})]

    def setUp(self):
        super(HypervisorsSampleJson228Tests, self).setUp()
        self.api.microversion = self.microversion


class HypervisorsSampleJson233Tests(api_sample_base.ApiSampleTestBaseV21):
    ADMIN_API = True
    sample_dir = "os-hypervisors"
    microversion = '2.33'
    scenarios = [('v2_33', {'api_major_version': 'v2.1'})]

    def setUp(self):
        super(HypervisorsSampleJson233Tests, self).setUp()
        self.api.microversion = self.microversion
        # Start a new compute service to fake a record with hypervisor id=2
        # for pagination test.
        host = 'host1'
        self.start_service('compute', host=host)

    def test_hypervisors_list(self):
        response = self._do_get('os-hypervisors?limit=1&marker=1')
        self._verify_response('hypervisors-list-resp', {}, response, 200)

    def test_hypervisors_detail(self):
        subs = {
            'hypervisor_id': '2',
            'host': 'host1',
            'host_name': 'host1',
            'service_id': '[0-9]+',
        }
        response = self._do_get('os-hypervisors/detail?limit=1&marker=1')
        self._verify_response('hypervisors-detail-resp', subs, response, 200)


class HypervisorsSampleJson253Tests(HypervisorsSampleJson228Tests):
    microversion = '2.53'
    scenarios = [('v2_53', {'api_major_version': 'v2.1'})]

    def setUp(self):
        super(HypervisorsSampleJson253Tests, self).setUp()
        self.compute_node_1 = self.compute.service_ref.compute_node

    def generalize_subs(self, subs, vanilla_regexes):
        """Give the test a chance to modify subs after the server response
        was verified, and before the on-disk doc/api_samples file is checked.
        """
        # When comparing the template to the sample we just care that the
        # hypervisor id and service id are UUIDs.
        subs['hypervisor_id'] = vanilla_regexes['uuid']
        subs['service_id'] = vanilla_regexes['uuid']
        return subs

    def test_hypervisors_list(self):
        # Start another compute service to get a 2nd compute for paging tests.
        compute_node_2 = self.start_service(
            'compute', host='host2').service_ref.compute_node
        marker = self.compute_node_1.uuid
        response = self._do_get('os-hypervisors?limit=1&marker=%s' % marker)
        subs = {'hypervisor_id': compute_node_2.uuid}
        self._verify_response('hypervisors-list-resp', subs, response, 200)

    def test_hypervisors_detail(self):
        # Start another compute service to get a 2nd compute for paging tests.
        host = 'host2'
        service_2 = self.start_service('compute', host=host).service_ref
        compute_node_2 = service_2.compute_node
        marker = self.compute_node_1.uuid
        subs = {
            'hypervisor_id': compute_node_2.uuid,
            'service_id': service_2.uuid
        }
        response = self._do_get('os-hypervisors/detail?limit=1&marker=%s' %
                                marker)
        self._verify_response('hypervisors-detail-resp', subs, response, 200)

    @mock.patch("nova.compute.api.HostAPI.instance_get_all_by_host")
    def test_hypervisors_detail_with_servers(self, instance_get_all_by_host):
        """List hypervisors with details and with hosted servers."""
        instances = [
            {
                "name": "test_server1",
                "uuid": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
            },
            {
                "name": "test_server2",
                "uuid": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
            }]
        instance_get_all_by_host.return_value = instances
        response = self._do_get('os-hypervisors/detail?with_servers=1')
        subs = {
            'hypervisor_id': self.compute_node_1.uuid,
            'service_id': self.compute.service_ref.uuid,
        }
        self._verify_response('hypervisors-detail-with-servers-resp',
                              subs, response, 200)

    def test_hypervisors_search(self):
        """The search route is deprecated in 2.53 and is now a query parameter
        on the GET /os-hypervisors API.
        """
        response = self._do_get(
            'os-hypervisors?hypervisor_hostname_pattern=fake')
        subs = {'hypervisor_id': self.compute_node_1.uuid}
        self._verify_response('hypervisors-search-resp', subs, response, 200)

    @mock.patch("nova.compute.api.HostAPI.instance_get_all_by_host")
    def test_hypervisors_with_servers(self, instance_get_all_by_host):
        """The servers route is deprecated in 2.53 and is now a query parameter
        on the GET /os-hypervisors API.
        """
        instances = [
            {
                "name": "test_server1",
                "uuid": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
            },
            {
                "name": "test_server2",
                "uuid": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
            }]
        instance_get_all_by_host.return_value = instances
        response = self._do_get('os-hypervisors?with_servers=true')
        subs = {'hypervisor_id': self.compute_node_1.uuid}
        self._verify_response('hypervisors-with-servers-resp', subs,
                              response, 200)

    def test_hypervisors_without_servers(self):
        # This is the same as GET /os-hypervisors in 2.53 which is covered by
        # test_hypervisors_list already.
        pass

    def test_hypervisors_uptime(self):
        def fake_get_host_uptime(self, context, hyp):
            return (" 08:32:11 up 93 days, 18:25, 12 users,  load average:"
                    " 0.20, 0.12, 0.14")

        self.stub_out('nova.compute.api.HostAPI.get_host_uptime',
                      fake_get_host_uptime)
        hypervisor_id = self.compute_node_1.uuid
        response = self._do_get('os-hypervisors/%s/uptime' % hypervisor_id)
        subs = {
            'hypervisor_id': hypervisor_id,
        }
        self._verify_response('hypervisors-uptime-resp', subs, response, 200)

    def test_hypervisors_show(self):
        hypervisor_id = self.compute_node_1.uuid
        subs = {
            'hypervisor_id': hypervisor_id,
            'service_id': self.compute.service_ref.uuid,
        }
        response = self._do_get('os-hypervisors/%s' % hypervisor_id)
        self._verify_response('hypervisors-show-resp', subs, response, 200)

    @mock.patch("nova.compute.api.HostAPI.instance_get_all_by_host")
    def test_hypervisors_show_with_servers(self, instance_get_all_by_host):
        """Tests getting details for a specific hypervisor and including the
        hosted servers in the response.
        """
        instances = [
            {
                "name": "test_server1",
                "uuid": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
            },
            {
                "name": "test_server2",
                "uuid": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
            }]
        instance_get_all_by_host.return_value = instances
        hypervisor_id = self.compute_node_1.uuid
        subs = {
            'hypervisor_id': hypervisor_id,
            'service_id': self.compute.service_ref.uuid,
        }
        response = self._do_get('os-hypervisors/%s?with_servers=1' %
                                hypervisor_id)
        self._verify_response('hypervisors-show-with-servers-resp', subs,
                              response, 200)


class HypervisorsSampleJson288Tests(HypervisorsSampleJson253Tests):
    microversion = '2.88'
    scenarios = [('v2_88', {'api_major_version': 'v2.1'})]

    def test_hypervisors_uptime(self):
        """The uptime route is deprecated in 2.88 and now returns a 404.
        """
        hypervisor_id = '1'
        response = self._do_get('os-hypervisors/%s/statistics' % hypervisor_id)
        self.assertEqual(404, response.status_code)

    def test_hypervisors_statistics(self):
        """The statistics route is deprecated in 2.88 and now returns a 404.
        """
        response = self._do_get('os-hypervisors/statistics')
        self.assertEqual(404, response.status_code)