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", 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..909c11d 100644 --- a/packs/tests/actions/chains/test_run_pack_tests_tool.yaml +++ b/packs/tests/actions/chains/test_run_pack_tests_tool.yaml @@ -4,6 +4,7 @@ vars: # 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_with_version: "xml=0.3.0" test_timeout: 180 chain: @@ -18,8 +19,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 @@ -29,10 +39,19 @@ 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 + + - + name: test_installed_pack_2_version + ref: tests.test_installed_pack_version + params: + installed_pack: "{{ pack_to_install_2_with_version }}" 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