def test_with_no_additional_variables_extensions_preview( self, PreviewEnabledMock): """ Test assuming user has *not* passed any environment variables. Only AWS variables should be setup """ PreviewEnabledMock.return_value = True expected = { "AWS_SAM_LOCAL": "true", "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024", "AWS_LAMBDA_FUNCTION_TIMEOUT": "123", "AWS_LAMBDA_FUNCTION_HANDLER": "handler", "AWS_REGION": "some region", "AWS_DEFAULT_REGION": "some region", "AWS_ACCESS_KEY_ID": "some key", "AWS_SECRET_ACCESS_KEY": "some other secret", "AWS_SESSION_TOKEN": "some other token", "AWS_LAMBDA_FUNCTION_NAME": "name", "AWS_LAMBDA_FUNCTION_VERSION": "$LATEST", "AWS_LAMBDA_LOG_GROUP_NAME": f"aws/lambda/{self.name}", "AWS_LAMBDA_LOG_STREAM_NAME": "$LATEST", "AWS_ACCOUNT_ID": "123456789012", } environ = EnvironmentVariables(self.name, self.memory, self.timeout, self.handler, aws_creds=self.aws_creds) result = environ.resolve() # With no additional environment variables, resolve() should just return all AWS variables self.assertEqual(result, expected)
def test_must_work_with_overridden_aws_creds(self): expected = { "AWS_SAM_LOCAL": "true", "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024", "AWS_LAMBDA_FUNCTION_TIMEOUT": "123", "AWS_LAMBDA_FUNCTION_HANDLER": "handler", "AWS_LAMBDA_FUNCTION_NAME": self.name, "AWS_LAMBDA_FUNCTION_VERSION": "$LATEST", "AWS_REGION": "some region", "AWS_DEFAULT_REGION": "some region", "AWS_ACCESS_KEY_ID": "some key", "AWS_SECRET_ACCESS_KEY": "some other secret", "AWS_LAMBDA_LOG_GROUP_NAME": f"aws/lambda/{self.name}", "AWS_LAMBDA_LOG_STREAM_NAME": "$LATEST", "AWS_ACCOUNT_ID": "123456789012", "AWS_SESSION_TOKEN": "some other token", } environ = EnvironmentVariables(self.name, self.memory, self.timeout, self.handler, aws_creds=self.aws_creds) self.assertEqual(expected, environ._get_aws_variables())
def test_with_overrides_value(self): """ Given values for the variables from user specified overrides """ expected = { "AWS_SAM_LOCAL": "true", "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024", "AWS_LAMBDA_FUNCTION_TIMEOUT": "123", "AWS_LAMBDA_FUNCTION_HANDLER": "handler", "AWS_REGION": "us-east-1", "AWS_ACCESS_KEY_ID": "defaultkey", "AWS_SECRET_ACCESS_KEY": "defaultsecret", # This value is coming from user passed environment variable "AWS_DEFAULT_REGION": "user-specified-region", "variable2": "mystring", # Value coming from the overrides "variable1": "variable1 value from overrides", "list_var": "list value coming from overrides", "dict_var": "", "none_var": "", "true_var": "true", "false_var": "false", } environ = EnvironmentVariables( self.memory, self.timeout, self.handler, variables=self.variables, shell_env_values=self.shell_env, override_values=self.override, ) self.assertEqual(environ.resolve(), expected)
def test_with_only_default_values_for_variables(self): """ Given only environment variable values, without any shell env values or overridden values """ expected = { "AWS_SAM_LOCAL": "true", "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024", "AWS_LAMBDA_FUNCTION_TIMEOUT": "123", "AWS_LAMBDA_FUNCTION_HANDLER": "handler", "AWS_REGION": "us-east-1", "AWS_ACCESS_KEY_ID": "defaultkey", "AWS_SECRET_ACCESS_KEY": "defaultsecret", # This value is coming from user passed environment variable "AWS_DEFAULT_REGION": "user-specified-region", "variable1": "1", "variable2": "mystring", "list_var": "", "dict_var": "", "none_var": "", "true_var": "true", "false_var": "false" } environ = EnvironmentVariables(self.memory, self.timeout, self.handler, variables=self.variables) self.assertEquals(environ.resolve(), expected)
def test_must_work_with_partial_aws_creds(self): creds = { "region": "some other region", "sessiontoken": "my awesome token" } expected = { "AWS_SAM_LOCAL": "true", "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024", "AWS_LAMBDA_FUNCTION_TIMEOUT": "123", "AWS_LAMBDA_FUNCTION_HANDLER": "handler", # Values from the input creds "AWS_REGION": "some other region", "AWS_DEFAULT_REGION": "some other region", "AWS_SESSION_TOKEN": "my awesome token", # These variables still get the default value "AWS_ACCESS_KEY_ID": "defaultkey", "AWS_SECRET_ACCESS_KEY": "defaultsecret", } environ = EnvironmentVariables(self.memory, self.timeout, self.handler, aws_creds=creds) self.assertEqual(expected, environ._get_aws_variables())
def test_with_no_additional_variables(self): """ Test assuming user has *not* passed any environment variables. Only AWS variables should be setup """ expected = { "AWS_SAM_LOCAL": "true", "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024", "AWS_LAMBDA_FUNCTION_TIMEOUT": "123", "AWS_LAMBDA_FUNCTION_HANDLER": "handler", "AWS_REGION": "some region", "AWS_DEFAULT_REGION": "some region", "AWS_ACCESS_KEY_ID": "some key", "AWS_SECRET_ACCESS_KEY": "some other secret", "AWS_SESSION_TOKEN": "some other token", } environ = EnvironmentVariables(self.memory, self.timeout, self.handler, aws_creds=self.aws_creds) result = environ.resolve() # With no additional environment variables, resolve() should just return all AWS variables self.assertEqual(result, expected)
def test_must_initialize_with_optional_values(self): name = "name" memory = 123 timeout = 10 handler = "handler" variables = {"a": "b"} shell_values = {"c": "d"} overrides = {"e": "f"} aws_creds = {"g": "h"} environ = EnvironmentVariables( name, memory, timeout, handler, variables=variables, shell_env_values=shell_values, override_values=overrides, aws_creds=aws_creds, ) self.assertEqual(environ.variables, {"a": "b"}) self.assertEqual(environ.shell_env_values, {"c": "d"}) self.assertEqual(environ.override_values, {"e": "f"}) self.assertEqual(environ.aws_creds, {"g": "h"})
def test_must_work_without_any_aws_creds_extensions_preview( self, PreviewEnabledMock): PreviewEnabledMock.return_value = True expected = { "AWS_SAM_LOCAL": "true", "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024", "AWS_LAMBDA_FUNCTION_TIMEOUT": "123", "AWS_LAMBDA_FUNCTION_HANDLER": "handler", # Default values assigned to these variables "AWS_REGION": "us-east-1", "AWS_DEFAULT_REGION": "us-east-1", "AWS_ACCESS_KEY_ID": "defaultkey", "AWS_SECRET_ACCESS_KEY": "defaultsecret", "AWS_LAMBDA_FUNCTION_NAME": "name", "AWS_LAMBDA_FUNCTION_VERSION": "$LATEST", "AWS_LAMBDA_LOG_GROUP_NAME": f"aws/lambda/{self.name}", "AWS_LAMBDA_LOG_STREAM_NAME": "$LATEST", "AWS_ACCOUNT_ID": "123456789012", } environ = EnvironmentVariables(self.name, self.memory, self.timeout, self.handler) self.assertEqual(expected, environ._get_aws_variables())
def test_must_add_proper_variable(self): value = "foobar" environ = EnvironmentVariables() environ.add_lambda_event_body(value) self.assertEqual(environ.variables.get("AWS_LAMBDA_EVENT_BODY"), value)
def test_must_initialize_values_with_required_values(self): memory = 123 timeout = 10 handler = "handler" environ = EnvironmentVariables(memory, timeout, handler) self.assertEqual(environ.memory, memory) self.assertEqual(environ.timeout, timeout) self.assertEqual(environ.handler, handler) self.assertEqual(environ.variables, {}) self.assertEqual(environ.shell_env_values, {}) self.assertEqual(environ.override_values, {}) self.assertEqual(environ.aws_creds, {})
def test_must_initialize_with_empty_values(self): memory = 123 timeout = 10 handler = "handler" environ = EnvironmentVariables() environ.memory = memory environ.timeout = timeout environ.handler = handler self.assertEqual(environ.memory, memory) self.assertEqual(environ.timeout, timeout) self.assertEqual(environ.handler, handler)
def test_must_work_without_any_aws_creds(self): expected = { "AWS_SAM_LOCAL": "true", "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024", "AWS_LAMBDA_FUNCTION_TIMEOUT": "123", "AWS_LAMBDA_FUNCTION_HANDLER": "handler", # Default values assigned to these variables "AWS_REGION": "us-east-1", "AWS_DEFAULT_REGION": "us-east-1", "AWS_ACCESS_KEY_ID": "defaultkey", "AWS_SECRET_ACCESS_KEY": "defaultsecret", } environ = EnvironmentVariables(self.memory, self.timeout, self.handler) self.assertEqual(expected, environ._get_aws_variables())
def _make_env_vars(self, function): """ Returns the environment variables configuration for this function :param samcli.commands.local.lib.provider.Function function: Lambda function to generate the configuration for :return samcli.local.lambdafn.env_vars.EnvironmentVariables: Environment variable configuration for this function """ name = function.name variables = None if function.environment and isinstance( function.environment, dict) and "Variables" in function.environment: variables = function.environment["Variables"] else: LOG.debug("No environment variables found for function '%s'", name) # This could either be in standard format, or a CloudFormation parameter file format. # # Standard format is {FunctionName: {key:value}, FunctionName: {key:value}} # CloudFormation parameter file is {"Parameters": {key:value}} if "Parameters" in self.env_vars_values: LOG.debug( "Environment variables overrides data is in CloudFormation parameter file format" ) # CloudFormation parameter file format overrides = self.env_vars_values["Parameters"] else: # Standard format LOG.debug( "Environment variables overrides data is standard format") overrides = self.env_vars_values.get(name, None) shell_env = os.environ aws_creds = self.get_aws_creds() return EnvironmentVariables(function.memory, function.timeout, function.handler, variables=variables, shell_env_values=shell_env, override_values=overrides, aws_creds=aws_creds)
def test_must_work_with_overridden_aws_creds(self): expected = { "AWS_SAM_LOCAL": "true", "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024", "AWS_LAMBDA_FUNCTION_TIMEOUT": "123", "AWS_LAMBDA_FUNCTION_HANDLER": "handler", "AWS_REGION": "some region", "AWS_DEFAULT_REGION": "some region", "AWS_ACCESS_KEY_ID": "some key", "AWS_SECRET_ACCESS_KEY": "some other secret", "AWS_SESSION_TOKEN": "some other token", } environ = EnvironmentVariables(self.memory, self.timeout, self.handler, aws_creds=self.aws_creds) self.assertEqual(expected, environ._get_aws_variables())
def test_with_shell_env_value(self): """ Given values for the variables from shell environment """ expected = { "AWS_SAM_LOCAL": "true", "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024", "AWS_LAMBDA_FUNCTION_TIMEOUT": "123", "AWS_LAMBDA_FUNCTION_HANDLER": "handler", "AWS_LAMBDA_FUNCTION_NAME": self.name, "AWS_LAMBDA_FUNCTION_VERSION": "$LATEST", "AWS_LAMBDA_LOG_GROUP_NAME": f"aws/lambda/{self.name}", "AWS_LAMBDA_LOG_STREAM_NAME": "$LATEST", "AWS_ACCOUNT_ID": "123456789012", "AWS_REGION": "us-east-1", "AWS_ACCESS_KEY_ID": "defaultkey", "AWS_SECRET_ACCESS_KEY": "defaultsecret", # This value is coming from user passed environment variable "AWS_DEFAULT_REGION": "user-specified-region", # Value coming from the shell "variable1": "variable1 value from shell_env", "variable2": "mystring", "list_var": "", "dict_var": "", "none_var": "", "true_var": "true", "false_var": "false", } environ = EnvironmentVariables( self.name, self.memory, self.timeout, self.handler, variables=self.variables, shell_env_values=self.shell_env, ) self.assertEqual(environ.resolve(), expected)
def test_with_only_default_values_for_variables_extensions_preview( self, PreviewEnabledMock): """ Given only environment variable values, without any shell env values or overridden values """ PreviewEnabledMock.return_value = True expected = { "AWS_SAM_LOCAL": "true", "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024", "AWS_LAMBDA_FUNCTION_TIMEOUT": "123", "AWS_LAMBDA_FUNCTION_HANDLER": "handler", "AWS_REGION": "us-east-1", "AWS_ACCESS_KEY_ID": "defaultkey", "AWS_SECRET_ACCESS_KEY": "defaultsecret", "AWS_LAMBDA_FUNCTION_NAME": "name", "AWS_LAMBDA_FUNCTION_VERSION": "$LATEST", "AWS_LAMBDA_LOG_GROUP_NAME": f"aws/lambda/{self.name}", "AWS_LAMBDA_LOG_STREAM_NAME": "$LATEST", "AWS_ACCOUNT_ID": "123456789012", # This value is coming from user passed environment variable "AWS_DEFAULT_REGION": "user-specified-region", "variable1": "1", "variable2": "mystring", "list_var": "", "dict_var": "", "none_var": "", "true_var": "true", "false_var": "false", } environ = EnvironmentVariables(self.name, self.memory, self.timeout, self.handler, variables=self.variables) self.assertEqual(environ.resolve(), expected)
def _make_env_vars(self, function): """Returns the environment variables configuration for this function Parameters ---------- function : samcli.commands.local.lib.provider.Function Lambda function to generate the configuration for Returns ------- samcli.local.lambdafn.env_vars.EnvironmentVariables Environment variable configuration for this function Raises ------ samcli.commands.local.lib.exceptions.OverridesNotWellDefinedError If the environment dict is in the wrong format to process environment vars """ name = function.name variables = None if function.environment and isinstance(function.environment, dict) and "Variables" in function.environment: variables = function.environment["Variables"] else: LOG.debug("No environment variables found for function '%s'", name) # This could either be in standard format, or a CloudFormation parameter file format. # # Standard format is {FunctionName: {key:value}, FunctionName: {key:value}} # CloudFormation parameter file is {"Parameters": {key:value}} for env_var_value in self.env_vars_values.values(): if not isinstance(env_var_value, dict): reason = """ Environment variables must be in either CloudFormation parameter file format or in {FunctionName: {key:value}} JSON pairs """ LOG.debug(reason) raise OverridesNotWellDefinedError(reason) if "Parameters" in self.env_vars_values: LOG.debug("Environment variables overrides data is in CloudFormation parameter file format") # CloudFormation parameter file format overrides = self.env_vars_values["Parameters"] else: # Standard format LOG.debug("Environment variables overrides data is standard format") overrides = self.env_vars_values.get(name, None) shell_env = os.environ aws_creds = self.get_aws_creds() return EnvironmentVariables(function.memory, function.timeout, function.handler, variables=variables, shell_env_values=shell_env, override_values=overrides, aws_creds=aws_creds)
def setUp(self): self.environ = EnvironmentVariables(1024, 10, "handler")