<s> import copy <EOL> from django . conf import settings <EOL> from django . core . urlresolvers import reverse <EOL> from django import http <EOL> from django . test . utils import override_settings <EOL> from django . utils . http import urlunquote <EOL> from mox3 . mox import IsA <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard . dashboards . project . volumes . backups import tables as backup_tables <EOL> from openstack_dashboard . dashboards . project . volumes . snapshots import tables as snapshot_tables <EOL> from openstack_dashboard . dashboards . project . volumes . volumes import tables as volume_tables <EOL> from openstack_dashboard . test import helpers as test <EOL> INDEX_URL = reverse ( '<STR_LIT>' ) <EOL> VOLUME_SNAPSHOTS_TAB_URL = urlunquote ( reverse ( <EOL> '<STR_LIT>' ) ) <EOL> VOLUME_BACKUPS_TAB_URL = urlunquote ( reverse ( <EOL> '<STR_LIT>' ) ) <EOL> class VolumeAndSnapshotsAndBackupsTests ( test . TestCase ) : <EOL> @ test . create_stubs ( { api . cinder : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) , <EOL> api . nova : ( '<STR_LIT>' , ) } ) <EOL> def _test_index ( self , backup_supported = True , instanceless_volumes = False ) : <EOL> vol_backups = self . cinder_volume_backups . list ( ) <EOL> vol_snaps = self . cinder_volume_snapshots . list ( ) <EOL> volumes = self . cinder_volumes . list ( ) <EOL> if instanceless_volumes : <EOL> for volume in volumes : <EOL> volume . attachments = [ ] <EOL> api . cinder . volume_backup_supported ( IsA ( http . HttpRequest ) ) . MultipleTimes ( ) . AndReturn ( backup_supported ) <EOL> api . cinder . volume_list_paged ( <EOL> IsA ( http . HttpRequest ) , marker = None , search_opts = None , <EOL> sort_dir = '<STR_LIT>' , paginate = True ) . AndReturn ( [ volumes , False , False ] ) <EOL> if not instanceless_volumes : <EOL> api . nova . server_list ( IsA ( http . HttpRequest ) , search_opts = None ) . AndReturn ( [ self . servers . list ( ) , False ] ) <EOL> api . cinder . volume_snapshot_list ( IsA ( http . HttpRequest ) ) . AndReturn ( vol_snaps ) <EOL> api . cinder . volume_snapshot_list_paged ( <EOL> IsA ( http . HttpRequest ) , paginate = True , marker = None , <EOL> sort_dir = '<STR_LIT>' ) . AndReturn ( [ vol_snaps , False , False ] ) <EOL> api . cinder . volume_list ( IsA ( http . HttpRequest ) ) . AndReturn ( volumes ) <EOL> if backup_supported : <EOL> api . cinder . volume_backup_list_paged ( <EOL> IsA ( http . HttpRequest ) , marker = None , sort_dir = '<STR_LIT>' , <EOL> paginate = True ) . AndReturn ( [ vol_backups , False , False ] ) <EOL> api . cinder . volume_list ( IsA ( http . HttpRequest ) ) . AndReturn ( volumes ) <EOL> api . cinder . tenant_absolute_limits ( IsA ( http . HttpRequest ) ) . MultipleTimes ( ) . AndReturn ( self . cinder_limits [ '<STR_LIT>' ] ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( INDEX_URL ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> res = self . client . get ( VOLUME_SNAPSHOTS_TAB_URL ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> if backup_supported : <EOL> res = self . client . get ( VOLUME_BACKUPS_TAB_URL ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> def test_index_backup_supported ( self ) : <EOL> self . _test_index ( backup_supported = True ) <EOL> def test_index_backup_not_supported ( self ) : <EOL> self . _test_index ( backup_supported = False ) <EOL> def test_index_no_volume_attachments ( self ) : <EOL> self . _test_index ( instanceless_volumes = True ) <EOL> @ test . create_stubs ( { api . cinder : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> api . nova : ( '<STR_LIT>' , ) } ) <EOL> def _test_index_paginated ( self , marker , sort_dir , volumes , url , <EOL> has_more , has_prev ) : <EOL> backup_supported = True <EOL> vol_snaps = self . cinder_volume_snapshots . list ( ) <EOL> api . cinder . volume_backup_supported ( IsA ( http . HttpRequest ) ) . MultipleTimes ( ) . AndReturn ( backup_supported ) <EOL> api . cinder . volume_list_paged ( IsA ( http . HttpRequest ) , marker = marker , <EOL> sort_dir = sort_dir , search_opts = None , <EOL> paginate = True ) . AndReturn ( [ volumes , has_more , has_prev ] ) <EOL> api . cinder . volume_snapshot_list ( <EOL> IsA ( http . HttpRequest ) , search_opts = None ) . AndReturn ( vol_snaps ) <EOL> api . nova . server_list ( IsA ( http . HttpRequest ) , search_opts = None ) . AndReturn ( [ self . servers . list ( ) , False ] ) <EOL> api . cinder . tenant_absolute_limits ( IsA ( http . HttpRequest ) ) . MultipleTimes ( ) . AndReturn ( self . cinder_limits [ '<STR_LIT>' ] ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( urlunquote ( url ) ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> self . mox . UnsetStubs ( ) <EOL> return res <EOL> def ensure_attachments_exist ( self , volumes ) : <EOL> volumes = copy . copy ( volumes ) <EOL> for volume in volumes : <EOL> if not volume . attachments : <EOL> volume . attachments . append ( { <EOL> "<STR_LIT:id>" : "<STR_LIT:1>" , "<STR_LIT>" : '<STR_LIT:1>' , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> return volumes <EOL> @ override_settings ( API_RESULT_PAGE_SIZE = <NUM_LIT:2> ) <EOL> def test_index_paginated ( self ) : <EOL> mox_volumes = self . ensure_attachments_exist ( self . cinder_volumes . list ( ) ) <EOL> size = settings . API_RESULT_PAGE_SIZE <EOL> expected_volumes = mox_volumes [ : size ] <EOL> url = INDEX_URL <EOL> res = self . _test_index_paginated ( marker = None , sort_dir = "<STR_LIT>" , <EOL> volumes = expected_volumes , url = url , <EOL> has_more = True , has_prev = False ) <EOL> volumes = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( volumes , expected_volumes ) <EOL> expected_volumes = mox_volumes [ size : <NUM_LIT:2> * size ] <EOL> marker = expected_volumes [ <NUM_LIT:0> ] . id <EOL> next = volume_tables . VolumesTable . _meta . pagination_param <EOL> url = "<STR_LIT:?>" . join ( [ INDEX_URL , "<STR_LIT:=>" . join ( [ next , marker ] ) ] ) <EOL> res = self . _test_index_paginated ( marker = marker , sort_dir = "<STR_LIT>" , <EOL> volumes = expected_volumes , url = url , <EOL> has_more = True , has_prev = True ) <EOL> volumes = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( volumes , expected_volumes ) <EOL> expected_volumes = mox_volumes [ - size : ] <EOL> marker = expected_volumes [ <NUM_LIT:0> ] . id <EOL> next = volume_tables . VolumesTable . _meta . pagination_param <EOL> url = "<STR_LIT:?>" . join ( [ INDEX_URL , "<STR_LIT:=>" . join ( [ next , marker ] ) ] ) <EOL> res = self . _test_index_paginated ( marker = marker , sort_dir = "<STR_LIT>" , <EOL> volumes = expected_volumes , url = url , <EOL> has_more = False , has_prev = True ) <EOL> volumes = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( volumes , expected_volumes ) <EOL> @ override_settings ( API_RESULT_PAGE_SIZE = <NUM_LIT:2> ) <EOL> def test_index_paginated_prev_page ( self ) : <EOL> mox_volumes = self . ensure_attachments_exist ( self . cinder_volumes . list ( ) ) <EOL> size = settings . API_RESULT_PAGE_SIZE <EOL> expected_volumes = mox_volumes [ size : <NUM_LIT:2> * size ] <EOL> marker = expected_volumes [ <NUM_LIT:0> ] . id <EOL> prev = volume_tables . VolumesTable . _meta . prev_pagination_param <EOL> url = "<STR_LIT:?>" . join ( [ INDEX_URL , "<STR_LIT:=>" . join ( [ prev , marker ] ) ] ) <EOL> res = self . _test_index_paginated ( marker = marker , sort_dir = "<STR_LIT>" , <EOL> volumes = expected_volumes , url = url , <EOL> has_more = True , has_prev = True ) <EOL> volumes = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( volumes , expected_volumes ) <EOL> expected_volumes = mox_volumes [ : size ] <EOL> marker = expected_volumes [ <NUM_LIT:0> ] . id <EOL> prev = volume_tables . VolumesTable . _meta . prev_pagination_param <EOL> url = "<STR_LIT:?>" . join ( [ INDEX_URL , "<STR_LIT:=>" . join ( [ prev , marker ] ) ] ) <EOL> res = self . _test_index_paginated ( marker = marker , sort_dir = "<STR_LIT>" , <EOL> volumes = expected_volumes , url = url , <EOL> has_more = True , has_prev = False ) <EOL> volumes = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( volumes , expected_volumes ) <EOL> @ test . create_stubs ( { api . cinder : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) , <EOL> api . nova : ( '<STR_LIT>' , ) } ) <EOL> def _test_snapshots_index_paginated ( self , marker , sort_dir , snapshots , url , <EOL> has_more , has_prev ) : <EOL> backup_supported = True <EOL> api . cinder . volume_backup_supported ( IsA ( http . HttpRequest ) ) . MultipleTimes ( ) . AndReturn ( backup_supported ) <EOL> api . cinder . volume_snapshot_list_paged ( <EOL> IsA ( http . HttpRequest ) , marker = marker , sort_dir = sort_dir , <EOL> paginate = True ) . AndReturn ( [ snapshots , has_more , has_prev ] ) <EOL> api . cinder . volume_list ( IsA ( http . HttpRequest ) ) . AndReturn ( <EOL> self . cinder_volumes . list ( ) ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( urlunquote ( url ) ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> self . mox . UnsetStubs ( ) <EOL> return res <EOL> @ override_settings ( API_RESULT_PAGE_SIZE = <NUM_LIT:1> ) <EOL> def test_snapshots_index_paginated ( self ) : <EOL> mox_snapshots = self . cinder_volume_snapshots . list ( ) <EOL> size = settings . API_RESULT_PAGE_SIZE <EOL> base_url = reverse ( '<STR_LIT>' ) <EOL> next = snapshot_tables . VolumeSnapshotsTable . _meta . pagination_param <EOL> expected_snapshots = mox_snapshots [ : size ] <EOL> res = self . _test_snapshots_index_paginated ( <EOL> marker = None , sort_dir = "<STR_LIT>" , snapshots = expected_snapshots , <EOL> url = base_url , has_more = True , has_prev = False ) <EOL> snapshots = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( snapshots , expected_snapshots ) <EOL> expected_snapshots = mox_snapshots [ size : <NUM_LIT:2> * size ] <EOL> marker = expected_snapshots [ <NUM_LIT:0> ] . id <EOL> url = "<STR_LIT:&>" . join ( [ base_url , "<STR_LIT:=>" . join ( [ next , marker ] ) ] ) <EOL> res = self . _test_snapshots_index_paginated ( <EOL> marker = marker , sort_dir = "<STR_LIT>" , snapshots = expected_snapshots , <EOL> url = url , has_more = True , has_prev = True ) <EOL> snapshots = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( snapshots , expected_snapshots ) <EOL> expected_snapshots = mox_snapshots [ - size : ] <EOL> marker = expected_snapshots [ <NUM_LIT:0> ] . id <EOL> url = "<STR_LIT:&>" . join ( [ base_url , "<STR_LIT:=>" . join ( [ next , marker ] ) ] ) <EOL> res = self . _test_snapshots_index_paginated ( <EOL> marker = marker , sort_dir = "<STR_LIT>" , snapshots = expected_snapshots , <EOL> url = url , has_more = False , has_prev = True ) <EOL> snapshots = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( snapshots , expected_snapshots ) <EOL> @ override_settings ( API_RESULT_PAGE_SIZE = <NUM_LIT:1> ) <EOL> def test_snapshots_index_paginated_prev_page ( self ) : <EOL> mox_snapshots = self . cinder_volume_snapshots . list ( ) <EOL> size = settings . API_RESULT_PAGE_SIZE <EOL> base_url = reverse ( '<STR_LIT>' ) <EOL> prev = snapshot_tables . VolumeSnapshotsTable . _meta . prev_pagination_param <EOL> expected_snapshots = mox_snapshots [ size : <NUM_LIT:2> * size ] <EOL> marker = expected_snapshots [ <NUM_LIT:0> ] . id <EOL> url = "<STR_LIT:&>" . join ( [ base_url , "<STR_LIT:=>" . join ( [ prev , marker ] ) ] ) <EOL> res = self . _test_snapshots_index_paginated ( <EOL> marker = marker , sort_dir = "<STR_LIT>" , snapshots = expected_snapshots , <EOL> url = url , has_more = True , has_prev = True ) <EOL> snapshots = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( snapshots , expected_snapshots ) <EOL> expected_snapshots = mox_snapshots [ : size ] <EOL> marker = expected_snapshots [ <NUM_LIT:0> ] . id <EOL> url = "<STR_LIT:&>" . join ( [ base_url , "<STR_LIT:=>" . join ( [ prev , marker ] ) ] ) <EOL> res = self . _test_snapshots_index_paginated ( <EOL> marker = marker , sort_dir = "<STR_LIT>" , snapshots = expected_snapshots , <EOL> url = url , has_more = True , has_prev = False ) <EOL> snapshots = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( snapshots , expected_snapshots ) <EOL> @ test . create_stubs ( { api . cinder : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) , <EOL> api . nova : ( '<STR_LIT>' , ) } ) <EOL> def _test_backups_index_paginated ( self , marker , sort_dir , backups , url , <EOL> has_more , has_prev ) : <EOL> backup_supported = True <EOL> api . cinder . volume_backup_supported ( IsA ( http . HttpRequest ) ) . MultipleTimes ( ) . AndReturn ( backup_supported ) <EOL> api . cinder . volume_backup_list_paged ( <EOL> IsA ( http . HttpRequest ) , marker = marker , sort_dir = sort_dir , <EOL> paginate = True ) . AndReturn ( [ backups , has_more , has_prev ] ) <EOL> api . cinder . volume_list ( IsA ( http . HttpRequest ) ) . AndReturn ( <EOL> self . cinder_volumes . list ( ) ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( urlunquote ( url ) ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> self . mox . UnsetStubs ( ) <EOL> return res <EOL> @ override_settings ( API_RESULT_PAGE_SIZE = <NUM_LIT:1> ) <EOL> def test_backups_index_paginated ( self ) : <EOL> mox_backups = self . cinder_volume_backups . list ( ) <EOL> size = settings . API_RESULT_PAGE_SIZE <EOL> base_url = reverse ( '<STR_LIT>' ) <EOL> next = backup_tables . BackupsTable . _meta . pagination_param <EOL> expected_backups = mox_backups [ : size ] <EOL> res = self . _test_backups_index_paginated ( <EOL> marker = None , sort_dir = "<STR_LIT>" , backups = expected_backups , <EOL> url = base_url , has_more = True , has_prev = False ) <EOL> backups = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( backups , expected_backups ) <EOL> expected_backups = mox_backups [ size : <NUM_LIT:2> * size ] <EOL> marker = expected_backups [ <NUM_LIT:0> ] . id <EOL> url = "<STR_LIT:&>" . join ( [ base_url , "<STR_LIT:=>" . join ( [ next , marker ] ) ] ) <EOL> res = self . _test_backups_index_paginated ( <EOL> marker = marker , sort_dir = "<STR_LIT>" , backups = expected_backups , url = url , <EOL> has_more = True , has_prev = True ) <EOL> backups = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( backups , expected_backups ) <EOL> expected_backups = mox_backups [ - size : ] <EOL> marker = expected_backups [ <NUM_LIT:0> ] . id <EOL> url = "<STR_LIT:&>" . join ( [ base_url , "<STR_LIT:=>" . join ( [ next , marker ] ) ] ) <EOL> res = self . _test_backups_index_paginated ( <EOL> marker = marker , sort_dir = "<STR_LIT>" , backups = expected_backups , url = url , <EOL> has_more = False , has_prev = True ) <EOL> backups = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( backups , expected_backups ) <EOL> @ override_settings ( API_RESULT_PAGE_SIZE = <NUM_LIT:1> ) <EOL> def test_backups_index_paginated_prev_page ( self ) : <EOL> mox_backups = self . cinder_volume_backups . list ( ) <EOL> size = settings . API_RESULT_PAGE_SIZE <EOL> base_url = reverse ( '<STR_LIT>' ) <EOL> prev = backup_tables . BackupsTable . _meta . prev_pagination_param <EOL> expected_backups = mox_backups [ size : <NUM_LIT:2> * size ] <EOL> marker = expected_backups [ <NUM_LIT:0> ] . id <EOL> url = "<STR_LIT:&>" . join ( [ base_url , "<STR_LIT:=>" . join ( [ prev , marker ] ) ] ) <EOL> res = self . _test_backups_index_paginated ( <EOL> marker = marker , sort_dir = "<STR_LIT>" , backups = expected_backups , url = url , <EOL> has_more = True , has_prev = True ) <EOL> backups = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( backups , expected_backups ) <EOL> expected_backups = mox_backups [ : size ] <EOL> marker = expected_backups [ <NUM_LIT:0> ] . id <EOL> url = "<STR_LIT:&>" . join ( [ base_url , "<STR_LIT:=>" . join ( [ prev , marker ] ) ] ) <EOL> res = self . _test_backups_index_paginated ( <EOL> marker = marker , sort_dir = "<STR_LIT>" , backups = expected_backups , url = url , <EOL> has_more = True , has_prev = False ) <EOL> backups = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( backups , expected_backups ) </s>
