|
|
|
|
@ -18,7 +18,7 @@
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
|
|
|
|
from ansible.executor.task_result import TaskResult
|
|
|
|
|
|
|
|
|
|
@ -29,32 +29,28 @@ class TestTaskResult(unittest.TestCase):
|
|
|
|
|
mock_task = MagicMock()
|
|
|
|
|
|
|
|
|
|
# test loading a result with a dict
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict())
|
|
|
|
|
|
|
|
|
|
# test loading a result with a JSON string
|
|
|
|
|
with patch('ansible.parsing.dataloader.DataLoader.load') as p:
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, '{}')
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, {}, {})
|
|
|
|
|
|
|
|
|
|
def test_task_result_is_changed(self):
|
|
|
|
|
mock_host = MagicMock()
|
|
|
|
|
mock_task = MagicMock()
|
|
|
|
|
|
|
|
|
|
# test with no changed in result
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict())
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, {}, {})
|
|
|
|
|
self.assertFalse(tr.is_changed())
|
|
|
|
|
|
|
|
|
|
# test with changed in the result
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(changed=True))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(changed=True), {})
|
|
|
|
|
self.assertTrue(tr.is_changed())
|
|
|
|
|
|
|
|
|
|
# test with multiple results but none changed
|
|
|
|
|
mock_task.loop = 'foo'
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(foo='bar'), dict(bam='baz'), True]))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(foo='bar'), dict(bam='baz'), True]), {})
|
|
|
|
|
self.assertFalse(tr.is_changed())
|
|
|
|
|
|
|
|
|
|
# test with multiple results and one changed
|
|
|
|
|
mock_task.loop = 'foo'
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(changed=False), dict(changed=True), dict(some_key=False)]))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(changed=False), dict(changed=True), dict(some_key=False)]), {})
|
|
|
|
|
self.assertTrue(tr.is_changed())
|
|
|
|
|
|
|
|
|
|
def test_task_result_is_skipped(self):
|
|
|
|
|
@ -62,35 +58,35 @@ class TestTaskResult(unittest.TestCase):
|
|
|
|
|
mock_task = MagicMock()
|
|
|
|
|
|
|
|
|
|
# test with no skipped in result
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict())
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(), {})
|
|
|
|
|
self.assertFalse(tr.is_skipped())
|
|
|
|
|
|
|
|
|
|
# test with skipped in the result
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(skipped=True))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(skipped=True), {})
|
|
|
|
|
self.assertTrue(tr.is_skipped())
|
|
|
|
|
|
|
|
|
|
# test with multiple results but none skipped
|
|
|
|
|
mock_task.loop = 'foo'
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(foo='bar'), dict(bam='baz'), True]))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(foo='bar'), dict(bam='baz'), True]), {})
|
|
|
|
|
self.assertFalse(tr.is_skipped())
|
|
|
|
|
|
|
|
|
|
# test with multiple results and one skipped
|
|
|
|
|
mock_task.loop = 'foo'
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(skipped=False), dict(skipped=True), dict(some_key=False)]))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(skipped=False), dict(skipped=True), dict(some_key=False)]), {})
|
|
|
|
|
self.assertFalse(tr.is_skipped())
|
|
|
|
|
|
|
|
|
|
# test with multiple results and all skipped
|
|
|
|
|
mock_task.loop = 'foo'
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(skipped=True), dict(skipped=True), dict(skipped=True)]))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(skipped=True), dict(skipped=True), dict(skipped=True)]), {})
|
|
|
|
|
self.assertTrue(tr.is_skipped())
|
|
|
|
|
|
|
|
|
|
# test with multiple squashed results (list of strings)
|
|
|
|
|
# first with the main result having skipped=False
|
|
|
|
|
mock_task.loop = 'foo'
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=["a", "b", "c"], skipped=False))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=["a", "b", "c"], skipped=False), {})
|
|
|
|
|
self.assertFalse(tr.is_skipped())
|
|
|
|
|
# then with the main result having skipped=True
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=["a", "b", "c"], skipped=True))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=["a", "b", "c"], skipped=True), {})
|
|
|
|
|
self.assertTrue(tr.is_skipped())
|
|
|
|
|
|
|
|
|
|
def test_task_result_is_unreachable(self):
|
|
|
|
|
@ -98,21 +94,21 @@ class TestTaskResult(unittest.TestCase):
|
|
|
|
|
mock_task = MagicMock()
|
|
|
|
|
|
|
|
|
|
# test with no unreachable in result
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict())
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, {}, {})
|
|
|
|
|
self.assertFalse(tr.is_unreachable())
|
|
|
|
|
|
|
|
|
|
# test with unreachable in the result
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(unreachable=True))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(unreachable=True), {})
|
|
|
|
|
self.assertTrue(tr.is_unreachable())
|
|
|
|
|
|
|
|
|
|
# test with multiple results but none unreachable
|
|
|
|
|
mock_task.loop = 'foo'
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(foo='bar'), dict(bam='baz'), True]))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(foo='bar'), dict(bam='baz'), True]), {})
|
|
|
|
|
self.assertFalse(tr.is_unreachable())
|
|
|
|
|
|
|
|
|
|
# test with multiple results and one unreachable
|
|
|
|
|
mock_task.loop = 'foo'
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(unreachable=False), dict(unreachable=True), dict(some_key=False)]))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(results=[dict(unreachable=False), dict(unreachable=True), dict(some_key=False)]), {})
|
|
|
|
|
self.assertTrue(tr.is_unreachable())
|
|
|
|
|
|
|
|
|
|
def test_task_result_is_failed(self):
|
|
|
|
|
@ -120,21 +116,21 @@ class TestTaskResult(unittest.TestCase):
|
|
|
|
|
mock_task = MagicMock()
|
|
|
|
|
|
|
|
|
|
# test with no failed in result
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict())
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(), {})
|
|
|
|
|
self.assertFalse(tr.is_failed())
|
|
|
|
|
|
|
|
|
|
# test failed result with rc values (should not matter)
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(rc=0))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(rc=0), {})
|
|
|
|
|
self.assertFalse(tr.is_failed())
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(rc=1))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(rc=1), {})
|
|
|
|
|
self.assertFalse(tr.is_failed())
|
|
|
|
|
|
|
|
|
|
# test with failed in result
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(failed=True))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(failed=True), {})
|
|
|
|
|
self.assertTrue(tr.is_failed())
|
|
|
|
|
|
|
|
|
|
# test with failed_when in result
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(failed_when_result=True))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(failed_when_result=True), {})
|
|
|
|
|
self.assertTrue(tr.is_failed())
|
|
|
|
|
|
|
|
|
|
def test_task_result_no_log(self):
|
|
|
|
|
@ -142,7 +138,7 @@ class TestTaskResult(unittest.TestCase):
|
|
|
|
|
mock_task = MagicMock()
|
|
|
|
|
|
|
|
|
|
# no_log should remove secrets
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(_ansible_no_log=True, secret='DONTSHOWME'))
|
|
|
|
|
tr = TaskResult(mock_host, mock_task, dict(_ansible_no_log=True, secret='DONTSHOWME'), {})
|
|
|
|
|
clean = tr.clean_copy()
|
|
|
|
|
self.assertTrue('secret' not in clean._result)
|
|
|
|
|
|
|
|
|
|
@ -160,7 +156,8 @@ class TestTaskResult(unittest.TestCase):
|
|
|
|
|
attempts=5,
|
|
|
|
|
changed=False,
|
|
|
|
|
foo='bar',
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
task_fields={},
|
|
|
|
|
)
|
|
|
|
|
clean = tr.clean_copy()
|
|
|
|
|
self.assertTrue('retries' in clean._result)
|
|
|
|
|
|