Skip to content

Commit

Permalink
style: merge and code reformatted
Browse files Browse the repository at this point in the history
  • Loading branch information
maugde committed Jun 13, 2024
2 parents c1cc7cb + 598edde commit 8b7de40
Show file tree
Hide file tree
Showing 21 changed files with 254 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/antares_web_installer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ def copy_files(self):
copytree(elt_path, self.target_dir.joinpath(elt_path.name), dirs_exist_ok=True)

# handle permission errors
except PermissionError as e:
raise InstallError(f"Error : Cannot write in {self.target_dir}:") from e
except PermissionError as e: # pragma: no cover
relpath = elt_path.relative_to(self.source_dir).as_posix()
raise InstallError(f"Error: Cannot write '{relpath}' in {self.target_dir}: {e}")

def check_version(self) -> str:
"""
Expand Down
8 changes: 4 additions & 4 deletions src/antares_web_installer/shortcuts/_win32_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ def create_shortcut(
wscript.Arguments = " ".join(arguments)
wscript.WorkingDirectory = str(working_dir)
wscript.WindowStyle = 0
wscript.Description = str(description) if description else "null"
wscript.IconLocation = str(exe_path) or "null"
wscript.Description = str(description) if description else "$null"
wscript.IconLocation = str(exe_path) if exe_path else "$null"
wscript.save()
except TypeError as e:
raise ShortcutCreationError(f"Unsupported type for shortcut configuration: {e}") from e
raise ShortcutCreationError(f"Unsupported field type for shortcut configuration: {e}") from e
except AttributeError as e:
raise ShortcutCreationError(f"Unknown attribute: {e}") from e
except pywintypes.com_error as e:
raise ShortcutCreationError(f"An error occured while saving shortcut: {e}") from e
raise ShortcutCreationError(f"An error occurred while saving shortcut: {e}") from e
100 changes: 100 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import hashlib
from pathlib import Path

from _pytest.monkeypatch import MonkeyPatch

from antares_web_installer.app import App, EXCLUDED_FILES


class TestApp:
def test_run(self) -> None:
pass

def test_kill_running_server(self) -> None:
# 1. Lancer un serveur
# 2. Appeler la méthode kill_running_server
# 3. Vérifier que le serveur a bien été tué
pass

def test_install_files__from_scratch(self, tmp_path: Path) -> None:
"""
Case where the target directory does not exist.
"""
# Prepare the test resources
source_dir = tmp_path / "source"
source_dir.mkdir()
target_dir = tmp_path / "target"

# Say we have dummy files in the source directory
expected_files = ["dummy.txt", "folder/dummy2.txt"]
for name in expected_files:
dummy_file = source_dir / name
dummy_file.parent.mkdir(parents=True, exist_ok=True)
dummy_file.touch()

# Run the test
app = App(source_dir=source_dir, target_dir=target_dir)
app.install_files()

# Check the results
for name in expected_files:
assert (target_dir / name).exists(), f"File {name} must be copied"

def test_install_files__update_config(self, datadir: Path, monkeypatch: MonkeyPatch) -> None:
# Replace the original `check_version` method with a mock
monkeypatch.setattr(App, "check_version", lambda _: "2.14")

# Prepare the test resources
source_dir = datadir.joinpath("install_files/source_files")
target_dir = datadir.joinpath("install_files/target_files")
old_config = (target_dir / "config.yaml").read_text()

app = App(source_dir=source_dir, target_dir=target_dir)
app.install_files()

# Check that `dummy.txt` has been copied
assert (target_dir / "dummy.txt").exists()

# Check that the config file has been updated
new_config = (target_dir / "config.yaml").read_text()
assert old_config != new_config

def test_copy_files__nominal_case(self, datadir: Path) -> None:
# Prepare the test resources
source_dir = datadir.joinpath("copy_files/source_files")
target_dir = datadir.joinpath("copy_files/target_files")

# collect the checksum of all files in the target directory
old_checksum_by_name = {}
for file in target_dir.rglob("*.*"):
content = file.read_bytes()
old_checksum_by_name[file.relative_to(target_dir)] = hashlib.md5(content).hexdigest()

# Run the test
app = App(source_dir=source_dir, target_dir=target_dir)
app.copy_files()

# Check the results
for file in target_dir.rglob("*.*"):
relative_path = file.relative_to(target_dir)
content = file.read_bytes()
old_checksum = old_checksum_by_name[relative_path]
new_checksum = hashlib.md5(content).hexdigest()
root_name = relative_path.parts[0]
if root_name in EXCLUDED_FILES:
assert old_checksum == new_checksum, f"File {file} must not have been modified"
else:
assert old_checksum != new_checksum, f"File {file} must have been updated"

def test_check_version(self, tmp_path: Path) -> None:
# 3 cas à tester :
# 1. Le programme exécutable n'existe pas => InstallError
# 2. Le programme exécutable existe, mais le programme plante => InstallError
# 3. Le programme exécutable existe et fonctionne => pas d'erreur
pass

def test_create_icons(self) -> None:
pass

def test_start_server(self) -> None:
pass
6 changes: 6 additions & 0 deletions tests/test_app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This directory contains the resources for the `test_app` test.

This directory contains one folder for each test case:

- `install_files`: Contains the files that are used for source_dir and target_dir
...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dummy Antares Web Server v2.15
1 change: 1 addition & 0 deletions tests/test_app/copy_files/source_files/config.prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy: source
1 change: 1 addition & 0 deletions tests/test_app/copy_files/source_files/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy: source
1 change: 1 addition & 0 deletions tests/test_app/copy_files/source_files/examples/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source
1 change: 1 addition & 0 deletions tests/test_app/copy_files/source_files/logs/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source
1 change: 1 addition & 0 deletions tests/test_app/copy_files/source_files/matrices/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source
1 change: 1 addition & 0 deletions tests/test_app/copy_files/source_files/tmp/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dummy Antares Web Server v2.14
1 change: 1 addition & 0 deletions tests/test_app/copy_files/target_files/config.prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy: target
1 change: 1 addition & 0 deletions tests/test_app/copy_files/target_files/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummy: target
1 change: 1 addition & 0 deletions tests/test_app/copy_files/target_files/examples/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
1 change: 1 addition & 0 deletions tests/test_app/copy_files/target_files/logs/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
1 change: 1 addition & 0 deletions tests/test_app/copy_files/target_files/matrices/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
1 change: 1 addition & 0 deletions tests/test_app/copy_files/target_files/tmp/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
66 changes: 66 additions & 0 deletions tests/test_app/install_files/source_files/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
security:
disabled: false
jwt:
key: super-secret
login:
admin:
pwd: admin

db:
url: "sqlite:////home/john/antares_data/database.db"

storage:
tmp_dir: /tmp
matrixstore: /home/john/antares_data/matrices
archive_dir: /home/john/antares_data/archives
allow_deletion: false
workspaces:
default:
path: /home/john/antares_data/internal_studies/
studies:
path: /home/john/antares_data/studies/

launcher:
default: slurm
local:
binaries:
850: /home/john/opt/antares-8.5.0-Ubuntu-20.04/antares-solver
860: /home/john/opt/antares-8.6.0-Ubuntu-20.04/antares-8.6-solver
enable_nb_cores_detection: True

slurm:
local_workspace: /home/john/antares_data/slurm_workspace

username: antares
hostname: slurm-prod-01

port: 22
private_key_file: /home/john/.ssh/id_rsa
key_password:
default_wait_time: 900
default_time_limit: 172800
enable_nb_cores_detection: False
nb_cores:
min: 1
default: 20
max: 24
default_json_db_name: launcher_db.json
slurm_script_path: /applis/antares/launchAntares.sh
db_primary_key: name
antares_versions_on_remote_server:
- '850' # 8.5.1/antares-8.5-solver
- '860' # 8.6.2/antares-8.6-solver
- '870' # 8.7.0/antares-8.7-solver

debug: false

root_path: ""

server:
worker_threadpool_size: 12
services:
- watcher
- matrix_gc

logging:
level: INFO
Empty file.
61 changes: 61 additions & 0 deletions tests/test_app/install_files/target_files/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
security:
disabled: false
jwt:
key: super-secret
login:
admin:
pwd: admin

db:
url: "sqlite:////home/john/antares_data/database.db"

storage:
tmp_dir: /tmp
matrixstore: /home/john/antares_data/matrices
archive_dir: /home/john/antares_data/archives
allow_deletion: false
workspaces:
default:
path: /home/john/antares_data/internal_studies/
studies:
path: /home/john/antares_data/studies/

launcher:
default: slurm
local:
binaries:
850: /home/john/opt/antares-8.5.0-Ubuntu-20.04/antares-solver
860: /home/john/opt/antares-8.6.0-Ubuntu-20.04/antares-8.6-solver

slurm:
local_workspace: /home/john/antares_data/slurm_workspace

username: antares
hostname: slurm-prod-01

port: 22
private_key_file: /home/john/.ssh/id_rsa
key_password:
default_wait_time: 900
default_time_limit: 172800
default_n_cpu: 12
default_json_db_name: launcher_db.json
slurm_script_path: /applis/antares/launchAntares.sh
db_primary_key: name
antares_versions_on_remote_server:
- '850' # 8.5.1/antares-8.5-solver
- '860' # 8.6.2/antares-8.6-solver
- '870' # 8.7.0/antares-8.7-solver

debug: false

root_path: ""

server:
worker_threadpool_size: 12
services:
- watcher
- matrix_gc

logging:
level: INFO

0 comments on commit 8b7de40

Please sign in to comment.