How to use temporary_env method in localstack

Best Python code snippet using localstack_python

test_config.py

Source:test_config.py Github

copy

Full Screen

2from contextlib import contextmanager3from typing import Any, Dict4from localstack import config5@contextmanager6def temporary_env(env: Dict[str, Any]):7 old = os.environ.copy()8 try:9 os.environ.update(env)10 yield os.environ11 finally:12 os.environ.clear()13 os.environ.update(old)14class TestProviderConfig:15 def test_provider_default_value(self):16 default_value = "default_value"17 override_value = "override_value"18 provider_config = config.ServiceProviderConfig(default_value=default_value)19 assert provider_config.get_provider("ec2") == default_value20 provider_config.set_provider("ec2", override_value)21 assert provider_config.get_provider("ec2") == override_value22 def test_provider_set_if_not_exists(self):23 default_value = "default_value"24 override_value = "override_value"25 provider_config = config.ServiceProviderConfig(default_value=default_value)26 provider_config.set_provider("ec2", default_value)27 provider_config.set_provider_if_not_exists("ec2", override_value)28 assert provider_config.get_provider("ec2") == default_value29 def test_provider_config_overrides(self, monkeypatch):30 default_value = "default_value"31 override_value = "override_value"32 provider_config = config.ServiceProviderConfig(default_value=default_value)33 monkeypatch.setenv("PROVIDER_OVERRIDE_EC2", override_value)34 provider_config.load_from_environment()35 assert provider_config.get_provider("ec2") == override_value36 assert provider_config.get_provider("sqs") == default_value37 monkeypatch.setenv("PROVIDER_OVERRIDE_SQS", override_value)38 provider_config.load_from_environment()39 assert provider_config.get_provider("sqs") == override_value40 def test_bulk_set_if_not_exists(self):41 default_value = "default_value"42 custom_value = "custom_value"43 override_value = "override_value"44 override_services = ["sqs", "sns", "lambda", "ec2"]45 provider_config = config.ServiceProviderConfig(default_value=default_value)46 provider_config.set_provider("ec2", default_value)47 provider_config.set_provider("lambda", custom_value)48 provider_config.bulk_set_provider_if_not_exists(override_services, override_value)49 assert provider_config.get_provider("sqs") == override_value50 assert provider_config.get_provider("sns") == override_value51 assert provider_config.get_provider("lambda") == custom_value52 assert provider_config.get_provider("ec2") == default_value53 assert provider_config.get_provider("kinesis") == default_value54class TestParseServicePorts:55 def test_returns_default_service_ports(self):56 result = config.parse_service_ports()57 assert result == config.DEFAULT_SERVICE_PORTS58 def test_with_service_subset(self):59 with temporary_env({"SERVICES": "s3,sqs"}):60 result = config.parse_service_ports()61 assert len(result) == 262 assert "s3" in result63 assert "sqs" in result64 assert result["s3"] == 456665 assert result["sqs"] == 456666 def test_custom_service_default_port(self):67 with temporary_env({"SERVICES": "foobar"}):68 result = config.parse_service_ports()69 assert len(result) == 170 assert "foobar" not in config.DEFAULT_SERVICE_PORTS71 assert "foobar" in result72 # foobar is not a default service so it is assigned 073 assert result["foobar"] == 074 def test_custom_port_mapping(self):75 with temporary_env({"SERVICES": "foobar", "FOOBAR_PORT": "1234"}):76 result = config.parse_service_ports()77 assert len(result) == 178 assert "foobar" not in config.DEFAULT_SERVICE_PORTS79 assert "foobar" in result80 assert result["foobar"] == 123481 def test_custom_illegal_port_mapping(self):82 with temporary_env({"SERVICES": "foobar", "FOOBAR_PORT": "asdf"}):83 result = config.parse_service_ports()84 assert len(result) == 185 assert "foobar" not in config.DEFAULT_SERVICE_PORTS86 assert "foobar" in result87 # FOOBAR_PORT cannot be parsed88 assert result["foobar"] == 089 def test_custom_port_mapping_in_services_env(self):90 with temporary_env({"SERVICES": "foobar:1235"}):91 result = config.parse_service_ports()92 assert len(result) == 193 assert "foobar" not in config.DEFAULT_SERVICE_PORTS94 assert "foobar" in result95 # FOOBAR_PORT cannot be parsed...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful