Best Python code snippet using localstack_python
test_route53_probes.py
Source:test_route53_probes.py
1import json2import os3from unittest.mock import MagicMock, patch4import pytest5from botocore.exceptions import ClientError6from chaoslib.exceptions import FailedActivity7from chaosaws.route53.probes import (8 get_dns_answer,9 get_health_check_status,10 get_hosted_zone,11)12module_path = os.path.dirname(os.path.abspath(__file__))13def read_in_data(filename):14 with open(os.path.join(module_path, "data", filename)) as fh:15 data = json.loads(fh.read())16 return data17@patch("chaosaws.route53.probes.aws_client", autospec=True)18def test_get_hosted_zone(m_client):19 mock_response = read_in_data("get_hosted_zone_1.json")20 client = MagicMock()21 m_client.return_value = client22 client.get_hosted_zone.return_value = mock_response23 response = get_hosted_zone(zone_id="AAAAAAAAAAAAA")24 client.get_hosted_zone.assert_called_with(Id="AAAAAAAAAAAAA")25 assert response["HostedZone"]["Name"] == "aws.testrecord.com."26@patch("chaosaws.route53.probes.aws_client", autospec=True)27def test_get_hosted_zone_not_found(m_client):28 mock_response = ClientError(29 operation_name="get_hosted_zone",30 error_response={"Error": {"Message": "Test Error", "Code": "NoSuchHostedZone"}},31 )32 client = MagicMock()33 m_client.return_value = client34 client.get_hosted_zone.side_effect = mock_response35 with pytest.raises(FailedActivity) as e:36 get_hosted_zone(zone_id="BBBBBBBBBBBBB")37 assert "Hosted Zone BBBBBBBBBBBBB not found." in str(e)38@patch("chaosaws.route53.probes.aws_client", autospec=True)39def test_get_health_check_status(m_client):40 mock_response = read_in_data("get_health_check_status_1.json")41 client = MagicMock()42 m_client.return_value = client43 client.get_health_check_status.return_value = mock_response44 response = get_health_check_status(check_id="00000000-0000-0000-0000-000000000000")45 client.get_health_check_status.assert_called_with(46 HealthCheckId="00000000-0000-0000-0000-000000000000"47 )48 assert len(response["HealthCheckObservations"]) == 249@patch("chaosaws.route53.probes.aws_client", autospec=True)50def test_get_health_check_status_not_found(m_client):51 mock_response = ClientError(52 operation_name="get_hosted_zone",53 error_response={54 "Error": {"Message": "Test Error", "Code": "NoSuchHealthCheck"}55 },56 )57 client = MagicMock()58 m_client.return_value = client59 client.get_health_check_status.side_effect = mock_response60 with pytest.raises(FailedActivity) as e:61 get_health_check_status(check_id="00000000-1111-1111-1111-000000000000")62 assert "Test Error" in str(e)63@patch("chaosaws.route53.probes.aws_client", autospec=True)64def test_get_health_check_status_no_results(m_client):65 mock_response = {"HealthCheckObservations": []}66 client = MagicMock()67 m_client.return_value = client68 client.get_health_check_status.return_value = mock_response69 with pytest.raises(FailedActivity) as e:70 get_health_check_status(check_id="00000000-2222-2222-2222-000000000000")71 assert "No results found for" in str(e)72@patch("chaosaws.route53.probes.aws_client", autospec=True)73def test_get_dns_answer(m_client):74 mock_response = read_in_data("test_dns_answer_1.json")75 client = MagicMock()76 m_client.return_value = client77 client.test_dns_answer.return_value = mock_response78 response = get_dns_answer(79 zone_id="AAAAAAAAAAAAA", record_name="aws.testrecord.com", record_type="A"80 )81 client.test_dns_answer.assert_called_with(82 HostedZoneId="AAAAAAAAAAAAA", RecordName="aws.testrecord.com", RecordType="A"83 )84 assert response["ResponseCode"] == "NOERROR"85@patch("chaosaws.route53.probes.aws_client", autospec=True)86def test_get_dns_answer_not_found(m_client):87 mock_response = ClientError(88 operation_name="get_hosted_zone",89 error_response={90 "Error": {"Message": "Test Error", "Code": "NoSuchHealthCheck"}91 },92 )93 client = MagicMock()94 m_client.return_value = client95 client.test_dns_answer.side_effect = mock_response96 with pytest.raises(FailedActivity) as e:97 get_dns_answer(98 zone_id="BBBBBBBBBBBBB", record_name="aws.testrecord.com", record_type="A"99 )...
test_lambda.py
Source:test_lambda.py
1"""Unit tests for the lambda module."""2import os3from functools import wraps4from unittest.mock import patch, DEFAULT5from src import dns_lambda6def clean_env(func):7 """Empty the environment for the decorated function."""8 @wraps(func)9 def wrapper(*args, **kwargs):10 """Wraps the decorated function."""11 backup_env = dict(os.environ)12 os.environ = {}13 result = func(*args, **kwargs)14 os.environ = backup_env15 return result16 return wrapper17def test_get_dns_tag():18 """Test for the get_dns_tag function."""19 event = {"detail": {"instance-id": "i-xxx"}}20 with patch("src.dns_lambda.boto3") as boto3:21 instance = boto3.resource().Instance()22 instance.tags = [{"Key": "dns-name", "Value": "my-ec2"}]23 instance.public_ip_address = "1.1.1.1"24 result = dns_lambda.get_dns_tag(event)25 assert result == ("my-ec2", "1.1.1.1")26 instance.tags = []27 result = dns_lambda.get_dns_tag(event)28 assert result == (None, None)29@clean_env30def test_create_record():31 """Test for the create_record function."""32 with patch.multiple(33 "src.dns_lambda",34 get_dns_tag=DEFAULT,35 boto3=DEFAULT,36 ) as mocks:37 mocks["get_dns_tag"].return_value = ("my-ec2", "1.1.1.1")38 dns_lambda.create_record({}, None)39 mocks["boto3"].client().change_resource_record_sets.assert_called_with(40 HostedZoneId=None,41 ChangeBatch={42 "Comment": "Add my-ec2 EC2 record",43 "Changes": [44 {45 "Action": "UPSERT",46 "ResourceRecordSet": {47 "Name": "my-ec2.None",48 "Type": "A",49 "TTL": 10,50 "ResourceRecords": [{"Value": "1.1.1.1"}],51 },52 }53 ],54 },55 )56@clean_env57def test_delete_record():58 """Test for the delete_record funtion."""59 with patch.multiple(60 "src.dns_lambda",61 get_dns_tag=DEFAULT,62 boto3=DEFAULT,63 ) as mocks:64 mocks["get_dns_tag"].return_value = ("my-ec2", "1.1.1.1")65 mocks["boto3"].client().test_dns_answer.return_value = {66 "RecordData": ["1.1.1.1"]67 }68 dns_lambda.delete_record({}, None)69 mocks["boto3"].client().test_dns_answer.assert_called_with(70 HostedZoneId=None, RecordName="my-ec2.None", RecordType="A"71 )72 mocks["boto3"].client().change_resource_record_sets.assert_called_with(73 HostedZoneId=None,74 ChangeBatch={75 "Comment": "Add my-ec2 EC2 record",76 "Changes": [77 {78 "Action": "DELETE",79 "ResourceRecordSet": {80 "Name": "my-ec2.None",81 "Type": "A",82 "TTL": 10,83 "ResourceRecords": [{"Value": "1.1.1.1"}],84 },85 }86 ],87 },...
testdnsanswer.py
Source:testdnsanswer.py
...7from r53utils import get_client, test_dns_answer8if __name__ == '__main__':9 zoneid, qname, qtype = sys.argv[1:]10 client = get_client()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!