From e4988d9f4838058184c3131556533ff4b8e2f0d6 Mon Sep 17 00:00:00 2001 From: blag Date: Sat, 28 Nov 2020 19:05:47 -0800 Subject: [PATCH 1/4] Add object_key_string_contains action to the asserts pack --- packs/asserts/README.md | 2 ++ .../actions/object_key_string_contains.py | 23 +++++++++++++++++++ .../actions/object_key_string_contains.yaml | 19 +++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 packs/asserts/actions/object_key_string_contains.py create mode 100644 packs/asserts/actions/object_key_string_contains.yaml diff --git a/packs/asserts/README.md b/packs/asserts/README.md index 2a1dde8..e18802c 100644 --- a/packs/asserts/README.md +++ b/packs/asserts/README.md @@ -7,3 +7,5 @@ Contains actions that perform assert valuations. ``object_key_number_equals`` - Given an object and a key in the object, evaluate whether value corresponding to key is equal to an expected numerical value. ``object_key_string_equals`` - Given an object and a key in the object, evaluate whether value corresponding to key is equal to an expected string value. + +``object_key_string_contains`` - Given an object and a key in the object, evaluate whether value corresponding to key contains an expected string value. diff --git a/packs/asserts/actions/object_key_string_contains.py b/packs/asserts/actions/object_key_string_contains.py new file mode 100644 index 0000000..d73de78 --- /dev/null +++ b/packs/asserts/actions/object_key_string_contains.py @@ -0,0 +1,23 @@ +import sys + +from st2actions.runners.pythonrunner import Action + +__all__ = [ + 'AssertObjectKeyStringContains' +] + + +class AssertObjectKeyStringContains(Action): + def run(self, object, key, value): + if not isinstance(object, dict): + raise ValueError('object shoud be of type "dict".') + if key not in object: + sys.stderr.write('KEY %s DOESN\'T EXIST.' % key) + raise ValueError('Key %s doesn\'t exist in object %s' % (key, object)) + result = (value in object[key]) + if result: + sys.stdout.write('EQUAL') + else: + sys.stdout.write('NOT EQUAL') + raise ValueError('Value not found. Expected "%s", got "%s". ' % (value, object[key])) + return result diff --git a/packs/asserts/actions/object_key_string_contains.yaml b/packs/asserts/actions/object_key_string_contains.yaml new file mode 100644 index 0000000..f660f3a --- /dev/null +++ b/packs/asserts/actions/object_key_string_contains.yaml @@ -0,0 +1,19 @@ +--- +description: Check if key in object contains a given string value. +enabled: true +entry_point: object_key_string_contains.py +name: object_key_string_contains +parameters: + object: + type: object + description: Object input. + required: true + key: + type: string + description: Key in object to pick the value for to test. + required: true + value: + type: string + description: Expected string value. + required: true +runner_type: "python-script" From c0e5fd540d53f9680e28a0baea719c24aaf6e0e6 Mon Sep 17 00:00:00 2001 From: blag Date: Sat, 28 Nov 2020 19:06:10 -0800 Subject: [PATCH 2/4] Use the new object_key_string_contains action instead of simple string equality --- packs/tests/actions/chains/test_quickstart_python_actions.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packs/tests/actions/chains/test_quickstart_python_actions.yaml b/packs/tests/actions/chains/test_quickstart_python_actions.yaml index bfe3530..80bbafb 100644 --- a/packs/tests/actions/chains/test_quickstart_python_actions.yaml +++ b/packs/tests/actions/chains/test_quickstart_python_actions.yaml @@ -30,7 +30,7 @@ chain: on-success: "assert_stderr_field_stderr_test" - name: "assert_stderr_field_stderr_test" - ref: "asserts.object_key_string_equals" + ref: "asserts.object_key_string_contains" params: object: "{{test_stderr_python_action}}" key: "stderr" From 08ce423f569a8424c53e3db6b72fc6f43d502283 Mon Sep 17 00:00:00 2001 From: blag Date: Tue, 1 Dec 2020 21:40:29 -0800 Subject: [PATCH 3/4] Use assert_line instead of assert_output --- docs/test_basic.bats | 24 ++++++++++++------------ docs/test_integration_packs_doc.bats | 20 ++++++++++---------- docs/test_key_triggers.bats | 4 ++-- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/docs/test_basic.bats b/docs/test_basic.bats index e5c4eb2..f241843 100644 --- a/docs/test_basic.bats +++ b/docs/test_basic.bats @@ -8,8 +8,8 @@ load '../test_helpers/bats-assert/load' run st2 --version assert_success - assert_output --regexp "^st2 " - assert_output --partial "on Python" + assert_line --regexp "^st2 " + assert_line --partial "on Python" } @test "st2 usage works" { @@ -24,7 +24,7 @@ load '../test_helpers/bats-assert/load' run st2 -h assert_success - assert_output --regexp "^usage:" + assert_line --regexp "^usage:" assert_output --partial "CLI for StackStorm event-driven automation platform." assert_output --partial "Enable debug mode" } @@ -54,7 +54,7 @@ load '../test_helpers/bats-assert/load' run eval "st2 action get -j core.http | jq -r '.ref'" assert_success - assert_output "core.http" + assert_line "core.http" run eval "st2 action get -j core.http | jq -r '.parameters.method.enum | .[]'" assert_success @@ -74,19 +74,19 @@ load '../test_helpers/bats-assert/load' run eval "st2 run -j core.local -- date -R | jq -r '.status'" assert_success - assert_output "succeeded" + assert_line "succeeded" run eval "st2 run -j core.local -- date -R | jq -r '.parameters.cmd'" assert_success - assert_output "date -R" + assert_line "date -R" run eval "st2 execution list -n 1 -j | jq -r '.[].action.ref'" assert_success - assert_output "core.local" + assert_line "core.local" run eval "st2 execution list -n 1 -j | jq -r '.[].status'" assert_success - assert_output "succeeded" + assert_line "succeeded" } @test "sensor list works" { @@ -100,7 +100,7 @@ load '../test_helpers/bats-assert/load' run eval "st2 trigger list -j -a=all | jq -r '.[].pack = \"core\" | length'" assert_success - [[ "$output" -gt 15 ]] + assert_line --regexp "[[:digit:]]{1,}" } @test "trigger get works" { @@ -108,17 +108,17 @@ load '../test_helpers/bats-assert/load' run eval "st2 trigger get -j $TRIGGER_ID | jq -r '.id'" assert_success - assert_output "$TRIGGER_ID" + assert_line "$TRIGGER_ID" } @test "core.remote action works" { run eval "st2 run -j core.remote hosts=\"localhost\" -- uname -a | jq -r '.parameters.cmd'" assert_success - assert_output "uname -a" + assert_line "uname -a" run eval "st2 run -j core.remote hosts=\"localhost\" -- uname -a | jq -r '.parameters.hosts'" assert_success - assert_output "localhost" + assert_line "localhost" } diff --git a/docs/test_integration_packs_doc.bats b/docs/test_integration_packs_doc.bats index 7599d57..1002945 100644 --- a/docs/test_integration_packs_doc.bats +++ b/docs/test_integration_packs_doc.bats @@ -24,7 +24,7 @@ load '../test_helpers/bats-assert/load' run eval "st2 action list --pack libcloud -j | jq '. | length'" assert_success - assert_output --regexp '^[[:digit:]]{1,}$' + assert_line --regexp '^[[:digit:]]{1,}$' refute_output '0' } @@ -32,7 +32,7 @@ load '../test_helpers/bats-assert/load' run eval "st2 action list --pack consul -j | jq '. | length'" assert_success - assert_output --regexp '^[[:digit:]]{1,}$' + assert_line --regexp '^[[:digit:]]{1,}$' refute_output '0' } @@ -48,14 +48,14 @@ load '../test_helpers/bats-assert/load' run st2 action list --pack libcloud -j assert_success - assert_output "No matching items found" + assert_line "No matching items found" } @test "no actions in consul pack" { run st2 action list --pack consul -j assert_success - assert_output "No matching items found" + assert_line "No matching items found" } @test "packs can be downloaded using packs.download" { @@ -63,24 +63,24 @@ load '../test_helpers/bats-assert/load' run eval "echo '$DOWNLOAD_RESULTS' | jq -r '.status'" assert_success - assert_output "succeeded" + assert_line "succeeded" run eval "echo '$DOWNLOAD_RESULTS' | jq -r '.result.result.libcloud'" assert_success - assert_output "Success." + assert_line "Success." } @test "can run packs.setup_virtualenv for a pack downloaded in previous step" { run eval "st2 run packs.setup_virtualenv packs=libcloud -j | jq -r '.status'" assert_success - assert_output "succeeded" + assert_line "succeeded" run eval "st2 run packs.setup_virtualenv packs=libcloud -j | jq -r '.result.result'" assert_success - assert_output "Successfully set up virtualenv for the following packs: libcloud" + assert_line "Successfully set up virtualenv for the following packs: libcloud" } @test "packs register registers all packs" { @@ -140,12 +140,12 @@ load '../test_helpers/bats-assert/load' run eval "st2 run packs.download packs=bitcoin -j | jq -r '.result.result.bitcoin'" assert_success - assert_output "Success." + assert_line "Success." } @test "pack reinstall with no config" { run eval "st2 run packs.download packs=bitcoin -j | jq -r '.result.result.bitcoin'" assert_success - assert_output "Success." + assert_line "Success." } diff --git a/docs/test_key_triggers.bats b/docs/test_key_triggers.bats index 542dc24..76f8f7d 100644 --- a/docs/test_key_triggers.bats +++ b/docs/test_key_triggers.bats @@ -138,7 +138,7 @@ KEY_JSON_FILE="docs/test_key_triggers.json" run eval "st2 key delete \"$ROBOT_KEY\"" assert_success - assert_output "Resource with id \"$ROBOT_KEY\" has been successfully deleted." + assert_line "Resource with id \"$ROBOT_KEY\" has been successfully deleted." KEY_DELETE_RESULTS=$(st2 trigger-instance list --trigger=$TRIGGER_KEY_DELETE -n 1 -j) run eval "echo '$KEY_DELETE_RESULTS' | jq -r '.[].trigger'" @@ -215,7 +215,7 @@ KEY_JSON_FILE="docs/test_key_triggers.json" run eval "st2 key delete 1 -j" assert_success - assert_output "Resource with id \"1\" has been successfully deleted." + assert_line "Resource with id \"1\" has been successfully deleted." } @test "key-value operations with expiries" { From 0654ef598323161c80e19f6f94680168883d7b88 Mon Sep 17 00:00:00 2001 From: blag Date: Tue, 1 Dec 2020 21:42:03 -0800 Subject: [PATCH 4/4] Add wheel dependency to install slackclient and wrapt --- test-requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test-requirements.txt b/test-requirements.txt index f52424b..10ae68f 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -20,4 +20,5 @@ traceback2==1.4.0 unittest2==1.1.0 urllib3==1.24.2 websocket-client==0.54.0 +wheel==0.36.0 wrapt==1.11.1