Skip to content

Commit

Permalink
update test workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
pandomic committed Nov 28, 2024
1 parent 4f66506 commit 89f620e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
10 changes: 10 additions & 0 deletions lambdas/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import json

def prepare_response(status_code, body):
return {
"statusCode": status_code,
"body": json.dumps(body),
"headers": {
"Content-Type": "application/json",
}
}
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import logging

logging.basicConfig(level=logging.DEBUG)
33 changes: 27 additions & 6 deletions tests/test_infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import localstack.sdk.aws
import json
import time
import logging

from lambdas.utils import prepare_response

LOG = logging.getLogger(__name__)

@pytest.fixture(scope='module')
def api_endpoint():
Expand All @@ -21,7 +26,7 @@ def api_endpoint():
API_ID = api['id']
API_ENDPOINT = f"http://localhost:4566/restapis/{API_ID}/test/_user_request_"

print(f"API Endpoint: {API_ENDPOINT}")
LOG.info(f"API Endpoint: {API_ENDPOINT}")

time.sleep(2)

Expand Down Expand Up @@ -78,7 +83,7 @@ def test_quiz_workflow(api_endpoint):
assert 'QuizID' in quiz_creation_response
quiz_id = quiz_creation_response['QuizID']

print(f"Quiz created with ID: {quiz_id}")
LOG.info(f"Quiz created with ID: {quiz_id}")

response = requests.get(f"{api_endpoint}/listquizzes")
assert response.status_code == 200
Expand Down Expand Up @@ -149,7 +154,7 @@ def test_quiz_workflow(api_endpoint):
"SubmissionID": submission_response["SubmissionID"]
})

print(f"{user['Username']} submitted quiz with SubmissionID: {submission_response['SubmissionID']}")
LOG.info(f"{user['Username']} submitted quiz with SubmissionID: {submission_response['SubmissionID']}")

time.sleep(5)

Expand Down Expand Up @@ -191,7 +196,7 @@ def calculate_user_score(user_answers):
expected_score = expected_scores[username]
assert actual_score == pytest.approx(expected_score, abs=0.01)

print(f"{username} - Expected Score: {expected_score}, Actual Score: {actual_score}")
LOG.info(f"{username} - Expected Score: {expected_score}, Actual Score: {actual_score}")

for submission in submissions:
response = requests.get(f"{api_endpoint}/getsubmission?submission_id={submission['SubmissionID']}")
Expand All @@ -206,7 +211,7 @@ def calculate_user_score(user_answers):
actual_score = submission_data['Score']
assert actual_score == pytest.approx(expected_score, abs=0.01)

print(f"Verified submission for {submission['Username']} with Score: {actual_score}")
LOG.info(f"Verified submission for {submission['Username']} with Score: {actual_score}")

client = localstack.sdk.aws.AWSClient()
sender_email = "[email protected]"
Expand All @@ -227,6 +232,22 @@ def calculate_user_score(user_answers):
assert hasattr(body, 'html_part')
html_content = body.html_part

print(f"Email content: {html_content}")
LOG.info(f"Email content: {html_content}")

assert email_found, f"No email found sent from {sender_email}"

def test_faulty_assertion(api_endpoint):
test_quiz_workflow(api_endpoint=api_endpoint)

response = requests.get(f"{api_endpoint}/getleaderboard?quiz_id=wrongid&top=3")
assert response.status_code == 200

def test_faulty_invocation(api_endpoint):
test_quiz_workflow(api_endpoint=api_endpoint)

class NonSerializable:
pass

response = prepare_response(200, NonSerializable())

assert response['statusCode'] == 200
13 changes: 8 additions & 5 deletions tests/test_outage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import boto3
import json
import requests
import logging

import localstack.sdk.chaos
from localstack.sdk.models import FaultRule
from localstack.sdk.chaos.managers import fault_configuration

LOG = logging.getLogger(__name__)

LOCALSTACK_ENDPOINT = "http://localhost.localstack.cloud:4566"
API_NAME = 'QuizAPI'

Expand All @@ -30,7 +33,7 @@ def api_endpoint(apigateway_client):
API_ID = api['id']
API_ENDPOINT = f"{LOCALSTACK_ENDPOINT}/restapis/{API_ID}/test/_user_request_"

print(f"API Endpoint: {API_ENDPOINT}")
LOG.info(f"API Endpoint: {API_ENDPOINT}")

time.sleep(2)

Expand All @@ -41,7 +44,7 @@ def test_dynamodb_outage(api_endpoint):

# Using fault_configuration context manager to apply and automatically clean up the fault rule
with fault_configuration(fault_rules=[outage_rule]):
print("DynamoDB outage initiated within context.")
LOG.info("DynamoDB outage initiated within context.")

# Attempt to create a quiz during the outage
create_quiz_payload = {
Expand All @@ -68,10 +71,10 @@ def test_dynamodb_outage(api_endpoint):
assert response.status_code == 500
response_data = response.json()
assert "Error storing quiz data. It has been queued for retry." in response_data.get("message", "")
print("Received expected error message during outage.")
LOG.info("Received expected error message during outage.")

# After the context manager exits, the outage should be resolved
print("Waiting for the system to process the queued request...")
LOG.info("Waiting for the system to process the queued request...")
time.sleep(15)

# Check if the quiz was eventually created successfully
Expand All @@ -80,4 +83,4 @@ def test_dynamodb_outage(api_endpoint):
quizzes_list = response.json().get('Quizzes', [])
quiz_titles = [quiz['Title'] for quiz in quizzes_list]
assert "Outage Test Quiz" in quiz_titles
print("Quiz successfully created after outage resolved.")
LOG.info("Quiz successfully created after outage resolved.")

0 comments on commit 89f620e

Please sign in to comment.