Skip to content

Commit 59c3b54

Browse files
author
Alan Christie
committed
Now handles 'checks'
Restructured (Compose class)
1 parent 58c6c0b commit 59c3b54

File tree

4 files changed

+271
-127
lines changed

4 files changed

+271
-127
lines changed

README.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ there::
4545
This is a Python 3 utility, so try to run it from a recent (ideally 3.10)
4646
Python environment.
4747

48-
To use the utility you will need to have installed `Docker`_
49-
and `docker-compose`_.
48+
To use the utility you will need to have installed `Docker`_.
5049

5150
.. _PyPI: https://pypi.org/project/im-jote/
5251
.. _Docker: https://docs.docker.com/get-docker/
53-
.. _docker-compose: https://pypi.org/project/docker-compose/
5452

5553
Running tests
5654
-------------

jote/compose.py

Lines changed: 119 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
job:
1818
image: {image}
1919
command: {command}
20+
working_dir: {working_directory}
2021
environment:
2122
- DM_INSTANCE_DIRECTORY={instance_directory}
2223
volumes:
@@ -31,9 +32,6 @@
3132
# A default, 30 minute timeout
3233
_DEFAULT_TEST_TIMEOUT: int = 30 * 60
3334

34-
# The docker-compose version (for the first test)
35-
_COMPOSE_VERSION: Optional[str] = None
36-
3735

3836
def _get_docker_compose_version() -> str:
3937

@@ -47,95 +45,121 @@ def _get_docker_compose_version() -> str:
4745
return result.stdout.decode("utf-8").split('\n')[0][23:]
4846

4947

50-
def get_test_path(test_name: str) -> str:
51-
"""Returns the path to the root directory for a given test.
52-
"""
53-
cwd: str = os.getcwd()
54-
return f'{cwd}/data-manager/jote/{test_name}'
55-
56-
57-
def create(test_name: str,
58-
image: str,
59-
project_directory: str,
60-
command: str) -> str:
61-
"""Writes a docker-compose file
62-
and creates the test directory structure returning the
63-
full path to the test (project) directory.
64-
"""
65-
global _COMPOSE_VERSION
66-
67-
print('# Creating test environment...')
68-
69-
# Do we have the docker-compose version the user's installed?
70-
if not _COMPOSE_VERSION:
71-
_COMPOSE_VERSION = _get_docker_compose_version()
72-
print(f'# docker-compose ({_COMPOSE_VERSION})')
73-
74-
# Make the test directory...
75-
test_path: str = get_test_path(test_name)
76-
project_path: str = f'{test_path}/project'
77-
inst_path: str = f'{project_path}/{_INSTANCE_DIRECTORY}'
78-
if not os.path.exists(inst_path):
79-
os.makedirs(inst_path)
80-
81-
# Write the Docker compose content to a file to the test directory
82-
variables: Dict[str, str] = {'test_path': project_path,
83-
'image': image,
84-
'command': command,
85-
'project_directory': project_directory,
86-
'instance_directory': _INSTANCE_DIRECTORY}
87-
compose_content: str = _COMPOSE_CONTENT.format(**variables)
88-
compose_path: str = f'{test_path}/docker-compose.yml'
89-
with open(compose_path, 'wt') as compose_file:
90-
compose_file.write(compose_content)
91-
92-
print('# Created')
93-
94-
return project_path
95-
96-
97-
def run(test_name: str) -> Tuple[int, str, str]:
98-
"""Runs the container for the test, expecting the docker-compose file
99-
written by the 'create()'. The container exit code is returned to the
100-
caller along with the stdout and stderr content.
101-
A non-zero exit code does not necessarily mean the test has failed.
102-
"""
103-
104-
print('# Executing the test ("docker-compose up")...')
105-
106-
cwd = os.getcwd()
107-
os.chdir(get_test_path(test_name))
108-
109-
timeout: int = _DEFAULT_TEST_TIMEOUT
110-
try:
111-
# Run the container
112-
# and then cleanup
113-
test: subprocess.CompletedProcess =\
114-
subprocess.run(['docker-compose', 'up',
115-
'--exit-code-from', 'job',
116-
'--abort-on-container-exit'],
117-
capture_output=True,
118-
timeout=timeout)
119-
_ = subprocess.run(['docker-compose', 'down'],
120-
capture_output=True,
121-
timeout=120)
122-
finally:
123-
os.chdir(cwd)
124-
125-
print(f'# Executed ({test.returncode})')
126-
127-
return test.returncode,\
128-
test.stdout.decode("utf-8"),\
129-
test.stderr.decode("utf-8")
130-
131-
132-
def delete(test_name: str, quiet: bool = False) -> None:
133-
"""Deletes a test directory created by 'crete()'.
134-
"""
135-
print(f'# Deleting the test...')
136-
137-
test_path: str = get_test_path(test_name)
138-
if os.path.exists(test_path):
139-
shutil.rmtree(test_path)
140-
141-
print('# Deleted')
48+
class Compose:
49+
50+
# The docker-compose version (for the first test)
51+
_COMPOSE_VERSION: Optional[str] = None
52+
53+
def __init__(self, collection: str,
54+
job: str,
55+
test: str,
56+
image: str,
57+
project_directory: str,
58+
working_directory: str,
59+
command: str):
60+
61+
self._collection = collection
62+
self._job = job
63+
self._test = test
64+
self._image = image
65+
self._project_directory = project_directory
66+
self._working_directory = working_directory
67+
self._command = command
68+
69+
def get_test_path(self) -> str:
70+
"""Returns the path to the root directory for a given test.
71+
"""
72+
cwd: str = os.getcwd()
73+
return f'{cwd}/data-manager/jote/{self._collection}.{self._job}.{self._test}'
74+
75+
def get_test_project_path(self) -> str:
76+
"""Returns the path to the root directory for a given test.
77+
"""
78+
test_path: str = self.get_test_path()
79+
return f'{test_path}/project'
80+
81+
def create(self) -> str:
82+
"""Writes a docker-compose file
83+
and creates the test directory structure returning the
84+
full path to the test (project) directory.
85+
"""
86+
87+
print('# Creating test environment...')
88+
89+
# First, delete
90+
test_path: str = self.get_test_path()
91+
if os.path.exists(test_path):
92+
shutil.rmtree(test_path)
93+
94+
# Do we have the docker-compose version the user's installed?
95+
if not Compose._COMPOSE_VERSION:
96+
Compose._COMPOSE_VERSION = _get_docker_compose_version()
97+
print(f'# docker-compose ({Compose._COMPOSE_VERSION})')
98+
99+
# Make the test directory...
100+
test_path: str = self.get_test_path()
101+
project_path: str = self.get_test_project_path()
102+
inst_path: str = f'{project_path}/{_INSTANCE_DIRECTORY}'
103+
if not os.path.exists(inst_path):
104+
os.makedirs(inst_path)
105+
106+
# Write the Docker compose content to a file to the test directory
107+
variables: Dict[str, str] = {'test_path': project_path,
108+
'image': self._image,
109+
'command': self._command,
110+
'project_directory': self._project_directory,
111+
'working_directory': self._working_directory,
112+
'instance_directory': _INSTANCE_DIRECTORY}
113+
compose_content: str = _COMPOSE_CONTENT.format(**variables)
114+
compose_path: str = f'{test_path}/docker-compose.yml'
115+
with open(compose_path, 'wt') as compose_file:
116+
compose_file.write(compose_content)
117+
118+
print('# Created')
119+
120+
return project_path
121+
122+
def run(self) -> Tuple[int, str, str]:
123+
"""Runs the container for the test, expecting the docker-compose file
124+
written by the 'create()'. The container exit code is returned to the
125+
caller along with the stdout and stderr content.
126+
A non-zero exit code does not necessarily mean the test has failed.
127+
"""
128+
129+
print('# Executing the test ("docker-compose up")...')
130+
131+
cwd = os.getcwd()
132+
os.chdir(self.get_test_path())
133+
134+
timeout: int = _DEFAULT_TEST_TIMEOUT
135+
try:
136+
# Run the container
137+
# and then cleanup
138+
test: subprocess.CompletedProcess =\
139+
subprocess.run(['docker-compose', 'up',
140+
'--exit-code-from', 'job',
141+
'--abort-on-container-exit'],
142+
capture_output=True,
143+
timeout=timeout)
144+
_ = subprocess.run(['docker-compose', 'down'],
145+
capture_output=True,
146+
timeout=120)
147+
finally:
148+
os.chdir(cwd)
149+
150+
print(f'# Executed ({test.returncode})')
151+
152+
return test.returncode,\
153+
test.stdout.decode("utf-8"),\
154+
test.stderr.decode("utf-8")
155+
156+
def delete(self) -> None:
157+
"""Deletes a test directory created by 'crete()'.
158+
"""
159+
print(f'# Deleting the test...')
160+
161+
test_path: str = self.get_test_path()
162+
if os.path.exists(test_path):
163+
shutil.rmtree(test_path)
164+
165+
print('# Deleted')

0 commit comments

Comments
 (0)