11import pytest
22import time
33import boto3
4- import requests
54import json
5+ import requests
6+
7+ import localstack .sdk .chaos
8+ from localstack .sdk .models import FaultRule
9+ from localstack .sdk .chaos .managers import fault_configuration
610
7- LOCALSTACK_ENDPOINT = "http://localhost:4566"
8- CHAOS_ENDPOINT = f"{ LOCALSTACK_ENDPOINT } /_localstack/chaos/faults"
11+ LOCALSTACK_ENDPOINT = "http://localhost.localstack.cloud:4566"
912API_NAME = 'QuizAPI'
1013
14+ class TestLocalStackClient :
15+ client = localstack .sdk .chaos .ChaosClient ()
16+
17+ @pytest .fixture (scope = 'module' )
18+ def apigateway_client ():
19+ return boto3 .client ('apigateway' , endpoint_url = LOCALSTACK_ENDPOINT )
20+
1121@pytest .fixture (scope = 'module' )
12- def api_endpoint ():
13- apigateway_client = boto3 .client ('apigateway' , endpoint_url = LOCALSTACK_ENDPOINT )
22+ def api_endpoint (apigateway_client ):
1423 response = apigateway_client .get_rest_apis ()
1524 api_list = response .get ('items' , [])
1625 api = next ((item for item in api_list if item ['name' ] == API_NAME ), None )
@@ -27,29 +36,14 @@ def api_endpoint():
2736
2837 return API_ENDPOINT
2938
30- def initiate_dynamodb_outage ():
31- outage_payload = [{"service" : "dynamodb" , "region" : "us-east-1" }]
32- response = requests .post (CHAOS_ENDPOINT , json = outage_payload )
33- assert response .ok , "Failed to initiate DynamoDB outage"
34- print ("DynamoDB outage initiated." )
35- return outage_payload
36-
37- def check_outage_status (expected_status ):
38- response = requests .get (CHAOS_ENDPOINT )
39- assert response .ok , "Failed to get outage status"
40- outage_status = response .json ()
41- assert outage_status == expected_status , "Outage status does not match expected status"
42-
43- def stop_dynamodb_outage ():
44- response = requests .post (CHAOS_ENDPOINT , json = [])
45- assert response .ok , "Failed to stop DynamoDB outage"
46- check_outage_status ([])
47- print ("DynamoDB outage stopped." )
48-
4939def test_dynamodb_outage (api_endpoint ):
50- outage_payload = initiate_dynamodb_outage ()
51-
52- try :
40+ outage_rule = FaultRule (region = "us-east-1" , service = "dynamodb" )
41+
42+ # Using fault_configuration context manager to apply and automatically clean up the fault rule
43+ with fault_configuration (fault_rules = [outage_rule ]):
44+ print ("DynamoDB outage initiated within context." )
45+
46+ # Attempt to create a quiz during the outage
5347 create_quiz_payload = {
5448 "Title" : "Outage Test Quiz" ,
5549 "Visibility" : "Public" ,
@@ -70,19 +64,17 @@ def test_dynamodb_outage(api_endpoint):
7064 data = json .dumps (create_quiz_payload )
7165 )
7266
67+ # Expecting a 500 error due to the outage
7368 assert response .status_code == 500
7469 response_data = response .json ()
7570 assert "Error storing quiz data. It has been queued for retry." in response_data .get ("message" , "" )
7671 print ("Received expected error message during outage." )
7772
78- check_outage_status (outage_payload )
79-
80- finally :
81- stop_dynamodb_outage ()
82-
73+ # After the context manager exits, the outage should be resolved
8374 print ("Waiting for the system to process the queued request..." )
8475 time .sleep (15 )
8576
77+ # Check if the quiz was eventually created successfully
8678 response = requests .get (f"{ api_endpoint } /listquizzes" )
8779 assert response .status_code == 200
8880 quizzes_list = response .json ().get ('Quizzes' , [])
0 commit comments