def test_call_with_resolvers_nothing_to_resolve(self, processor, target_mock):
            command = "foo"
            request = Request(command=command, parameters={"p1": "param"})

            ret_val = processor._invoke_command(target_mock, request, {})
            assert ret_val == getattr(target_mock, command).return_value
            getattr(target_mock, command).assert_called_with(p1="param")
Example #2
0
def initialize_logging(instance_id: str = None,
                       instance: Instance = None,
                       system: System = None) -> Instance:
    """Initialize logging of Instance.

    Args:
        instance_id: The Instance ID
        instance: The Instance
        system: The System

    Returns:
        The Instance
    """
    system, instance = _from_kwargs(system=system,
                                    instance=instance,
                                    instance_id=instance_id)

    logger.debug(f"Initializing logging for instance {system}[{instance}]")

    requests.process_request(
        Request.from_template(
            initialize_logging_request,
            namespace=system.namespace,
            system=system.name,
            system_version=system.version,
            instance_name=instance.name,
        ),
        is_admin=True,
        priority=1,
    )

    return instance
        def test_non_callable_attribute(self, processor, target_mock):
            target_mock.command = "this should be a function"

            request = Request(command="command", parameters={})

            with pytest.raises(RequestProcessingError):
                processor._invoke_command(target_mock, request, {})
 def test_validate_regex_nullable(self, validator):
     req = Request(system="foo", command="command1", parameters={"key1": None})
     command_parameter = Parameter(
         key="key1", multi=False, type="String", regex=r"^Hi.*", nullable=True
     )
     command = Command("test", parameters=[command_parameter])
     validator.get_and_validate_parameters(req, command)
        def test_success_none_parameters(self, processor, target_mock):
            command = "start"
            request = Request(command=command, parameters=None)

            ret_val = processor._invoke_command(target_mock, request, {})
            assert ret_val == getattr(target_mock, command).return_value
            getattr(target_mock, command).assert_called_once_with()
    def test_validate_parameter_based_on_type_null_not_nullable(self, validator):
        req = Request(system="foo", command="command1", parameters={"key1": None})
        command_parameter = Mock(key="key1", multi=False, type="String", nullable=False)
        command = Mock(parameters=[command_parameter])

        with pytest.raises(ModelValidationError):
            validator.get_and_validate_parameters(req, command)
Example #7
0
    def test_invoke_command_admin(self, start_mock):
        params = {'p1': 'param'}
        request = Request(system='test_system', system_version='1.0.0', command='_start',
                          parameters=params)

        self.plugin._invoke_command(self.plugin, request)
        start_mock.assert_called_once_with(self.plugin, **params)
        def test_missing_attribute(self, processor, target_mock):
            target_mock.mock_add_spec("other_command")

            request = Request(command="command", parameters={})

            with pytest.raises(RequestProcessingError):
                processor._invoke_command(target_mock, request, {})
    def test_validate_command_choices_bad_value_type(self, monkeypatch, validator):
        _process_mock(monkeypatch, return_value='["value"]')

        request = Request(
            system="foo",
            command="command1",
            parameters={"key1": "value"},
            system_version="0.0.1",
            instance_name="instance_name",
        )
        command = Mock(
            parameters=[
                Parameter(
                    key="key1",
                    type="String",
                    optional=False,
                    choices=Choices(
                        type="command",
                        value=1,
                        strict=True,
                    ),
                )
            ]
        )

        with pytest.raises(ModelValidationError):
            validator.get_and_validate_parameters(request, command)
    def test_validate_command_choices_dictionary_list_response(
        self, monkeypatch, validator
    ):
        process_mock = _process_mock(monkeypatch, return_value='[{"value": "value"}]')

        request = Request(
            system="foo",
            command="command1",
            parameters={"key1": "value"},
            system_version="0.0.1",
            instance_name="instance_name",
        )
        command = Mock(
            parameters=[
                Parameter(
                    key="key1",
                    type="String",
                    choices=Choices(type="command", value="command_name", strict=True),
                    optional=False,
                )
            ]
        )

        validator.get_and_validate_parameters(request, command)

        choices_request = process_mock.call_args[0][0]
        assert choices_request.command == "command_name"
        assert choices_request.system == "foo"
        assert choices_request.system_version == "0.0.1"
        assert choices_request.instance_name == "instance_name"
Example #11
0
def parent_request(parent_request_dict, ts_dt):
    """A parent request as a model."""
    dict_copy = copy.deepcopy(parent_request_dict)
    dict_copy["created_at"] = ts_dt
    dict_copy["updated_at"] = ts_dt
    dict_copy["status_updated_at"] = ts_dt
    return Request(**dict_copy)
Example #12
0
def child_request(child_request_dict, ts_dt):
    """A child request as a model."""
    dict_copy = copy.deepcopy(child_request_dict)
    dict_copy["created_at"] = ts_dt
    dict_copy["updated_at"] = ts_dt
    dict_copy["status_updated_at"] = ts_dt
    return Request(**dict_copy)
Example #13
0
    def _construct_bg_request(self, **kwargs):
        # type: (**Any) -> Request
        """Create a request that can be used with EasyClient.create_request"""

        command = kwargs.pop("_command", None)
        system_name = kwargs.pop("_system_name", None)
        system_version = kwargs.pop("_system_version", None)
        system_display = kwargs.pop("_system_display", None)
        system_namespace = kwargs.pop("_system_namespace", None)
        instance_name = kwargs.pop("_instance_name", None)
        comment = kwargs.pop("_comment", None)
        output_type = kwargs.pop("_output_type", None)
        metadata = kwargs.pop("_metadata", {})
        parent = kwargs.pop("_parent", self._get_parent_for_request())

        if system_display:
            metadata["system_display_name"] = system_display

        # Don't check namespace - https://github.com/beer-garden/beer-garden/issues/827
        if command is None:
            raise ValidationError("Unable to send a request with no command")
        if system_name is None:
            raise ValidationError(
                "Unable to send a request with no system name")
        if system_version is None:
            raise ValidationError(
                "Unable to send a request with no system version")
        if instance_name is None:
            raise ValidationError(
                "Unable to send a request with no instance name")

        request = Request(
            command=command,
            system=system_name,
            system_version=system_version,
            namespace=system_namespace,
            instance_name=instance_name,
            comment=comment,
            output_type=output_type,
            parent=parent,
            metadata=metadata,
            parameters=kwargs,
        )

        request.parameters = self._resolve_parameters(command, request)

        return request
Example #14
0
    def test_invoke_command_request(self):
        params = {'p1': 'param'}
        request = Request(system='test_system', system_version='1.0.0', command='command',
                          parameters=params)
        self.client.command = Mock()

        self.plugin._invoke_command(self.client, request)
        self.client.command.assert_called_once_with(**params)
Example #15
0
def bg_request(request_dict, parent_request, child_request, ts_dt):
    """A request as a model."""
    dict_copy = copy.deepcopy(request_dict)
    dict_copy['parent'] = parent_request
    dict_copy['children'] = [child_request]
    dict_copy['created_at'] = ts_dt
    dict_copy['updated_at'] = ts_dt
    return Request(**dict_copy)
 def test_command_lookup(self, monkeypatch, validator):
     request = Request(parameters={})
     lookup_mock = Mock(return_value=Mock(parameters=[]))
     monkeypatch.setattr(
         validator, "get_and_validate_command_for_system", lookup_mock
     )
     validator.get_and_validate_parameters(request)
     lookup_mock.assert_called_once_with(request)
Example #17
0
 def test_from_template_overrides(self, bg_request_template):
     request = Request.from_template(bg_request_template,
                                     command_type="INFO")
     assert request.command_type == "INFO"
     for key in bg_request_template.__dict__:
         if key != "command_type":
             assert getattr(request,
                            key) == getattr(bg_request_template, key)
Example #18
0
def bg_request(request_dict, parent_request, child_request, ts_dt):
    """A request as a model."""
    dict_copy = copy.deepcopy(request_dict)
    dict_copy["parent"] = parent_request
    dict_copy["children"] = [child_request]
    dict_copy["created_at"] = ts_dt
    dict_copy["updated_at"] = ts_dt
    dict_copy["status_updated_at"] = ts_dt
    return Request(**dict_copy)
Example #19
0
 def simple_request(self):
     return Request(
         system="something_v3",
         system_version="3.0.0",
         instance_name="my_bg",
         namespace="file_testing",
         command="something_cool",
         id=str(ObjectId()),
     )
Example #20
0
    def _handle_request_update_failure(self, request, headers, exc):

        # If brew-view is down, we always want to try again
        # Yes, even if it is the 'final_attempt'
        if isinstance(exc, (RequestsConnectionError, RestConnectionError)):
            self.brew_view_down = True
            self.logger.error(
                "Error updating request status: {0} exception: {1}".format(
                    request.id, exc))
            raise RepublishRequestException(request, headers)

        elif isinstance(exc, TooLargeError):
            self.logger.error(
                "Error updating request {0} - the request exceeds the 16MB size "
                "limitation. The status of this request will be marked as ERROR, but "
                "it's possible the request actually completed successfully. If this "
                "happens often please contact the plugin developer.".format(
                    request.id))

            raise RepublishRequestException(
                Request(
                    id=request.id,
                    status="ERROR",
                    output="Request size greater than 16MB",
                    error_class=BGGivesUpError.__name__,
                ),
                headers,
            )

        elif isinstance(exc, RestClientError):
            message = (
                "Error updating request {0} and it is a client error. Probable "
                "cause is that this request is already updated. In which case, "
                "ignore this message. If request {0} did not complete, please "
                "file an issue. Discarding request to avoid an infinte loop. "
                "exception: {1}".format(request.id, exc))
            self.logger.error(message)
            raise DiscardMessageException(message)

        # Time to discard the message because we've given up
        elif self._should_be_final_attempt(headers):
            message = (
                "Could not update request {0} even with a known good status, "
                "output and error_class. We have reached the final attempt and "
                "will now discard the message. Attempted to process this "
                "message {1} times".format(request.id,
                                           headers["retry_attempt"]))
            self.logger.error(message)
            raise DiscardMessageException(message)

        else:
            self._update_retry_attempt_information(headers)
            self.logger.exception(
                "Error updating request (Attempt #{0}: request: {1} exception: "
                "{2}".format(headers.get("retry_attempt", 0), request.id, exc))
            raise RepublishRequestException(request, headers)
    def test_extract_parameter_non_multi_calls_no_default(
        self, validate_mock, validator
    ):
        req = Request(system="foo", command="command1", parameters={"key1": "value1"})
        command_parameter = Mock(key="key1", multi=False)
        command = Mock(parameters=[command_parameter])
        validate_mock.side_effect = lambda w, x, y, z: w

        validator.get_and_validate_parameters(req, command)
        validate_mock.assert_called_once_with("value1", command_parameter, command, req)
Example #22
0
    def __init__(self, heartbeat_interval=10, timeout_seconds=30):
        self.logger = logging.getLogger(__name__)
        self.display_name = "Plugin Status Monitor"
        self.heartbeat_interval = heartbeat_interval
        self.timeout = timedelta(seconds=timeout_seconds)
        self.status_request = Request(command="_status",
                                      command_type="EPHEMERAL")

        super(StatusMonitor, self).__init__(logger=self.logger,
                                            name="PluginStatusMonitor")
        def test_call_resolve(self, processor, target_mock, bg_command):
            request = Request(command=bg_command.name, parameters={"message": "test"})

            processor._invoke_command(target_mock, request, {})
            processor._resolver.resolve.assert_called_once_with(
                request.parameters, definitions=bg_command.parameters, upload=False
            )
            getattr(target_mock, bg_command.name).assert_called_once_with(
                message="test"
            )
    def test_missing_nested_parameters(self, validator):
        req = Request(system="foo", command="command1", parameters={"key1": {}})
        nested_parameter = Mock(key="foo", multi=False, type="String", optional=False)
        command_parameter = Mock(
            key="key1", multi=False, type="Dictionary", parameters=[nested_parameter]
        )
        command = Mock(parameters=[command_parameter])

        with pytest.raises(ModelValidationError):
            validator.get_and_validate_parameters(req, command)
 def test_validate_value_value_in_choices_not_multi_valid_choice(self, validator):
     req = Request(system="foo", command="command1", parameters={"key1": "value"})
     command_parameter = Mock(
         key="key1",
         multi=False,
         type="String",
         choices=Mock(type="static", value=["value"]),
     )
     command = Mock(parameters=[command_parameter])
     validator.get_and_validate_parameters(req, command)
 def test_update_and_validate_parameter_extract_parameter_nullable_no_default(
     self, validator
 ):
     req = Request(system="foo", command="command1", parameters={})
     command_parameter = Parameter(
         key="key1", multi=False, nullable=True, default=None, optional=True
     )
     command = Mock(parameters=[command_parameter])
     validated_parameters = validator.get_and_validate_parameters(req, command)
     assert validated_parameters["key1"] is None
Example #27
0
    def test_invoke_request(self, plugin, client):
        request = Request(
            system="test_system",
            system_version="1.0.0",
            command="command",
            parameters={"p1": "param"},
        )

        plugin._invoke_command(client, request)
        client.command.assert_called_once_with(**request.parameters)
Example #28
0
 def test_failure(self, plugin, client, command):
     with pytest.raises(RequestProcessingError):
         plugin._invoke_command(
             client,
             Request(
                 system="name",
                 system_version="1.0.0",
                 command=command,
                 parameters={"p1": "param"},
             ),
         )
    def test_update_and_validate_parameter_extract_parameter_multi_not_list(
        self, validator
    ):
        req = Request(
            system="foo", command="command1", parameters={"key1": "NOT A LIST"}
        )
        command_parameter = Mock(key="key1", multi=True)
        command = Mock(parameters=[command_parameter])

        with pytest.raises(ModelValidationError):
            validator.get_and_validate_parameters(req, command)
    def test_update_and_validate_parameter_extract_parameter_optional_no_default(
        self, validator
    ):
        req = Request(system="foo", command="command1", parameters={})
        command_parameter = Parameter(
            key="key1", multi=False, optional=True, default=None
        )
        command = Mock(parameters=[command_parameter])

        with pytest.raises(ModelValidationError):
            validator.get_and_validate_parameters(req, command)