Skip to content

Commit

Permalink
updated several tests that were not returning a result that matches w…
Browse files Browse the repository at this point in the history
…hat st2 is currently outputting, so they were failing with the new schema being applied
  • Loading branch information
rebrowning committed Dec 22, 2023
1 parent 9ddd7f8 commit 012a347
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 23 deletions.
2 changes: 1 addition & 1 deletion contrib/runners/orquesta_runner/tests/unit/test_rerun.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
)
RUNNER_RESULT_SUCCEEDED = (
action_constants.LIVEACTION_STATUS_SUCCEEDED,
{"stdout": "foobar"},
{"stdout": "foobar", "succeeded": True, "failed": False, "stderr": ""},
{},
)

Expand Down
4 changes: 2 additions & 2 deletions contrib/runners/orquesta_runner/tests/unit/test_with_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@

RUNNER_RESULT_RUNNING = (
action_constants.LIVEACTION_STATUS_RUNNING,
{"stdout": "..."},
{"stdout": "...", "stderr": ""},
{},
)

RUNNER_RESULT_SUCCEEDED = (
action_constants.LIVEACTION_STATUS_SUCCEEDED,
{"stdout": "..."},
{"stdout": "...", "stderr": "", "succeeded": True, "failed": False},
{},
)

Expand Down
37 changes: 37 additions & 0 deletions st2actions/tests/unit/test_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,43 @@ def test_apply(self):
FakeConcurrencyApplicator.apply_after.assert_called_once_with(liveaction)
RaiseExceptionApplicator.apply_after.assert_called_once_with(liveaction)

@mock.patch.object(
FakeConcurrencyApplicator,
"apply_before",
mock.MagicMock(
side_effect=FakeConcurrencyApplicator(None, None, threshold=3).apply_before
),
)
@mock.patch.object(
RaiseExceptionApplicator,
"apply_before",
mock.MagicMock(side_effect=RaiseExceptionApplicator(None, None).apply_before),
)
@mock.patch.object(
FakeConcurrencyApplicator,
"apply_after",
mock.MagicMock(
side_effect=FakeConcurrencyApplicator(None, None, threshold=3).apply_after
),
)
@mock.patch.object(
RaiseExceptionApplicator,
"apply_after",
mock.MagicMock(side_effect=RaiseExceptionApplicator(None, None).apply_after),
)
def test_apply_with_dict(self):
liveaction = LiveActionDB(
action="wolfpack.action-1", parameters={"actionstr": "dict_resp"}
)
liveaction, _ = action_service.request(liveaction)
liveaction = self._wait_on_status(
liveaction, action_constants.LIVEACTION_STATUS_SUCCEEDED
)
FakeConcurrencyApplicator.apply_before.assert_called_once_with(liveaction)
RaiseExceptionApplicator.apply_before.assert_called_once_with(liveaction)
FakeConcurrencyApplicator.apply_after.assert_called_once_with(liveaction)
RaiseExceptionApplicator.apply_after.assert_called_once_with(liveaction)

@mock.patch.object(
FakeConcurrencyApplicator, "get_threshold", mock.MagicMock(return_value=0)
)
Expand Down
40 changes: 23 additions & 17 deletions st2api/tests/unit/controllers/v1/test_executions.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def test_get_one_max_result_size_query_parameter(self):

# Update it with the result (this populates result and result size attributes)
data = {
"result": {"fooo": "a" * 1000},
"result": {"stdout": {"fooo": "a" * 1000}, "succeeded": True, "failed": False, "stderr": ""},
"status": "succeeded",
}
actual_result_size = len(json_encode(data["result"]))
Expand Down Expand Up @@ -1281,16 +1281,16 @@ def test_put_status_and_result(self):
self.assertEqual(post_resp.status_int, 201)

execution_id = self._get_actionexecution_id(post_resp)
updates = {"status": "succeeded", "result": {"stdout": "foobar"}}
updates = {"status": "succeeded", "result": {"stdout": "foobar", "succeeded": True, "failed": False, "stderr": ""}}
put_resp = self._do_put(execution_id, updates)
self.assertEqual(put_resp.status_int, 200)
self.assertEqual(put_resp.json["status"], "succeeded")
self.assertDictEqual(put_resp.json["result"], {"stdout": "foobar"})
self.assertDictEqual(put_resp.json["result"], {"stdout": "foobar", "succeeded": True, "failed": False, "stderr": ""})

get_resp = self._do_get_one(execution_id)
self.assertEqual(get_resp.status_int, 200)
self.assertEqual(get_resp.json["status"], "succeeded")
self.assertDictEqual(get_resp.json["result"], {"stdout": "foobar"})
self.assertDictEqual(get_resp.json["result"], {"stdout": "foobar", "succeeded": True, "failed": False, "stderr": ""})

def test_put_bad_state(self):
post_resp = self._do_post(LIVE_ACTION_1)
Expand Down Expand Up @@ -1329,11 +1329,11 @@ def test_put_status_to_completed_execution(self):
self.assertEqual(post_resp.status_int, 201)

execution_id = self._get_actionexecution_id(post_resp)
updates = {"status": "succeeded", "result": {"stdout": "foobar"}}
updates = {"status": "succeeded", "result": {"stdout": "foobar", "succeeded": True, "failed": False, "stderr": ""}}
put_resp = self._do_put(execution_id, updates)
self.assertEqual(put_resp.status_int, 200)
self.assertEqual(put_resp.json["status"], "succeeded")
self.assertDictEqual(put_resp.json["result"], {"stdout": "foobar"})
self.assertDictEqual(put_resp.json["result"], {"stdout": "foobar", "succeeded": True, "failed": False, "stderr": ""})

updates = {"status": "abandoned"}
put_resp = self._do_put(execution_id, updates, expect_errors=True)
Expand All @@ -1345,7 +1345,7 @@ def test_put_execution_missing_liveaction(self):
self.assertEqual(post_resp.status_int, 201)

execution_id = self._get_actionexecution_id(post_resp)
updates = {"status": "succeeded", "result": {"stdout": "foobar"}}
updates = {"status": "succeeded", "result": {"stdout": "foobar", "succeeded": True, "failed": False, "stderr": ""}}
put_resp = self._do_put(execution_id, updates, expect_errors=True)
self.assertEqual(put_resp.status_int, 500)

Expand Down Expand Up @@ -1612,22 +1612,22 @@ def test_get_raw_result(self):
execution_id = self._get_actionexecution_id(get_resp)
updates = {
"status": "succeeded",
"result": {"stdout": "foobar", "stderr": "barfoo"},
"result": {"stdout": "foobar", "stderr": "barfoo", "succeeded": True, "failed": False},
}
put_resp = self._do_put(execution_id, updates)
self.assertEqual(put_resp.status_int, 200)
self.assertEqual(put_resp.json["status"], "succeeded")
self.assertDictEqual(
put_resp.json["result"], {"stdout": "foobar", "stderr": "barfoo"}
put_resp.json["result"], {"stdout": "foobar", "stderr": "barfoo", "succeeded": True, "failed": False}
)
self.assertEqual(
put_resp.json["result_size"], len('{"stdout":"foobar","stderr":"barfoo"}')
put_resp.json["result_size"], len('{"stdout":"foobar","stderr":"barfoo","succeeded":true,"failed":false}')
)

# 1. download=False, compress=False, pretty_format=False
get_resp = self.app.get("/v1/executions/%s/result" % (execution_id))
self.assertEqual(get_resp.headers["Content-Type"], "text/json")
self.assertEqual(get_resp.body, b'{"stdout":"foobar","stderr":"barfoo"}')
self.assertEqual(get_resp.body, b'{"stdout":"foobar","stderr":"barfoo","succeeded":true,"failed":false}')

# 2. download=False, compress=False, pretty_format=True
get_resp = self.app.get(
Expand All @@ -1636,7 +1636,9 @@ def test_get_raw_result(self):
expected_result = b"""
{
"stdout": "foobar",
"stderr": "barfoo"
"stderr": "barfoo",
"succeeded": true,
"failed": false
}""".strip()
self.assertEqual(get_resp.headers["Content-Type"], "text/json")
self.assertEqual(get_resp.body, expected_result)
Expand All @@ -1645,7 +1647,7 @@ def test_get_raw_result(self):
# NOTE: webtest auto decompresses the result
get_resp = self.app.get("/v1/executions/%s/result?compress=1" % (execution_id))
self.assertEqual(get_resp.headers["Content-Type"], "application/x-gzip")
self.assertEqual(get_resp.body, b'{"stdout":"foobar","stderr":"barfoo"}')
self.assertEqual(get_resp.body, b'{"stdout":"foobar","stderr":"barfoo","succeeded":true,"failed":false}')

# 4. download=True, compress=False, pretty_format=False
get_resp = self.app.get("/v1/executions/%s/result?download=1" % (execution_id))
Expand All @@ -1654,7 +1656,7 @@ def test_get_raw_result(self):
get_resp.headers["Content-Disposition"],
"attachment; filename=execution_%s_result.json" % (execution_id),
)
self.assertEqual(get_resp.body, b'{"stdout":"foobar","stderr":"barfoo"}')
self.assertEqual(get_resp.body, b'{"stdout":"foobar","stderr":"barfoo","succeeded":true,"failed":false}')

# 5. download=True, compress=False, pretty_format=True
get_resp = self.app.get(
Expand All @@ -1663,7 +1665,9 @@ def test_get_raw_result(self):
expected_result = b"""
{
"stdout": "foobar",
"stderr": "barfoo"
"stderr": "barfoo",
"succeeded": true,
"failed": false
}""".strip()

self.assertEqual(get_resp.headers["Content-Type"], "text/json")
Expand All @@ -1681,7 +1685,9 @@ def test_get_raw_result(self):
expected_result = b"""
{
"stdout": "foobar",
"stderr": "barfoo"
"stderr": "barfoo",
"succeeded": true,
"failed": false
}""".strip()

self.assertEqual(get_resp.headers["Content-Type"], "application/x-gzip")
Expand Down Expand Up @@ -1774,7 +1780,7 @@ def test_get_single_attribute_success(self):

data = {}
data["status"] = action_constants.LIVEACTION_STATUS_SUCCEEDED
data["result"] = {"foo": "bar"}
data["result"] = {"foo": "bar", "stdout": "hello world", "stderr": "", "succeeded": True, "failed": False}

resp = self.app.put_json("/v1/executions/%s" % (exec_id), data)
self.assertEqual(resp.status_int, 200)
Expand Down
2 changes: 1 addition & 1 deletion st2common/tests/unit/services/test_workflow_rerun.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
RUNNER_RESULT_FAILED = (action_constants.LIVEACTION_STATUS_FAILED, {}, {})
RUNNER_RESULT_SUCCEEDED = (
action_constants.LIVEACTION_STATUS_SUCCEEDED,
{"stdout": "foobar"},
{"stdout": "foobar", "succeeded": True, "failed": False, "stderr": ""},
{},
)

Expand Down
18 changes: 16 additions & 2 deletions st2tests/st2tests/mocks/runners/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def get_runner(config=None):
class MockActionRunner(ActionRunner):
def __init__(self):
super(MockActionRunner, self).__init__(runner_id="1")

self.pre_run_called = False
self.run_called = False
self.post_run_called = False
Expand All @@ -45,7 +44,22 @@ def run(self, action_params):
if self.runner_parameters.get("raise", False):
raise Exception("Raise required.")

default_result = {"ran": True, "action_params": action_params}
default_result = {
"ran": True,
"action_params": action_params,
"failed": False,
"stdout": "res",
"stderr": "",
"succeeded": True
}
if action_params.get("actionstr", "") == "dict_resp":
default_result["stdout"] = {
"key": "value",
"key2": {
"sk1": "v1"
}
}

default_context = {"third_party_system": {"ref_id": "1234"}}

status = self.runner_parameters.get("mock_status", LIVEACTION_STATUS_SUCCEEDED)
Expand Down

0 comments on commit 012a347

Please sign in to comment.