def __bootstrap_infrastructure(self) -> None:
     sub = ContinuousSubprocess(
         TestingManager.__aws_cdk_bootstrap_command())
     output = sub.execute(path=self.__config.cdk_app_path,
                          env=self.__config.deployment_process_environment)
     for line in output:
         logger.info(line.strip())
Exemplo n.º 2
0
def test_subprocess_error() -> None:
    """
    Checks whether an exception is raised when an unknown command is passed.

    :return: No return.
    """
    try:
        list(ContinuousSubprocess('TestCommandThatProbablyDoesNotExist').execute())
    except CalledProcessError as ex:
        error_output = json.loads(ex.output)

        # Error message.
        message = error_output['message']
        # Stack trace.
        trace = error_output['trace']
        # The length of a stack trace (in lines).
        trace_size = error_output['trace_size']
        # The maximum possible (allowed) length of a stack trace.
        max_trace_size = error_output['max_trace_size']

        assert message == 'An error has occurred while running the specified command.'
        assert max_trace_size == 1000
        assert len(trace) > 0
        assert trace_size > 0

        print(f'For debug reasons, printing stack error trace: {trace}.', flush=True)

        return

    raise AssertionError('Expected to raise an exception.')
Exemplo n.º 3
0
def test_subprocess_output() -> None:
    """
    Checks whether the output has an expected string.

    :return: No return.
    """
    generator = ContinuousSubprocess('python --version').execute()
    complete_output = '\n'.join(list(generator)).lower()

    assert 'python' in complete_output
Exemplo n.º 4
0
    def execute(self) -> None:
        """
        Executes the CDK deployment command.

        :return: No return.
        """
        # We already know that at this stage the AWS CDK application was
        # synthesized and cdk.out directory with assets produced. Hence,
        # when deploying, we can reuse already synthesized templates with
        # assets that are in cdk.out dir. More on deployments:
        # https://taimos.de/blog/deploying-your-cdk-app-to-different-stages-and-environments
        app_stack = f'--app "cdk.out/" "{self.__stack}"'

        if self.__deployment_type == DeploymentType.DEPLOY:
            command = f'cdk deploy {app_stack}'
        elif self.__deployment_type == DeploymentType.DESTROY:
            command = f'cdk destroy {app_stack} -f'
        else:
            raise ValueError('Invalid enum value.')

        # We want to ensure that each stack deployment can have its own output.
        command += f' --output=./cdk_stacks/{self.__stack}'
        command += ' --progress events --require-approval never'

        cprint(PrintColors.OKBLUE, f'Executing command: {command}.')
        process = ContinuousSubprocess(command)
        process_generator = process.execute(
            path=self.__path,
            env=self.__env,
            max_error_trace_lines=50,
        )

        try:
            for line in process_generator:
                print(f'[{self.__stack}]\t{line}', end='')
            cprint(PrintColors.OKGREEN,
                   f'Deployment of stack {self.__stack} was successful.')
        except subprocess.CalledProcessError as ex:
            cprint(
                PrintColors.FAIL,
                f'Exception raised in {self.__stack} stack. Error: {repr(ex)}.'
            )

            error_output = json.loads(ex.output)

            # Error message.
            message = error_output['message']
            # Stack trace.
            trace = ''.join(error_output['trace'])
            # The length of a stack trace (in lines).
            trace_size = error_output['trace_size']
            # The maximum possible (allowed) length of a stack trace.
            max_trace_size = error_output['max_trace_size']

            cprint(
                PrintColors.FAIL, f'{trace=}, '
                f'{message=}, '
                f'{trace_size=}, '
                f'{max_trace_size=}, '
                f'{ex.returncode=}, '
                f'{ex.cmd=}')

            raise
Exemplo n.º 5
0
 def __destroy_infrastructure(self) -> None:
     sub = ContinuousSubprocess(TestingManager.__aws_cdk_destroy_command())
     output = sub.execute(path=self.__config.cdk_app_path, env=self.__env)
     for line in output:
         logger.info(line)