Browse Source

Train-only make tripleo_diff_exec py2-compatible

Train still supports centos-7 and subprocess.run() did not exist
there.

Tested on train on both cents 8 and 7.

Change-Id: I0b1a288fb3b1ae8d748690e4415a5c58ce793595
Closes-Bug: #1917422
changes/46/778146/4
Michele Baldessari 2 months ago
committed by ramishra
parent
commit
2c9575d4e8
2 changed files with 14 additions and 10 deletions
  1. +5
    -4
      tripleo_ansible/ansible_plugins/modules/tripleo_diff_exec.py
  2. +9
    -6
      tripleo_ansible/tests/modules/test_tripleo_diff_exec.py

+ 5
- 4
tripleo_ansible/ansible_plugins/modules/tripleo_diff_exec.py View File

@ -111,9 +111,10 @@ def run(module):
try:
tmp_environment = os.environ.copy()
tmp_environment.update(environment)
r = subprocess.run(command, shell=True, env=tmp_environment,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
r = subprocess.Popen(command, shell=True, env=tmp_environment,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
cmd_stdout, cmd_stderr = r.communicate()
if r.returncode in return_codes:
results['changed'] = True
# copy old to bkup
@ -123,7 +124,7 @@ def run(module):
results['error'] = "Failed running command"
results['msg'] = ("Error running %s. rc: %s, stdout: %s, "
"stderr: %s" % (command, r.returncode,
r.stdout, r.stderr))
cmd_stdout, cmd_stderr))
except Exception as e:
results['failed'] = True
results['error'] = traceback.format_exc()


+ 9
- 6
tripleo_ansible/tests/modules/test_tripleo_diff_exec.py View File

@ -22,7 +22,7 @@ import mock
class TestTripleoDiffExec(tests_base.TestCase):
@mock.patch.dict('os.environ', dict(), clear=True)
@mock.patch('shutil.copy2')
@mock.patch('subprocess.run')
@mock.patch('subprocess.Popen')
@mock.patch('filecmp.cmp')
@mock.patch('os.path.exists')
def test_first_run(self, mock_exists, mock_cmp, mock_run, mock_copy2):
@ -39,6 +39,7 @@ class TestTripleoDiffExec(tests_base.TestCase):
mock_module.exit_json = mock_exit
mock_return = mock.MagicMock()
mock_return.returncode = 0
mock_return.communicate.return_value = (-1, -1)
mock_run.return_value = mock_return
tripleo_diff_exec.run(mock_module)
mock_exit.assert_called_once_with(changed=True)
@ -49,7 +50,7 @@ class TestTripleoDiffExec(tests_base.TestCase):
@mock.patch.dict('os.environ', dict(), clear=True)
@mock.patch('shutil.copy2')
@mock.patch('subprocess.run')
@mock.patch('subprocess.Popen')
@mock.patch('filecmp.cmp')
@mock.patch('os.path.exists')
def test_no_change(self, mock_exists, mock_cmp, mock_run, mock_copy2):
@ -71,7 +72,7 @@ class TestTripleoDiffExec(tests_base.TestCase):
@mock.patch.dict('os.environ', dict(), clear=True)
@mock.patch('shutil.copy2')
@mock.patch('subprocess.run')
@mock.patch('subprocess.Popen')
@mock.patch('filecmp.cmp')
@mock.patch('os.path.exists')
def test_file_changed(self, mock_exists, mock_cmp, mock_run, mock_copy2):
@ -88,6 +89,7 @@ class TestTripleoDiffExec(tests_base.TestCase):
mock_module.exit_json = mock_exit
mock_return = mock.MagicMock()
mock_return.returncode = 0
mock_return.communicate.return_value = (-1, -1)
mock_run.return_value = mock_return
tripleo_diff_exec.run(mock_module)
mock_run.assert_called_once_with(
@ -98,7 +100,7 @@ class TestTripleoDiffExec(tests_base.TestCase):
@mock.patch.dict('os.environ', dict(), clear=True)
@mock.patch('shutil.copy2')
@mock.patch('subprocess.run')
@mock.patch('subprocess.Popen')
@mock.patch('filecmp.cmp')
@mock.patch('os.path.exists')
def test_missing_state(self, mock_exists, mock_cmp, mock_run, mock_copy2):
@ -121,7 +123,7 @@ class TestTripleoDiffExec(tests_base.TestCase):
@mock.patch.dict('os.environ', dict(), clear=True)
@mock.patch('shutil.copy2')
@mock.patch('subprocess.run')
@mock.patch('subprocess.Popen')
@mock.patch('filecmp.cmp')
@mock.patch('os.path.exists')
def test_exec_exception(self, mock_exists, mock_cmp, mock_run, mock_copy2):
@ -144,7 +146,7 @@ class TestTripleoDiffExec(tests_base.TestCase):
@mock.patch.dict('os.environ', dict(), clear=True)
@mock.patch('shutil.copy2')
@mock.patch('subprocess.run')
@mock.patch('subprocess.Popen')
@mock.patch('filecmp.cmp')
@mock.patch('os.path.exists')
def test_exec_failed(self, mock_exists, mock_cmp, mock_run, mock_copy2):
@ -160,6 +162,7 @@ class TestTripleoDiffExec(tests_base.TestCase):
mock_module.exit_json = mock_exit
mock_return = mock.MagicMock()
mock_return.returncode = 1
mock_return.communicate.return_value = ('out', 'err')
mock_return.stdout = 'out'
mock_return.stderr = 'err'
mock_run.return_value = mock_return


Loading…
Cancel
Save