Skip to content

Commit

Permalink
Account for double slash in legacy bin paths rockstor#2757
Browse files Browse the repository at this point in the history
in rockstor#2558, we implemented
an automatic conversion of legacy paths to poetry bin paths for rockstor
scripts. This, however, failed for legacy paths that somehow included
double slashes.

This commit updates the pattern used to recognize this legacy paths so
that those with double slashes are also converted to poetry paths.

Includes 1 new unit test to cover underlying function.
Includes black formatting.
  • Loading branch information
FroggyFlox committed Dec 20, 2023
1 parent 0f7f997 commit 4a03ea5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/rockstor/scripts/initrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def establish_poetry_paths():
The local files in questions are defined in the LOCAL_FILES constant.
"""
logger.info("### BEGIN Establishing poetry path to binaries in local files...")
pattern = "/opt/rockstor/bin/"
pattern = "/opt/rockstor[/]+bin/"
replacement = "/opt/rockstor/.venv/bin/"
for local_file in LOCAL_FILES:
if os.path.isfile(LOCAL_FILES[local_file].path):
Expand Down
93 changes: 91 additions & 2 deletions src/rockstor/system/tests/test_osi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"""
import operator
import unittest
from unittest.mock import patch
from unittest.mock import patch, mock_open, call

from system.exceptions import CommandException
from system.osi import get_dev_byid_name, Disk, scan_disks, get_byid_name_map, run_command
from system.osi import get_dev_byid_name, Disk, scan_disks, get_byid_name_map, run_command, replace_pattern_inline


class Pool(object):
Expand Down Expand Up @@ -2004,3 +2004,92 @@ def test_run_command(self):
f"returned = {returned}.\n"
f"expected = {expected}.",
)

def test_replace_pattern_inline(self):
"""
This function is only used during initrock.establish_poetry_paths() for now
so test using 'pattern' and 'replacement' as used during that call.
"""
# Define vars
source_file = "/path/to/fake/source"
target_file = "/path/to/fake/target"
# Make sure `pattern` and `replacement` below match
# those defined in initrock.establish_poetry_paths()
# for this test to be meaningful
pattern = "/opt/rockstor[/]+bin/"
replacement = "/opt/rockstor/.venv/bin/"

source_contents = []
target_contents = []
outputs = []

# /etc/samba/smb.conf, True
source_contents.append(
' root preexec = "/opt/rockstor/bin/mnt-share test_share01"'
)
target_contents.append(
' root preexec = "/opt/rockstor/.venv/bin/mnt-share test_share01"'
)
outputs.append(True)

# /etc/cron.d/rockstortab, True
source_contents.append(
'42 3 * * 6 root /opt/rockstor/bin/st-system-power 1 \*-*-*-*-*-*'
)
target_contents.append(
'42 3 * * 6 root /opt/rockstor/.venv/bin/st-system-power 1 \*-*-*-*-*-*'
)
outputs.append(True)

# /etc/samba/smb.conf, False
source_contents.append(
' root preexec = "/opt/rockstor/.venv/bin/mnt-share test_share01"'
)
target_contents.append(None)
outputs.append(False)

# /etc/cron.d/rockstortab, False
source_contents.append(
'42 3 * * 6 root /opt/rockstor/.venv/bin/st-system-power 1 \*-*-*-*-*-*'
)
target_contents.append(None)
outputs.append(False)

# /etc/cron.d/rockstortab, True
source_contents.append(
'42 3 * * 5 root /opt/rockstor//bin/st-pool-scrub 1 \*-*-*-*-*-*'
)
target_contents.append(
'42 3 * * 5 root /opt/rockstor/.venv/bin/st-pool-scrub 1 \*-*-*-*-*-*'
)
outputs.append(True)

for content, target, out in zip(source_contents, target_contents, outputs):
m = mock_open(read_data=content)
with patch("system.osi.open", m):
returned = replace_pattern_inline(
source_file, target_file, pattern, replacement
)

# Test that open() was called twice
# (once for source_file, once for target_file)
self.assertEqual(m.call_count, 2)
calls = [
call(source_file),
call(target_file, "w"),
]
m.assert_has_calls(calls, any_order=True)

if target is not None:
# Write should be called
m().write.assert_has_calls([call(target)])

# Test that the correct boolean is returned
expected = out
self.assertEqual(
returned,
expected,
msg="Un-expected replace_pattern_inline() output:\n"
f"returned = {returned}.\n"
f"expected = {expected}.",
)

0 comments on commit 4a03ea5

Please sign in to comment.