Skip to content

Commit

Permalink
tests: improve handling of temporary directories
Browse files Browse the repository at this point in the history
Use tempfile.TemporaryDirectory() to manage temporary directories in
tests. This will help with testing on Python 3.10+.

Also misc. cleaning and improvements with temporary files to avoid
false negatives and reduce warnings.
  • Loading branch information
thiell committed Sep 17, 2023
1 parent 8c0930f commit 628f982
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 257 deletions.
26 changes: 13 additions & 13 deletions tests/CLIClushTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,10 @@ def run(self):
def test_027_warn_shell_globbing_nodes(self):
"""test clush warning on shell globbing (-w)"""
tdir = make_temp_dir()
tfile = open(os.path.join(tdir, HOSTNAME), "w")
tfile = open(os.path.join(tdir.name, HOSTNAME), "w")
curdir = os.getcwd()
try:
os.chdir(tdir)
os.chdir(tdir.name)
s = "Warning: using '-w %s' and local path '%s' exists, was it " \
"expanded by the shell?\n" % (HOSTNAME, HOSTNAME)
self._clush_t(["-w", HOSTNAME, "echo", "ok"], None,
Expand All @@ -415,15 +415,15 @@ def test_027_warn_shell_globbing_nodes(self):
os.chdir(curdir)
tfile.close()
os.unlink(tfile.name)
os.rmdir(tdir)
tdir.cleanup()

def test_028_warn_shell_globbing_exclude(self):
"""test clush warning on shell globbing (-x)"""
tdir = make_temp_dir()
tfile = open(os.path.join(tdir, HOSTNAME), "wb")
tfile = open(os.path.join(tdir.name, HOSTNAME), "wb")
curdir = os.getcwd()
try:
os.chdir(tdir)
os.chdir(tdir.name)
rxs = r"^Warning: using '-x %s' and local path " \
r"'%s' exists, was it expanded by the shell\?\n" \
% (HOSTNAME, HOSTNAME)
Expand All @@ -434,7 +434,7 @@ def test_028_warn_shell_globbing_exclude(self):
os.chdir(curdir)
tfile.close()
os.unlink(tfile.name)
os.rmdir(tdir)
tdir.cleanup()

def test_029_hostfile(self):
"""test clush --hostfile"""
Expand Down Expand Up @@ -629,18 +629,18 @@ def test_041_outdir_errdir(self):
"""test clush --outdir and --errdir"""
odir = make_temp_dir()
edir = make_temp_dir()
tofilepath = os.path.join(odir, HOSTNAME)
tefilepath = os.path.join(edir, HOSTNAME)
tofilepath = os.path.join(odir.name, HOSTNAME)
tefilepath = os.path.join(edir.name, HOSTNAME)
try:
self._clush_t(["-w", HOSTNAME, "--outdir", odir, "echo", "ok"],
self._clush_t(["-w", HOSTNAME, "--outdir", odir.name, "echo", "ok"],
None, self.output_ok)
self.assertTrue(os.path.isfile(tofilepath))
with open(tofilepath, "r") as f:
self.assertEqual(f.read(), "ok\n")
finally:
os.unlink(tofilepath)
try:
self._clush_t(["-w", HOSTNAME, "--errdir", edir, "echo", "ok", ">&2"],
self._clush_t(["-w", HOSTNAME, "--errdir", edir.name, "echo", "ok", ">&2"],
None, None, 0, self.output_ok)
self.assertTrue(os.path.isfile(tefilepath))
with open(tefilepath, "r") as f:
Expand All @@ -649,7 +649,7 @@ def test_041_outdir_errdir(self):
os.unlink(tefilepath)
try:
serr = "%s: err\n" % HOSTNAME
self._clush_t(["-w", HOSTNAME, "--outdir", odir, "--errdir", edir,
self._clush_t(["-w", HOSTNAME, "--outdir", odir.name, "--errdir", edir.name,
"echo", "ok", ";", "echo", "err", ">&2"], None,
self.output_ok, 0, serr.encode())
self.assertTrue(os.path.isfile(tofilepath))
Expand All @@ -661,8 +661,8 @@ def test_041_outdir_errdir(self):
finally:
os.unlink(tofilepath)
os.unlink(tefilepath)
os.rmdir(odir)
os.rmdir(edir)
odir.cleanup()
edir.cleanup()

def test_042_command_prefix(self):
"""test clush -O command_prefix"""
Expand Down
36 changes: 20 additions & 16 deletions tests/CLIConfigTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,12 @@ def testClushConfigCustomGlobal(self):
custom_config_save = os.environ.get('CLUSTERSHELL_CFGDIR')

# Create fake CLUSTERSHELL_CFGDIR
custom_cfg_dir = make_temp_dir()
custom_cfg_dir = make_temp_dir(ignore_cleanup_errors=True)

try:
os.environ['CLUSTERSHELL_CFGDIR'] = custom_cfg_dir
os.environ['CLUSTERSHELL_CFGDIR'] = custom_cfg_dir.name

cfgfile = open(os.path.join(custom_cfg_dir, 'clush.conf'), 'w')
cfgfile = open(os.path.join(custom_cfg_dir.name, 'clush.conf'), 'w')
cfgfile.write(dedent("""
[Main]
fanout: 42
Expand Down Expand Up @@ -368,7 +368,7 @@ def testClushConfigCustomGlobal(self):
os.environ['CLUSTERSHELL_CFGDIR'] = custom_config_save
else:
del os.environ['CLUSTERSHELL_CFGDIR']
shutil.rmtree(custom_cfg_dir, ignore_errors=True)
custom_cfg_dir.cleanup()


def testClushConfigUserOverride(self):
Expand All @@ -377,12 +377,12 @@ def testClushConfigUserOverride(self):
xdg_config_home_save = os.environ.get('XDG_CONFIG_HOME')

# Create fake XDG_CONFIG_HOME
dname = make_temp_dir()
tdir = make_temp_dir(ignore_cleanup_errors=True)
try:
os.environ['XDG_CONFIG_HOME'] = dname
os.environ['XDG_CONFIG_HOME'] = tdir.name

# create $XDG_CONFIG_HOME/clustershell/clush.conf
usercfgdir = os.path.join(dname, 'clustershell')
usercfgdir = os.path.join(tdir.name, 'clustershell')
os.mkdir(usercfgdir)
cfgfile = open(os.path.join(usercfgdir, 'clush.conf'), 'w')
cfgfile.write(dedent("""
Expand Down Expand Up @@ -420,12 +420,14 @@ def testClushConfigUserOverride(self):
os.environ['XDG_CONFIG_HOME'] = xdg_config_home_save
else:
del os.environ['XDG_CONFIG_HOME']
shutil.rmtree(dname, ignore_errors=True)
tdir.cleanup()

def testClushConfigConfDirModesEmpty(self):
"""test CLI.Config.ClushConfig (confdir with no modes)"""
dname1 = make_temp_dir()
dname2 = make_temp_dir()
tdir1 = make_temp_dir()
dname1 = tdir1.name
tdir2 = make_temp_dir()
dname2 = tdir2.name
f = make_temp_file(dedent("""
[Main]
fanout: 42
Expand Down Expand Up @@ -465,14 +467,16 @@ def testClushConfigConfDirModesEmpty(self):
self.assertRaises(ClushConfigError, config.set_mode, "sshpass")
finally:
f.close()
os.rmdir(dname2)
os.rmdir(dname1)
tdir2.cleanup()
tdir1.cleanup()


def testClushConfigConfDirModes(self):
"""test CLI.Config.ClushConfig (confdir and modes)"""
dname1 = make_temp_dir()
dname2 = make_temp_dir()
tdir1 = make_temp_dir()
dname1 = tdir1.name
tdir2 = make_temp_dir()
dname2 = tdir2.name
# Notes:
# - use dname1 two times to check dup checking code
# - use quotes on one of the directory path
Expand Down Expand Up @@ -594,5 +598,5 @@ def testClushConfigConfDirModes(self):
f2.close()
f1.close()
f.close()
os.rmdir(dname2)
os.rmdir(dname1)
tdir2.cleanup()
tdir1.cleanup()
12 changes: 6 additions & 6 deletions tests/CLINodesetTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,25 +797,25 @@ class CLINodesetGroupResolverConfigErrorTest(CLINodesetTestBase):
"""Unit test class for testing GroupResolverConfigError"""

def setUp(self):
self.dname = make_temp_dir()
self.tdir = make_temp_dir()
self.gconff = make_temp_file(dedent("""
[Main]
default: default
autodir: %s
""" % self.dname).encode('ascii'))
""" % self.tdir.name).encode('ascii'))
self.yamlf = make_temp_file(dedent("""
default:
compute: 'foo'
broken: i am not a dict
""").encode('ascii'), suffix=".yaml", dir=self.dname)
""").encode('ascii'), suffix=".yaml", dir=self.tdir.name)

set_std_group_resolver(GroupResolverConfig(self.gconff.name))

def tearDown(self):
set_std_group_resolver(None)
self.gconff = None
self.yamlf = None
os.rmdir(self.dname)
self.yamlf.close()
self.gconff.close()
self.tdir.cleanup()

def test_bad_yaml_config(self):
"""test nodeset with bad yaml config"""
Expand Down
8 changes: 4 additions & 4 deletions tests/DefaultsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,20 @@ def test_004_workerclass(self):
self.assertTrue(task.default("distant_worker") is WorkerSsh)
task_terminate()

dname = make_temp_dir()
modfile = open(os.path.join(dname, 'OutOfTree.py'), 'w')
tdir = make_temp_dir(ignore_cleanup_errors=True)
modfile = open(os.path.join(tdir.name, 'OutOfTree.py'), 'w')
modfile.write(dedent("""
class OutOfTreeWorker(object):
pass
WORKER_CLASS = OutOfTreeWorker"""))
modfile.flush()
modfile.close()
sys.path.append(dname)
sys.path.append(tdir.name)
self.defaults.distant_workername = 'OutOfTree'
task = task_self(self.defaults)
self.assertEqual(task.default("distant_worker").__name__, 'OutOfTreeWorker')
task_terminate()
shutil.rmtree(dname, ignore_errors=True)
tdir.cleanup()

def test_005_misc_value_errors(self):
"""test Defaults misc value errors"""
Expand Down
Loading

0 comments on commit 628f982

Please sign in to comment.