def setUp(self): """Setting test values.""" self.definition = "eb7_sls_helper/test/complete.yml" self.service = "eb7-sls-helper" self.runtime = "python2.7" self.provider_name = "aws" self.Lambda = Lambda(self.definition)
def setUp(self): """Sets up base parameters.""" self.definition = "eb7_sls_helper/test/complete.yml" self.stage = "dev" self.profile = "default" self.region = "eu-central-1" self.Lambda = Lambda(self.definition) self.Deployment = self.Lambda.Deployment(self.stage, self.region, self.profile) self.FailingDeployment = self.Lambda.Deployment( self.stage, "foo", "notexisting")
def deploy( sls: List[str], inputs: Dict[str, Union[str, int, bool, List[str]]]) -> List[Deployment_Dict]: """Deploys the sls definitions.""" log.info("Setting up sls profile") aws_key = inputs["aws_key"] aws_secret = inputs["aws_secret"] assert isinstance(aws_key, Secret) assert isinstance(aws_secret, Secret) set_profile(aws_key, aws_secret) deployments: List[Deployment_Dict] = [] for service in sls: current_fn = Lambda(service) assert isinstance(inputs["stage"], str) assert isinstance(inputs["profile"], str) current_deployment = current_fn.Deployment(inputs["stage"], "eu-central-1", inputs["profile"]) log.info(f"Deploying service.") current_deployment.deploy() log.info(f"Deployment successful.") deployment: Deployment_Dict = {} assert current_deployment.stage assert current_fn.service deployment["endpoints"] = defaultdict(list) deployment["stage"] = current_deployment.stage deployment["service"] = current_fn.service # === Temporarily disabled because manifest files are missing === # # stage = current_deployment.stage if current_deployment.stage else "" # info = current_deployment.get_info() # assert info # urls = info[stage]["urls"]["byMethod"] # assert isinstance(deployment["endpoints"], dict) # for key, value in urls.items(): # deployment["endpoints"][key] += value # log.info(f"Endpoint deployed: {key} {value}") deployments.append(deployment) return deployments
class LambdaTestCase(unittest.TestCase): """Testing the Lambda objecti.""" def setUp(self): """Setting test values.""" self.definition = "eb7_sls_helper/test/complete.yml" self.service = "eb7-sls-helper" self.runtime = "python2.7" self.provider_name = "aws" self.Lambda = Lambda(self.definition) def test_custom_definition(self): """Asserts that the init of the object works.""" self.assertEqual(self.Lambda.definition, self.definition) def test_definition_after_init(self): """Asserts that definition of params with empty init works.""" a = Lambda() a.definition = self.definition self.assertEqual(vars(self.Lambda), vars(a)) def test_redefinition(self): """Assertst that the Lambda object can only be defined once.""" with self.assertRaises(RuntimeError): self.Lambda.definition = "new.yml" def test_service(self): """Asserts that the service property works as intended.""" self.assertEqual(self.Lambda.service, self.service) def test_provider_name(self): """Asserts that the provider_name property works as intended.""" self.assertEqual(self.Lambda.provider_name, self.provider_name) def test_runtime(self): """Asserts that the runtime property works as intended.""" self.assertEqual(self.Lambda.runtime, self.runtime) def test_invalid_definition(self): """Asserts that incomplete sls definitions raise errors.""" with self.assertRaises(ValueError): Lambda("eb7_sls_helper/test/incomplete.yml") def test_str(self): """Asserts that string representation works.""" output = str( { "definition": "eb7_sls_helper/test/complete.yml", "service": "eb7-sls-helper", "provider_name": "aws", "runtime": "python2.7", } ) self.assertEqual(output, self.Lambda.__str__()) # noqa: WPS609
def test(sls: List[str], inputs: Dict[str, Union[str, int, bool, List[str]]]) -> None: """Tests the sls definitions.""" log.info("Setting up sls profile") logging.getLogger("boto3").setLevel(logging.CRITICAL) logging.getLogger("botocore").setLevel(logging.CRITICAL) logging.getLogger("apigateway").setLevel(logging.CRITICAL) aws_key = inputs["aws_key"] aws_secret = inputs["aws_secret"] assert isinstance(aws_key, Secret) assert isinstance(aws_secret, Secret) set_profile(aws_key, aws_secret) test_failed = False message = "" for service in sls: current_fn = Lambda(service) assert isinstance(inputs["stage"], str) assert isinstance(inputs["profile"], str) current_deployment = current_fn.Deployment(inputs["stage"], "eu-central-1", inputs["profile"]) log.info(f"Testing service.") assert isinstance(inputs["postman_api_key"], Secret) cmd, output, error, return_code = current_deployment.test( inputs["postman_api_key"].value) log.info(output) message += output if return_code > 0: test_failed = True log.warning(cmd) log.warning(output) log.warning(error) set_output(f"formatted", message) print(message) if test_failed: sys.exit(1)
def test_fallback_incomplete_definition(self): """Asserts fallback to default values.""" a = (Lambda( "eb7_sls_helper/test/defaults.yml").Deployment().from_definition()) self.assertEqual(a.stage, self.stage) self.assertEqual(a.region, self.region)
class DeploymentTestCase(unittest.TestCase): """Testing Deployment class.""" def setUp(self): """Sets up base parameters.""" self.definition = "eb7_sls_helper/test/complete.yml" self.stage = "dev" self.profile = "default" self.region = "eu-central-1" self.Lambda = Lambda(self.definition) self.Deployment = self.Lambda.Deployment(self.stage, self.region, self.profile) self.FailingDeployment = self.Lambda.Deployment( self.stage, "foo", "notexisting") def test_custom_definition(self): """Asserts that definition is initialized.""" self.assertEqual(self.Deployment.definition, self.definition) def test_from_definition(self): """Asserts that parsing values from sls file works.""" a = self.Lambda.Deployment().from_definition() deployment_dict = vars(self.Deployment) deployment_dict.pop("_newman_environment") parsed_deployment_dict = vars(a) parsed_deployment_dict.pop("_newman_environment") self.assertEqual(deployment_dict, parsed_deployment_dict) def test_redefinition(self): """Asserts that deployments cannot be redefined.""" with self.assertRaises(RuntimeError): self.Deployment.from_definition() def test_fallback_incomplete_definition(self): """Asserts fallback to default values.""" a = (Lambda( "eb7_sls_helper/test/defaults.yml").Deployment().from_definition()) self.assertEqual(a.stage, self.stage) self.assertEqual(a.region, self.region) def test_stage(self): """Asserts stage property is set correctly.""" self.assertEqual(self.Deployment.stage, self.stage) def test_profile(self): """Asserts profile property is set correctly.""" self.assertEqual(self.Deployment.profile, self.profile) def test_region(self): """Asiserts region property is set correctly.""" self.assertEqual(self.Deployment.region, self.region) def test_str(self): """Asiserts human-readible output is correct.""" output = str({ "definition": self.definition, "stage": self.stage, "region": self.region, "profile": self.profile, }) self.assertEqual( output, self.Deployment.__str__() # noqa: WPS609 # Ok for testing ) def test_info(self): """Asserts that info is intialized empty.""" self.assertEqual(self.Deployment.get_info(), None) @patch("subprocess.Popen", side_effect=mock_subprocess) def test_deploy(self, mock): """Asserts that deployment of service works.""" self.Deployment.deploy() self.assertTrue(mock.called_once()) @patch("subprocess.Popen", side_effect=mock_subprocess) def test_deploy_failing(self, mock): """Asserts that misspecified service raises RuntimeError.""" with self.assertRaises(RuntimeError): self.FailingDeployment.deploy() @patch("subprocess.Popen", side_effect=mock_subprocess) def test_remove(self, mock): """Asserts that removing service works.""" self.Deployment.deploy() self.Deployment.remove() self.assertEqual(None, self.Deployment.get_info())
def test_invalid_definition(self): """Asserts that incomplete sls definitions raise errors.""" with self.assertRaises(ValueError): Lambda("eb7_sls_helper/test/incomplete.yml")
def test_definition_after_init(self): """Asserts that definition of params with empty init works.""" a = Lambda() a.definition = self.definition self.assertEqual(vars(self.Lambda), vars(a))