# Chaos Toolkit wiremock driver

[![pypi](https://img.shields.io/pypi/v/chaostoolkit-wiremock.svg)](https://pypi.python.org/pypi/chaostoolkit-wiremock) [![travis](https://img.shields.io/travis/grubert65/chaostp.svg)](https://travis-ci.org/grubert65/chaostp) [![readthedocs](https://readthedocs.org/projects/chaostp/badge/?version=latest)](https://chaostp.readthedocs.io/en/latest/?badge=latest)

This module provides actions to inject delays in a downstream service stubbed
with Wiremock.

## Install

To be used from your experiment, this package must be installed in the Python
environment where [chaostoolkit][] already lives.

[chaostoolkit]: https://github.com/chaostoolkit/chaostoolkit

```
$ pip install chaostoolkit-wiremock
```

## Usage

### Configuration

The following keys can be configured in the experiment global configuration
section, under the "wiremock" key:

-   **host**: the wiremock server host
-   **port**: the wiremock server port
    **url**:  the complete url to 
    **timeout**: accepted timeout (defaults to 1 sec)
-   **down**: the delayDistribution section used by the ``down`` action


An example of configuration section:

```json
{
    "configuration": {
        "wiremock": {
            "host": "localhost",
            "port": 8080,
            "url": "...",
            "timeout": 10,
            "down": {
                "type": "lognormal",
                "median": 3000,
                "sigma": 0.2
            }
        }
    }
}
```

Note: Wiremock server can be defined setting the host/port pair or giving the base url (url up to the "/__admin" path excluded)

## Exported Actions

### Adding a mapping

```json
{
  "method": [
    {
      "type": "action",
      "name": "adding a mapping",
      "provider": {
          "type": "python",
          "module": "chaoswm.actions",
          "func": "add_mapping",
          "arguments": {
              "request": {
                  "method": "GET",
                  "url": "/some/thing"
              },
              "response": {
                  "status": 200,
                  "body": "Hello world!",
                  "headers": {
                      "Content-Type": "text/plain"
                  }
              } 
          }
      }
    }
  ]
}
```

### Deleting a mapping

```json
{
  "method": [
    {
      "type": "action",
      "name": "deleting a mapping",
      "provider": {
          "type": "python",
          "module": "chaoswm.actions",
          "func": "delete_mapping",
          },
          "arguments": {
              "method": "GET",
              "url": "/some/thing"
          }
      }
  ]
}
```

### Adding a global fixed delay (in milliseconds)
```json
{
  "method": [
    {
      "type": "action",
      "name": "Adding a global fixed delay",
      "provider": {
          "type": "python",
          "module": "chaoswm.actions",
          "func": "global_fixed_delay"
          },
          "arguments": {
              "fixedDelay": 10
          }
      }
  ]
}
```

### Adding a global random delay
```json
{
  "method": [
    {
      "type": "action",
      "name": "Adding a global random delay",
      "provider": {
          "type": "python",
          "module": "chaoswm.actions",
          "func": "global_random_delay"
          },
          "arguments": {
            "delayDistribution": {
                "type": "lognormal",
                "median": 20,
                "sigma": 0.1
            }
          }
      }
  ]
}
```


### Adding a fixed delay to a mapping

```json
{
  "method": [
    {
      "type": "action",
      "name": "Adding a fixed delay to a mapping",
      "provider": {
          "type": "python",
          "module": "chaoswm.actions",
          "func": "fixed_delay"
          },
          "arguments": {
              "method": "GET",
              "url": "/some/thing",
              "fixedDelayMilliseconds": 100
          }
      }
  ]
}
```

### Adding a fixed delay to multiple mappings
If a consistent number of mappings have to be delayed, the following
action might be useful:

```json
{
  "method": [
    {
      "type": "action",
      "name": "Adding a fixed delay to a set of mappings",
      "provider": {
          "type": "python",
          "module": "chaoswm.actions",
          "func": "fixed_delay_to_many_mappings"
          },
          "arguments": {[{
                "method": "GET",
                "url": "/some/thing",
              },{
                "method": "POST",
                "url": "/some/thing/else",
              }],
              "fixedDelayMilliseconds": 100
          }
      }
  ]
}
```


### Adding a random delay to a mapping

```json
{
  "method": [
    {
      "type": "action",
      "name": "Adding a random delay to a mapping",
      "provider": {
          "type": "python",
          "module": "chaoswm.actions",
          "func": "random_delay"
          },
          "arguments": {
              "method": "GET",
              "url": "/some/thing",
              "delayDistribution": {
                  "type": "lognormal",
                  "median": 80,
                  "sigma": 0.4
              }
          }
      }
  ]
}
```

### Adding a ChunkedDribbleDelay to a mapping

```json
{
  "method": [
    {
      "type": "action",
      "name": "Adding a ChunkedDribbleDelay to a mapping",
      "provider": {
          "type": "python",
          "module": "chaoswm.actions",
          "func": "chunked_dribble_delay"
          },
          "arguments": {
              "method": "GET",
              "url": "/some/thing",
              "chunkedDribbleDelay": {
                  "numberOfChunks": 5,
                  "totalDuration": 1000
              }
          }
      }
  ]
}
```

### Taking a mapping down (heavy distribution delay)
This action will use the parameters specified in the "down" key of
the configuration section.

```json
{
  "method": [
    {
      "type": "action",
      "name": "Taking a mapping down",
      "provider": {
          "type": "python",
          "module": "chaoswm.actions",
          "func": "mapping_down"
          },
          "arguments": {
              "method": "GET",
              "url": "/some/thing",
          }
      }
  ]
}
```

### Taking more mappings down at the same time

```json
{
  "method": [
    {
      "type": "action",
      "name": "Taking a mapping down",
      "provider": {
          "type": "python",
          "module": "chaoswm.actions",
          "func": "more_mappings_down"
          },
          "arguments": {[{
                "method": "GET",
                "url": "/some/thing",
             },{
                "method": "POST",
                "url": "/some/thing/else",
             }]
          }
      }
  ]
}
```

### Resetting a mapping (deleting all custom delays)

```json
{
  "method": [
    {
      "type": "action",
      "name": "Resetting a mapping",
      "provider": {
          "type": "python",
          "module": "chaoswm.actions",
          "func": "reset_mapping"
          },
          "arguments": {
              "method": "GET",
              "url": "/some/thing",
          }
      }
  ]
}
```

### Resetting more mappings (deleting all custom delays)

```json
{
  "method": [
    {
      "type": "action",
      "name": "Resetting more mapping",
      "provider": {
          "type": "python",
          "module": "chaoswm.actions",
          "func": "reset_mappings"
          },
          "arguments": {[{
              "method": "GET",
              "url": "/some/thing",
          },{
              "method": "GET",
              "url": "/some/thing/else",
          }
          ]}
      }
  ]
}
```


### Discovery

You may use the Chaos Toolkit to discover the capabilities of this extension:

```
$ chaos discover chaostoolkit-wiremock  --no-install
```

## Configuration

Wiremock server host/port can be provided at action level or experiment level
through the configuration keys "wiremock_host", "wiremock_port".


## Contribute

If you wish to contribute more functions to this package, you are more than
welcome to do so. Please fork this project, make your changes following the
usual [PEP 8][pep8] code style, add appropriate tests and submit a PR for
review.

[pep8]: https://pycodestyle.readthedocs.io/en/latest/

The Chaos Toolkit projects require all contributors must sign a
[Developer Certificate of Origin][dco] on each commit they would like to merge
into the master branch of the repository. Please, make sure you can abide by
the rules of the DCO before submitting a PR.

[dco]: https://github.com/probot/dco#how-it-works
