]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
test_ansible: Use mock_open()
authorZack Cerza <zack@redhat.com>
Wed, 5 Jul 2023 23:07:32 +0000 (17:07 -0600)
committerZack Cerza <zack@redhat.com>
Wed, 5 Jul 2023 23:45:23 +0000 (17:45 -0600)
This provides a more complete interface than what we were constructing.

Signed-off-by: Zack Cerza <zack@redhat.com>
teuthology/test/task/test_ansible.py

index eb98fc378db072e985a8c216573fab4f1d518a2d..5daf55cdc0fc77a5ffcc1b2c2ce555277922676e 100644 (file)
@@ -2,7 +2,7 @@ import json
 import os
 import yaml
 
-from mock import patch, DEFAULT, Mock
+from unittest.mock import patch, DEFAULT, Mock, mock_open
 from pytest import raises, mark
 from teuthology.util.compat import PY3
 if PY3:
@@ -364,11 +364,7 @@ class TestAnsibleTask(TestTask):
         task = self.klass(self.ctx, self.task_config)
         task.setup()
         with patch.object(ansible.pexpect, 'run') as m_run:
-            with patch('teuthology.task.ansible.open') as m_open:
-                fake_failure_log = Mock()
-                fake_failure_log.__enter__ = Mock()
-                fake_failure_log.__exit__ = Mock()
-                m_open.return_value = fake_failure_log
+            with patch('teuthology.task.ansible.open', mock_open()):
                 m_run.return_value = ('', 1)
                 with raises(CommandFailedError):
                     task.execute_playbook()
@@ -584,11 +580,7 @@ class TestCephLabTask(TestAnsibleTask):
         task.ctx.summary = dict()
         task.setup()
         with patch.object(ansible.pexpect, 'run') as m_run:
-            with patch('teuthology.task.ansible.open') as m_open:
-                fake_failure_log = Mock()
-                fake_failure_log.__enter__ = Mock()
-                fake_failure_log.__exit__ = Mock()
-                m_open.return_value = fake_failure_log
+            with patch('teuthology.task.ansible.open', mock_open()):
                 m_run.return_value = ('', 1)
                 with raises(CommandFailedError):
                     task.execute_playbook()
@@ -599,11 +591,7 @@ class TestCephLabTask(TestAnsibleTask):
         task = self.klass(self.ctx, self.task_config)
         task.setup()
         with patch.object(ansible.pexpect, 'run') as m_run:
-            with patch('teuthology.task.ansible.open') as m_open:
-                fake_failure_log = Mock()
-                fake_failure_log.__enter__ = Mock()
-                fake_failure_log.__exit__ = Mock()
-                m_open.return_value = fake_failure_log
+            with patch('teuthology.task.ansible.open', mock_open()):
                 m_run.return_value = ('', 1)
                 with raises(CommandFailedError):
                     task.execute_playbook()