Skip to content
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

Mostieri/container name #117

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions src/ansys/dynamicreporting/core/docker_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ def start(self, host_directory: str, db_directory: str, port: int) -> None:
}

# get a unique name for the container to run
existing_names = [x.name for x in self._docker_client.from_env().containers.list()]
container_name = "nexus"
while container_name in existing_names:
container_name += random.choice(string.ascii_letters)
if len(container_name) > 500:
raise RuntimeError("Can't determine a unique Docker container name.")
self._container_name = container_name
# existing_names = [x.name for x in self._docker_client.from_env().containers.list()]
# container_name = "nexus"
# while container_name in existing_names:
# container_name += random.choice(string.ascii_letters)
# if len(container_name) > 500:
# raise RuntimeError("Can't determine a unique Docker container name.")
# self._container_name = container_name

# Start the container in detached mode and override
# the default entrypoint so multiple commands can be
Expand All @@ -177,13 +177,12 @@ def start(self, host_directory: str, db_directory: str, port: int) -> None:
volumes=data_volume,
environment=container_env,
ports=ports_to_map,
name=self._container_name,
tty=True,
detach=True,
)
except Exception as e: # pragma: no cover
raise RuntimeError("Can't run Docker container: " + self._image_name + "\n\n" + str(e))

self._container_name = self._container.name
# Build up the command to run and send it to the container
# as a detached command.
#
Expand Down Expand Up @@ -405,6 +404,28 @@ def save_config(self) -> str:
ret = self.run_in_container(nexus_cmd)
return ret

def status(self) -> str:
"""
Run the ``nexus_launcher save_config ...`` command in the Docker container.

This command runs on the previously specified database directory.

Returns
-------
str
Output from the command.

Raises
------
RuntimeError
"""
nexus_cmd = self._cei_home + "/bin/nexus_launcher"
nexus_cmd += " --db_directory /db_directory"
nexus_cmd += " status"
ret = self.run_in_container(nexus_cmd)
print(ret)
return ret

def launch_nexus_server(
self,
username: str,
Expand Down
26 changes: 21 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from ansys.dynamicreporting.core import Service
from ansys.dynamicreporting.core.constants import DOCKER_DEV_REPO_URL
from ansys.dynamicreporting.core.utils.report_utils import find_unused_ports


def pytest_addoption(parser):
Expand Down Expand Up @@ -44,33 +45,43 @@ def get_exec(pytestconfig: pytest.Config) -> str:
return exec_basis


used_ports = None


@pytest.fixture
def adr_service_create(request, pytestconfig: pytest.Config) -> Service:
global used_ports
if not used_ports:
used_ports = []
use_local = pytestconfig.getoption("use_local_launcher")
dir_name = "auto_delete_" + "".join(choice(ascii_letters) for x in range(5))
db_dir = os.path.join(os.path.join(request.fspath.dirname, "test_data"), dir_name)
tmp_docker_dir = os.path.join(os.path.join(request.fspath.dirname, "test_data"), "tmp_docker")
port = find_unused_ports(1, start=8000, avoid=used_ports)[0]
if use_local:
tmp_service = Service(
ansys_installation=pytestconfig.getoption("install_path"),
docker_image=DOCKER_DEV_REPO_URL,
db_directory=db_dir,
port=8000 + int(random() * 4000),
port=port,
)
else:
cleanup_docker(request)
tmp_service = Service(
ansys_installation="docker",
docker_image=DOCKER_DEV_REPO_URL,
db_directory=db_dir,
data_directory=tmp_docker_dir,
port=8000 + int(random() * 4000),
port=port,
)
used_ports.append(port)
return tmp_service


@pytest.fixture
def adr_service_query(request, pytestconfig: pytest.Config) -> Service:
global used_ports
if not used_ports:
used_ports = []
use_local = pytestconfig.getoption("use_local_launcher")
local_db = os.path.join("test_data", "query_db")
db_dir = os.path.join(request.fspath.dirname, local_db)
Expand All @@ -80,16 +91,21 @@ def adr_service_query(request, pytestconfig: pytest.Config) -> Service:
if use_local:
ansys_installation = pytestconfig.getoption("install_path")
else:
cleanup_docker(request)
ansys_installation = "docker"
port = find_unused_ports(1, start=8000, avoid=used_ports)[0]
tmp_service = Service(
ansys_installation=ansys_installation,
docker_image=DOCKER_DEV_REPO_URL,
db_directory=db_dir,
data_directory=tmp_docker_dir,
port=8000 + int(random() * 4000),
port=port,
)
used_ports.append(port)
if not use_local:
tmp_service._container.save_config()
tmp_service._container.status()
print(tmp_service._container.run_in_container("ls /db_directory/"))
print(tmp_service._container.run_in_container("cat /db_directory/nexus.log"))
tmp_service.start(create_db=False, exit_on_close=True, delete_db=False)
tmp_service._container.status()
return tmp_service
Loading