From 6fda346d0783a30e62f58738f8942da286ba84e5 Mon Sep 17 00:00:00 2001 From: blag Date: Mon, 1 Jul 2019 17:48:28 -0700 Subject: [PATCH 1/3] Add a test for bad arguments to the pack alias --- .../actions/test_aliases_with_slack.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/packs/chatops_tests/actions/test_aliases_with_slack.py b/packs/chatops_tests/actions/test_aliases_with_slack.py index e34ac36..2bf2e85 100644 --- a/packs/chatops_tests/actions/test_aliases_with_slack.py +++ b/packs/chatops_tests/actions/test_aliases_with_slack.py @@ -245,6 +245,49 @@ def test_run_command_on_localhost(self): # Drain the event buffer self.client.rtm_read() + def test_run_command_on_localhost_with_bad_argument(self): + post_message_response = self.client.api_call( + "chat.postMessage", + channel=self.channel, + text="!pack get pack=example", + as_user=True) + + messages = [] + for i in range(self.WAIT_FOR_MESSAGES_TIMEOUT): + if len(messages) >= 1: + break + time.sleep(1) + + all_messages = self.client.rtm_read() + + filtered_messages = filter(self.filter, all_messages) + + if filtered_messages: + messages.extend(filtered_messages) + + self.assertEqual(1, len(messages)) + if len(messages) != 1: + time.sleep(self.WAIT_FOR_MESSAGES_TIMEOUT) + + # Test for response + self.assertIsNotNone(messages[0].get('bot_id')) + self.assertIsNotNone(messages[0].get('attachments')) + self.assertGreater(len(messages[0]['attachments']), 0) + self.assertIsNotNone(messages[0]['attachments'][0].get('color')) + self.assertEqual(messages[0]['attachments'][0]['color'], 'F35A00') + self.assertIsNotNone(messages[0]['attachments'][0].get('text')) + + # Check the pretext + msg_pretext = messages[0]['attachments'][0]['pretext'] + self.assertRegex(msg_pretext, r"<@{userid}>: I'm sorry, Dave. I'm afraid I can't do that. ".format(userid=self.userid)) + + # Test attachment + msg_text = messages[0]['attachments'][0]['text'] + self.assertRegex(msg_text, r"Command \"pack get pack=example\" doesn't match format string \"pack get \{\{ pack \}\}\"") + + # Drain the event buffer + self.client.rtm_read() + def test_run_exact_command_on_localhost(self): post_message_response = self.client.api_call( "chat.postMessage", From 5e5b2a1887f5d25b0b19300986e479d005bfed42 Mon Sep 17 00:00:00 2001 From: Jinping Han Date: Fri, 26 Jul 2019 18:01:08 -0700 Subject: [PATCH 2/3] Check installed pack version as expected With new improvement code https://github.com/StackStorm/st2/pull/4743, if pack version is not provided, the latest version for pack from index.json is installed. Part of Fixes: https://github.com/StackStorm/discussions/issues/354 Modify existing `e2e` test `tests.test_run_pack_tests_tool` to check: 1. Install pack without version and check if the latest version for pack from index.json is installed. `st2 pack install csv` 2) install pack with version and check if the version specified is the version installed. `st2 pack install xml=0.3.0 --- .../chains/test_run_pack_tests_tool.yaml | 20 ++++++- .../actions/test_installed_pack_version.py | 55 +++++++++++++++++++ .../actions/test_installed_pack_version.yaml | 12 ++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 packs/tests/actions/test_installed_pack_version.py create mode 100644 packs/tests/actions/test_installed_pack_version.yaml diff --git a/packs/tests/actions/chains/test_run_pack_tests_tool.yaml b/packs/tests/actions/chains/test_run_pack_tests_tool.yaml index e976b21..08fddd3 100644 --- a/packs/tests/actions/chains/test_run_pack_tests_tool.yaml +++ b/packs/tests/actions/chains/test_run_pack_tests_tool.yaml @@ -3,7 +3,7 @@ vars: base_repo_url: "https://github.com/StackStorm" # Note: Pack 1 should have no external dependencies beyond Python stdlib ones. pack_to_install_1: "csv" - pack_to_install_2: "xml" + pack_to_install_2: "xml=0.3.0" test_timeout: 180 chain: @@ -18,8 +18,17 @@ chain: ST2_AUTH_TOKEN: "{{token}}" cmd: "st2 pack install {{ pack_to_install_1 }}" timeout: "{{test_timeout}}" + on-success: test_installed_pack_1_version + on-failure: error_handler + + - + name: test_installed_pack_1_version + ref: tests.test_installed_pack_version + params: + installed_pack: "{{ pack_to_install_1 }}" on-success: install_pack_2 on-failure: error_handler + - name: install_pack_2 ref: core.local @@ -31,8 +40,17 @@ chain: ST2_AUTH_TOKEN: "{{token}}" cmd: "st2 pack install {{ pack_to_install_2 }}" timeout: "{{test_timeout}}" + on-success: test_installed_pack_2_version + on-failure: error_handler + + - + name: test_installed_pack_2_version + ref: tests.test_installed_pack_version + params: + installed_pack: "{{ pack_to_install_2 }}" on-success: run_pack_tests_without_creating_virtualenv on-failure: error_handler + - name: run_pack_tests_without_creating_virtualenv ref: core.local diff --git a/packs/tests/actions/test_installed_pack_version.py b/packs/tests/actions/test_installed_pack_version.py new file mode 100644 index 0000000..d90ef63 --- /dev/null +++ b/packs/tests/actions/test_installed_pack_version.py @@ -0,0 +1,55 @@ +# Copyright 2019 Extreme Networks, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from st2common.constants.pack import PACK_VERSION_SEPARATOR +from st2common.runners.base_action import Action +from st2common.util.pack import get_pack_metadata +from st2common.util.pack_management import get_repo_url + + +class TesetInstalledPackVersionAction(Action): + def run(self, installed_pack): + """ + :param installed_pack: Installed pack name with version + :type: installed_pack: ``string`` + """ + + if not installed_pack: + return False, False + + pack_and_version = installed_pack.split(PACK_VERSION_SEPARATOR) + pack_name = pack_and_version[0] + pack_version = pack_and_version[1] if len(pack_and_version) > 1 else None + + # Pack version is not specified. Get pack version from index.json file. + if not pack_version: + try: + _, pack_version = get_repo_url(pack_name, proxy_config=None) + except Exception: + print ('No record of the "%s" pack in the index.' % (pack_name)) + return False, False + + # Get installed pack version from local pack metadata file. + try: + pack_dir = '/opt/stackstorm/packs/%s/' % (pack_name) + pack_metadata = get_pack_metadata(pack_dir=pack_dir) + local_pack_version = pack_metadata.get('version', None) + except Exception: + print ('Could not open pack.yaml file at location %s' % (pack_dir)) + return False, False + + if pack_version == local_pack_version: + return True, True + else: + return False, False diff --git a/packs/tests/actions/test_installed_pack_version.yaml b/packs/tests/actions/test_installed_pack_version.yaml new file mode 100644 index 0000000..1c47757 --- /dev/null +++ b/packs/tests/actions/test_installed_pack_version.yaml @@ -0,0 +1,12 @@ +--- +name: "test_installed_pack_version" +runner_type: "python-script" +description: "Test installed pack version." +pack: tests +enabled: true +entry_point: "test_installed_pack_version.py" +parameters: + installed_pack: + type: "string" + description: "Name of pack to check" + required: true From 50241126ea917a6905731832723b122470f3fb8f Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Mon, 29 Jul 2019 21:22:44 +0200 Subject: [PATCH 3/3] Fix failure introduced in #176. --- packs/tests/actions/chains/test_run_pack_tests_tool.yaml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packs/tests/actions/chains/test_run_pack_tests_tool.yaml b/packs/tests/actions/chains/test_run_pack_tests_tool.yaml index 08fddd3..909c11d 100644 --- a/packs/tests/actions/chains/test_run_pack_tests_tool.yaml +++ b/packs/tests/actions/chains/test_run_pack_tests_tool.yaml @@ -3,7 +3,8 @@ vars: base_repo_url: "https://github.com/StackStorm" # Note: Pack 1 should have no external dependencies beyond Python stdlib ones. pack_to_install_1: "csv" - pack_to_install_2: "xml=0.3.0" + pack_to_install_2: "xml" + pack_to_install_2_with_version: "xml=0.3.0" test_timeout: 180 chain: @@ -38,7 +39,7 @@ chain: ST2_AUTH_URL: "{{protocol}}://{{hostname}}:9100" ST2_API_URL: "{{protocol}}://{{hostname}}:9101" ST2_AUTH_TOKEN: "{{token}}" - cmd: "st2 pack install {{ pack_to_install_2 }}" + cmd: "st2 pack install {{ pack_to_install_2_with_version }}" timeout: "{{test_timeout}}" on-success: test_installed_pack_2_version on-failure: error_handler @@ -47,7 +48,7 @@ chain: name: test_installed_pack_2_version ref: tests.test_installed_pack_version params: - installed_pack: "{{ pack_to_install_2 }}" + installed_pack: "{{ pack_to_install_2_with_version }}" on-success: run_pack_tests_without_creating_virtualenv on-failure: error_handler