Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the time_create error in azure instance #887

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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