Skip to content

Commit

Permalink
gce: Add test for cleanup_blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed May 29, 2024
1 parent f830718 commit b303010
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions tests/test_gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def list_next(self, *args, **kwargs):
return self.responses.pop(0)

def delete(self, *args, **kwargs):
for resource in ('image', 'disk', 'instance', 'firewall', 'forwardingRule', 'route', 'network', 'subnetwork'):
for resource in ('object', 'image', 'disk', 'instance', 'firewall', 'forwardingRule', 'route', 'network', 'subnetwork'):
if resource in kwargs:
if self.error_reason:
return MockRequest(error_reason=self.error_reason)
Expand All @@ -58,6 +58,7 @@ def something():


class MockClient:
def objects(self): pass
def disks(self): pass
def firewalls(self): pass
def forwardingRules(self): pass
Expand All @@ -76,6 +77,7 @@ def gce():
patch.object(GCE, 'read_auth_json', return_value={}),
):
gce = GCE('fake')
gce.storage_client = MockClient
gce.compute_client = MockClient
gce.compute_client.instances = MockResource([MockRequest({'items': ['instance1', 'instance2']}), None])
yield gce
Expand All @@ -98,14 +100,14 @@ def mocked_resource():
return MockResource([
MockRequest({ # on images().list()
'items': [
{'name': 'keep', 'creationTimestamp': now_age, 'network': 'mynetwork'},
{'name': 'delete1', 'creationTimestamp': older_than_max_age, 'network': 'mynetwork'}
{'name': 'keep', 'creationTimestamp': now_age, 'timeCreated': now_age, 'network': 'mynetwork'},
{'name': 'delete1', 'creationTimestamp': older_than_max_age, 'timeCreated': older_than_max_age, 'network': 'mynetwork'}
], 'id': "id"}),
MockRequest(), # on images().delete()
MockRequest({ # on images().list_next()
'items': [
{'name': 'keep', 'creationTimestamp': now_age, 'network': 'mynetwork'},
{'name': 'delete2', 'creationTimestamp': older_than_max_age, 'network': 'mynetwork'}
{'name': 'keep', 'creationTimestamp': now_age, 'timeCreated': now_age, 'network': 'mynetwork'},
{'name': 'delete2', 'creationTimestamp': older_than_max_age, 'timeCreated': older_than_max_age, 'network': 'mynetwork'}
], 'id': "id"}),
MockRequest({'error': {'errors': [{'message': 'err message'}]},
'warnings': [{'message': 'warning message'}]}),
Expand Down Expand Up @@ -150,13 +152,20 @@ def _test_cleanup(gce, resource_type, cleanup_call, resources):
patch.object(gce, 'list_zones', return_value=['zone1']),
):
setattr(gce.compute_client, resource_type, resources)
setattr(gce.storage_client, resource_type, resources)
cleanup_call()
if gce.dry_run:
assert resources.deleted_resources == []
else:
assert resources.deleted_resources == ['delete1', 'delete2']


@mark.parametrize("dry_run", [True, False])
def test_cleanup_blobs(gce, mocked_resource, dry_run):
gce.dry_run = dry_run
_test_cleanup(gce, "objects", gce.cleanup_blobs, mocked_resource)


@mark.parametrize("dry_run", [True, False])
def test_cleanup_disks(gce, mocked_resource, dry_run):
gce.dry_run = dry_run
Expand Down Expand Up @@ -214,6 +223,7 @@ def test_cleanup_networks(gce, mocked_resource, dry_run):


def test_cleanup_all(gce):
gce.cleanup_blobs = MagicMock()
gce.cleanup_disks = MagicMock()
gce.cleanup_images = MagicMock()
gce.cleanup_firewalls = MagicMock()
Expand All @@ -222,6 +232,7 @@ def test_cleanup_all(gce):
gce.cleanup_subnetworks = MagicMock()
gce.cleanup_networks = MagicMock()
gce.cleanup_all()
gce.cleanup_blobs.assert_called_once()
gce.cleanup_disks.assert_called_once()
gce.cleanup_images.assert_called_once()
gce.cleanup_firewalls.assert_called_once()
Expand Down

0 comments on commit b303010

Please sign in to comment.