"Fossies" - the Fresh Open Source Software Archive

Member "horizon-14.0.4/openstack_dashboard/dashboards/project/overview/tests.py" (22 Oct 2019, 13106 Bytes) of package /linux/misc/openstack/horizon-14.0.4.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 2012 United States Government as represented by the
    2 # Administrator of the National Aeronautics and Space Administration.
    3 # All Rights Reserved.
    4 #
    5 # Copyright 2012 Nebula, Inc.
    6 #
    7 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    8 #    not use this file except in compliance with the License. You may obtain
    9 #    a copy of the License at
   10 #
   11 #         http://www.apache.org/licenses/LICENSE-2.0
   12 #
   13 #    Unless required by applicable law or agreed to in writing, software
   14 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   15 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   16 #    License for the specific language governing permissions and limitations
   17 #    under the License.
   18 
   19 import datetime
   20 import logging
   21 
   22 from django.test.utils import override_settings
   23 from django.urls import reverse
   24 from django.utils import timezone
   25 
   26 import mock
   27 
   28 from openstack_dashboard import api
   29 from openstack_dashboard.test import helpers as test
   30 from openstack_dashboard import usage
   31 
   32 
   33 INDEX_URL = reverse('horizon:project:overview:index')
   34 
   35 
   36 class UsageViewTests(test.TestCase):
   37 
   38     @test.create_mocks({api.nova: (
   39         'usage_get',
   40         'extension_supported',
   41     )})
   42     def _stub_api_calls(self, nova_stu_enabled=True,
   43                         stu_exception=False, overview_days_range=1,
   44                         quota_usage_overrides=None):
   45         self.mock_extension_supported.side_effect = [nova_stu_enabled,
   46                                                      nova_stu_enabled]
   47         if nova_stu_enabled:
   48             self._nova_stu_enabled(stu_exception,
   49                                    overview_days_range=overview_days_range)
   50 
   51         self._stub_tenant_quota_usages(overrides=quota_usage_overrides)
   52 
   53     def _check_api_calls(self, nova_stu_enabled=True,
   54                          stu_exception=False, overview_days_range=1):
   55         self.assert_mock_multiple_calls_with_same_arguments(
   56             self.mock_extension_supported, 2,
   57             mock.call('SimpleTenantUsage', test.IsHttpRequest()))
   58         if nova_stu_enabled:
   59             self._check_stu_enabled(stu_exception,
   60                                     overview_days_range=overview_days_range)
   61         else:
   62             self.mock_usage_get.assert_not_called()
   63         self._check_tenant_quota_usages()
   64 
   65     @staticmethod
   66     def _add_quota_usages(usages, quota_usages, excludes=None):
   67         excludes = excludes or []
   68         for k in quota_usages.usages:
   69             if k in excludes:
   70                 continue
   71             quota = quota_usages[k]['quota']
   72             if quota == float('inf'):
   73                 quota = -1
   74             usages.add_quota(api.base.Quota(k, quota))
   75             usages.tally(k, quota_usages[k]['used'])
   76 
   77     @test.create_mocks({usage.quotas: ('tenant_quota_usages',)})
   78     def _stub_tenant_quota_usages(self, overrides):
   79         usages_data = usage.quotas.QuotaUsage()
   80         self._add_quota_usages(usages_data, self.quota_usages.first(),
   81                                # At now, nova quota_usages contains
   82                                # volumes and gigabytes.
   83                                excludes=('volumes', 'gigabytes'))
   84         self._add_quota_usages(
   85             usages_data, self.neutron_quota_usages.first())
   86         self._add_quota_usages(usages_data, self.cinder_quota_usages.first())
   87         if overrides:
   88             for key, value in overrides.items():
   89                 if 'quota' in value:
   90                     usages_data.add_quota(api.base.Quota(key, value['quota']))
   91                 if 'used' in value:
   92                     usages_data.tally(key, value['used'])
   93         self.mock_tenant_quota_usages.return_value = usages_data
   94 
   95     def _check_tenant_quota_usages(self):
   96         self.mock_tenant_quota_usages.assert_called_once_with(
   97             test.IsHttpRequest())
   98 
   99     def _nova_stu_enabled(self, exception=False, overview_days_range=1):
  100         if exception:
  101             self.mock_usage_get.side_effect = exception
  102         else:
  103             usage = api.nova.NovaUsage(self.usages.first())
  104             self.mock_usage_get.return_value = usage
  105 
  106     def _check_stu_enabled(self, exception=False, overview_days_range=1):
  107         now = timezone.now()
  108         if overview_days_range:
  109             start_day = now - datetime.timedelta(days=overview_days_range)
  110         else:
  111             start_day = datetime.date(now.year, now.month, 1)
  112         start = datetime.datetime(start_day.year, start_day.month,
  113                                   start_day.day, 0, 0, 0, 0)
  114         end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0)
  115 
  116         self.mock_usage_get.assert_called_once_with(
  117             test.IsHttpRequest(), self.tenant.id, start, end)
  118 
  119     def _common_assertions(self, nova_stu_enabled,
  120                            maxTotalFloatingIps=50):
  121         res = self.client.get(reverse('horizon:project:overview:index'))
  122         usages = res.context['usage']
  123         self.assertTemplateUsed(res, 'project/overview/usage.html')
  124         self.assertIsInstance(usages, usage.ProjectUsage)
  125         self.assertEqual(nova_stu_enabled,
  126                          res.context['simple_tenant_usage_enabled'])
  127         if nova_stu_enabled:
  128             self.assertContains(res, 'form-inline')
  129         else:
  130             self.assertNotContains(res, 'form-inline')
  131         self.assertEqual(usages.limits['floatingip']['quota'],
  132                          maxTotalFloatingIps)
  133 
  134     @override_settings(OVERVIEW_DAYS_RANGE=None)
  135     def test_usage(self):
  136         self._test_usage(nova_stu_enabled=True, overview_days_range=None)
  137 
  138     def test_usage_1_day(self):
  139         self._test_usage(nova_stu_enabled=True)
  140 
  141     @override_settings(OVERVIEW_DAYS_RANGE=None)
  142     def test_usage_disabled(self):
  143         self._test_usage(nova_stu_enabled=False, overview_days_range=None)
  144 
  145     def _test_usage(self, nova_stu_enabled, overview_days_range=1):
  146         self._stub_api_calls(nova_stu_enabled,
  147                              overview_days_range=overview_days_range)
  148 
  149         self._common_assertions(nova_stu_enabled)
  150 
  151         self._check_api_calls(nova_stu_enabled,
  152                               overview_days_range=overview_days_range)
  153 
  154     def test_unauthorized(self):
  155         url = reverse('horizon:admin:volumes:index')
  156 
  157         # Avoid the log message in the test
  158         # when unauthorized exception will be logged
  159         logging.disable(logging.ERROR)
  160         res = self.client.get(url)
  161         logging.disable(logging.NOTSET)
  162 
  163         self.assertEqual(403, res.status_code)
  164 
  165     def test_usage_csv(self):
  166         self._test_usage_csv(nova_stu_enabled=True)
  167 
  168     @override_settings(OVERVIEW_DAYS_RANGE=1)
  169     def test_usage_csv_1_day(self):
  170         self._test_usage_csv(nova_stu_enabled=True, overview_days_range=1)
  171 
  172     def test_usage_csv_disabled(self):
  173         self._test_usage_csv(nova_stu_enabled=False)
  174 
  175     def _test_usage_csv(self, nova_stu_enabled=True, overview_days_range=1):
  176         self._stub_api_calls(nova_stu_enabled,
  177                              overview_days_range=overview_days_range)
  178 
  179         res = self.client.get(reverse('horizon:project:overview:index') +
  180                               "?format=csv")
  181         self.assertTemplateUsed(res, 'project/overview/usage.csv')
  182         self.assertIsInstance(res.context['usage'], usage.ProjectUsage)
  183         self._check_api_calls(nova_stu_enabled,
  184                               overview_days_range=overview_days_range)
  185 
  186     def test_usage_exception_usage(self):
  187         self._stub_api_calls(stu_exception=self.exceptions.nova)
  188 
  189         res = self.client.get(reverse('horizon:project:overview:index'))
  190         self.assertTemplateUsed(res, 'project/overview/usage.html')
  191         self.assertEqual(res.context['usage'].usage_list, [])
  192 
  193         self._check_api_calls(stu_exception=self.exceptions.nova)
  194 
  195     def test_usage_default_tenant(self):
  196         self._stub_api_calls()
  197 
  198         res = self.client.get(reverse('horizon:project:overview:index'))
  199         self.assertTemplateUsed(res, 'project/overview/usage.html')
  200         self.assertIsInstance(res.context['usage'], usage.ProjectUsage)
  201 
  202         self._check_api_calls()
  203 
  204     @test.update_settings(OPENSTACK_NEUTRON_NETWORK={'enable_quotas': True})
  205     def test_usage_with_neutron(self):
  206         self._test_usage_with_neutron(neutron_sg_enabled=True)
  207 
  208     @test.update_settings(OPENSTACK_NEUTRON_NETWORK={'enable_quotas': True})
  209     def test_usage_with_neutron_nova_security_group(self):
  210         self._test_usage_with_neutron(neutron_sg_enabled=False)
  211 
  212     @test.update_settings(OPENSTACK_NEUTRON_NETWORK={'enable_quotas': True})
  213     def test_usage_with_neutron_floating_ip_disabled(self):
  214         self._test_usage_with_neutron(neutron_fip_enabled=False)
  215 
  216     def _test_usage_with_neutron(self,
  217                                  neutron_sg_enabled=True,
  218                                  neutron_fip_enabled=True):
  219         self._stub_api_calls()
  220         self._test_usage_with_neutron_check(neutron_sg_enabled,
  221                                             neutron_fip_enabled)
  222         self._check_api_calls()
  223 
  224     def _test_usage_with_neutron_check(self, neutron_sg_enabled=True,
  225                                        neutron_fip_expected=True,
  226                                        max_fip_expected=50,
  227                                        max_sg_expected=20):
  228         res = self.client.get(reverse('horizon:project:overview:index'))
  229         if neutron_fip_expected:
  230             self.assertContains(res, 'Floating IPs')
  231         self.assertContains(res, 'Security Groups')
  232 
  233         res_limits = res.context['usage'].limits
  234         max_floating_ips = res_limits['floatingip']['quota']
  235         self.assertEqual(max_floating_ips, max_fip_expected)
  236         if neutron_sg_enabled:
  237             max_security_groups = res_limits['security_group']['quota']
  238             self.assertEqual(max_security_groups, max_sg_expected)
  239 
  240     def test_usage_cinder(self):
  241         self._stub_api_calls(
  242             quota_usage_overrides={'volumes': {'used': 4},
  243                                    'gigabytes': {'used': 400}}
  244         )
  245 
  246         res = self.client.get(reverse('horizon:project:overview:index'))
  247         usages = res.context['usage']
  248         self.assertTemplateUsed(res, 'project/overview/usage.html')
  249         self.assertIsInstance(usages, usage.ProjectUsage)
  250 
  251         self.assertEqual(usages.limits['volumes']['used'], 4)
  252         self.assertEqual(usages.limits['volumes']['quota'], 10)
  253         self.assertEqual(usages.limits['gigabytes']['used'], 400)
  254         self.assertEqual(usages.limits['gigabytes']['quota'], 1000)
  255 
  256         self._check_api_calls(nova_stu_enabled=True)
  257 
  258     def _test_usage_charts(self, quota_usage_overrides=None):
  259         self._stub_api_calls(nova_stu_enabled=False,
  260                              quota_usage_overrides=quota_usage_overrides)
  261 
  262         res = self.client.get(reverse('horizon:project:overview:index'))
  263 
  264         self._check_api_calls(nova_stu_enabled=False)
  265 
  266         return res
  267 
  268     def test_usage_charts_created(self):
  269         res = self._test_usage_charts(
  270             quota_usage_overrides={'floatingip': {'quota': -1, 'used': 1234}})
  271         self.assertIn('charts', res.context)
  272         charts = res.context['charts']
  273 
  274         self.assertEqual(['Compute', 'Volume', 'Network'],
  275                          [c['title'] for c in charts])
  276 
  277         compute_charts = [c for c in charts if c['title'] == 'Compute'][0]
  278         chart_ram = [c for c in compute_charts['charts']
  279                      if c['type'] == 'ram'][0]
  280         # Check mb_float_format filter is applied
  281         self.assertEqual(10000, chart_ram['quota'])
  282         self.assertEqual('9.8GB', chart_ram['quota_display'])
  283         self.assertEqual(0, chart_ram['used'])
  284         self.assertEqual('0Bytes', chart_ram['used_display'])
  285 
  286         volume_charts = [c for c in charts if c['title'] == 'Volume'][0]
  287         chart_gigabytes = [c for c in volume_charts['charts']
  288                            if c['type'] == 'gigabytes'][0]
  289         # Check diskgbformat filter is applied
  290         self.assertEqual(1000, chart_gigabytes['quota'])
  291         self.assertEqual('1000GB', chart_gigabytes['quota_display'])
  292         self.assertEqual(0, chart_gigabytes['used'])
  293         self.assertEqual('0Bytes', chart_gigabytes['used_display'])
  294 
  295         network_charts = [c for c in charts if c['title'] == 'Network'][0]
  296         chart_fip = [c for c in network_charts['charts']
  297                      if c['type'] == 'floatingip'][0]
  298         # Check intcomma default filter is applied
  299         self.assertEqual(float('inf'), chart_fip['quota'])
  300         self.assertEqual(float('inf'), chart_fip['quota_display'])
  301         self.assertEqual(1234, chart_fip['used'])
  302         self.assertEqual('1,234', chart_fip['used_display'])
  303 
  304     def test_usage_charts_infinite_quota(self):
  305         res = self._test_usage_charts(
  306             quota_usage_overrides={'floatingip': {'quota': -1}})
  307 
  308         max_floating_ips = res.context['usage'].limits['floatingip']['quota']
  309         self.assertEqual(max_floating_ips, float("inf"))
  310 
  311         self.assertContains(res, '(No Limit)')