Skip to content

Commit 4a99ba9

Browse files
add expect_one_of feature in jsonpath tolerance
Signed-off-by: saravanan palanisamy <saravanan30erd@gmail.com> add debug log - jsonpath tolerance Signed-off-by: saravanan palanisamy <saravanan30erd@gmail.com> add new field for expect alternative values Signed-off-by: saravanan palanisamy <saravanan30erd@gmail.com> add debug log for expect_alt Signed-off-by: saravanan palanisamy <saravanan30erd@gmail.com> add test cases for expect_alt Signed-off-by: saravanan palanisamy <saravanan30erd@gmail.com> rename the second expect field & add changelog Signed-off-by: saravanan palanisamy <saravanan30erd@gmail.com>
1 parent f781dfe commit 4a99ba9

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

CHANGELOG.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44

55
[Unreleased]: https://github.com/chaostoolkit/chaostoolkit-lib/compare/1.15.0...HEAD
66

7+
### Added
8+
9+
- New field `expect_one_of` in `jsonpath` tolerance. Sometimes the json payload
10+
values will be dynamic, e.g. field `status` in response payload may provide
11+
any values(either `ok` or `error` or `info`). Using `expect` field, we can
12+
mention only one value as expected value but sometimes steady state can be
13+
met with one or more values. e.g. you want to define two values either `ok` or
14+
`info` as expected value. In these cases, you can use both `expect` and
15+
`expect_one_of` to define both expected values.[#191][191]
16+
17+
[191]: https://github.com/chaostoolkit/chaostoolkit-lib/pull/191
18+
19+
720
## [1.15.0][] - 2020-09-11
821

922
[1.15.0]: https://github.com/chaostoolkit/chaostoolkit-lib/compare/1.14.1...1.15.0
@@ -136,7 +149,7 @@
136149
was interrupted from a control. With the strategies, you can now decide
137150
that they are always applied, never or only when the experiment deviated.
138151
This is a flag passed to the settings as follows:
139-
152+
140153
```
141154
runtime:
142155
rollbacks:
@@ -215,13 +228,13 @@
215228
### Added
216229

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

222235
### Changed
223236

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

227240
[159]: https://github.com/chaostoolkit/chaostoolkit-lib/issues/159
@@ -333,7 +346,7 @@
333346

334347
### Changed
335348

336-
- Fix to ensure a control's `configuration` parameter is populated when it the
349+
- Fix to ensure a control's `configuration` parameter is populated when it the
337350
control is being `configured` [#114][114]
338351
- Load and apply global controls, those declared in the settings, from the
339352
`run_experiment` function rather than out of band [#116][116]
@@ -400,7 +413,7 @@
400413
#### Added
401414

402415
- a new tolerance type called `range` to support scenarios such as:
403-
416+
404417
value type is:
405418
```
406419
{
@@ -765,7 +778,7 @@
765778

766779
### Changed
767780

768-
- Log a message when loading the configuration
781+
- Log a message when loading the configuration
769782
- Raise `InvalidExperiment` when a configuration or secret references a key
770783
in the environment and that key does not exist (it may not be set however)
771784
[#40][40]. This bails the experiment at validation time so before it runs.

chaoslib/hypothesis.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,21 @@ def _(tolerance: dict, value: Any, configuration: Configuration = None,
334334
else:
335335
result = values == expect
336336

337+
if "expect" in tolerance and result is False:
338+
expect_one_of = tolerance.get("expect_one_of")
339+
if "expect_one_of" in tolerance:
340+
if not isinstance(expect_one_of, list):
341+
result = values == [expect_one_of]
342+
else:
343+
result = values == expect_one_of
344+
337345
if result is False:
338-
if "expect" in tolerance:
346+
if "expect" in tolerance and "expect_one_of" in tolerance:
347+
logger.debug(
348+
"jsonpath found '{}' but expected '{}' or '{}'".format(
349+
str(values), str(tolerance["expect"]),
350+
str(tolerance["expect_one_of"])))
351+
elif "expect" in tolerance:
339352
logger.debug(
340353
"jsonpath found '{}' but expected '{}'".format(
341354
str(values), str(tolerance["expect"])))

tests/test_tolerance.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ def test_tolerance_jsonpath_must_match_expected_value():
139139
}
140140
) is True
141141

142+
t = {
143+
"type": "jsonpath",
144+
"path": "$.foo[?(@.baz)].baz",
145+
"expect": [["hello", "bonjour"]],
146+
"expect_one_of": [["hello", "joe"]]
147+
}
148+
ensure_hypothesis_tolerance_is_valid(t)
149+
assert within_tolerance(
150+
t, value={
151+
'foo': {"baz": ["hello", "joe"]}
152+
}
153+
) is True
142154

143155
t = {
144156
"type": "jsonpath",
@@ -152,6 +164,19 @@ def test_tolerance_jsonpath_must_match_expected_value():
152164
}
153165
) is True
154166

167+
t = {
168+
"type": "jsonpath",
169+
"path": "$.foo[?(@.baz)].baz",
170+
"expect": [[["hello"], ["bonjour"]]],
171+
"expect_one_of": [[["hello"], ["joe"]]]
172+
}
173+
ensure_hypothesis_tolerance_is_valid(t)
174+
assert within_tolerance(
175+
t, value={
176+
'foo': {"baz": [["hello"], ["joe"]]}
177+
}
178+
) is True
179+
155180
t = {
156181
"type": "jsonpath",
157182
"path": "$.foo[?(@.baz)].baz",
@@ -201,7 +226,7 @@ def test_tolerance_jsonpath_must_match_expected_values():
201226
assert within_tolerance(
202227
t, value={
203228
'foo': [{"baz": "hello"}, {"baz": "bonjour"}]
204-
},
229+
},
205230
) is True
206231

207232

0 commit comments

Comments
 (0)