<s> import time <EOL> from testtools import content as test_content <EOL> import tempest . cloudscaling . base as base <EOL> import tempest . cloudscaling . thirdparty . scenario . aws_compat . base as aws_base <EOL> import tempest . cloudscaling . utils as utils <EOL> from tempest . common . utils . linux import remote_client <EOL> from tempest import test <EOL> from tempest . thirdparty . boto . utils import wait as boto_wait <EOL> import logging <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . CRITICAL ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class VolumeBenchmarkTest ( base . BaseBenchmarkTest , aws_base . BaseAWSTest ) : <EOL> class Context : <EOL> instance = None <EOL> ssh = None <EOL> volume = None <EOL> part_lines = None <EOL> volume_ready = False <EOL> volume_filled = False <EOL> snapshot = None <EOL> @ classmethod <EOL> @ test . safe_setup <EOL> def setUpClass ( cls ) : <EOL> super ( VolumeBenchmarkTest , cls ) . setUpClass ( ) <EOL> cls . _load_benchmark_data ( "<STR_LIT>" ) <EOL> cfg = cls . config . cloudscaling <EOL> image_name = cfg . general_image_name <EOL> cls . ssh_user = cfg . general_ssh_user_name <EOL> cls . volume_size = cfg . volume_benchmark_volume_size_gb <EOL> cls . volume_fill = cfg . volume_benchmark_volume_fill_percent <EOL> cls . volume_attach_name = "<STR_LIT>" <EOL> cls . ctx = cls . Context ( ) <EOL> cls . image_id = cls . _prepare_image_id ( image_name ) <EOL> cls . keypair = cls . _prepare_key_pair ( ) <EOL> sg = cls . _prepare_security_group ( ) <EOL> cls . sec_group_name = sg . name <EOL> reservation = cls . ec2_client . run_instances ( cls . image_id , <EOL> instance_type = cls . instance_type , <EOL> key_name = cls . keypair . name , <EOL> security_groups = ( cls . sec_group_name , ) ) <EOL> cls . addResourceCleanUp ( cls . destroy_reservation , reservation ) <EOL> instance = reservation . instances [ <NUM_LIT:0> ] <EOL> LOG . info ( "<STR_LIT>" , instance . state ) <EOL> cls . _wait_instance_state ( instance , "<STR_LIT>" ) <EOL> cls . ctx . instance = instance <EOL> ip_address = cls . _prepare_public_ip ( instance ) <EOL> ssh = remote_client . RemoteClient ( ip_address , <EOL> cls . ssh_user , <EOL> pkey = cls . keypair . material ) <EOL> cls . ctx . ssh = ssh <EOL> def _volume_state ( self ) : <EOL> self . ctx . volume . update ( validate = True ) <EOL> return self . ctx . volume . status <EOL> def _part_state ( self ) : <EOL> current = self . ctx . ssh . get_partitions ( ) . split ( '<STR_LIT:\n>' ) <EOL> if len ( current ) > len ( self . ctx . part_lines ) : <EOL> return <NUM_LIT:1> <EOL> if len ( current ) < len ( self . ctx . part_lines ) : <EOL> return - <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def _start_test ( self ) : <EOL> self . start_time = time . time ( ) <EOL> def _end_test ( self , detail_description ) : <EOL> end_time = time . time ( ) <EOL> self . test_time = end_time - self . start_time <EOL> content = test_content . text_content ( <EOL> detail_description + "<STR_LIT>" + str ( self . test_time ) + "<STR_LIT:s>" ) <EOL> self . addDetail ( "<STR_LIT>" , content ) <EOL> reference_time = self . _get_benchmark_result ( ) <EOL> if reference_time is not None : <EOL> content = test_content . text_content ( <EOL> "<STR_LIT>" + str ( reference_time [ <NUM_LIT:0> ] ) + "<STR_LIT>" + <EOL> "<STR_LIT>" + str ( reference_time [ <NUM_LIT:1> ] ) + "<STR_LIT:s>" ) <EOL> self . addDetail ( "<STR_LIT>" , content ) <EOL> def _check_test ( self ) : <EOL> reference_time = self . _get_benchmark_result ( ) <EOL> if reference_time is not None : <EOL> self . assertLessEqual ( self . test_time , float ( reference_time [ <NUM_LIT:1> ] ) , <EOL> str ( self . test_time ) + "<STR_LIT>" + <EOL> reference_time [ <NUM_LIT:1> ] + "<STR_LIT>" ) <EOL> @ test . attr ( type = '<STR_LIT>' ) <EOL> def test_001_attach_volume ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . ctx . ssh is None : <EOL> raise self . skipException ( "<STR_LIT>" ) <EOL> self . _start_test ( ) <EOL> zone = self . ctx . instance . placement <EOL> volume = self . ec2_client . create_volume ( self . volume_size , zone ) <EOL> self . addResourceCleanUp ( self . destroy_volume_wait , volume ) <EOL> self . ctx . volume = volume <EOL> self . assertVolumeStatusWait ( volume , "<STR_LIT>" ) <EOL> self . ctx . part_lines = self . ctx . ssh . get_partitions ( ) . split ( '<STR_LIT:\n>' ) <EOL> volume . attach ( self . ctx . instance . id , "<STR_LIT>" + self . volume_attach_name ) <EOL> self . assertVolumeStatusWait ( self . _volume_state , "<STR_LIT>" ) <EOL> boto_wait . re_search_wait ( self . _volume_state , "<STR_LIT>" ) <EOL> boto_wait . state_wait ( self . _part_state , <NUM_LIT:1> ) <EOL> part_lines_new = self . ctx . ssh . get_partitions ( ) . split ( '<STR_LIT:\n>' ) <EOL> volume_name = utils . detect_new_volume ( self . ctx . part_lines , <EOL> part_lines_new ) <EOL> self . ctx . part_lines = part_lines_new <EOL> self . _end_test ( "<STR_LIT>" ) <EOL> self . ctx . ssh . exec_command ( "<STR_LIT>" <EOL> "<STR_LIT>" + volume_name ) <EOL> self . ctx . ssh . exec_command ( "<STR_LIT>" <EOL> "<STR_LIT>" + volume_name + "<STR_LIT>" ) <EOL> self . ctx . volume_ready = True <EOL> self . _check_test ( ) <EOL> @ test . attr ( type = '<STR_LIT>' ) <EOL> def test_002_fill_volume ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . ctx . ssh is None : <EOL> raise self . skipException ( "<STR_LIT>" ) <EOL> if not self . ctx . volume_ready : <EOL> raise self . skipException ( "<STR_LIT>" ) <EOL> self . _start_test ( ) <EOL> self . ctx . ssh . exec_command ( "<STR_LIT>" ) <EOL> file_lines = <NUM_LIT> * int ( self . volume_size ) <EOL> for i in xrange ( int ( self . volume_fill ) ) : <EOL> self . ctx . ssh . exec_command ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" + str ( file_lines ) + <EOL> "<STR_LIT>" + str ( i ) ) <EOL> self . _end_test ( "<STR_LIT>" ) <EOL> self . ctx . volume_filled = True <EOL> self . _check_test ( ) <EOL> @ test . attr ( type = '<STR_LIT>' ) <EOL> def test_003_snapshot_volume ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . ctx . ssh is None : <EOL> raise self . skipException ( "<STR_LIT>" ) <EOL> if not self . ctx . volume_filled : <EOL> raise self . skipException ( "<STR_LIT>" ) <EOL> self . _start_test ( ) <EOL> snapshot = self . ec2_client . create_snapshot ( self . ctx . volume . id ) <EOL> self . addResourceCleanUp ( self . destroy_snapshot_wait , snapshot ) <EOL> self . assertSnapshotStatusWait ( snapshot , "<STR_LIT>" ) <EOL> self . _end_test ( "<STR_LIT>" ) <EOL> self . ctx . snapshot = snapshot <EOL> self . _check_test ( ) <EOL> @ test . attr ( type = '<STR_LIT>' ) <EOL> def test_004_clone_volume_snapshot ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . ctx . ssh is None : <EOL> raise self . skipException ( "<STR_LIT>" ) <EOL> if self . ctx . snapshot is None : <EOL> raise self . skipException ( "<STR_LIT>" ) <EOL> self . _start_test ( ) <EOL> zone = self . ctx . instance . placement <EOL> volume2 = self . ec2_client . create_volume ( <EOL> self . volume_size , zone , snapshot = self . ctx . snapshot ) <EOL> self . addResourceCleanUp ( self . destroy_volume_wait , volume2 ) <EOL> self . assertVolumeStatusWait ( volume2 , "<STR_LIT>" ) <EOL> self . _end_test ( "<STR_LIT>" ) <EOL> self . _check_test ( ) <EOL> @ test . attr ( type = '<STR_LIT>' ) <EOL> def test_005_detach_volume ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . ctx . ssh is None : <EOL> raise self . skipException ( "<STR_LIT>" ) <EOL> if not self . ctx . volume_ready : <EOL> raise self . skipException ( "<STR_LIT>" ) <EOL> self . _start_test ( ) <EOL> self . ctx . ssh . exec_command ( "<STR_LIT>" ) <EOL> self . ctx . volume . detach ( ) <EOL> self . assertVolumeStatusWait ( self . _volume_state , "<STR_LIT>" ) <EOL> boto_wait . re_search_wait ( self . _volume_state , "<STR_LIT>" ) <EOL> self . _end_test ( "<STR_LIT>" ) <EOL> boto_wait . state_wait ( self . _part_state , - <NUM_LIT:1> ) <EOL> self . _check_test ( ) </s>
