def test_present_with_failure(self): self.conn.list_functions.side_effect = [{ 'Functions': [] }, { 'Functions': [function_ret] }] self.conn.create_function.side_effect = ClientError( error_content, 'create_function') with patch.dict( self.funcs, {'boto_iam.get_account_id': MagicMock(return_value='1234')}): with TempZipFile() as zipfile: with patch('hashlib.sha256') as sha256: with patch('os.path.getsize', return_value=199): sha = sha256() digest = sha.digest() encoded = sha.encode() encoded.strip.return_value = function_ret['CodeSha256'] result = self.salt_states[ 'boto_lambda.function_present']( 'function present', FunctionName=function_ret['FunctionName'], Runtime=function_ret['Runtime'], Role=function_ret['Role'], Handler=function_ret['Handler'], ZipFile=zipfile, Description=function_ret['Description'], Timeout=function_ret['Timeout']) self.assertFalse(result['result']) self.assertTrue('An error occurred' in result['comment'])
def test_present_when_function_does_not_exist(self): ''' Tests present on a function that does not exist. ''' self.conn.list_functions.side_effect = [{ 'Functions': [] }, { 'Functions': [function_ret] }] self.conn.create_function.return_value = function_ret with patch.dict( self.funcs, {'boto_iam.get_account_id': MagicMock(return_value='1234')}): with TempZipFile() as zipfile: result = self.salt_states['boto_lambda.function_present']( 'function present', FunctionName=function_ret['FunctionName'], Runtime=function_ret['Runtime'], Role=function_ret['Role'], Handler=function_ret['Handler'], ZipFile=zipfile) self.assertTrue(result['result']) self.assertEqual(result['changes']['new']['function']['FunctionName'], function_ret['FunctionName'])
def test_present_when_function_exists(self): self.conn.list_functions.return_value = {'Functions': [function_ret]} self.conn.update_function_code.return_value = function_ret with patch.dict( self.funcs, {'boto_iam.get_account_id': MagicMock(return_value='1234')}): with TempZipFile() as zipfile: with patch('hashlib.sha256') as sha256: with patch('os.path.getsize', return_value=199): sha = sha256() digest = sha.digest() encoded = sha.encode() encoded.strip.return_value = function_ret['CodeSha256'] result = self.salt_states[ 'boto_lambda.function_present']( 'function present', FunctionName=function_ret['FunctionName'], Runtime=function_ret['Runtime'], Role=function_ret['Role'], Handler=function_ret['Handler'], ZipFile=zipfile, Description=function_ret['Description'], Timeout=function_ret['Timeout']) self.assertTrue(result['result']) self.assertEqual(result['changes'], {})
def test_present_with_failure(self): self.conn.list_functions.side_effect = [ { "Functions": [] }, { "Functions": [function_ret] }, ] self.conn.create_function.side_effect = ClientError( error_content, "create_function") with patch.dict( self.funcs, {"boto_iam.get_account_id": MagicMock(return_value="1234")}): with TempZipFile() as zipfile: with patch("hashlib.sha256") as sha256: with patch("os.path.getsize", return_value=199): sha = sha256() digest = sha.digest() encoded = sha.encode() encoded.strip.return_value = function_ret["CodeSha256"] result = self.salt_states[ "boto_lambda.function_present"]( "function present", FunctionName=function_ret["FunctionName"], Runtime=function_ret["Runtime"], Role=function_ret["Role"], Handler=function_ret["Handler"], ZipFile=zipfile, Description=function_ret["Description"], Timeout=function_ret["Timeout"], ) self.assertFalse(result["result"]) self.assertTrue("An error occurred" in result["comment"])
def test_present_when_function_exists(self): self.conn.list_functions.return_value = {"Functions": [function_ret]} self.conn.update_function_code.return_value = function_ret with patch.dict( self.funcs, {"boto_iam.get_account_id": MagicMock(return_value="1234")}): with TempZipFile() as zipfile: with patch("hashlib.sha256") as sha256: with patch("os.path.getsize", return_value=199): sha = sha256() digest = sha.digest() encoded = sha.encode() encoded.strip.return_value = function_ret["CodeSha256"] result = self.salt_states[ "boto_lambda.function_present"]( "function present", FunctionName=function_ret["FunctionName"], Runtime=function_ret["Runtime"], Role=function_ret["Role"], Handler=function_ret["Handler"], ZipFile=zipfile, Description=function_ret["Description"], Timeout=function_ret["Timeout"], ) self.assertTrue(result["result"]) self.assertEqual(result["changes"], {})
def test_present_when_function_does_not_exist(self): """ Tests present on a function that does not exist. """ self.conn.list_functions.side_effect = [ { "Functions": [] }, { "Functions": [function_ret] }, ] self.conn.create_function.return_value = function_ret with patch.dict( self.funcs, {"boto_iam.get_account_id": MagicMock(return_value="1234")}): with TempZipFile() as zipfile: result = self.salt_states["boto_lambda.function_present"]( "function present", FunctionName=function_ret["FunctionName"], Runtime=function_ret["Runtime"], Role=function_ret["Role"], Handler=function_ret["Handler"], ZipFile=zipfile, ) self.assertTrue(result["result"]) self.assertEqual( result["changes"]["new"]["function"]["FunctionName"], function_ret["FunctionName"], )
def test_present_when_function_exists_and_permissions(self): self.conn.list_functions.return_value = {"Functions": [function_ret]} self.conn.update_function_code.return_value = function_ret self.conn.get_policy.return_value = { "Policy": salt.utils.json.dumps( { "Version": "2012-10-17", "Statement": [ { "Condition": { "ArnLike": { "AWS:SourceArn": "arn:aws:events:us-east-1:9999999999:rule/fooo" } }, "Action": "lambda:InvokeFunction", "Resource": "arn:aws:lambda:us-east-1:999999999999:function:testfunction", "Effect": "Allow", "Principal": {"Service": "events.amazonaws.com"}, "Sid": "AWSEvents_foo-bar999999999999", } ], "Id": "default", } ) } with patch.dict(self.funcs, {"boto_iam.get_account_id": MagicMock(return_value="1234")}): with TempZipFile() as zipfile: with patch("hashlib.sha256") as sha256: with patch("os.path.getsize", return_value=199): sha = sha256() digest = sha.digest() encoded = sha.encode() encoded.strip.return_value = function_ret["CodeSha256"] result = self.salt_states["boto_lambda.function_present"]( "function present", FunctionName=function_ret["FunctionName"], Runtime=function_ret["Runtime"], Role=function_ret["Role"], Handler=function_ret["Handler"], ZipFile=zipfile, Description=function_ret["Description"], Timeout=function_ret["Timeout"], ) assert result["result"] assert result["changes"] == { "old": { "Permissions": { "AWSEvents_foo-bar999999999999": { "Action": "lambda:InvokeFunction", "Principal": "events.amazonaws.com", "SourceArn": "arn:aws:events:us-east-1:9999999999:rule/fooo", } } }, "new": {"Permissions": {"AWSEvents_foo-bar999999999999": {}}}, }
def test_present_with_failure_func(global_config): global_config.conn_parameters["key"] = "".join( random.choice(string.ascii_lowercase + string.digits) for _ in range(50)) patcher = patch("boto3.session.Session") mock_session = patcher.start() session_instance = mock_session.return_value conn = MagicMock() session_instance.client.return_value = conn conn.list_functions.side_effect = [ { "Functions": [] }, { "Functions": [global_config.function_ret] }, ] conn.create_function.side_effect = botocore.exceptions.ClientError( global_config.error_content, "create_function") with patch.dict( boto_lambda.__salt__, {"boto_iam.get_account_id": MagicMock(return_value="1234")}, ): with TempZipFile() as zipfile: with patch("hashlib.sha256") as sha256: with patch("os.path.getsize", return_value=199): sha = sha256() digest = sha.digest() encoded = sha.encode() encoded.strip.return_value = global_config.function_ret[ "CodeSha256"] result = boto_lambda.__states__[ "boto_lambda.function_present"]( "function present", FunctionName=global_config. function_ret["FunctionName"], Runtime=global_config.function_ret["Runtime"], Role=global_config.function_ret["Role"], Handler=global_config.function_ret["Handler"], ZipFile=zipfile, Description=global_config. function_ret["Description"], Timeout=global_config.function_ret["Timeout"], ) assert not result["result"] assert "An error occurred" in result["comment"]
def test_present_when_function_exists_and_permissions(self): self.conn.list_functions.return_value = {'Functions': [function_ret]} self.conn.update_function_code.return_value = function_ret self.conn.get_policy.return_value = { "Policy": salt.utils.json.dumps( {"Version": "2012-10-17", "Statement": [ {"Condition": {"ArnLike": { "AWS:SourceArn": "arn:aws:events:us-east-1:9999999999:rule/fooo"}}, "Action": "lambda:InvokeFunction", "Resource": "arn:aws:lambda:us-east-1:999999999999:function:testfunction", "Effect": "Allow", "Principal": {"Service": "events.amazonaws.com"}, "Sid": "AWSEvents_foo-bar999999999999"}], "Id": "default"}) } with patch.dict(self.funcs, {'boto_iam.get_account_id': MagicMock(return_value='1234')}): with TempZipFile() as zipfile: with patch('hashlib.sha256') as sha256: with patch('os.path.getsize', return_value=199): sha = sha256() digest = sha.digest() encoded = sha.encode() encoded.strip.return_value = function_ret['CodeSha256'] result = self.salt_states['boto_lambda.function_present']( 'function present', FunctionName=function_ret['FunctionName'], Runtime=function_ret['Runtime'], Role=function_ret['Role'], Handler=function_ret['Handler'], ZipFile=zipfile, Description=function_ret['Description'], Timeout=function_ret['Timeout']) self.assertTrue(result['result']) self.assertEqual(result['changes'], { 'old': { 'Permissions': { 'AWSEvents_foo-bar999999999999': {'Action': 'lambda:InvokeFunction', 'Principal': 'events.amazonaws.com', 'SourceArn': 'arn:aws:events:us-east-1:9999999999:rule/fooo'}}}, 'new': { 'Permissions': { 'AWSEvents_foo-bar999999999999': {}}}})
def test_present_when_function_exists_func(global_config): global_config.conn_parameters["key"] = "".join( random.choice(string.ascii_lowercase + string.digits) for _ in range(50)) patcher = patch("boto3.session.Session") mock_session = patcher.start() session_instance = mock_session.return_value conn = MagicMock() session_instance.client.return_value = conn conn.list_functions.return_value = { "Functions": [global_config.function_ret] } conn.update_function_code.return_value = global_config.function_ret with patch.dict( boto_lambda.__salt__, {"boto_iam.get_account_id": MagicMock(return_value="1234")}, ): with TempZipFile() as zipfile: with patch("hashlib.sha256") as sha256: with patch("os.path.getsize", return_value=199): sha = sha256() digest = sha.digest() encoded = sha.encode() encoded.strip.return_value = global_config.function_ret[ "CodeSha256"] result = boto_lambda.__states__[ "boto_lambda.function_present"]( "function present", FunctionName=global_config. function_ret["FunctionName"], Runtime=global_config.function_ret["Runtime"], Role=global_config.function_ret["Role"], Handler=global_config.function_ret["Handler"], ZipFile=zipfile, Description=global_config. function_ret["Description"], Timeout=global_config.function_ret["Timeout"], ) assert result["result"] assert result["changes"] == {}
def test_present_when_function_does_not_exist_func(global_config): """ Tests present on a function that does not exist. """ global_config.conn_parameters["key"] = "".join( random.choice(string.ascii_lowercase + string.digits) for _ in range(50)) patcher = patch("boto3.session.Session") mock_session = patcher.start() session_instance = mock_session.return_value conn = MagicMock() session_instance.client.return_value = conn conn.list_functions.side_effect = [ { "Functions": [] }, { "Functions": [global_config.function_ret] }, ] conn.create_function.return_value = global_config.function_ret with patch.dict( boto_lambda.__salt__, {"boto_iam.get_account_id": MagicMock(return_value="1234")}, ): with TempZipFile() as zipfile: result = boto_lambda.__states__["boto_lambda.function_present"]( "function present", FunctionName=global_config.function_ret["FunctionName"], Runtime=global_config.function_ret["Runtime"], Role=global_config.function_ret["Role"], Handler=global_config.function_ret["Handler"], ZipFile=zipfile, ) assert result["result"] assert (result["changes"]["new"]["function"]["FunctionName"] == global_config.function_ret["FunctionName"])
def test_present_when_function_exists_and_permissions_func(global_config): global_config.conn_parameters["key"] = "".join( random.choice(string.ascii_lowercase + string.digits) for _ in range(50)) patcher = patch("boto3.session.Session") mock_session = patcher.start() session_instance = mock_session.return_value conn = MagicMock() session_instance.client.return_value = conn conn.list_functions.return_value = { "Functions": [global_config.function_ret] } conn.update_function_code.return_value = global_config.function_ret conn.get_policy.return_value = { "Policy": salt.utils.json.dumps({ "Version": "2012-10-17", "Statement": [{ "Condition": { "ArnLike": { "AWS:SourceArn": ("arn:aws:events:us-east-1:9999999999:rule/fooo") } }, "Action": "lambda:InvokeFunction", "Resource": "arn:aws:lambda:us-east-1:999999999999:function:testfunction", "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Sid": "AWSEvents_foo-bar999999999999", }], "Id": "default", }) } with patch.dict( boto_lambda.__salt__, {"boto_iam.get_account_id": MagicMock(return_value="1234")}, ): with TempZipFile() as zipfile: with patch("hashlib.sha256") as sha256: with patch("os.path.getsize", return_value=199): sha = sha256() digest = sha.digest() encoded = sha.encode() encoded.strip.return_value = global_config.function_ret[ "CodeSha256"] result = boto_lambda.__states__[ "boto_lambda.function_present"]( "function present", FunctionName=global_config. function_ret["FunctionName"], Runtime=global_config.function_ret["Runtime"], Role=global_config.function_ret["Role"], Handler=global_config.function_ret["Handler"], ZipFile=zipfile, Description=global_config. function_ret["Description"], Timeout=global_config.function_ret["Timeout"], ) assert result["result"] assert result["changes"] == { "old": { "Permissions": { "AWSEvents_foo-bar999999999999": { "Action": "lambda:InvokeFunction", "Principal": "events.amazonaws.com", "SourceArn": "arn:aws:events:us-east-1:9999999999:rule/fooo", } } }, "new": { "Permissions": { "AWSEvents_foo-bar999999999999": {} } }, }