Skip to content

Commit 8b3c81c

Browse files
committed
Merge remote-tracking branch 'origin/master' into edge
2 parents 687a842 + 5748ddd commit 8b3c81c

File tree

2 files changed

+121
-5
lines changed

2 files changed

+121
-5
lines changed

tests/support/__init__.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# --- BEGIN_HEADER ---
55
#
6-
# __init__ - package marker
6+
# __init__ - package marker and core package functions
77
# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH
88
#
99
# This file is part of MiG.
@@ -40,6 +40,7 @@
4040
import sys
4141
from unittest import TestCase, main as testmain
4242

43+
from tests.support.configsupp import FakeConfiguration
4344
from tests.support.suppconst import MIG_BASE, TEST_BASE, TEST_FIXTURE_DIR, \
4445
TEST_OUTPUT_DIR
4546

@@ -48,6 +49,20 @@
4849
# force defaults to a local environment
4950
os.environ['MIG_ENV'] = 'local'
5051

52+
# expose the configured environment as a constant
53+
MIG_ENV = os.environ['MIG_ENV']
54+
55+
if MIG_ENV == 'local':
56+
# force testconfig as the conig file path
57+
is_py2 = PY2
58+
_conf_dir_suffix = "-py%s" % ('2' if is_py2 else '3',)
59+
_conf_dir = "testconfs%s" % (_conf_dir_suffix,)
60+
_local_conf = os.path.join(
61+
MIG_BASE, 'envhelp/output', _conf_dir, 'MiGserver.conf')
62+
_config_file = os.getenv('MIG_CONF', None)
63+
if _config_file is None:
64+
os.environ['MIG_CONF'] = _local_conf
65+
5166
# All MiG related code will at some point include bits from the mig module
5267
# namespace. Rather than have this knowledge spread through every test file,
5368
# make the sole responsbility of test files to find the support file and
@@ -103,6 +118,7 @@ def __init__(self, *args):
103118
super(MigTestCase, self).__init__(*args)
104119
self._cleanup_checks = list()
105120
self._cleanup_paths = set()
121+
self._configuration = None
106122
self._logger = None
107123
self._skip_logging = False
108124

@@ -153,6 +169,31 @@ def _reset_logging(self, stream):
153169
root_handler = root_logger.handlers[0]
154170
root_handler.stream = stream
155171

172+
# testcase defaults
173+
174+
@staticmethod
175+
def _make_configuration_instance(configuration_to_make):
176+
if configuration_to_make == 'fakeconfig':
177+
return FakeConfiguration()
178+
elif configuration_to_make == 'testconfig':
179+
from mig.shared.conf import get_configuration_object
180+
return get_configuration_object(skip_log=True, disable_auth_log=True)
181+
else:
182+
raise AssertionError(
183+
"MigTestCase: unknown configuration %r" % (configuration_to_make,))
184+
185+
def _provide_configuration(self):
186+
return 'fakeconfig'
187+
188+
@property
189+
def configuration(self):
190+
"""Init a fake configuration if not already done"""
191+
if self._configuration is None:
192+
configuration_to_make = self._provide_configuration()
193+
self._configuration = self._make_configuration_instance(
194+
configuration_to_make)
195+
return self._configuration
196+
156197
@property
157198
def logger(self):
158199
"""Init a fake logger if not already done"""
@@ -199,6 +240,10 @@ def assertPathExists(self, relative_path):
199240
assert not os.path.isabs(
200241
relative_path), "expected relative path within output folder"
201242
absolute_path = os.path.join(TEST_OUTPUT_DIR, relative_path)
243+
return MigTestCase._absolute_path_kind(absolute_path)
244+
245+
@staticmethod
246+
def _absolute_path_kind(absolute_path):
202247
stat_result = os.lstat(absolute_path)
203248
if stat.S_ISLNK(stat_result.st_mode):
204249
return "symlink"

tests/test_support.py

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# --- BEGIN_HEADER ---
4+
#
5+
# test_support - unit test of the corresponding tests module
6+
# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH
7+
#
8+
# This file is part of MiG.
9+
#
10+
# MiG is free software: you can redistribute it and/or modify
11+
# it under the terms of the GNU General Public License as published by
12+
# the Free Software Foundation; either version 2 of the License, or
13+
# (at your option) any later version.
14+
#
15+
# MiG is distributed in the hope that it will be useful,
16+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
# GNU General Public License for more details.
19+
#
20+
# You should have received a copy of the GNU General Public License
21+
# along with this program; if not, write to the Free Software
22+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23+
# USA.
24+
#
25+
# --- END_HEADER ---
26+
#
27+
28+
"""Unit tests for the tests module pointed to in the filename"""
29+
130
from __future__ import print_function
231
import os
332
import sys
433
import unittest
534

635
from tests.support import MigTestCase, PY2, testmain, temppath, \
7-
AssertOver
36+
AssertOver, FakeConfiguration
37+
38+
from mig.shared.conf import get_configuration_object
39+
from mig.shared.configuration import Configuration
840

941

1042
class InstrumentedAssertOver(AssertOver):
43+
"""Helper to keep track of AssertOver runs"""
44+
1145
def __init__(self, *args, **kwargs):
1246
AssertOver.__init__(self, *args, **kwargs)
1347
self._check_callable = None
@@ -24,6 +58,7 @@ def was_check_callable_called(self):
2458

2559
def to_check_callable(self):
2660
_check_callable = AssertOver.to_check_callable(self)
61+
2762
def _wrapped_check_callable():
2863
self._check_callable_called = True
2964
_check_callable()
@@ -32,13 +67,26 @@ def _wrapped_check_callable():
3267

3368

3469
class SupportTestCase(MigTestCase):
70+
"""Coverage of base Support helpers"""
71+
3572
def _class_attribute(self, name, **kwargs):
3673
cls = type(self)
3774
if 'value' in kwargs:
3875
setattr(cls, name, kwargs['value'])
3976
else:
4077
return getattr(cls, name, None)
4178

79+
def test_provides_a_fake_configuration(self):
80+
configuration = self.configuration
81+
82+
self.assertIsInstance(configuration, FakeConfiguration)
83+
84+
def test_provides_a_fake_configuration_for_the_duration_of_the_test(self):
85+
c1 = self.configuration
86+
c2 = self.configuration
87+
88+
self.assertIs(c2, c1)
89+
4290
@unittest.skipIf(PY2, "Python 3 only")
4391
def test_unclosed_files_are_recorded(self):
4492
tmp_path = temppath("support-unclosed", self)
@@ -67,18 +115,21 @@ def test_when_asserting_over_multiple_values(self):
67115
def assert_is_int(value):
68116
assert isinstance(value, int)
69117

70-
attempt_wrapper = self.assert_over(values=(1, 2, 3), _AssertOver=InstrumentedAssertOver)
118+
attempt_wrapper = self.assert_over(
119+
values=(1, 2, 3), _AssertOver=InstrumentedAssertOver)
71120

72121
# record the wrapper on the test case so the subsequent test can assert against it
73-
self._class_attribute('surviving_attempt_wrapper', value=attempt_wrapper)
122+
self._class_attribute('surviving_attempt_wrapper',
123+
value=attempt_wrapper)
74124

75125
with attempt_wrapper as attempt:
76126
attempt(assert_is_int)
77127
attempt_wrapper.assert_success()
78128

79129
self.assertTrue(attempt_wrapper.has_check_callable())
80130
# cleanup was recorded
81-
self.assertIn(attempt_wrapper.get_check_callable(), self._cleanup_checks)
131+
self.assertIn(attempt_wrapper.get_check_callable(),
132+
self._cleanup_checks)
82133

83134
def test_when_asserting_over_multiple_values_after(self):
84135
# test name is purposefully after ..._recorded in sort order
@@ -88,5 +139,25 @@ def test_when_asserting_over_multiple_values_after(self):
88139
self.assertTrue(attempt_wrapper.was_check_callable_called())
89140

90141

142+
class SupportTestCase_overridden_configuration(MigTestCase):
143+
"""Coverage of base Support helpers extension with configuration override"""
144+
145+
def _provide_configuration(self):
146+
return 'testconfig'
147+
148+
def test_provides_the_test_configuration(self):
149+
expected_last_dir = 'testconfs-py2' if PY2 else 'testconfs-py3'
150+
151+
configuration = self.configuration
152+
153+
# check we have a real config object
154+
self.assertIsInstance(configuration, Configuration)
155+
# check for having loaded a config file from a test config dir
156+
config_file_path_parts = configuration.config_file.split(os.path.sep)
157+
config_file_path_parts.pop() # discard file part
158+
config_file_last_dir = config_file_path_parts.pop()
159+
self.assertTrue(config_file_last_dir, expected_last_dir)
160+
161+
91162
if __name__ == '__main__':
92163
testmain()

0 commit comments

Comments
 (0)