<s> import datetime <EOL> import six <EOL> from nailgun . statistics import utils <EOL> from nailgun . test . base import BaseTestCase <EOL> from nailgun import consts <EOL> from nailgun . objects import OpenStackWorkloadStats <EOL> from nailgun . objects import OpenStackWorkloadStatsCollection <EOL> from nailgun . statistics . oswl . saver import oswl_data_checksum <EOL> from nailgun . statistics . oswl . saver import oswl_statistics_save <EOL> class TestOSWLServerInfoSaving ( BaseTestCase ) : <EOL> vms_info = { <EOL> "<STR_LIT:id>" : <NUM_LIT:1> , <EOL> "<STR_LIT:status>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> @ property <EOL> def empty_data ( self ) : <EOL> return { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : consts . OSWL_RESOURCE_TYPES . vm , <EOL> '<STR_LIT>' : datetime . datetime . utcnow ( ) . date ( ) , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] } , <EOL> '<STR_LIT>' : oswl_data_checksum ( [ ] ) , <EOL> '<STR_LIT>' : False <EOL> } <EOL> def data_w_default_vm_info ( self , time ) : <EOL> data = self . empty_data <EOL> data [ '<STR_LIT>' ] . update ( { <EOL> '<STR_LIT>' : [ { '<STR_LIT:time>' : time . isoformat ( ) , '<STR_LIT:id>' : <NUM_LIT:1> } ] , <EOL> '<STR_LIT>' : [ self . vms_info ] <EOL> } ) <EOL> return data <EOL> def check_overall_rec_count ( self , count ) : <EOL> saved = OpenStackWorkloadStatsCollection . all ( ) <EOL> self . assertEqual ( saved . count ( ) , count ) <EOL> return saved <EOL> def check_data_vs_rec ( self , data , rec ) : <EOL> data [ '<STR_LIT>' ] = oswl_data_checksum ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> for k , v in six . iteritems ( data ) : <EOL> if isinstance ( v , ( list , tuple ) ) : <EOL> self . assertItemsEqual ( v , getattr ( rec , k ) ) <EOL> else : <EOL> self . assertEqual ( v , getattr ( rec , k ) ) <EOL> def save_data_and_check_record ( self , data ) : <EOL> oswl_statistics_save ( <NUM_LIT:1> , consts . OSWL_RESOURCE_TYPES . vm , data ) <EOL> last = OpenStackWorkloadStats . get_last_by ( <EOL> <NUM_LIT:1> , consts . OSWL_RESOURCE_TYPES . vm ) <EOL> self . assertEqual ( last , self . check_overall_rec_count ( <NUM_LIT:1> ) . first ( ) ) <EOL> return last <EOL> def add_default_vm_info_and_check ( self ) : <EOL> last = self . save_data_and_check_record ( [ self . vms_info ] ) <EOL> time_update = last . updated_time <EOL> data = self . data_w_default_vm_info ( time_update ) <EOL> self . check_data_vs_rec ( data , last ) <EOL> return time_update , data <EOL> def test_empty_data ( self ) : <EOL> last = self . save_data_and_check_record ( [ ] ) <EOL> self . check_data_vs_rec ( self . empty_data , last ) <EOL> def test_added_same_info ( self ) : <EOL> time_update , data = self . add_default_vm_info_and_check ( ) <EOL> last = self . save_data_and_check_record ( [ self . vms_info ] ) <EOL> self . assertEqual ( time_update , last . updated_time ) <EOL> self . check_data_vs_rec ( data , last ) <EOL> def test_added_one_by_one ( self ) : <EOL> time_update1 , data = self . add_default_vm_info_and_check ( ) <EOL> two_vms = [ dict ( self . vms_info ) , dict ( self . vms_info ) ] <EOL> two_vms [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] = <NUM_LIT:2> <EOL> last = self . save_data_and_check_record ( two_vms ) <EOL> time_update2 = last . updated_time <EOL> data [ '<STR_LIT>' ] . update ( { <EOL> '<STR_LIT>' : [ { '<STR_LIT:time>' : time_update1 . isoformat ( ) , '<STR_LIT:id>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT:time>' : time_update2 . isoformat ( ) , '<STR_LIT:id>' : <NUM_LIT:2> } ] , <EOL> '<STR_LIT>' : two_vms <EOL> } ) <EOL> self . check_data_vs_rec ( data , last ) <EOL> def test_added_on_cluster_reset ( self ) : <EOL> time_update1 , data = self . add_default_vm_info_and_check ( ) <EOL> two_vms = [ self . vms_info ] <EOL> self . save_data_and_check_record ( two_vms ) <EOL> self . save_data_and_check_record ( [ ] ) <EOL> last = self . save_data_and_check_record ( two_vms ) <EOL> time_update2 = last . updated_time <EOL> time_removed2 = last . resource_data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:time>' ] <EOL> data [ '<STR_LIT>' ] . update ( { <EOL> '<STR_LIT>' : [ { '<STR_LIT:time>' : time_update1 . isoformat ( ) , '<STR_LIT:id>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT:time>' : time_update2 . isoformat ( ) , '<STR_LIT:id>' : <NUM_LIT:1> } ] , <EOL> '<STR_LIT>' : two_vms , <EOL> '<STR_LIT>' : [ dict ( two_vms [ <NUM_LIT:0> ] , ** { '<STR_LIT:time>' : time_removed2 } ) ] <EOL> } ) <EOL> self . check_data_vs_rec ( data , last ) <EOL> def test_added_then_removed ( self ) : <EOL> time_update , data = self . add_default_vm_info_and_check ( ) <EOL> last = self . save_data_and_check_record ( [ ] ) <EOL> time_update = last . updated_time <EOL> removed = dict ( self . vms_info ) <EOL> removed [ '<STR_LIT:time>' ] = time_update . isoformat ( ) <EOL> data [ '<STR_LIT>' ] . update ( { <EOL> '<STR_LIT>' : [ removed ] , <EOL> '<STR_LIT>' : [ ] <EOL> } ) <EOL> self . check_data_vs_rec ( data , last ) <EOL> def test_modified ( self ) : <EOL> time_update , data = self . add_default_vm_info_and_check ( ) <EOL> vms_new = [ dict ( self . vms_info ) ] <EOL> vms_new [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> vms_new [ <NUM_LIT:0> ] [ '<STR_LIT:status>' ] = '<STR_LIT>' <EOL> last = self . save_data_and_check_record ( vms_new ) <EOL> time_update = last . updated_time <EOL> modified1 = { '<STR_LIT>' : self . vms_info [ '<STR_LIT>' ] , <EOL> '<STR_LIT:status>' : self . vms_info [ '<STR_LIT:status>' ] , <EOL> '<STR_LIT:time>' : time_update . isoformat ( ) , <EOL> '<STR_LIT:id>' : self . vms_info [ '<STR_LIT:id>' ] } <EOL> data [ '<STR_LIT>' ] . update ( { <EOL> '<STR_LIT>' : [ modified1 ] , <EOL> '<STR_LIT>' : vms_new <EOL> } ) <EOL> self . check_data_vs_rec ( data , last ) <EOL> vms_new1 = [ dict ( vms_new [ <NUM_LIT:0> ] ) ] <EOL> vms_new1 [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> last = self . save_data_and_check_record ( vms_new1 ) <EOL> time_update = last . updated_time <EOL> modified2 = { '<STR_LIT>' : vms_new [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT:time>' : time_update . isoformat ( ) , <EOL> '<STR_LIT:id>' : vms_new [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] } <EOL> data [ '<STR_LIT>' ] . update ( { <EOL> '<STR_LIT>' : [ modified1 , modified2 ] , <EOL> '<STR_LIT>' : vms_new1 <EOL> } ) <EOL> self . check_data_vs_rec ( data , last ) <EOL> last = self . save_data_and_check_record ( [ self . vms_info ] ) <EOL> time_update = last . updated_time <EOL> modified3 = { '<STR_LIT:status>' : vms_new1 [ <NUM_LIT:0> ] [ '<STR_LIT:status>' ] , <EOL> '<STR_LIT:time>' : time_update . isoformat ( ) , <EOL> '<STR_LIT:id>' : vms_new1 [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] } <EOL> data [ '<STR_LIT>' ] . update ( { <EOL> '<STR_LIT>' : [ modified1 , modified2 , modified3 ] , <EOL> '<STR_LIT>' : [ self . vms_info ] <EOL> } ) <EOL> self . check_data_vs_rec ( data , last ) <EOL> def test_add_row_per_day ( self ) : <EOL> last = self . save_data_and_check_record ( [ self . vms_info ] ) <EOL> date_cur = last . created_date <EOL> time_update = last . updated_time <EOL> date_1st_rec = date_cur - datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> OpenStackWorkloadStats . update ( last , <EOL> { '<STR_LIT>' : date_1st_rec } ) <EOL> self . assertEqual ( last , <EOL> self . save_data_and_check_record ( [ self . vms_info ] ) ) <EOL> oswl_statistics_save ( <NUM_LIT:1> , consts . OSWL_RESOURCE_TYPES . vm , [ ] ) <EOL> saved = self . check_overall_rec_count ( <NUM_LIT:2> ) <EOL> last = OpenStackWorkloadStats . get_last_by ( <EOL> <NUM_LIT:1> , consts . OSWL_RESOURCE_TYPES . vm ) <EOL> self . assertEqual ( last . created_date , date_cur ) <EOL> for rec in saved : <EOL> if rec . created_date == date_cur : <EOL> self . assertEqual ( rec , last ) <EOL> data = self . empty_data <EOL> removed = dict ( self . vms_info ) <EOL> removed [ '<STR_LIT:time>' ] = last . updated_time . isoformat ( ) <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = [ removed ] <EOL> self . check_data_vs_rec ( data , rec ) <EOL> elif rec . created_date == date_1st_rec : <EOL> data = self . data_w_default_vm_info ( time_update ) <EOL> data [ '<STR_LIT>' ] = date_1st_rec <EOL> self . check_data_vs_rec ( data , rec ) <EOL> def test_oswl_is_sent_restored_on_changes ( self ) : <EOL> cluster_id = <NUM_LIT:1> <EOL> vm_info = { <EOL> "<STR_LIT:id>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> } <EOL> oswl_statistics_save ( cluster_id , consts . OSWL_RESOURCE_TYPES . vm , <EOL> [ vm_info ] ) <EOL> last = OpenStackWorkloadStats . get_last_by ( <EOL> cluster_id , consts . OSWL_RESOURCE_TYPES . vm ) <EOL> OpenStackWorkloadStats . update ( last , { '<STR_LIT>' : True } ) <EOL> self . assertEqual ( True , last . is_sent ) <EOL> oswl_statistics_save ( cluster_id , consts . OSWL_RESOURCE_TYPES . vm , <EOL> [ vm_info ] ) <EOL> last_no_change = OpenStackWorkloadStats . get_last_by ( <EOL> cluster_id , consts . OSWL_RESOURCE_TYPES . vm ) <EOL> self . assertEqual ( True , last_no_change . is_sent ) <EOL> vm_info [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> oswl_statistics_save ( cluster_id , consts . OSWL_RESOURCE_TYPES . vm , <EOL> [ vm_info ] ) <EOL> last_changed = OpenStackWorkloadStats . get_last_by ( <EOL> cluster_id , consts . OSWL_RESOURCE_TYPES . vm ) <EOL> self . assertEqual ( False , last_changed . is_sent ) <EOL> def test_oswl_statistics_save_version_info ( self ) : <EOL> cluster = self . env . create ( ) <EOL> oswl_statistics_save ( cluster . id , consts . OSWL_RESOURCE_TYPES . vm , [ ] ) <EOL> oswl = OpenStackWorkloadStats . get_last_by ( <EOL> cluster . id , consts . OSWL_RESOURCE_TYPES . vm ) <EOL> self . assertEqual ( { } , oswl . version_info ) <EOL> oswl_statistics_save ( <EOL> cluster . id , consts . OSWL_RESOURCE_TYPES . vm , [ { '<STR_LIT:id>' : <NUM_LIT:1> } ] , <EOL> version_info = utils . get_version_info ( cluster ) <EOL> ) <EOL> oswl = OpenStackWorkloadStats . get_last_by ( <EOL> cluster . id , consts . OSWL_RESOURCE_TYPES . vm ) <EOL> self . assertEqual ( utils . get_version_info ( cluster ) , oswl . version_info ) </s>
