Skip to content

Commit 88c3cdd

Browse files
committed
feat (diracx): start a diracx container when running the Integration tests
1 parent b5f977e commit 88c3cdd

File tree

4 files changed

+183
-11
lines changed

4 files changed

+183
-11
lines changed

integration_tests.py

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@
2929
"DIRACOSVER": "master",
3030
"DIRACOS_TARBALL_PATH": None,
3131
"TEST_HTTPS": "Yes",
32+
"TEST_DIRACX": "No",
3233
"DIRAC_FEWER_CFG_LOCKS": None,
3334
"DIRAC_USE_JSON_ENCODE": None,
3435
"INSTALLATION_BRANCH": "",
3536
}
36-
DEFAULT_MODULES = {
37-
"DIRAC": Path(__file__).parent.absolute(),
38-
}
37+
DIRACX_OPTIONS = (
38+
"DIRAC_ENABLE_DIRACX_LOGIN",
39+
"DIRAC_ENABLE_DIRACX_JOB_MONITORING",
40+
)
41+
DEFAULT_MODULES = {"DIRAC": Path(__file__).parent.absolute()}
3942

4043
# Static configuration
4144
DB_USER = "Dirac"
@@ -180,7 +183,7 @@ def destroy():
180183
with _gen_docker_compose(DEFAULT_MODULES) as docker_compose_fn:
181184
os.execvpe(
182185
"docker-compose",
183-
["docker-compose", "-f", docker_compose_fn, "down", "--remove-orphans", "-t", "0"],
186+
["docker-compose", "-f", docker_compose_fn, "down", "--remove-orphans", "-t", "0", "--volumes"],
184187
_make_env({}),
185188
)
186189

@@ -193,7 +196,6 @@ def prepare_environment(
193196
release_var: Optional[str] = None,
194197
):
195198
"""Prepare the local environment for installing DIRAC."""
196-
197199
_check_containers_running(is_up=False)
198200
if editable is None:
199201
editable = sys.stdout.isatty()
@@ -224,7 +226,7 @@ def prepare_environment(
224226
typer.secho("Running docker-compose to create containers", fg=c.GREEN)
225227
with _gen_docker_compose(modules) as docker_compose_fn:
226228
subprocess.run(
227-
["docker-compose", "-f", docker_compose_fn, "up", "-d"],
229+
["docker-compose", "-f", docker_compose_fn, "up", "-d", "dirac-server", "dirac-client"],
228230
check=True,
229231
env=docker_compose_env,
230232
)
@@ -313,6 +315,27 @@ def prepare_environment(
313315
)
314316
subprocess.run(command, check=True, shell=True)
315317

318+
docker_compose_fn_final = Path(tempfile.mkdtemp()) / "ci"
319+
typer.secho("Running docker-compose to create DiracX containers", fg=c.GREEN)
320+
typer.secho(f"Will eave a folder behind: {docker_compose_fn_final}", fg=c.YELLOW)
321+
322+
with _gen_docker_compose(modules) as docker_compose_fn:
323+
# We cannot use the temporary directory created in the context manager because
324+
# we don't stay in the contect manager (Popen)
325+
# So we need something that outlives it.
326+
shutil.copytree(docker_compose_fn.parent, docker_compose_fn_final, dirs_exist_ok=True)
327+
# We use Popen because we don't want to wait for this command to finish.
328+
# It is going to start all the diracx containers, including one which waits
329+
# for the DIRAC installation to be over.
330+
subprocess.Popen(
331+
["docker-compose", "-f", docker_compose_fn_final / "docker-compose.yml", "up", "-d", "diracx"],
332+
env=docker_compose_env,
333+
stdin=None,
334+
stdout=None,
335+
stderr=None,
336+
close_fds=True,
337+
)
338+
316339

317340
@app.command()
318341
def install_server():
@@ -326,6 +349,15 @@ def install_server():
326349
check=True,
327350
)
328351

352+
# This runs a continuous loop that exports the config in yaml
353+
# for the diracx container to use
354+
typer.secho("Starting configuration export loop for diracx", fg=c.GREEN)
355+
base_cmd = _build_docker_cmd("server", tty=False, daemon=True)
356+
subprocess.run(
357+
base_cmd + ["bash", "/home/dirac/LocalRepo/ALTERNATIVE_MODULES/DIRAC/tests/CI/exportCSLoop.sh"],
358+
check=True,
359+
)
360+
329361
typer.secho("Copying credentials and certificates", fg=c.GREEN)
330362
base_cmd = _build_docker_cmd("client", tty=False)
331363
subprocess.run(
@@ -508,13 +540,24 @@ def _gen_docker_compose(modules):
508540
# Load the docker-compose configuration and mount the necessary volumes
509541
input_fn = Path(__file__).parent / "tests/CI/docker-compose.yml"
510542
docker_compose = yaml.safe_load(input_fn.read_text())
543+
# diracx-wait-for-db needs the volume to be able to run the witing script
544+
for ctn in ("dirac-server", "dirac-client", "diracx-wait-for-db"):
545+
if "volumes" not in docker_compose["services"][ctn]:
546+
docker_compose["services"][ctn]["volumes"] = []
511547
volumes = [f"{path}:/home/dirac/LocalRepo/ALTERNATIVE_MODULES/{name}" for name, path in modules.items()]
512548
volumes += [f"{path}:/home/dirac/LocalRepo/TestCode/{name}" for name, path in modules.items()]
513-
docker_compose["services"]["dirac-server"]["volumes"] = volumes[:]
514-
docker_compose["services"]["dirac-client"]["volumes"] = volumes[:]
549+
docker_compose["services"]["dirac-server"]["volumes"].extend(volumes[:])
550+
docker_compose["services"]["dirac-client"]["volumes"].extend(volumes[:])
551+
docker_compose["services"]["diracx-wait-for-db"]["volumes"].extend(volumes[:])
552+
553+
module_configs = _load_module_configs(modules)
554+
if "diracx" in module_configs:
555+
docker_compose["services"]["diracx"]["volumes"].append(
556+
f"{modules['diracx']}/src/diracx:{module_configs['diracx']['install-location']}"
557+
)
515558

516559
# Add any extension services
517-
for module_name, module_configs in _load_module_configs(modules).items():
560+
for module_name, module_configs in module_configs.items():
518561
for service_name, service_config in module_configs["extra-services"].items():
519562
typer.secho(f"Adding service {service_name} for {module_name}", err=True, fg=c.GREEN)
520563
docker_compose["services"][service_name] = service_config.copy()
@@ -981,6 +1024,8 @@ def _make_config(modules, flags, release_var, editable):
9811024
"CLIENT_HOST": "client",
9821025
# Test specific variables
9831026
"WORKSPACE": "/home/dirac",
1027+
# DiracX variable
1028+
"DIRACX_URL": "http://diracx:8000",
9841029
}
9851030

9861031
if editable:
@@ -1006,6 +1051,12 @@ def _make_config(modules, flags, release_var, editable):
10061051
except KeyError:
10071052
typer.secho(f"Required feature variable {key!r} is missing", err=True, fg=c.RED)
10081053
raise typer.Exit(code=1)
1054+
1055+
# If we test DiracX, enable all the options
1056+
if config["TEST_DIRACX"].lower() in ("yes", "true"):
1057+
for key in DIRACX_OPTIONS:
1058+
config[key] = "Yes"
1059+
10091060
config["TESTREPO"] = [f"/home/dirac/LocalRepo/TestCode/{name}" for name in modules]
10101061
config["ALTERNATIVE_MODULES"] = [f"/home/dirac/LocalRepo/ALTERNATIVE_MODULES/{name}" for name in modules]
10111062

@@ -1027,7 +1078,7 @@ def _load_module_configs(modules):
10271078
return module_ci_configs
10281079

10291080

1030-
def _build_docker_cmd(container_name, *, use_root=False, cwd="/home/dirac", tty=True):
1081+
def _build_docker_cmd(container_name, *, use_root=False, cwd="/home/dirac", tty=True, daemon=False):
10311082
if use_root or os.getuid() == 0:
10321083
user = "root"
10331084
else:
@@ -1042,6 +1093,8 @@ def _build_docker_cmd(container_name, *, use_root=False, cwd="/home/dirac", tty=
10421093
err=True,
10431094
fg=c.YELLOW,
10441095
)
1096+
if daemon:
1097+
cmd += ["-d"]
10451098
cmd += [
10461099
"-e=TERM=xterm-color",
10471100
"-e=INSTALLROOT=/home/dirac",

tests/CI/check_db_initialized.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
dbMissing=true;
3+
allDBs=(JobDB FileCatalogDB)
4+
while ${dbMissing};
5+
do
6+
dbMissing=false;
7+
allExistingDBs=$(mysql -uDirac -pDirac -h mysql -P 3306 -e "show databases;");
8+
for db in "${allDBs[@]}";
9+
do
10+
if grep -q "${db}" <<< "${allExistingDBs}";
11+
then
12+
echo "${db} OK";
13+
else
14+
echo "${db} not created";
15+
dbMissing=true;
16+
fi;
17+
done;
18+
if ${dbMissing};
19+
then
20+
sleep 1;
21+
fi
22+
done

tests/CI/docker-compose.yml

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
version: '3.4'
22

3+
volumes:
4+
# Volume used to store the config of diracx
5+
diracx-cs-store:
6+
# Volume used to store the pair of keys to sign the tokens
7+
diracx-key-store:
8+
39
services:
410
mysql:
511
image: ${MYSQL_VER}
@@ -42,7 +48,6 @@ services:
4248
retries: 15
4349
start_period: 60s
4450

45-
4651
# Mock of an S3 storage
4752
s3-direct:
4853
image: adobe/s3mock
@@ -55,18 +60,45 @@ services:
5560
- initialBuckets=my-first-bucket
5661
- debug=true
5762

63+
64+
diracx-wait-for-db:
65+
66+
image: ${MYSQL_VER}
67+
container_name: diracx-wait-for-db
68+
depends_on:
69+
mysql:
70+
condition: service_healthy
71+
command: /home/dirac/LocalRepo/ALTERNATIVE_MODULES/DIRAC/tests/CI/check_db_initialized.sh
72+
73+
74+
5875
dirac-server:
5976
image: ${CI_REGISTRY_IMAGE}/${HOST_OS}-dirac
6077
container_name: server
6178
hostname: server
6279
user: "${DIRAC_UID}:${DIRAC_GID}"
80+
6381
depends_on:
6482
mysql:
6583
condition: service_healthy
6684
elasticsearch:
6785
condition: service_healthy
86+
s3-direct:
87+
condition: service_started
88+
iam-login-service:
89+
condition: service_started
90+
diracx-init-key:
91+
condition: service_completed_successfully # Let the init container create the cs
92+
diracx-init-cs:
93+
condition: service_completed_successfully # Let the init container create the cs
6894
ulimits:
6995
nofile: 8192
96+
volumes:
97+
- diracx-cs-store:/cs_store
98+
- diracx-key-store:/signing-key
99+
environment:
100+
- DIRACX_CONFIG_BACKEND_URL=git+file:///cs_store/initialRepo
101+
- DIRACX_SERVICE_AUTH_TOKEN_KEY=file:///signing-key/rs256.key
70102

71103
dirac-client:
72104
image: ${CI_REGISTRY_IMAGE}/${HOST_OS}-dirac
@@ -77,3 +109,54 @@ services:
77109
- dirac-server
78110
ulimits:
79111
nofile: 8192
112+
113+
114+
115+
diracx-init-key:
116+
image: ghcr.io/diracgrid/diracx/server
117+
container_name: diracx-init-key
118+
environment:
119+
- DIRACX_SERVICE_AUTH_TOKEN_KEY="file:///signing-key/rs256.key"
120+
volumes:
121+
- diracx-key-store:/signing-key/
122+
# We need to allow everybody to read the private keys
123+
# Because the users are different between the DIRAC and DiracX containers
124+
entrypoint: |
125+
/dockerMicroMambaEntrypoint.sh bash -c "ssh-keygen -P '' -trsa -b4096 -mPEM -f/signing-key/rs256.key && /dockerMicroMambaEntrypoint.sh chmod o+r /signing-key/rs256.*"
126+
127+
diracx-init-cs:
128+
image: ghcr.io/diracgrid/diracx/server
129+
container_name: diracx-init-cs
130+
environment:
131+
- DIRACX_CONFIG_BACKEND_URL=git+file:///cs_store/initialRepo
132+
- DIRACX_SERVICE_AUTH_TOKEN_KEY=file:///signing-key/rs256.key
133+
volumes:
134+
- diracx-cs-store:/cs_store/
135+
- diracx-key-store:/signing-key/
136+
entrypoint: |
137+
/dockerMicroMambaEntrypoint.sh dirac internal generate-cs /cs_store/initialRepo --vo=diracAdmin --user-group=admin --idp-url=http://dsdsd.csds/a/b
138+
139+
diracx:
140+
image: ghcr.io/diracgrid/diracx/server
141+
container_name: diracx
142+
environment:
143+
- DIRACX_CONFIG_BACKEND_URL=git+file:///cs_store/initialRepo
144+
- "DIRACX_DB_URL_AUTHDB=sqlite+aiosqlite:///:memory:"
145+
- DIRACX_DB_URL_JOBDB=mysql+aiomysql://Dirac:Dirac@mysql/JobDB
146+
- DIRACX_SERVICE_AUTH_TOKEN_KEY=file:///signing-key/rs256.key
147+
- DIRACX_SERVICE_AUTH_ALLOWED_REDIRECTS=["http://diracx:8000/docs/oauth2-redirect"]
148+
ports:
149+
- 8000:8000
150+
depends_on:
151+
diracx-wait-for-db:
152+
condition: service_completed_successfully
153+
volumes:
154+
- diracx-cs-store:/cs_store/
155+
- diracx-key-store:/signing-key/
156+
157+
healthcheck:
158+
test: ["CMD", "/dockerMicroMambaEntrypoint.sh", "curl", "-f", "http://localhost:8000/.well-known/openid-configuration"]
159+
interval: 5s
160+
timeout: 2s
161+
retries: 15
162+
start_period: 60s

tests/CI/exportCSLoop.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
# This script will export to the `Production.cfg` file to the
3+
# yaml format for diracx every 5 seconds
4+
5+
source /home/dirac/ServerInstallDIR/bashrc
6+
git config --global user.name "DIRAC Server CI"
7+
git config --global user.email "dirac-server-ci@invalid"
8+
9+
while true;
10+
do
11+
curl -L https://gitlab.cern.ch/chaen/chris-hackaton-cs/-/raw/master/convert-from-legacy.py |DIRAC_COMPAT_ENABLE_CS_CONVERSION=True ~/ServerInstallDIR/diracos/bin/python - ~/ServerInstallDIR/etc/Production.cfg /cs_store/initialRepo/
12+
git -C /cs_store/initialRepo/ commit -am "export $(date)"
13+
sleep 5;
14+
done

0 commit comments

Comments
 (0)