Skip to content

Commit

Permalink
Fixed the time_create error in azure instance (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma authored Jan 2, 2025
1 parent 0b74b46 commit 98fbe75
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
28 changes: 20 additions & 8 deletions tests/unittest/cloud_governance/policy/azure/test_instance_idle.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def test_instance_idle():
environment_variables.environment_variables_dict['policy'] = 'instance_idle'
monitor_client = MonitorManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
compute_client = ComputeManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
instance = compute_client.virtual_machines.begin_create_or_update(vm_name='test-unitest', location='useast')
time_created = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(8)
instance = compute_client.virtual_machines.begin_create_or_update(vm_name='test-unitest', location='useast',
time_created=time_created)
monitor_client.metrics.create_metric(resource_id=instance.id, type='VirtualMachine', name='test-cpu-metric',
unit='Percentage CPU',
timeseries=[TimeSeriesElement(data=[
Expand Down Expand Up @@ -63,7 +65,9 @@ def test_instance_idle__check_not_idle():
environment_variables.environment_variables_dict['dry_run'] = 'yes'
environment_variables.environment_variables_dict['policy'] = 'instance_idle'
compute_client = ComputeManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
instance = compute_client.virtual_machines.begin_create_or_update(vm_name='test-unitest', location='useast')
time_created = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(8)
instance = compute_client.virtual_machines.begin_create_or_update(vm_name='test-unitest', location='useast',
time_created=time_created)
monitor_client = MonitorManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
monitor_client.metrics.create_metric(resource_id=instance.id, type='VirtualMachine', name='test-cpu-metric',
unit='Percentage CPU',
Expand Down Expand Up @@ -98,7 +102,9 @@ def test_instance_idle__skip_cluster():
environment_variables.environment_variables_dict['dry_run'] = 'yes'
environment_variables.environment_variables_dict['policy'] = 'instance_idle'
compute_client = ComputeManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
compute_client.virtual_machines.begin_create_or_update(vm_name='test-unitest', location='useast', tags=tags)
time_created = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(8)
compute_client.virtual_machines.begin_create_or_update(vm_name='test-unitest', location='useast', tags=tags,
time_created=time_created)
instance_idle = InstanceIdle()
response = instance_idle.run()
assert len(response) == 0
Expand All @@ -117,7 +123,9 @@ def test_instance_idle__dryrun_no():
environment_variables.environment_variables_dict['dry_run'] = 'yes'
environment_variables.environment_variables_dict['policy'] = 'instance_idle'
compute_client = ComputeManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
instance = compute_client.virtual_machines.begin_create_or_update(vm_name='test-unitest', location='useast', tags=tags)
time_created = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(8)
instance = compute_client.virtual_machines.begin_create_or_update(vm_name='test-unitest', location='useast',
tags=tags, time_created=time_created)
monitor_client = MonitorManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
monitor_client.metrics.create_metric(resource_id=instance.id, type='VirtualMachine', name='test-cpu-metric',
unit='Percentage CPU',
Expand Down Expand Up @@ -152,7 +160,9 @@ def test_instance_idle__dryrun_no_delete():
environment_variables.environment_variables_dict['dry_run'] = 'no'
environment_variables.environment_variables_dict['policy'] = 'instance_idle'
compute_client = ComputeManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
instance = compute_client.virtual_machines.begin_create_or_update(vm_name='test-unitest', location='useast', tags=tags)
time_created = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(8)
instance = compute_client.virtual_machines.begin_create_or_update(vm_name='test-unitest', location='useast',
tags=tags, time_created=time_created)
monitor_client = MonitorManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
monitor_client.metrics.create_metric(resource_id=instance.id, type='VirtualMachine', name='test-cpu-metric',
unit='Percentage CPU',
Expand Down Expand Up @@ -189,8 +199,9 @@ def test_instance_idle__skips_delete():
environment_variables.environment_variables_dict['dry_run'] = 'no'
environment_variables.environment_variables_dict['policy'] = 'instance_idle'
compute_client = ComputeManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
time_created = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(8)
instance = compute_client.virtual_machines.begin_create_or_update(vm_name='test-unitest', location='useast',
tags=tags)
tags=tags, time_created=time_created)
monitor_client = MonitorManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
monitor_client.metrics.create_metric(resource_id=instance.id, type='VirtualMachine', name='test-cpu-metric',
unit='Percentage CPU',
Expand Down Expand Up @@ -219,16 +230,17 @@ def test_instance_idle__skips_delete():
@mock_monitor
def test_instance_idle__set_counter_zero():
"""
This method tests unused_nat_gateway to set days counter to 0
This method tests unused_nat_gateway to set days counter to 0
:return:
:rtype:
"""
tags = {'DaysCount': f'{CURRENT_DATE}@7'}
environment_variables.environment_variables_dict['dry_run'] = 'yes'
environment_variables.environment_variables_dict['policy'] = 'instance_idle'
compute_client = ComputeManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
time_created = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(8)
instance = compute_client.virtual_machines.begin_create_or_update(vm_name='test-unitest', location='useast',
tags=tags)
tags=tags, time_created=time_created)
monitor_client = MonitorManagementClient(subscription_id=SUBSCRIPTION_ID, credential=MockDefaultAzureCredential())
monitor_client.metrics.create_metric(resource_id=instance.id, type='VirtualMachine', name='test-cpu-metric',
unit='Percentage CPU',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ def __init__(self, vm_name: str, *args, **kwargs):

if not kwargs.get('time_created'):
self.time_created = CURRENT_DATE_TIME
else:
self.time_created = kwargs.get('time_created')
if not kwargs.get('hardware_profile'):
self.hardware_profile = HardwareProfile(vm_size='Standard_D2s_v3')
else:
self.hardware_profile = kwargs.get('hardware_profile')


class MockVirtualMachinesOperations:

VIRTUAL_MACHINE = 'virtualMachines'

def __init__(self):
Expand Down

0 comments on commit 98fbe75

Please sign in to comment.