<s> from __future__ import absolute_import <EOL> from django import http <EOL> from mox import IsA <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard . api import cinder <EOL> from openstack_dashboard . test import helpers as test <EOL> from openstack_dashboard . usage import quotas <EOL> class QuotaTests ( test . APITestCase ) : <EOL> def get_usages ( self , with_volume = True ) : <EOL> usages = { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:1> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:1> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:1> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:10> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:20> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:10> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:1> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:10> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:10> } } <EOL> if with_volume : <EOL> usages . update ( { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT>' : <NUM_LIT:1> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:1> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:1000> } } ) <EOL> return usages <EOL> @ test . create_stubs ( { api . nova : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ) , <EOL> api . network : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> api . base : ( '<STR_LIT>' , ) , <EOL> cinder : ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ) } ) <EOL> def test_tenant_quota_usages ( self ) : <EOL> servers = [ s for s in self . servers . list ( ) <EOL> if s . tenant_id == self . request . user . tenant_id ] <EOL> api . base . is_service_enabled ( IsA ( http . HttpRequest ) , <EOL> '<STR_LIT>' ) . AndReturn ( True ) <EOL> api . base . is_service_enabled ( IsA ( http . HttpRequest ) , <EOL> '<STR_LIT>' ) . AndReturn ( False ) <EOL> api . nova . flavor_list ( IsA ( http . HttpRequest ) ) . AndReturn ( self . flavors . list ( ) ) <EOL> api . nova . tenant_quota_get ( IsA ( http . HttpRequest ) , '<STR_LIT:1>' ) . AndReturn ( self . quotas . first ( ) ) <EOL> api . network . floating_ip_supported ( IsA ( http . HttpRequest ) ) . AndReturn ( True ) <EOL> api . network . tenant_floating_ip_list ( IsA ( http . HttpRequest ) ) . AndReturn ( self . floating_ips . list ( ) ) <EOL> search_opts = { '<STR_LIT>' : self . request . user . tenant_id } <EOL> api . nova . server_list ( IsA ( http . HttpRequest ) , search_opts = search_opts , <EOL> all_tenants = True ) . AndReturn ( [ servers , False ] ) <EOL> opts = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : self . request . user . tenant_id } <EOL> cinder . volume_list ( IsA ( http . HttpRequest ) , opts ) . AndReturn ( self . volumes . list ( ) ) <EOL> cinder . volume_snapshot_list ( IsA ( http . HttpRequest ) , opts ) . AndReturn ( self . snapshots . list ( ) ) <EOL> cinder . tenant_quota_get ( IsA ( http . HttpRequest ) , '<STR_LIT:1>' ) . AndReturn ( self . cinder_quotas . first ( ) ) <EOL> self . mox . ReplayAll ( ) <EOL> quota_usages = quotas . tenant_quota_usages ( self . request ) <EOL> expected_output = self . get_usages ( ) <EOL> self . assertEqual ( expected_output , quota_usages . usages ) <EOL> @ test . create_stubs ( { api . nova : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ) , <EOL> api . network : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> api . base : ( '<STR_LIT>' , ) } ) <EOL> def test_tenant_quota_usages_without_volume ( self ) : <EOL> servers = [ s for s in self . servers . list ( ) <EOL> if s . tenant_id == self . request . user . tenant_id ] <EOL> api . base . is_service_enabled ( IsA ( http . HttpRequest ) , <EOL> '<STR_LIT>' ) . AndReturn ( False ) <EOL> api . base . is_service_enabled ( IsA ( http . HttpRequest ) , <EOL> '<STR_LIT>' ) . AndReturn ( False ) <EOL> api . nova . flavor_list ( IsA ( http . HttpRequest ) ) . AndReturn ( self . flavors . list ( ) ) <EOL> api . nova . tenant_quota_get ( IsA ( http . HttpRequest ) , '<STR_LIT:1>' ) . AndReturn ( self . quotas . first ( ) ) <EOL> api . network . floating_ip_supported ( IsA ( http . HttpRequest ) ) . AndReturn ( True ) <EOL> api . network . tenant_floating_ip_list ( IsA ( http . HttpRequest ) ) . AndReturn ( self . floating_ips . list ( ) ) <EOL> search_opts = { '<STR_LIT>' : self . request . user . tenant_id } <EOL> api . nova . server_list ( IsA ( http . HttpRequest ) , search_opts = search_opts , <EOL> all_tenants = True ) . AndReturn ( [ servers , False ] ) <EOL> self . mox . ReplayAll ( ) <EOL> quota_usages = quotas . tenant_quota_usages ( self . request ) <EOL> expected_output = self . get_usages ( with_volume = False ) <EOL> self . assertEqual ( expected_output , quota_usages . usages ) <EOL> self . assertIn ( '<STR_LIT>' , quota_usages ) <EOL> self . assertIsNotNone ( quota_usages . get ( '<STR_LIT>' ) ) <EOL> @ test . create_stubs ( { api . nova : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ) , <EOL> api . network : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> api . base : ( '<STR_LIT>' , ) } ) <EOL> def test_tenant_quota_usages_no_instances_running ( self ) : <EOL> api . base . is_service_enabled ( IsA ( http . HttpRequest ) , <EOL> '<STR_LIT>' ) . AndReturn ( False ) <EOL> api . base . is_service_enabled ( IsA ( http . HttpRequest ) , <EOL> '<STR_LIT>' ) . AndReturn ( False ) <EOL> api . nova . flavor_list ( IsA ( http . HttpRequest ) ) . AndReturn ( self . flavors . list ( ) ) <EOL> api . nova . tenant_quota_get ( IsA ( http . HttpRequest ) , '<STR_LIT:1>' ) . AndReturn ( self . quotas . first ( ) ) <EOL> api . network . floating_ip_supported ( IsA ( http . HttpRequest ) ) . AndReturn ( True ) <EOL> api . network . tenant_floating_ip_list ( IsA ( http . HttpRequest ) ) . AndReturn ( [ ] ) <EOL> search_opts = { '<STR_LIT>' : self . request . user . tenant_id } <EOL> api . nova . server_list ( IsA ( http . HttpRequest ) , search_opts = search_opts , <EOL> all_tenants = True ) . AndReturn ( [ [ ] , False ] ) <EOL> self . mox . ReplayAll ( ) <EOL> quota_usages = quotas . tenant_quota_usages ( self . request ) <EOL> expected_output = self . get_usages ( with_volume = False ) <EOL> expected_output . update ( { <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:1> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:10> , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:10> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:10> , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:10> } } ) <EOL> self . assertEqual ( expected_output , quota_usages . usages ) <EOL> @ test . create_stubs ( { api . nova : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ) , <EOL> api . network : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> api . base : ( '<STR_LIT>' , ) , <EOL> cinder : ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ) } ) <EOL> def test_tenant_quota_usages_unlimited_quota ( self ) : <EOL> inf_quota = self . quotas . first ( ) <EOL> inf_quota [ '<STR_LIT>' ] = - <NUM_LIT:1> <EOL> servers = [ s for s in self . servers . list ( ) <EOL> if s . tenant_id == self . request . user . tenant_id ] <EOL> api . base . is_service_enabled ( IsA ( http . HttpRequest ) , <EOL> '<STR_LIT>' ) . AndReturn ( True ) <EOL> api . base . is_service_enabled ( IsA ( http . HttpRequest ) , <EOL> '<STR_LIT>' ) . AndReturn ( False ) <EOL> api . nova . flavor_list ( IsA ( http . HttpRequest ) ) . AndReturn ( self . flavors . list ( ) ) <EOL> api . nova . tenant_quota_get ( IsA ( http . HttpRequest ) , '<STR_LIT:1>' ) . AndReturn ( inf_quota ) <EOL> api . network . floating_ip_supported ( IsA ( http . HttpRequest ) ) . AndReturn ( True ) <EOL> api . network . tenant_floating_ip_list ( IsA ( http . HttpRequest ) ) . AndReturn ( self . floating_ips . list ( ) ) <EOL> search_opts = { '<STR_LIT>' : self . request . user . tenant_id } <EOL> api . nova . server_list ( IsA ( http . HttpRequest ) , search_opts = search_opts , <EOL> all_tenants = True ) . AndReturn ( [ servers , False ] ) <EOL> opts = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : self . request . user . tenant_id } <EOL> cinder . volume_list ( IsA ( http . HttpRequest ) , opts ) . AndReturn ( self . volumes . list ( ) ) <EOL> cinder . volume_snapshot_list ( IsA ( http . HttpRequest ) , opts ) . AndReturn ( self . snapshots . list ( ) ) <EOL> cinder . tenant_quota_get ( IsA ( http . HttpRequest ) , '<STR_LIT:1>' ) . AndReturn ( self . cinder_quotas . first ( ) ) <EOL> self . mox . ReplayAll ( ) <EOL> quota_usages = quotas . tenant_quota_usages ( self . request ) <EOL> expected_output = self . get_usages ( ) <EOL> expected_output . update ( { '<STR_LIT>' : { '<STR_LIT>' : float ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : float ( "<STR_LIT>" ) } } ) <EOL> self . assertEqual ( quota_usages . usages , expected_output ) <EOL> @ test . create_stubs ( { api . nova : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ) , <EOL> api . network : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> api . base : ( '<STR_LIT>' , ) , <EOL> cinder : ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ) } ) <EOL> def test_tenant_quota_usages_neutron_fip_disabled ( self ) : <EOL> servers = [ s for s in self . servers . list ( ) <EOL> if s . tenant_id == self . request . user . tenant_id ] <EOL> api . base . is_service_enabled ( IsA ( http . HttpRequest ) , <EOL> '<STR_LIT>' ) . AndReturn ( True ) <EOL> api . base . is_service_enabled ( IsA ( http . HttpRequest ) , <EOL> '<STR_LIT>' ) . AndReturn ( False ) <EOL> api . nova . flavor_list ( IsA ( http . HttpRequest ) ) . AndReturn ( self . flavors . list ( ) ) <EOL> api . nova . tenant_quota_get ( IsA ( http . HttpRequest ) , '<STR_LIT:1>' ) . AndReturn ( self . quotas . first ( ) ) <EOL> api . network . floating_ip_supported ( IsA ( http . HttpRequest ) ) . AndReturn ( False ) <EOL> search_opts = { '<STR_LIT>' : self . request . user . tenant_id } <EOL> api . nova . server_list ( IsA ( http . HttpRequest ) , search_opts = search_opts , <EOL> all_tenants = True ) . AndReturn ( [ servers , False ] ) <EOL> opts = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : self . request . user . tenant_id } <EOL> cinder . volume_list ( IsA ( http . HttpRequest ) , opts ) . AndReturn ( self . volumes . list ( ) ) <EOL> cinder . volume_snapshot_list ( IsA ( http . HttpRequest ) , opts ) . AndReturn ( self . snapshots . list ( ) ) <EOL> cinder . tenant_quota_get ( IsA ( http . HttpRequest ) , '<STR_LIT:1>' ) . AndReturn ( self . cinder_quotas . first ( ) ) <EOL> self . mox . ReplayAll ( ) <EOL> quota_usages = quotas . tenant_quota_usages ( self . request ) <EOL> expected_output = self . get_usages ( ) <EOL> expected_output [ '<STR_LIT>' ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> expected_output [ '<STR_LIT>' ] [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> self . assertEqual ( expected_output , quota_usages . usages ) </s>
