def test_string_render(self):
     deploy_ctx = DeployContext.create_deploy_context(application="foo", role="bar", environment="unit-test",
                                                      defaults=self.default_config)
     self._validate_deploy_ctx(deploy_ctx)
     deploy_ctx = DeployContext.create_deploy_context(application="foo-bar", role="baz", environment="unit-test",
                                                      defaults=self.default_config)
     self.assertEqual(deploy_ctx.vpcapp, "foo", "Failed to generate generate_short_app_name")
Beispiel #2
0
def cli(ctx, artifact_directory, application, role, environment, configuration_defaults, verbose):
    # type: (object, str, str, str, str, str, bool) -> None
    """
    CLI for managing the infrastructure for deploying micro-services in AWS.
    """
    print_utility.configure(verbose)
    loaded_defaults = None
    # if a defaults.json exists in the directory and it is not overriden with an explicit parameter - use it!
    if not configuration_defaults and os.path.exists('defaults.json'):
        configuration_defaults = 'defaults.json'
    if configuration_defaults or os.path.exists('defaults.json'):
        print_utility.info("Loading default settings from path: {}".format(configuration_defaults))
        with open(configuration_defaults, 'r') as fp:
            loaded_defaults = json.load(fp)
    if artifact_directory:
        if application or role:
            raise click.UsageError("When specifying --artifact-directory do not provide --application or --role")
        ctx.obj = DeployContext.create_deploy_context_artifact(artifact_directory=artifact_directory,
                                                               environment=environment,
                                                               defaults=loaded_defaults)
    else:
        ctx.obj = DeployContext.create_deploy_context(application=application,
                                                      role=role,
                                                      environment=environment,
                                                      defaults=loaded_defaults)
 def test_s3_config_url_path(self):
     east_deploy_ctx = DeployContext.create_deploy_context(application="foo",
                                                           role="bar-{}".format(self.run_random_word),
                                                           environment="unit-test",
                                                           defaults=self.east_config)
     self.assertTrue("s3." in east_deploy_ctx.config_templates_url)
     test_deploy_ctx = DeployContext.create_deploy_context(application="foo",
                                                           role="bar-{}".format(self.run_random_word),
                                                           environment="unit-test",
                                                           defaults=self.default_config)
     self.assertTrue("s3-us-west-1." in test_deploy_ctx.config_templates_url)
Beispiel #4
0
 def test_helper_funcs(self):
     ctx = DeployContext.create_deploy_context(application="dev-{}".format(
         self.run_random_word),
                                               role="cluster",
                                               environment="unit-test",
                                               defaults=self.default_config)
     cloudformation = CloudFormationBuddy(ctx)
     try:
         template_dir = ParentTestCase._get_resource_path(
             "parameter_load_tests/helper_func")
         deploy = CloudFormationDeploy(ctx.stack_name,
                                       NamedLocalTemplate(template_dir),
                                       ctx)
         deploy.do_deploy(dry_run=False)
         rp = helper_functions.calculate_rule_priority(ctx, ctx.stack_name)
         self.assertEqual(rp, "10",
                          "Failed to detect existing rule priority")
         rp = helper_functions.calculate_rule_priority(ctx, "foo-bar")
         self.assertEqual(rp, "31", "Failed to calculate rule priority")
         name = helper_functions.load_balancer_name(ctx)
         print("Name: " + name)
         self.assertEqual(name.count('/'), 2, "Failed to trim")
         self.assertEqual(name.count(':'), 0, "Failed to trim")
         self.assertTrue(name.startswith('app'), "Failed to trim")
     finally:
         self.clean(cloudformation=cloudformation)
Beispiel #5
0
 def test_bucket_configuration_us_east_1(self):
     test_deploy_ctx = DeployContext.create_deploy_context(
         application="foo",
         role="bar-{}".format(self.run_random_word),
         environment="unit-test",
         defaults=self.east_config)
     self._validate_s3(test_deploy_ctx)
 def test_artifact_directory_execution_plan(self):
     artifact_directory = self._get_resource_path('artifact_directory_tests/artifact_execution_plan_test')
     deploy_ctx = DeployContext.create_deploy_context_artifact(artifact_directory=artifact_directory,
                                                               environment="unit-test",
                                                               defaults=self.default_config)
     plan = deploy_ctx.get_execution_plan()
     self.assertEqual(len(plan), 4, "Failed to identify all elements of execution - {}".format(plan))
Beispiel #7
0
 def test_parameter_transformation(self):
     ctx = DeployContext.create_deploy_context(application="dev-{}".format(
         self.run_random_word),
                                               role="cluster",
                                               environment="unit-test",
                                               defaults=self.default_config)
     try:
         template_dir = ParentTestCase._get_resource_path(
             "parameter_load_tests/fargate")
         deploy = CloudFormationDeploy(ctx.stack_name,
                                       NamedLocalTemplate(template_dir),
                                       ctx)
         self.assertEqual(deploy.defaults['TASK_SOFT_MEMORY'], 512,
                          'Did not respect defaut')
         self.assertEqual(deploy.defaults['TASK_CPU'], 64,
                          'Did not respect defaut')
         ctx['USE_FARGATE'] = 'true'
         deploy = CloudFormationDeploy(ctx.stack_name,
                                       NamedLocalTemplate(template_dir),
                                       ctx)
         self.assertEqual(deploy.defaults['TASK_SOFT_MEMORY'], '512',
                          'Did not transform memory')
         self.assertEqual(deploy.defaults['TASK_CPU'], 256,
                          'Did not transform cpu')
     finally:
         pass
 def test_service_definition_env_params(self):
     artifact_directory = self._get_resource_path('artifact_directory_tests/artifact_service_definition')
     for env in ['unit-test', 'ci', 'prod']:
         deploy_ctx = DeployContext.create_deploy_context_artifact(artifact_directory=artifact_directory,
                                                                   environment=env,
                                                                   defaults=self.default_config)
         self.assertEqual(deploy_ctx['OVERRIDE'], "{}bar".format(env), "Failed to load {} value".format(env))
         self.assertEqual(deploy_ctx['API_PATH'], "bar", "Failed to load static value")
 def test_artifact_directory_error_handling(self):
     try:
         empty_artifact_directory = self._get_resource_path('artifact_directory_tests/empty_artifact_directory')
         err_ctx = DeployContext.create_deploy_context_artifact(artifact_directory=empty_artifact_directory,
                                                                environment="unit-test")
         self.fail("Failed to generate error")
     except Exception as e:
         pass
 def test_default_configure(self):
     try:
         no_default_ctx = DeployContext.create_deploy_context(application="foo",
                                                              role="bar",
                                                              environment="unit-test")
         self.fail("Failed to generate error")
     except:
         pass
     default_ctx = self.test_deploy_ctx
     region = default_ctx.get_region()
     self.assertEqual(region, "us-west-1", "Failed to load from defaults")
     def_region = "us-west-1"
     os.environ.setdefault(REGION, def_region)
     try:
         no_default_ctx = DeployContext.create_deploy_context(application="foo",
                                                              role="bar",
                                                              environment="unit-test")
         region = no_default_ctx.get_region()
         self.assertEqual(region, def_region, "Failed to leverage environment variable")
     finally:
         del os.environ[REGION]
Beispiel #11
0
 def create_project(self, service_definition, app_dir, service_type=None):
     if not service_type:
         service_type = service_definition.get_service_type()
     service_def_folder = os.path.join(
         service_definition.get_service_directory(app_dir), "service")
     safe_mkdir(service_def_folder)
     gen_service.do_command(DeployContext.create_deploy_context(
         application=service_definition.get_app(),
         role=service_definition.get_role(),
         environment='dev'),
                            service_type=service_type,
                            destination=service_def_folder)
Beispiel #12
0
    def test_fargate_processing(self):
        ctx = DeployContext.create_deploy_context(application="dev-{}".format(
            self.run_random_word),
                                                  role="cluster",
                                                  environment="unit-test",
                                                  defaults=self.default_config)
        ctx['USE_FARGATE'] = 'true'
        cpu_transforms = {
            128: 256,
            12: 256,
            257: 512,
            1023: 1024,
            1024: 1024,
            2000: 2048,
            10000: 4096,
        }
        for to_transform, expected in cpu_transforms.items():
            self.assertEqual(
                helper_functions.transform_fargate_cpu(ctx, to_transform),
                expected, 'Did not transform correctly')
        memory_transforms = {
            128: '512',
            12: '512',
            257: '512',
            1023: '1024',
            1024: '1024',
            2000: '2048',
            10000: '10240',
        }
        for to_transform, expected in memory_transforms.items():
            self.assertEqual(
                helper_functions.transform_fargate_memory(ctx, to_transform),
                expected, 'Did not transform correctly')

        valid_configurations = [
            [256, '512'],
            [2048, '5120'],
            [1024, '2048'],
        ]
        invalid_configuration = [[256, '5120'], [4096, '5120']]
        for config in valid_configurations:
            helper_functions._validate_fargate_resource_allocation(
                config[0], config[1], {})
        for config in invalid_configuration:
            try:
                helper_functions._validate_fargate_resource_allocation(
                    config[0], config[1], {})
                self.fail(
                    "Failed to detect invalid fargate configuration - {} CPU {} Memory"
                    .format(config[0], config[1]))
            except:
                pass
Beispiel #13
0
 def setUpClass(cls):
     super(ParentTestCase, cls).setUpClass()
     cls.resource_directory = RESOURCE_DIR
     cls.run_random_word = cls.randomWord(5)
     cls.default_config_path = ParentTestCase._get_resource_path(
         'default_config.json')
     cls.default_config = load_path_as_json(
         ParentTestCase._get_resource_path('default_config.json'))
     cls.east_config = load_path_as_json(
         ParentTestCase._get_resource_path('east_config.json'))
     cls.test_deploy_ctx = DeployContext.create_deploy_context(
         application="foo",
         role="bar-{}".format(cls.run_random_word),
         environment="unit-test",
         defaults=cls.default_config)
     print_utility.configure(False)
    def test_artifact_directory_service_load(self):
        artifact_directory = self._get_resource_path('artifact_directory_tests/artifact_service_definition')
        deploy_ctx = DeployContext.create_deploy_context_artifact(artifact_directory=artifact_directory,
                                                                  environment="unit-test",
                                                                  defaults=self.default_config)
        self._validate_deploy_ctx(deploy_ctx)
        self.assertListEqual(deploy_ctx.get_service_modifications(), ['autoscale'],
                             "Failed to load service modifications")
        self.assertEqual(deploy_ctx.docker_registry_url, "https://docker.io/my-registry", "Failed to load registry-url")
        self.assertEqual(deploy_ctx.service_definition.service_template_definition_locations,
                         [{"type": "github", "owner": "rspitler", "repo": "cloudformation-templates"}],
                         "Failed to load remote configuration")

        self.assertEqual(deploy_ctx['API_PATH'], "bar", "Failed to load deployment parameters")
        self.assertIsNotNone(deploy_ctx.artifact_definition, "Failed to populate artifact definition")
        self.assertEqual(deploy_ctx.artifact_definition.artifact_id, "39",
                         "Failed to load artifact definition parameters")
        self.assertEqual(deploy_ctx.artifact_definition.artifact_location, "https://docker.io/my-registry/artifact",
                         "Failed to load artifact definition parameters")
        self.assertEqual(deploy_ctx.artifact_definition.artifact_type, "container",
                         "Failed to load artifact definition parameters")
        self.assertIsNotNone(deploy_ctx.service_definition, "Failed to populate service definition")
 def test_deploy_validate(self):
     artifact_directory = self._get_resource_path('artifact_directory_tests/artifact_execution_plan_test')
     deploy_ctx = DeployContext.create_deploy_context_artifact(artifact_directory=artifact_directory,
                                                               environment="unit-test",
                                                               defaults=self.default_config)
     ds_command.do_command(deploy_ctx=deploy_ctx, dry_run=True)