"Fossies" - the Fresh Open Source Software Archive

Member "cinder-14.0.2/cinder/tests/unit/api/contrib/test_hosts.py" (4 Oct 2019, 9426 Bytes) of package /linux/misc/openstack/cinder-14.0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright (c) 2011 OpenStack Foundation
    2 # All Rights Reserved.
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 
   16 import datetime
   17 import mock
   18 
   19 import iso8601
   20 from oslo_utils import timeutils
   21 import webob.exc
   22 
   23 from cinder.api.contrib import hosts as os_hosts
   24 from cinder.common import constants
   25 from cinder import context
   26 from cinder import exception
   27 from cinder.objects import service
   28 from cinder import test
   29 from cinder.tests.unit import fake_constants
   30 from cinder.tests.unit import utils as test_utils
   31 
   32 
   33 created_time = datetime.datetime(2012, 11, 14, 1, 20, 41, 95099)
   34 curr_time = datetime.datetime(2013, 7, 3, 0, 0, 1)
   35 
   36 SERVICE_LIST = [
   37     {'created_at': created_time, 'updated_at': curr_time,
   38      'host': 'test.host.1', 'topic': 'cinder-volume', 'disabled': 0,
   39      'availability_zone': 'cinder',
   40      'uuid': 'a3a593da-7f8d-4bb7-8b4c-f2bc1e0b4824'},
   41     {'created_at': created_time, 'updated_at': curr_time,
   42      'host': 'test.host.1', 'topic': 'cinder-volume', 'disabled': 0,
   43      'availability_zone': 'cinder',
   44      'uuid': '4200b32b-0bf9-436c-86b2-0675f6ac218e'},
   45     {'created_at': created_time, 'updated_at': curr_time,
   46      'host': 'test.host.1', 'topic': 'cinder-volume', 'disabled': 0,
   47      'availability_zone': 'cinder',
   48      'uuid': '6d91e7f5-ca17-4e3b-bf4f-19ca77166dd7'},
   49     {'created_at': created_time, 'updated_at': curr_time,
   50      'host': 'test.host.1', 'topic': 'cinder-volume', 'disabled': 0,
   51      'availability_zone': 'cinder',
   52      'uuid': '18417850-2ca9-43d1-9619-ae16bfb0f655'},
   53     {'created_at': created_time, 'updated_at': None,
   54      'host': 'test.host.1', 'topic': 'cinder-volume', 'disabled': 0,
   55      'availability_zone': 'cinder',
   56      'uuid': 'f838f35c-4035-464f-9792-ce60e390c13d'},
   57 ]
   58 
   59 LIST_RESPONSE = [{'service-status': 'available', 'service': 'cinder-volume',
   60                   'zone': 'cinder', 'service-state': 'enabled',
   61                   'host_name': 'test.host.1', 'last-update': curr_time,
   62                   },
   63                  {'service-status': 'available', 'service': 'cinder-volume',
   64                   'zone': 'cinder', 'service-state': 'enabled',
   65                   'host_name': 'test.host.1', 'last-update': curr_time,
   66                   },
   67                  {'service-status': 'available', 'service': 'cinder-volume',
   68                   'zone': 'cinder', 'service-state': 'enabled',
   69                   'host_name': 'test.host.1', 'last-update': curr_time,
   70                   },
   71                  {'service-status': 'available', 'service': 'cinder-volume',
   72                   'zone': 'cinder', 'service-state': 'enabled',
   73                   'host_name': 'test.host.1', 'last-update': curr_time,
   74                   },
   75                  {'service-status': 'unavailable', 'service': 'cinder-volume',
   76                   'zone': 'cinder', 'service-state': 'enabled',
   77                   'host_name': 'test.host.1', 'last-update': None,
   78                   }, ]
   79 
   80 
   81 def stub_utcnow(with_timezone=False):
   82     tzinfo = iso8601.UTC if with_timezone else None
   83     return datetime.datetime(2013, 7, 3, 0, 0, 2, tzinfo=tzinfo)
   84 
   85 
   86 class FakeRequest(object):
   87     environ = {'cinder.context': context.get_admin_context()}
   88     GET = {}
   89 
   90 
   91 class FakeRequestWithcinderZone(object):
   92     environ = {'cinder.context': context.get_admin_context()}
   93     GET = {'zone': 'cinder'}
   94 
   95 
   96 class HostTestCase(test.TestCase):
   97     """Test Case for hosts."""
   98 
   99     def setUp(self):
  100         super(HostTestCase, self).setUp()
  101         self.controller = os_hosts.HostController()
  102         self.req = FakeRequest()
  103         self.patch('cinder.db.service_get_all', autospec=True,
  104                    return_value=SERVICE_LIST)
  105         self.mock_object(timeutils, 'utcnow', stub_utcnow)
  106 
  107     def _test_host_update(self, host, key, val, expected_value):
  108         body = {key: val}
  109         result = self.controller.update(self.req, host, body=body)
  110         self.assertEqual(expected_value, result[key])
  111 
  112     def test_list_hosts(self):
  113         """Verify that the volume hosts are returned."""
  114         hosts = os_hosts._list_hosts(self.req)
  115         self.assertEqual(LIST_RESPONSE, hosts)
  116 
  117         cinder_hosts = os_hosts._list_hosts(self.req, constants.VOLUME_BINARY)
  118         expected = [host for host in LIST_RESPONSE
  119                     if host['service'] == constants.VOLUME_BINARY]
  120         self.assertEqual(expected, cinder_hosts)
  121 
  122     def test_list_hosts_with_zone(self):
  123         req = FakeRequestWithcinderZone()
  124         hosts = os_hosts._list_hosts(req)
  125         self.assertEqual(LIST_RESPONSE, hosts)
  126 
  127     def test_bad_status_value(self):
  128         self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  129                           self.req, 'test.host.1', body={'status': 'bad'})
  130         self.assertRaises(webob.exc.HTTPBadRequest,
  131                           self.controller.update,
  132                           self.req,
  133                           'test.host.1',
  134                           body={'status': 'disablabc'})
  135 
  136     def test_bad_update_key(self):
  137         bad_body = {'crazy': 'bad'}
  138         self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  139                           self.req, 'test.host.1', body=bad_body)
  140 
  141     def test_bad_update_key_and_correct_udpate_key(self):
  142         bad_body = {'status': 'disable', 'crazy': 'bad'}
  143         self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  144                           self.req, 'test.host.1', body=bad_body)
  145 
  146     def test_good_udpate_keys(self):
  147         body = {'status': 'disable'}
  148         self.assertRaises(NotImplementedError, self.controller.update,
  149                           self.req, 'test.host.1', body=body)
  150 
  151     def test_bad_host(self):
  152         self.assertRaises(exception.HostNotFound,
  153                           self.controller.update,
  154                           self.req,
  155                           'bogus_host_name',
  156                           body={'disabled': 0})
  157 
  158     @mock.patch.object(service.Service, 'get_by_host_and_topic')
  159     def test_show_host(self, mock_get_host):
  160         host = 'test_host'
  161         test_service = service.Service(id=1, host=host,
  162                                        binary=constants.VOLUME_BINARY,
  163                                        topic=constants.VOLUME_TOPIC)
  164         mock_get_host.return_value = test_service
  165 
  166         ctxt1 = context.RequestContext(project_id=fake_constants.PROJECT_ID,
  167                                        is_admin=True)
  168         ctxt2 = context.RequestContext(project_id=fake_constants.PROJECT2_ID,
  169                                        is_admin=True)
  170         # Create two volumes with different project.
  171         volume1 = test_utils.create_volume(ctxt1,
  172                                            host=host, size=1)
  173         test_utils.create_volume(ctxt2, host=host, size=1)
  174         # This volume is not on the same host. It should not be counted.
  175         test_utils.create_volume(ctxt2, host='fake_host', size=1)
  176         test_utils.create_snapshot(ctxt1, volume_id=volume1.id)
  177 
  178         resp = self.controller.show(self.req, host)
  179 
  180         host_resp = resp['host']
  181         # There are 3 resource list: total, project1, project2
  182         self.assertEqual(3, len(host_resp))
  183         expected = [
  184             {
  185                 "resource": {
  186                     "volume_count": "2",
  187                     "total_volume_gb": "2",
  188                     "host": "test_host",
  189                     "total_snapshot_gb": "1",
  190                     "project": "(total)",
  191                     "snapshot_count": "1"}
  192             },
  193             {
  194                 "resource": {
  195                     "volume_count": "1",
  196                     "total_volume_gb": "1",
  197                     "host": "test_host",
  198                     "project": fake_constants.PROJECT2_ID,
  199                     "total_snapshot_gb": "0",
  200                     "snapshot_count": "0"}
  201             },
  202             {
  203                 "resource": {
  204                     "volume_count": "1",
  205                     "total_volume_gb": "1",
  206                     "host": "test_host",
  207                     "total_snapshot_gb": "1",
  208                     "project": fake_constants.PROJECT_ID,
  209                     "snapshot_count": "1"}
  210             }
  211         ]
  212         self.assertListEqual(expected, sorted(
  213             host_resp, key=lambda h: h['resource']['project']))
  214 
  215     def test_show_forbidden(self):
  216         self.req.environ['cinder.context'].is_admin = False
  217         dest = 'dummydest'
  218         self.assertRaises(exception.PolicyNotAuthorized,
  219                           self.controller.show,
  220                           self.req, dest)
  221         self.req.environ['cinder.context'].is_admin = True
  222 
  223     def test_show_host_not_exist(self):
  224         """A host given as an argument does not exists."""
  225         self.req.environ['cinder.context'].is_admin = True
  226         dest = 'dummydest'
  227         self.assertRaises(exception.ServiceNotFound,
  228                           self.controller.show,
  229                           self.req, dest)