Skip to content

Commit

Permalink
Merge pull request #8 from spreaker/check_if_there_are_no_registered_…
Browse files Browse the repository at this point in the history
…instances

Return true if there are no registered instances in the service
  • Loading branch information
Marco Pracucci authored Apr 4, 2019
2 parents 778c825 + 1f6c7e4 commit 3ac0495
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cloudunmap/unmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def unmapTerminatedInstancesFromService(serviceId: str, serviceRegion: str, inst
# List registered instances on CloudMap
serviceInstances = listServiceInstances(serviceId, sdClient)

# Skip in case there are no registered instances
if not serviceInstances:
logger.info(f"No registered instances in the service {serviceId}. Skipping")
return True

# Filter out service instances without the AWS_INSTANCE_IPV4 attribute
# and extract instance ids
serviceInstances = list(filter(lambda i: "AWS_INSTANCE_IPV4" in i["Attributes"], serviceInstances))
Expand Down
24 changes: 23 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
from unittest.mock import patch
from botocore.stub import Stubber
from cloudunmap.cli import main, parseArguments
from cloudunmap.cli import main, parseArguments, upMetric, lastReconcileTimestampMetric
from .mocks import mockBotoClient, mockServiceInstance, mockEC2Instance
from prometheus_client.registry import REGISTRY as prometheusDefaultRegistry

Expand All @@ -21,6 +21,11 @@ def setUp(self):

self.botoClientMock = mockBotoClient({"ec2": self.ec2Client, "servicediscovery": self.sdClient})

try:
lastReconcileTimestampMetric.remove("srv-1")
upMetric.remove("srv-1")
except KeyError:
pass
#
# main()
#
Expand Down Expand Up @@ -90,3 +95,20 @@ def testMainShouldDoNotDeregisterInstancesIfAllRegisteredInstancesWouldBeDeregis

self.ec2Stubber.assert_no_pending_responses()
self.sdStubber.assert_no_pending_responses()

def testMainShouldDoNotDeregisterInstancesIfThereAreNoRegisteredInstances(self):
# Mock Cloud Map client
self.sdStubber.add_response(
"list_instances",
{"Instances": []},
{"ServiceId": "srv-1", "MaxResults": 100})

with patch("boto3.client", side_effect=self.botoClientMock):
main(parseArguments(["--service-id", "srv-1", "--service-region", "eu-west-1", "--instances-region", "eu-west-1", "--single-run"]))

# Check exported metrics
self.assertEqual(prometheusDefaultRegistry.get_sample_value("aws_cloud_unmap_up", labels={"service_id": "srv-1"}), 1)
self.assertAlmostEqual(prometheusDefaultRegistry.get_sample_value("aws_cloud_unmap_last_reconcile_success_timestamp_seconds", labels={"service_id": "srv-1"}), time.time(), delta=2)

self.ec2Stubber.assert_no_pending_responses()
self.sdStubber.assert_no_pending_responses()

0 comments on commit 3ac0495

Please sign in to comment.