Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TestOsOpsCommon is added [generic os_ops tests] #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prev Previous commit
Next Next commit
TestOsOpsCommon is updated (+LocalOperations::is_executable)
New tests:
- test_is_executable_true
- test_is_executable_false
- test_makedirs_and_rmdirs_success
- test_makedirs_failure

LocalOperations::is_executable is corrected
  • Loading branch information
dmitry-lipetsk committed Apr 2, 2025
commit 23cb7616ec23d7050db257913f7f5ee84bbf80cd
3 changes: 2 additions & 1 deletion testgres/operations/local_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ def find_executable(self, executable):

def is_executable(self, file):
# Check if the file is executable
return os.stat(file).st_mode & stat.S_IXUSR
assert stat.S_IXUSR != 0
return (os.stat(file).st_mode & stat.S_IXUSR) == stat.S_IXUSR

def set_env(self, var_name, var_val):
# Check if the directory is already in PATH
Expand Down
60 changes: 60 additions & 0 deletions tests/test_os_ops_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,66 @@ def test_exec_command_failure__expect_error(self, os_ops: OsOperations):
assert b"nonexistent_command" in error
assert b"not found" in error

def test_is_executable_true(self, os_ops: OsOperations):
"""
Test is_executable for an existing executable.
"""
assert isinstance(os_ops, OsOperations)

RunConditions.skip_if_windows()

response = os_ops.is_executable("/bin/sh")

assert response is True

def test_is_executable_false(self, os_ops: OsOperations):
"""
Test is_executable for a non-executable.
"""
assert isinstance(os_ops, OsOperations)

response = os_ops.is_executable(__file__)

assert response is False

def test_makedirs_and_rmdirs_success(self, os_ops: OsOperations):
"""
Test makedirs and rmdirs for successful directory creation and removal.
"""
assert isinstance(os_ops, OsOperations)

RunConditions.skip_if_windows()

cmd = "pwd"
pwd = os_ops.exec_command(cmd, wait_exit=True, encoding='utf-8').strip()

path = "{}/test_dir".format(pwd)

# Test makedirs
os_ops.makedirs(path)
assert os.path.exists(path)
assert os_ops.path_exists(path)

# Test rmdirs
os_ops.rmdirs(path)
assert not os.path.exists(path)
assert not os_ops.path_exists(path)

def test_makedirs_failure(self, os_ops: OsOperations):
"""
Test makedirs for failure.
"""
# Try to create a directory in a read-only location
assert isinstance(os_ops, OsOperations)

RunConditions.skip_if_windows()

path = "/root/test_dir"

# Test makedirs
with pytest.raises(Exception):
os_ops.makedirs(path)

def test_listdir(self, os_ops: OsOperations):
"""
Test listdir for listing directory contents.
Expand Down
54 changes: 0 additions & 54 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

from ..testgres import ExecUtilException
from ..testgres import RemoteOperations
from ..testgres import LocalOperations
from ..testgres import ConnectionParams
from ..testgres import utils as testgres_utils


class TestRemoteOperations:
Expand All @@ -20,58 +18,6 @@ def setup(self):
ssh_key=os.getenv('RDBMS_TESTPOOL_SSHKEY'))
self.operations = RemoteOperations(conn_params)

def test_is_executable_true(self):
"""
Test is_executable for an existing executable.
"""
local_ops = LocalOperations()
cmd = testgres_utils.get_bin_path2(local_ops, "pg_config")
cmd = local_ops.exec_command([cmd, "--bindir"], encoding="utf-8")
cmd = cmd.rstrip()
cmd = os.path.join(cmd, "pg_config")
response = self.operations.is_executable(cmd)

assert response is True

def test_is_executable_false(self):
"""
Test is_executable for a non-executable.
"""
cmd = "python"
response = self.operations.is_executable(cmd)

assert response is False

def test_makedirs_and_rmdirs_success(self):
"""
Test makedirs and rmdirs for successful directory creation and removal.
"""
cmd = "pwd"
pwd = self.operations.exec_command(cmd, wait_exit=True, encoding='utf-8').strip()

path = "{}/test_dir".format(pwd)

# Test makedirs
self.operations.makedirs(path)
assert os.path.exists(path)
assert self.operations.path_exists(path)

# Test rmdirs
self.operations.rmdirs(path)
assert not os.path.exists(path)
assert not self.operations.path_exists(path)

def test_makedirs_failure(self):
"""
Test makedirs for failure.
"""
# Try to create a directory in a read-only location
path = "/root/test_dir"

# Test makedirs
with pytest.raises(Exception):
self.operations.makedirs(path)

def test_mkdtemp__default(self):
path = self.operations.mkdtemp()
logging.info("Path is [{0}].".format(path))
Expand Down