From e61d794cbc9c18b06c11014da666e25f3fce637b Mon Sep 17 00:00:00 2001 From: Connor <6322551+ccggeo@users.noreply.github.com> Date: Thu, 18 Feb 2021 19:32:06 +0000 Subject: [PATCH] feature/refresh_trusted_advisor_check (#3705) * Support - added refresh_trusted_advisor_check - Returns a random check status - Returns the check id in the response - Testing for these two functionalities * test_support - addressed PR comments, to cycle through a faked number of check status responses --- moto/support/models.py | 34 +++++++++ moto/support/responses.py | 6 ++ tests/test_support/test_support.py | 112 +++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) diff --git a/moto/support/models.py b/moto/support/models.py index 7e799ea67..84001ad66 100644 --- a/moto/support/models.py +++ b/moto/support/models.py @@ -13,6 +13,7 @@ class SupportBackend(BaseBackend): def __init__(self, region_name=None): super(SupportBackend, self).__init__() self.region_name = region_name + self.check_status = {} def reset(self): region_name = self.region_name @@ -24,6 +25,39 @@ class SupportBackend(BaseBackend): checks = ADVISOR_CHECKS["checks"] return checks + def refresh_trusted_advisor_check(self, check_id): + self.advance_check_status(check_id) + status = { + "status": { + "checkId": check_id, + "status": self.check_status[check_id], + "millisUntilNextRefreshable": 123, + } + } + return status + + def advance_check_status(self, check_id): + """ + Fake an advancement through statuses on refreshing TA checks + """ + if check_id not in self.check_status: + self.check_status[check_id] = "none" + + elif self.check_status[check_id] == "none": + self.check_status[check_id] = "enqueued" + + elif self.check_status[check_id] == "enqueued": + self.check_status[check_id] = "processing" + + elif self.check_status[check_id] == "processing": + self.check_status[check_id] = "success" + + elif self.check_status[check_id] == "success": + self.check_status[check_id] = "abandoned" + + elif self.check_status[check_id] == "abandoned": + self.check_status[check_id] = "none" + support_backends = {} diff --git a/moto/support/responses.py b/moto/support/responses.py index 9b1d5f213..deaa1cf84 100644 --- a/moto/support/responses.py +++ b/moto/support/responses.py @@ -18,3 +18,9 @@ class SupportResponse(BaseResponse): ) return json.dumps({"checks": checks}) + + def refresh_trusted_advisor_check(self): + check_id = self._get_param("checkId") + status = self.support_backend.refresh_trusted_advisor_check(check_id=check_id,) + + return json.dumps(status) diff --git a/tests/test_support/test_support.py b/tests/test_support/test_support.py index 6f6b84181..3b2d6c72a 100644 --- a/tests/test_support/test_support.py +++ b/tests/test_support/test_support.py @@ -43,3 +43,115 @@ def test_describe_trusted_advisor_checks_returns_an_expected_check_name(): check_names.append(check["name"]) check_names.should.contain("Unassociated Elastic IP Addresses") + + +@mock_support +def test_refresh_trusted_advisor_check_returns_expected_check(): + """ + A refresh of a trusted advisor check returns the check id + in the response + """ + client = boto3.client("support", "us-east-1") + check_name = "XXXIIIY" + response = client.refresh_trusted_advisor_check(checkId=check_name) + response["status"]["checkId"].should.equal(check_name) + + +@mock_support +def test_refresh_trusted_advisor_check_returns_an_expected_status(): + """ + A refresh of a trusted advisor check returns an expected status + """ + client = boto3.client("support", "us-east-1") + possible_statuses = ["none", "enqueued", "processing", "success", "abandoned"] + check_name = "XXXIIIY" + response = client.refresh_trusted_advisor_check(checkId=check_name) + actual_status = [response["status"]["status"]] + set(actual_status).issubset(possible_statuses).should.be.true + + +@mock_support +def test_refresh_trusted_advisor_check_cycles_to_new_status_on_each_call(): + """ + On each call, the next expected status is returned + """ + client = boto3.client("support", "us-east-1") + check_name = "XXXIIIY" + actual_statuses = [] + possible_statuses = ["none", "enqueued", "processing", "success", "abandoned"] + + for status in possible_statuses: + response = client.refresh_trusted_advisor_check(checkId=check_name) + actual_statuses.append(response["status"]["status"]) + + actual_statuses.should.equal(possible_statuses) + + +@mock_support +def test_refresh_trusted_advisor_check_cycles_to_new_status_on_each_call(): + """ + Called only three times, only three expected statuses are returned + """ + client = boto3.client("support", "us-east-1") + check_name = "XXXIIIY" + actual_statuses = [] + possible_statuses = ["none", "enqueued", "processing"] + + for status in possible_statuses: + response = client.refresh_trusted_advisor_check(checkId=check_name) + actual_statuses.append(response["status"]["status"]) + + unexpected_statuses = set(["success", "abandoned"]).issubset(actual_statuses) + + actual_statuses.should.equal( + possible_statuses + ) and unexpected_statuses.should.be.false + + +@mock_support +def test_refresh_trusted_advisor_check_cycles_to_new_status_on_with_two_checks(): + """ + On each call, the next expected status is returned when additional checks are made + """ + client = boto3.client("support", "us-east-1") + check_1_name = "XXXIIIY" + check_2_name = "XXXIIIZ" + check_1_statuses = [] + check_2_statuses = [] + possible_statuses = ["none", "enqueued", "processing", "success", "abandoned"] + + for check in possible_statuses: + response = client.refresh_trusted_advisor_check(checkId=check_1_name) + check_1_statuses.append(response["status"]["status"]) + + for check in possible_statuses: + response = client.refresh_trusted_advisor_check(checkId=check_2_name) + check_2_statuses.append(response["status"]["status"]) + + check_1_statuses.should.equal(possible_statuses) and check_2_statuses.should.equal( + possible_statuses + ) + + +@mock_support +def test_refresh_trusted_advisor_check_cycle_continues_on_full_cycle(): + """ + After cycling through all statuses, the check continues the cycle + """ + + client = boto3.client("support", "us-east-1") + check_name = "XXXIIIY" + possible_statuses = [ + "none", + "enqueued", + "processing", + "success", + "abandoned", + ] + + for status in possible_statuses: + client.refresh_trusted_advisor_check(checkId=check_name) + + expected_none_response = client.refresh_trusted_advisor_check(checkId=check_name) + + expected_none_response["status"]["status"].should.equal("none")