Пример #1
0
def _register_action_in_event_bus(action: Action, binding: Union[Callable, str, EventBinding], fail_fast=False):
    """
    Register a single event binding. It supports name property added by @event decorator on events callable.
    """
    if callable(binding) and hasattr(binding, "name"):
        binding = binding.name
    if isinstance(binding, str):
        binding = EventBinding(binding)
    if callable(binding.event) and hasattr(binding.event, "name"):
        binding.event = binding.event.name

    bus.on(binding.event, action_event_binding_runner_factory(action,
                                                              binding.event,
                                                              to_call=binding.call,
                                                              event_processor=binding.processor,
                                                              fail_fast=fail_fast).run)
    def test_emit_one_arg(self, project_loader):
        project_loader("emit-one-arg")

        features.register(DockerFeature())
        load_registered_features()

        custom_event_listeners = []

        def listener(data):
            custom_event_listeners.append(data)

        bus.on("some:test", listener)

        action = actions.get('docker:emit-docker-compose-config'
                             )  # type:EmitDockerComposeConfigAction
        action.execute()

        assert len(custom_event_listeners) == 1
        assert custom_event_listeners[0] == "emit-one-arg"
    def test_emit_complex(self, project_loader):
        project_loader("emit-complex")

        features.register(DockerFeature())
        load_registered_features()

        some_events = []
        another_events = []

        def someListener(*args, **kwargs):
            some_events.append({"args": args, "kwargs": kwargs})

        def anotherListener(*args, **kwargs):
            another_events.append({"args": args, "kwargs": kwargs})

        bus.on("some:test", someListener)
        bus.on("another:test", anotherListener)

        action = actions.get('docker:emit-docker-compose-config'
                             )  # type:EmitDockerComposeConfigAction
        action.execute()

        assert len(some_events) == 3
        assert {"args": ("emit-one-arg", ), "kwargs": {}} in some_events
        assert {"args": ("emit-one-arg-2", ), "kwargs": {}} in some_events
        # docker-compose >= 1.27.0 returns major version only. see https://github.com/docker/compose/issues/7730
        assert {"args": ("emit-some-arg",),
                "kwargs": {'image': 'ubuntu', 'kw1': 'emit-one-kwarg', 'kw2': 7, 'version': '3.7'}} in some_events or \
               {"args": ("emit-some-arg",),
                "kwargs": {'image': 'ubuntu', 'kw1': 'emit-one-kwarg', 'kw2': 7, 'version': '3'}} in some_events

        assert len(another_events) == 2
        assert {"args": ("emit-another-arg", ), "kwargs": {}} in another_events
        assert {
            "args": (),
            "kwargs": {
                "kw1": "emit-another-kwarg"
            }
        } in another_events
def test_lifecycle():
    register_default_caches()

    phases.register(DefaultPhase("step1"))
    phases.register(DefaultPhase("step2"))

    config.args.clear_cache = False

    events = []

    bus.on(None, lambda event: events.append(event))

    command = LifecycleCommand("test", "TestCommand", "step1", "step2",
                               DefaultPhase("step3"))

    command.execute()

    assert events == ["phase:step1", "phase:step2", "phase:step3"]
    events = []

    command.execute()
    assert events == ["phase:step1", "phase:step2", "phase:step3"]
    def test_ubuntu(self, project_loader):
        project_loader("ubuntu")

        features.register(DockerFeature())
        load_registered_features()

        custom_event_listeners = []

        def listener(docker_compose_config):
            custom_event_listeners.append(docker_compose_config)

        bus.on("docker:docker-compose-config", listener)

        action = actions.get('docker:emit-docker-compose-config'
                             )  # type:EmitDockerComposeConfigAction
        action.execute()

        assert len(custom_event_listeners) == 1
        assert custom_event_listeners[0] in [
            # docker-compose >= 1.27.0 returns major version only. see https://github.com/docker/compose/issues/7730
            {
                "version": "3",
                "services": {
                    "docker": {
                        "image": "ubuntu"
                    }
                }
            },
            {
                "version": "3.7",
                "services": {
                    "docker": {
                        "image": "ubuntu"
                    }
                }
            }
        ]
Пример #6
0
def main(args: Optional[Sequence[str]] = None,  # pylint:disable=too-many-statements
         reset_disabled=False,
         before_handle_command_line=None):
    """
    Load all features and handle command line
    """
    global _watch_started_event, _watch_stop_event  # pylint:disable=global-statement
    _watch_started_event = threading.Event()
    _watch_stop_event = threading.Event()
    initial_cwd = os.getcwd()
    try:
        load_plugins()
        bootstrap_register_features()
        preload_registered_features()

        try:
            command, args, unknown_args = parse_command_line(args)
            config.args = args
            config.unknown_args = unknown_args
        except ParseCommandLineException as exc:
            config.args = exc.parsed_args
            config.unknown_args = exc.unknown_args
            if not config.args.version:
                exc.opts.print_help()
                raise

        load_registered_features(False)
        register_actions_in_event_bus(config.args.fail_fast)

        if config.args.version:
            events.main.version(silent=config.args.silent)
            return []

        prepare_project_home()
        register_default_caches()

        def on_config_reloaded():
            global _watch_started_event, _watch_stop_event  # pylint:disable=global-statement
            _watch_stop_event.set()
            _watch_started_event = threading.Event()
            _watch_stop_event = threading.Event()

            for action in actions.all():
                if hasattr(action, "destroy") and callable(action.destroy):
                    action.destroy()
                if hasattr(action, "initialized") and action.initialized:
                    action.initialized = False

            context.reset()

            handle_command_line(command)

        bus.on(events.config.reloaded.name, on_config_reloaded)  # pylint:disable=no-member

        events.main.start(command=command)
        handle_command_line(command)

        events.main.terminate(command=command)

        return context.exceptions
    except ExpectedError as exception:
        return [exception]
    except Exception as exception:  # pylint:disable=broad-except
        try:
            clear_caches()
        except Exception as clear_cache_exception:  # pylint:disable=broad-except
            logging.getLogger('ddb').exception("An error has occured while clearing caches",
                                               exc_info=clear_cache_exception)
        raise exception
    finally:
        os.chdir(initial_cwd)
        if not reset_disabled:
            reset()