def _construct_toxi_env()

in dyno/app/api/control.py [0:0]


def _construct_toxi_env(
        job: str,
        port: str,
        scenario: str,
        error_weight: int,
        app_latency_weight=None,
        app_latency_label=None,
        app_latency_lower_bound=None,
        app_latency_upper_bound=None,

        ) -> dict:
    """
    Construct a dictionary representing an Opbeans environment
    which is fronted by a Toxiproxy instance.

    Note
    ----
    The `label_weight` and `label_name` parameters are currently used
    only in the context of the `dyno` scenario. Otherwise, they are
    ignored.

    Parameters
    ----------
    job : str
       The name of the job. Should be passed without including the `opbeans-`
       prefix.

    port : str
        The port for the environment. Can also be passed as a int type.

    scenario : str
        Thie scenario for use with this instance. Should be passed as simply
        the name and NOT as a filename.

    error_weight : int
        The relative "weight" of errors. Higher number result in the load
        generator choosing to hit pages known to produce errors a higher
        percentage of the time.

        This number is entirely arbitrary and is only relative to statically
        configured weights in the scenario file itself.

    app_latency_weight : int
        In the case of the `dyno` scenario, the app_latency_weight parameter
        can be passed which increases the rate at which an endpoint which
        artifically introduces latency is hit.

    app_latency_label : str
        Used in conjunction with `app_latency_weight` to specify a label which
        should be applied to latent requests.

    app_latency_lower_bound : int
        Used in conjunction with `app_latency_weight`, this parameter
        specifies the lower bound for latency. Requests will never be
        less latent than this value.

    app_latency_upper_bound : int
        Used in conjunction with `app_latency_weight`, this parameter
        specifies the upper bound for latency. Requests will never by (much)
        more latent than this value.

    app_latency_user_agent : str
        Used in conjunction with `app_latency_weight`, this parameter
        specifies a user agent for the latent requests.

    Returns
    -------
    dict
        Dictionary containing the environment keys and values

    Examples
    --------
    Sample call showing just the required parameters

    >>> _construct_toxi_env('python', 9999, 'my_great-scenario', 99)
    {
        'OPBEANS_BASE_URL': 'http://toxi:9999',
        'OPBEANS_NAME': 'opbeans-python',
        'ERROR_WEIGHT': '99'
    }

    """
    toxi_env = os.environ.copy()
    toxi_env['OPBEANS_BASE_URL'] = "http://toxi:{}".format(port)
    toxi_env['OPBEANS_NAME'] = "opbeans-" + job
    toxi_env['ERROR_WEIGHT'] = str(error_weight)
    if app_latency_weight:
        toxi_env['APP_LATENCY_WEIGHT'] = str(app_latency_weight)
    if app_latency_label:
        toxi_env['APP_LATENCY_LABEL'] = app_latency_label
    if app_latency_lower_bound:
        toxi_env['APP_LATENCY_LOWER_BOUND'] = str(app_latency_lower_bound)
    if app_latency_upper_bound:
        toxi_env['APP_LATENCY_UPPER_BOUND'] = str(app_latency_upper_bound)
    return toxi_env