Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multi-values (expect_one_of) in jsonpath tolerance expect #191

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@

[192]: https://github.com/chaostoolkit/chaostoolkit-lib/issues/192

### Added

- New field `expect_one_of` in `jsonpath` tolerance. Sometimes the json payload
values will be dynamic, e.g. field `status` in response payload may provide
any values(either `ok` or `error` or `info`). Using `expect` field, we can
mention only one value as expected value but sometimes steady state can be
met with one or more values. e.g. you want to define two values either `ok` or
`info` as expected value. In these cases, you can use both `expect` and
`expect_one_of` to define both expected values.[#191][191]

[191]: https://github.com/chaostoolkit/chaostoolkit-lib/pull/191


## [1.15.0][] - 2020-09-11

[1.15.0]: https://github.com/chaostoolkit/chaostoolkit-lib/compare/1.14.1...1.15.0
Expand Down Expand Up @@ -258,7 +271,7 @@
was interrupted from a control. With the strategies, you can now decide
that they are always applied, never or only when the experiment deviated.
This is a flag passed to the settings as follows:

```
runtime:
rollbacks:
Expand Down Expand Up @@ -337,13 +350,13 @@
### Added

- Optional default value for environment variable in configuration
- Warn the user for an action process returning a non-zero exit code
- Warn the user for an action process returning a non-zero exit code
- Support for process path relative to homedir ~
- Indicate path in validation when path is not found nor executable [#159][159]

### Changed

- Changed the method's one-step minimum requirement.
- Changed the method's one-step minimum requirement.
An experiment with an empty method (without any activities) is now valid.

[159]: https://github.com/chaostoolkit/chaostoolkit-lib/issues/159
Expand Down Expand Up @@ -455,7 +468,7 @@

### Changed

- Fix to ensure a control's `configuration` parameter is populated when it the
- Fix to ensure a control's `configuration` parameter is populated when it the
control is being `configured` [#114][114]
- Load and apply global controls, those declared in the settings, from the
`run_experiment` function rather than out of band [#116][116]
Expand Down Expand Up @@ -522,7 +535,7 @@
#### Added

- a new tolerance type called `range` to support scenarios such as:

value type is:
```
{
Expand Down Expand Up @@ -887,7 +900,7 @@

### Changed

- Log a message when loading the configuration
- Log a message when loading the configuration
- Raise `InvalidExperiment` when a configuration or secret references a key
in the environment and that key does not exist (it may not be set however)
[#40][40]. This bails the experiment at validation time so before it runs.
Expand Down
15 changes: 14 additions & 1 deletion chaoslib/hypothesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,21 @@ def _(tolerance: dict, value: Any, configuration: Configuration = None, #noqa:
else:
result = values == expect

if "expect" in tolerance and result is False:
expect_one_of = tolerance.get("expect_one_of")
if "expect_one_of" in tolerance:
if not isinstance(expect_one_of, list):
result = values == [expect_one_of]
else:
result = values == expect_one_of

if result is False:
if "expect" in tolerance:
if "expect" in tolerance and "expect_one_of" in tolerance:
logger.debug(
"jsonpath found '{}' but expected '{}' or '{}'".format(
str(values), str(tolerance["expect"]),
str(tolerance["expect_one_of"])))
elif "expect" in tolerance:
logger.debug(
"jsonpath found '{}' but expected '{}'".format(
str(values), str(tolerance["expect"])))
Expand Down
27 changes: 26 additions & 1 deletion tests/test_tolerance.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ def test_tolerance_jsonpath_must_match_expected_value():
}
) is True

t = {
"type": "jsonpath",
"path": "$.foo[?(@.baz)].baz",
"expect": [["hello", "bonjour"]],
"expect_one_of": [["hello", "joe"]]
}
ensure_hypothesis_tolerance_is_valid(t)
assert within_tolerance(
t, value={
'foo': {"baz": ["hello", "joe"]}
}
) is True

t = {
"type": "jsonpath",
Expand All @@ -152,6 +164,19 @@ def test_tolerance_jsonpath_must_match_expected_value():
}
) is True

t = {
"type": "jsonpath",
"path": "$.foo[?(@.baz)].baz",
"expect": [[["hello"], ["bonjour"]]],
"expect_one_of": [[["hello"], ["joe"]]]
}
ensure_hypothesis_tolerance_is_valid(t)
assert within_tolerance(
t, value={
'foo': {"baz": [["hello"], ["joe"]]}
}
) is True

t = {
"type": "jsonpath",
"path": "$.foo[?(@.baz)].baz",
Expand Down Expand Up @@ -201,7 +226,7 @@ def test_tolerance_jsonpath_must_match_expected_values():
assert within_tolerance(
t, value={
'foo': [{"baz": "hello"}, {"baz": "bonjour"}]
},
},
) is True


Expand Down