def test_get_cloud_account_overview_with_openshift_and_rhel_image(self): """Assert an account overview reports openshift and rhel correctly.""" powered_times = ( ( util_helper.utc_dt(2018, 1, 10, 0, 0, 0), util_helper.utc_dt(2018, 1, 10, 5, 0, 0) ), ) account_helper.generate_aws_instance_events( self.instance_1, powered_times, self.windows_image.ec2_ami_id ) # in addition to instance_1's events, we are creating an event for # instance_2 with a rhel & openshift_image account_helper.generate_single_aws_instance_event( self.instance_2, self.start, InstanceEvent.TYPE.power_on, self.openshift_and_rhel_image.ec2_ami_id) overview = reports.get_account_overview( self.account, self.start, self.end) # we expect to find 2 total images, 2 total instances, 1 rhel instance # and 1 openshift instance self.assertExpectedAccountOverview(overview, self.account, images=2, instances=2, rhel_instances=1, openshift_instances=1)
def test_generate_aws_events_with_some_times(self): """Assert generation of InstanceEvents with some times.""" account = helper.generate_aws_account() instance = helper.generate_aws_instance(account) powered_times = ( (None, util_helper.utc_dt(2017, 1, 1)), (util_helper.utc_dt(2017, 1, 2), util_helper.utc_dt(2017, 1, 3)), (util_helper.utc_dt(2017, 1, 4), None), ) events = helper.generate_aws_instance_events(instance, powered_times) self.assertEqual(len(events), 4) self.assertEqual(events[0].occurred_at, powered_times[0][1]) self.assertEqual(events[1].occurred_at, powered_times[1][0]) self.assertEqual(events[2].occurred_at, powered_times[1][1]) self.assertEqual(events[3].occurred_at, powered_times[2][0]) self.assertEqual(events[0].event_type, InstanceEvent.TYPE.power_off) self.assertEqual(events[1].event_type, InstanceEvent.TYPE.power_on) self.assertEqual(events[2].event_type, InstanceEvent.TYPE.power_off) self.assertEqual(events[3].event_type, InstanceEvent.TYPE.power_on) self.assertIsNotNone(events[0].machineimage) self.assertIsNotNone(events[0].instance_type) self.assertIsNotNone(events[0].subnet) # Assert they all have the same AMI, subnet, and instance type. for event in events[1:]: self.assertEqual(event.machineimage, events[0].machineimage) self.assertEqual(event.instance_type, events[0].instance_type) self.assertEqual(event.subnet, events[0].subnet)
def test_generate_aws_events_with_args_and_some_times(self): """Assert generation of InstanceEvents with all specified args.""" account = helper.generate_aws_account() instance = helper.generate_aws_instance(account) powered_times = ( (None, util_helper.utc_dt(2017, 1, 1)), (util_helper.utc_dt(2017, 1, 2), util_helper.utc_dt(2017, 1, 3)), (util_helper.utc_dt(2017, 1, 4), None), ) ec2_ami_id = util_helper.generate_dummy_image_id() instance_type = random.choice(util_helper.SOME_EC2_INSTANCE_TYPES) subnet = str(uuid.uuid4()) events = helper.generate_aws_instance_events( instance, powered_times, ec2_ami_id=ec2_ami_id, instance_type=instance_type, subnet=subnet, ) self.assertEqual(len(events), 4) # We don't care to check *everything* in the events since that should # already be covered by ``test_generate_events_with_some_times``. # Here we only care that argument values were set correctly. for event in events: self.assertEqual(event.machineimage.ec2_ami_id, ec2_ami_id) self.assertEqual(event.instance_type, instance_type) self.assertEqual(event.subnet, subnet)
def test_get_cloud_account_overview_with_events(self): """Assert an account overview reports instances/images correctly.""" powered_times = ( ( util_helper.utc_dt(2018, 1, 10, 0, 0, 0), util_helper.utc_dt(2018, 1, 10, 5, 0, 0) ), ) account_helper.generate_aws_instance_events( self.instance_1, powered_times, self.windows_image.ec2_ami_id ) overview = reports.get_account_overview( self.account, self.start, self.end) self.assertExpectedAccountOverview(overview, self.account, images=1, instances=1)
def generate_events(self, powered_times, instance=None, image=None): """ Generate events saved to the DB and returned. Args: powered_times (list[tuple]): Time periods instance is powered on. instance (Instance): Optional which instance has the events. If not specified, default is self.instance_1. image (AwsMachineImage): Optional which image seen in the events. If not specified, default is self.image_rhel. Returns: list[InstanceEvent]: The list of events """ if instance is None: instance = self.instance_1 if image is None: image = self.image_rhel events = account_helper.generate_aws_instance_events( instance, powered_times, image.ec2_ami_id, ) return events
def test_generate_aws_events_default_and_no_times(self): """Assert generation of InstanceEvents with minimal args.""" account = helper.generate_aws_account() instance = helper.generate_aws_instance(account) events = helper.generate_aws_instance_events(instance, tuple()) self.assertEqual(len(events), 0)
for account, account_instances in instances.items(): for instance in account_instances: print(f'instance:' f'id={instance.id};' f'account_id={instance.account_id};') times = [ (util_helper.utc_dt(2018, 1, 12, 5, 0, 0), util_helper.utc_dt(2018, 1, 12, 6, 0, 0)), ] events = [] events.extend( account_helper.generate_aws_instance_events( instances['normie_greatest'][0], times, ec2_ami_id=images['normie_greatest']['rhel7'].ec2_ami_id, )) events.extend( account_helper.generate_aws_instance_events( instances['normie_greatest'][1], times, ec2_ami_id=images['normie_greatest']['rhel7'].ec2_ami_id, )) events.extend( account_helper.generate_aws_instance_events( instances['normie_greatest'][2], times, ec2_ami_id=images['normie_greatest']['rhel8'].ec2_ami_id, )) events.extend(
f'account_id={instance.account_id};') times = [ (util_helper.utc_dt(2018, 1, 12, 5, 0, 0), util_helper.utc_dt(2018, 1, 12, 6, 0, 0)), ( util_helper.utc_dt(2018, 8, 23, 0, 0, 0), util_helper.utc_dt(2019, 8, 23, 0, 0, 0), ), ] events = [] events.extend( account_helper.generate_aws_instance_events( instances['normie_account'][0], times, ec2_ami_id=images['normie_account']['plain'].ec2_ami_id, )) events.extend( account_helper.generate_aws_instance_events( instances['normie_account'][1], times, ec2_ami_id=images['normie_account']['rhel7'].ec2_ami_id, )) events.extend( account_helper.generate_aws_instance_events( instances['normie_account'][2], times, ec2_ami_id=images['normie_account']['rhel7'].ec2_ami_id, )) events.extend(
image_rhel = account_helper.generate_aws_image( account_1, is_rhel=True, ec2_ami_id='ami-rhel') print(f'image_rhel.id={image_rhel.id};' f'image_rhel.ec2_ami_id={image_rhel.ec2_ami_id};' ) events = account_helper.generate_aws_instance_events( instance_1, [( util_helper.utc_dt(2018, 1, 11, 5, 0, 0), util_helper.utc_dt(2018, 1, 11, 6, 0, 0) ), ( util_helper.utc_dt(2018, 1, 11, 7, 0, 0), util_helper.utc_dt(2018, 1, 11, 8, 0, 0) ), ( util_helper.utc_dt(2018, 1, 11, 9, 0, 0), util_helper.utc_dt(2018, 1, 11, 10, 0, 0) )], ec2_ami_id=image_rhel.ec2_ami_id, ) for event in events: print(f'event.instance.account.id={event.instance.account.id};' f'event.occurred_at={event.occurred_at};' f'event.event_type={event.event_type};' f'event.machineimage.ec2_ami_id={event.machineimage.ec2_ami_id};')
f'image_ocp.ec2_ami_id={image_ocp.ec2_ami_id};' # f'image_ocp.rhel={image_ocp.rhel};' # f'image_ocp.openshift={image_ocp.openshift};' ) print( f'image_rhel_ocp.id={image_rhel_ocp.id};' f'image_rhel_ocp.ec2_ami_id={image_rhel_ocp.ec2_ami_id};' # f'image_rhel_ocp.rhel={image_rhel_ocp.rhel};' # f'image_rhel_ocp.openshift={image_rhel_ocp.openshift};' ) events = [] events.extend( account_helper.generate_aws_instance_events( instance_1, [(util_helper.utc_dt(2018, 1, 9, 3, 0, 0), util_helper.utc_dt(2018, 1, 11, 3, 0, 0))], ec2_ami_id=image_plain.ec2_ami_id, )) events.extend( account_helper.generate_aws_instance_events( instance_2, [ (util_helper.utc_dt(2017, 12, 24, 3, 0, 0), util_helper.utc_dt(2017, 12, 29, 3, 0, 0)), (util_helper.utc_dt(2018, 1, 10, 5, 0, 0), util_helper.utc_dt(2018, 1, 12, 5, 0, 0)), (util_helper.utc_dt(2018, 2, 20, 5, 0, 0), util_helper.utc_dt(2018, 2, 23, 5, 0, 0)), ], ec2_ami_id=image_rhel.ec2_ami_id, ))
def test_several_instances_with_whole_days(self): """ Assert correct report for instances with various run times. The RHEL-only running times over the month would look like: [ #### ## ] [ ## ## ] The plain running times over the month would look like: [ ##### ] The OpenShift-only running times over the month would look like: [ ### ] The RHEL+OpenShift running times over the month would look like: [ # ## ] """ powered_times_1 = ( ( util_helper.utc_dt(2018, 1, 2, 0, 0, 0), util_helper.utc_dt(2018, 1, 6, 0, 0, 0) ), ( util_helper.utc_dt(2018, 1, 12, 0, 0, 0), util_helper.utc_dt(2018, 1, 14, 0, 0, 0) ), ) account_helper.generate_aws_instance_events( self.instance_1, powered_times_1, ec2_ami_id=self.image_rhel.ec2_ami_id, ) powered_times_2 = ( ( util_helper.utc_dt(2018, 1, 3, 0, 0, 0), util_helper.utc_dt(2018, 1, 5, 0, 0, 0) ), ( util_helper.utc_dt(2018, 1, 21, 0, 0, 0), util_helper.utc_dt(2018, 1, 23, 0, 0, 0) ), ) account_helper.generate_aws_instance_events( self.instance_2, powered_times_2, ec2_ami_id=self.image_rhel.ec2_ami_id, ) powered_times_3 = ( ( util_helper.utc_dt(2018, 1, 3, 0, 0, 0), util_helper.utc_dt(2018, 1, 8, 0, 0, 0) ), ) account_helper.generate_aws_instance_events( self.instance_3, powered_times_3, ec2_ami_id=self.image_plain.ec2_ami_id, ) powered_times_4 = ( ( util_helper.utc_dt(2018, 1, 19, 0, 0, 0), util_helper.utc_dt(2018, 1, 22, 0, 0, 0) ), ) account_helper.generate_aws_instance_events( self.instance_4, powered_times_4, ec2_ami_id=self.image_ocp.ec2_ami_id, ) powered_times_5 = ( ( util_helper.utc_dt(2018, 1, 9, 0, 0, 0), util_helper.utc_dt(2018, 1, 10, 0, 0, 0) ), ( util_helper.utc_dt(2018, 1, 20, 0, 0, 0), util_helper.utc_dt(2018, 1, 22, 0, 0, 0) ), ) account_helper.generate_aws_instance_events( self.instance_5, powered_times_5, ec2_ami_id=self.image_rhel_ocp.ec2_ami_id, ) results = reports.get_daily_usage( self.account_1.user_id, self.start, self.end, ) self.assertEqual(len(results['daily_usage']), 31) self.assertInstancesSeen(results, rhel=3, openshift=2) # total of rhel seconds should be 13 days worth of seconds. # total of openshift seconds should be 6 days worth of seconds. self.assertTotalRunningTimes(results, rhel=DAY * 13, openshift=DAY * 6) # number of individual days in which we saw anything rhel is 10 # number of individual days in which we saw anything openshift is 4 self.assertDaysSeen(results, rhel=10, openshift=4) # number of days in which we saw 2 rhel running all day is 3 self.assertEqual(sum(( 1 for day in results['daily_usage'] if day['rhel_runtime_seconds'] == DAY * 2 )), 3) # number of days in which we saw 1 rhel running all day is 7 self.assertEqual(sum(( 1 for day in results['daily_usage'] if day['rhel_runtime_seconds'] == DAY )), 7) # number of days in which we saw 1 openshift running all day is 2 self.assertEqual(sum(( 1 for day in results['daily_usage'] if day['openshift_runtime_seconds'] == DAY * 2 )), 2) # number of days in which we saw 2 openshift running all day is 2 self.assertEqual(sum(( 1 for day in results['daily_usage'] if day['openshift_runtime_seconds'] == DAY * 2 )), 2)