<s> import datetime <EOL> import iso8601 <EOL> import mock <EOL> from oslo_utils import timeutils <EOL> from nova import context <EOL> from nova import db <EOL> from nova . objects import bandwidth_usage <EOL> from nova import test <EOL> from nova . tests . unit . objects import test_objects <EOL> from nova . tests import uuidsentinel as uuids <EOL> class _TestBandwidthUsage ( test . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( _TestBandwidthUsage , self ) . setUp ( ) <EOL> self . user_id = '<STR_LIT>' <EOL> self . project_id = '<STR_LIT>' <EOL> self . context = context . RequestContext ( self . user_id , self . project_id ) <EOL> now , start_period = self . _time_now_and_start_period ( ) <EOL> self . expected_bw_usage = self . _fake_bw_usage ( <EOL> time = now , start_period = start_period ) <EOL> @ staticmethod <EOL> def _compare ( test , db , obj , ignored_fields = None ) : <EOL> if ignored_fields is None : <EOL> ignored_fields = [ ] <EOL> for field , value in db . items ( ) : <EOL> if field in ignored_fields : <EOL> continue <EOL> obj_field = field <EOL> if obj_field == '<STR_LIT>' : <EOL> obj_field = '<STR_LIT>' <EOL> test . assertEqual ( db [ field ] , getattr ( obj , obj_field ) , <EOL> '<STR_LIT>' % field ) <EOL> @ staticmethod <EOL> def _fake_bw_usage ( time = None , start_period = None , bw_in = <NUM_LIT:100> , <EOL> bw_out = <NUM_LIT:200> , last_ctr_in = <NUM_LIT> , last_ctr_out = <NUM_LIT> ) : <EOL> fake_bw_usage = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : uuids . instance , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : start_period , <EOL> '<STR_LIT>' : bw_in , <EOL> '<STR_LIT>' : bw_out , <EOL> '<STR_LIT>' : last_ctr_in , <EOL> '<STR_LIT>' : last_ctr_out , <EOL> '<STR_LIT>' : time <EOL> } <EOL> return fake_bw_usage <EOL> @ staticmethod <EOL> def _time_now_and_start_period ( ) : <EOL> now = timeutils . utcnow ( ) . replace ( tzinfo = iso8601 . iso8601 . Utc ( ) , <EOL> microsecond = <NUM_LIT:0> ) <EOL> start_period = now - datetime . timedelta ( seconds = <NUM_LIT:10> ) <EOL> return now , start_period <EOL> @ mock . patch . object ( db , '<STR_LIT>' ) <EOL> def test_get_by_instance_uuid_and_mac ( self , mock_get ) : <EOL> mock_get . return_value = self . expected_bw_usage <EOL> bw_usage = bandwidth_usage . BandwidthUsage . get_by_instance_uuid_and_mac ( <EOL> self . context , uuids . instance , '<STR_LIT>' , <EOL> start_period = self . expected_bw_usage [ '<STR_LIT>' ] ) <EOL> self . _compare ( self , self . expected_bw_usage , bw_usage ) <EOL> @ mock . patch . object ( db , '<STR_LIT>' ) <EOL> def test_get_by_uuids ( self , mock_get_by_uuids ) : <EOL> mock_get_by_uuids . return_value = [ self . expected_bw_usage ] <EOL> bw_usages = bandwidth_usage . BandwidthUsageList . get_by_uuids ( <EOL> self . context , [ uuids . instance ] , <EOL> start_period = self . expected_bw_usage [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( bw_usages ) ) <EOL> self . _compare ( self , self . expected_bw_usage , bw_usages [ <NUM_LIT:0> ] ) <EOL> @ mock . patch . object ( db , '<STR_LIT>' ) <EOL> def test_create ( self , mock_create ) : <EOL> mock_create . return_value = self . expected_bw_usage <EOL> bw_usage = bandwidth_usage . BandwidthUsage ( context = self . context ) <EOL> bw_usage . create ( uuids . instance , '<STR_LIT>' , <EOL> <NUM_LIT:100> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , <EOL> start_period = self . expected_bw_usage [ '<STR_LIT>' ] ) <EOL> self . _compare ( self , self . expected_bw_usage , bw_usage ) <EOL> def test_update_with_db ( self ) : <EOL> expected_bw_usage1 = self . _fake_bw_usage ( <EOL> time = self . expected_bw_usage [ '<STR_LIT>' ] , <EOL> start_period = self . expected_bw_usage [ '<STR_LIT>' ] , <EOL> last_ctr_in = <NUM_LIT> , last_ctr_out = <NUM_LIT> ) <EOL> bw_usage = bandwidth_usage . BandwidthUsage ( context = self . context ) <EOL> bw_usage . create ( uuids . instance , '<STR_LIT>' , <EOL> <NUM_LIT:100> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , <EOL> start_period = self . expected_bw_usage [ '<STR_LIT>' ] ) <EOL> self . _compare ( self , expected_bw_usage1 , bw_usage , <EOL> ignored_fields = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> bw_usage . create ( uuids . instance , '<STR_LIT>' , <EOL> <NUM_LIT:100> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , <EOL> start_period = self . expected_bw_usage [ '<STR_LIT>' ] ) <EOL> self . _compare ( self , self . expected_bw_usage , bw_usage , <EOL> ignored_fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> @ mock . patch . object ( db , '<STR_LIT>' ) <EOL> def test_update ( self , mock_update ) : <EOL> expected_bw_usage1 = self . _fake_bw_usage ( <EOL> time = self . expected_bw_usage [ '<STR_LIT>' ] , <EOL> start_period = self . expected_bw_usage [ '<STR_LIT>' ] , <EOL> last_ctr_in = <NUM_LIT> , last_ctr_out = <NUM_LIT> ) <EOL> mock_update . side_effect = [ expected_bw_usage1 , self . expected_bw_usage ] <EOL> bw_usage = bandwidth_usage . BandwidthUsage ( context = self . context ) <EOL> bw_usage . create ( '<STR_LIT>' , '<STR_LIT>' , <EOL> <NUM_LIT:100> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , <EOL> start_period = self . expected_bw_usage [ '<STR_LIT>' ] ) <EOL> self . _compare ( self , expected_bw_usage1 , bw_usage ) <EOL> bw_usage . create ( '<STR_LIT>' , '<STR_LIT>' , <EOL> <NUM_LIT:100> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , <EOL> start_period = self . expected_bw_usage [ '<STR_LIT>' ] ) <EOL> self . _compare ( self , self . expected_bw_usage , bw_usage ) <EOL> class TestBandwidthUsageObject ( test_objects . _LocalTest , <EOL> _TestBandwidthUsage ) : <EOL> pass <EOL> class TestRemoteBandwidthUsageObject ( test_objects . _RemoteTest , <EOL> _TestBandwidthUsage ) : <EOL> pass </s>
