|
| 1 | +# Copyright 2021 The StackStorm Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import absolute_import |
| 16 | + |
| 17 | +import mock |
| 18 | + |
| 19 | +from http_runner import http_runner |
| 20 | +from python_runner import python_runner |
| 21 | +from orquesta_runner import orquesta_runner |
| 22 | + |
| 23 | +import st2tests |
| 24 | + |
| 25 | +import st2tests.config as tests_config |
| 26 | + |
| 27 | +tests_config.parse_args() |
| 28 | + |
| 29 | +from st2common.bootstrap import actionsregistrar |
| 30 | +from st2common.bootstrap import runnersregistrar |
| 31 | +from st2common.constants import action as ac_const |
| 32 | +from st2common.constants import secrets as secrets_const |
| 33 | +from st2common.models.api import execution as ex_api_models |
| 34 | +from st2common.models.db import liveaction as lv_db_models |
| 35 | +from st2common.services import action as action_service |
| 36 | +from st2common.transport import liveaction as lv_ac_xport |
| 37 | +from st2common.transport import publishers |
| 38 | +from st2tests.mocks import liveaction as mock_lv_ac_xport |
| 39 | + |
| 40 | + |
| 41 | +PACKS = [ |
| 42 | + st2tests.fixturesloader.get_fixtures_packs_base_path() + "/dummy_pack_1", |
| 43 | + st2tests.fixturesloader.get_fixtures_packs_base_path() + "/orquesta_tests", |
| 44 | +] |
| 45 | + |
| 46 | +MOCK_PYTHON_ACTION_RESULT = { |
| 47 | + "stderr": "", |
| 48 | + "stdout": "", |
| 49 | + "result": {"k1": "foobar", "k2": "shhhh!"}, |
| 50 | + "exit_code": 0, |
| 51 | +} |
| 52 | + |
| 53 | +MOCK_PYTHON_RUNNER_OUTPUT = ( |
| 54 | + ac_const.LIVEACTION_STATUS_SUCCEEDED, |
| 55 | + MOCK_PYTHON_ACTION_RESULT, |
| 56 | + None, |
| 57 | +) |
| 58 | + |
| 59 | +MOCK_HTTP_ACTION_RESULT = { |
| 60 | + "status_code": 200, |
| 61 | + "body": {"k1": "foobar", "k2": "shhhh!"}, |
| 62 | +} |
| 63 | + |
| 64 | +MOCK_HTTP_RUNNER_OUTPUT = ( |
| 65 | + ac_const.LIVEACTION_STATUS_SUCCEEDED, |
| 66 | + MOCK_HTTP_ACTION_RESULT, |
| 67 | + None, |
| 68 | +) |
| 69 | + |
| 70 | +MOCK_ORQUESTA_ACTION_RESULT = { |
| 71 | + "errors": [], |
| 72 | + "output": {"a6": "foobar", "b6": "foobar", "a7": "foobar", "b7": "shhhh!"}, |
| 73 | +} |
| 74 | + |
| 75 | +MOCK_ORQUESTA_RUNNER_OUTPUT = ( |
| 76 | + ac_const.LIVEACTION_STATUS_SUCCEEDED, |
| 77 | + MOCK_ORQUESTA_ACTION_RESULT, |
| 78 | + None, |
| 79 | +) |
| 80 | + |
| 81 | + |
| 82 | +@mock.patch.object( |
| 83 | + publishers.CUDPublisher, "publish_update", mock.MagicMock(return_value=None) |
| 84 | +) |
| 85 | +@mock.patch.object( |
| 86 | + publishers.CUDPublisher, |
| 87 | + "publish_create", |
| 88 | + mock.MagicMock(side_effect=mock_lv_ac_xport.MockLiveActionPublisher.publish_create), |
| 89 | +) |
| 90 | +@mock.patch.object( |
| 91 | + lv_ac_xport.LiveActionPublisher, |
| 92 | + "publish_state", |
| 93 | + mock.MagicMock(side_effect=mock_lv_ac_xport.MockLiveActionPublisher.publish_state), |
| 94 | +) |
| 95 | +class ActionExecutionOutputSchemaTest(st2tests.ExecutionDbTestCase): |
| 96 | + @classmethod |
| 97 | + def setUpClass(cls): |
| 98 | + super(ActionExecutionOutputSchemaTest, cls).setUpClass() |
| 99 | + |
| 100 | + # Register runners. |
| 101 | + runnersregistrar.register_runners() |
| 102 | + |
| 103 | + # Register test pack(s). |
| 104 | + actions_registrar = actionsregistrar.ActionsRegistrar( |
| 105 | + use_pack_cache=False, fail_on_failure=True |
| 106 | + ) |
| 107 | + |
| 108 | + for pack in PACKS: |
| 109 | + actions_registrar.register_from_pack(pack) |
| 110 | + |
| 111 | + @mock.patch.object( |
| 112 | + python_runner.PythonRunner, |
| 113 | + "run", |
| 114 | + mock.MagicMock(return_value=MOCK_PYTHON_RUNNER_OUTPUT), |
| 115 | + ) |
| 116 | + def test_python_action(self): |
| 117 | + # Execute a python action with output schema and secret |
| 118 | + lv_ac_db = lv_db_models.LiveActionDB(action="dummy_pack_1.my_py_action") |
| 119 | + lv_ac_db, ac_ex_db = action_service.request(lv_ac_db) |
| 120 | + ac_ex_db = self._wait_on_ac_ex_status( |
| 121 | + ac_ex_db, ac_const.LIVEACTION_STATUS_SUCCEEDED |
| 122 | + ) |
| 123 | + |
| 124 | + # Assert expected output written to the database |
| 125 | + expected_output = {"k1": "foobar", "k2": "shhhh!"} |
| 126 | + self.assertDictEqual(ac_ex_db.result["result"], expected_output) |
| 127 | + |
| 128 | + # Assert expected output on conversion to API model |
| 129 | + ac_ex_api = ex_api_models.ActionExecutionAPI.from_model( |
| 130 | + ac_ex_db, mask_secrets=True |
| 131 | + ) |
| 132 | + expected_masked_output = { |
| 133 | + "k1": "foobar", |
| 134 | + "k2": secrets_const.MASKED_ATTRIBUTE_VALUE, |
| 135 | + } |
| 136 | + self.assertDictEqual(ac_ex_api.result["result"], expected_masked_output) |
| 137 | + |
| 138 | + @mock.patch.object( |
| 139 | + http_runner.HttpRunner, |
| 140 | + "run", |
| 141 | + mock.MagicMock(return_value=MOCK_HTTP_RUNNER_OUTPUT), |
| 142 | + ) |
| 143 | + def test_http_action(self): |
| 144 | + # Execute a http action with output schema and secret |
| 145 | + lv_ac_db = lv_db_models.LiveActionDB(action="dummy_pack_1.my_http_action") |
| 146 | + lv_ac_db, ac_ex_db = action_service.request(lv_ac_db) |
| 147 | + ac_ex_db = self._wait_on_ac_ex_status( |
| 148 | + ac_ex_db, ac_const.LIVEACTION_STATUS_SUCCEEDED |
| 149 | + ) |
| 150 | + |
| 151 | + # Assert expected output written to the database |
| 152 | + expected_output = {"k1": "foobar", "k2": "shhhh!"} |
| 153 | + self.assertDictEqual(ac_ex_db.result["body"], expected_output) |
| 154 | + |
| 155 | + # Assert expected output on conversion to API model |
| 156 | + ac_ex_api = ex_api_models.ActionExecutionAPI.from_model( |
| 157 | + ac_ex_db, mask_secrets=True |
| 158 | + ) |
| 159 | + expected_masked_output = { |
| 160 | + "k1": "foobar", |
| 161 | + "k2": secrets_const.MASKED_ATTRIBUTE_VALUE, |
| 162 | + } |
| 163 | + self.assertDictEqual(ac_ex_api.result["body"], expected_masked_output) |
| 164 | + |
| 165 | + @mock.patch.object( |
| 166 | + orquesta_runner.OrquestaRunner, |
| 167 | + "run", |
| 168 | + mock.MagicMock(return_value=MOCK_ORQUESTA_RUNNER_OUTPUT), |
| 169 | + ) |
| 170 | + def test_orquesta_action(self): |
| 171 | + wf_input = "foobar" |
| 172 | + |
| 173 | + # Execute an orquesta action with output schema and secret |
| 174 | + lv_ac_db = lv_db_models.LiveActionDB( |
| 175 | + action="orquesta_tests.data-flow", parameters={"a1": wf_input} |
| 176 | + ) |
| 177 | + lv_ac_db, ac_ex_db = action_service.request(lv_ac_db) |
| 178 | + ac_ex_db = self._wait_on_ac_ex_status( |
| 179 | + ac_ex_db, ac_const.LIVEACTION_STATUS_SUCCEEDED |
| 180 | + ) |
| 181 | + |
| 182 | + # Assert expected output written to the database |
| 183 | + expected_output = { |
| 184 | + "a6": wf_input, |
| 185 | + "b6": wf_input, |
| 186 | + "a7": wf_input, |
| 187 | + "b7": "shhhh!", |
| 188 | + } |
| 189 | + self.assertDictEqual(ac_ex_db.result["output"], expected_output) |
| 190 | + |
| 191 | + # Assert expected output on conversion to API model |
| 192 | + ac_ex_api = ex_api_models.ActionExecutionAPI.from_model( |
| 193 | + ac_ex_db, mask_secrets=True |
| 194 | + ) |
| 195 | + expected_masked_output = { |
| 196 | + "a6": wf_input, |
| 197 | + "b6": wf_input, |
| 198 | + "a7": wf_input, |
| 199 | + "b7": secrets_const.MASKED_ATTRIBUTE_VALUE, |
| 200 | + } |
| 201 | + self.assertDictEqual(ac_ex_api.result["output"], expected_masked_output) |
0 commit comments