Skip to content

Commit 70709a2

Browse files
author
Alan Christie
committed
Can now set your own user ID for test containers
Now runs a test if told to (ignoring 'ignore' and 'run-level' declarations)
1 parent e2675f2 commit 70709a2

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

jote/compose.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def __init__(self, collection: str,
7070
cores: int,
7171
project_directory: str,
7272
working_directory: str,
73-
command: str):
73+
command: str,
74+
user_id: Optional[int] = None):
7475

7576
# Memory must have a Mi or Gi suffix.
7677
# For docker-compose we translate to 'm' and 'g'
@@ -88,6 +89,7 @@ def __init__(self, collection: str,
8889
self._project_directory: str = project_directory
8990
self._working_directory: str = working_directory
9091
self._command: str = command
92+
self._user_id: int = user_id
9193

9294
def get_test_path(self) -> str:
9395
"""Returns the path to the root directory for a given test.
@@ -126,6 +128,12 @@ def create(self) -> str:
126128
if not os.path.exists(inst_path):
127129
os.makedirs(inst_path)
128130

131+
# Run as a specific user ID?
132+
if self._user_id is not None:
133+
user_id = self._user_id
134+
else:
135+
user_id = os.getuid()
136+
129137
# Write the Docker compose content to a file in the test directory
130138
variables: Dict[str, Any] =\
131139
{'test_path': project_path,
@@ -134,7 +142,7 @@ def create(self) -> str:
134142
'image': self._image,
135143
'memory_limit': self._memory,
136144
'cpus': self._cores,
137-
'uid': _USER_ID,
145+
'uid': user_id,
138146
'command': self._command,
139147
'project_directory': self._project_directory,
140148
'working_directory': self._working_directory,

jote/jote.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -332,22 +332,30 @@ def _test(args: argparse.Namespace,
332332
test_status: bool = True
333333

334334
# Does the test have an 'ignore' declaration?
335+
# Obey it unless the test is named explicitly -
336+
# i.e. if th user has named a specific test, run it.
335337
if 'ignore' in job_definition.tests[job_test_name]:
336-
print('W Ignoring test (found "ignore")')
337-
tests_ignored += 1
338-
continue
338+
if args.test:
339+
print('W Ignoring the ignore: property (told to run this test)')
340+
else:
341+
print('W Ignoring test (found "ignore")')
342+
tests_ignored += 1
343+
continue
339344

340345
# Does the test have a 'run-level' declaration?
341346
# If so, is it higher than the run-level specified?
342-
if 'run-level' in job_definition.tests[job_test_name]:
343-
run_level = job_definition.tests[job_test_name]['run-level']
344-
print(f'> run-level={run_level}')
345-
if run_level > args.run_level:
346-
print(f'W Skipping test (test is "run-level: {run_level}")')
347-
tests_skipped += 1
348-
continue
347+
if args.test:
348+
print('W Ignoring any run-level check (told to run this test)')
349349
else:
350-
print('> run-level=Undefined')
350+
if 'run-level' in job_definition.tests[job_test_name]:
351+
run_level = job_definition.tests[job_test_name]['run-level']
352+
print(f'> run-level={run_level}')
353+
if run_level > args.run_level:
354+
print(f'W Skipping test (test is "run-level: {run_level}")')
355+
tests_skipped += 1
356+
continue
357+
else:
358+
print('> run-level=Undefined')
351359

352360
# Render the command for this test.
353361

@@ -460,7 +468,8 @@ def _test(args: argparse.Namespace,
460468
job_image_cores,
461469
job_project_directory,
462470
job_working_directory,
463-
job_command)
471+
job_command,
472+
args.run_as_user)
464473
project_path: str = t_compose.create()
465474

466475
test_path: str = t_compose.get_test_path()
@@ -544,6 +553,17 @@ def arg_check_run_level(value: str) -> int:
544553
return i_value
545554

546555

556+
def arg_check_run_as_user(value: str) -> int:
557+
"""A type checker for the argparse run-as-user.
558+
"""
559+
i_value = int(value)
560+
if i_value < 0:
561+
raise argparse.ArgumentTypeError('Minimum value is 0')
562+
if i_value > 65_535:
563+
raise argparse.ArgumentTypeError('Maximum value is 65535')
564+
return i_value
565+
566+
547567
# -----------------------------------------------------------------------------
548568
# main
549569
# -----------------------------------------------------------------------------
@@ -582,6 +602,11 @@ def main() -> int:
582602
' will be executed, a value from 1 to 100',
583603
default=1,
584604
type=arg_check_run_level)
605+
arg_parser.add_argument('-u', '--run-as-user',
606+
help='A user ID to run the tests as. If not set'
607+
' your user ID is used to run the test'
608+
' containers.',
609+
type=arg_check_run_as_user)
585610

586611
arg_parser.add_argument('-d', '--dry-run', action='store_true',
587612
help='Setting this flag will result in jote'

0 commit comments

Comments
 (0)