Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit c4c4984

Browse files
authored
Fixes and issue with the experiment_data.yml not being saved under a shared account. (#82)
* fix metadatafolder not being generated * Test and update version * def is_current_logged_user_owner(self) -> bool: property * Removed -cov, the same change was done by Bruno in autosubmit proj Added tests * Changed mock (didn't work under CI/CD ) * re-added cov * Added docstring
1 parent fc1ce37 commit c4c4984

File tree

6 files changed

+70
-18
lines changed

6 files changed

+70
-18
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.78
1+
1.0.79

autosubmitconfigparser/config/configcommon.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,14 @@ def is_current_real_user_owner(self) -> bool:
17611761
"""
17621762
return Path(self.experiment_data["ROOTDIR"]).owner() == self.experiment_data["AS_ENV_CURRENT_USER"]
17631763

1764+
@property
1765+
def is_current_logged_user_owner(self) -> bool:
1766+
"""
1767+
Check if the current user is the owner of the experiment folder
1768+
:return: bool
1769+
"""
1770+
return Path(self.experiment_data["ROOTDIR"]).owner() == os.environ.get("USER", None)
1771+
17641772
@staticmethod
17651773
def load_as_env_variables(parameters: Dict[str, Any]) -> Dict[str, Any]:
17661774
"""
@@ -1866,14 +1874,15 @@ def load_workflow_commit(self) -> None:
18661874
"""
18671875
Load the workflow commit from the .git folder
18681876
"""
1869-
project_dir = f"{self.experiment_data.get('ROOTDIR', '')}/proj/{self.experiment_data.get('PROJECT', {}).get('PROJECT_DESTINATION', 'git_project')}"
1870-
if Path(project_dir).joinpath(".git").exists():
1871-
with suppress(KeyError, ValueError, UnicodeDecodeError):
1872-
self.experiment_data["AUTOSUBMIT"]["WORKFLOW_COMMIT"] = subprocess.check_output(
1873-
"git rev-parse HEAD",
1874-
cwd=project_dir,
1875-
shell=True
1876-
).decode(locale.getpreferredencoding()).strip("\n")
1877+
if self.is_current_logged_user_owner:
1878+
project_dir = f"{self.experiment_data.get('ROOTDIR', '')}/proj/{self.experiment_data.get('PROJECT', {}).get('PROJECT_DESTINATION', 'git_project')}"
1879+
if Path(project_dir).joinpath(".git").exists():
1880+
with suppress(KeyError, ValueError, UnicodeDecodeError):
1881+
self.experiment_data["AUTOSUBMIT"]["WORKFLOW_COMMIT"] = subprocess.check_output(
1882+
"git rev-parse HEAD",
1883+
cwd=project_dir,
1884+
shell=True
1885+
).decode(locale.getpreferredencoding()).strip("\n")
18771886

18781887
def load_current_hpcarch_parameters(self) -> None:
18791888
"""
@@ -1889,7 +1898,7 @@ def save(self):
18891898
Saves the experiment data into the experiment_folder/conf/metadata folder as a yaml file
18901899
:return: True if the data has changed, False otherwise
18911900
"""
1892-
if self.is_current_real_user_owner:
1901+
if self.is_current_logged_user_owner:
18931902
if not self.metadata_folder.exists():
18941903
self.metadata_folder.mkdir(parents=True, exist_ok=True)
18951904
self.metadata_folder.chmod(0o755)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
setup(
2929
name="autosubmitconfigparser",
30-
version="1.0.78",
30+
version="1.0.79",
3131
author="Daniel Beltran Mora",
3232
author_email="[email protected]",
3333
description="An utility library that allows to read an Autosubmit 4 experiment configuration.",

test/unit/test_load_workflow_commit.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pathlib import Path
22
import subprocess
3+
4+
import pytest
35
from log.log import Log
46

57

@@ -15,10 +17,18 @@ def test_add_autosubmit_dict(autosubmit_config, mocker):
1517
Log.warning.assert_called_once()
1618

1719

18-
def test_load_workflow_commit(autosubmit_config, tmpdir):
20+
@pytest.mark.parametrize("is_owner", [True, False])
21+
def test_load_workflow_commit(autosubmit_config, tmpdir, mocker, is_owner):
22+
"""Test that the workflow commit is correctly injected into the experiment_data (Autosubmit will add it to the database)."""
1923
as_conf = autosubmit_config(
2024
expid='a000',
21-
experiment_data={})
25+
experiment_data={}
26+
)
27+
mocker.patch(
28+
"autosubmitconfigparser.config.configcommon.AutosubmitConfig.is_current_logged_user_owner",
29+
new_callable=mocker.PropertyMock, # This is needed for property mocking
30+
return_value=is_owner
31+
)
2232
as_conf.reload()
2333
assert "AUTOSUBMIT" in as_conf.experiment_data
2434
assert "WORKFLOW_COMMIT" not in as_conf.experiment_data["AUTOSUBMIT"]
@@ -33,12 +43,15 @@ def test_load_workflow_commit(autosubmit_config, tmpdir):
3343
project_dir = f"{as_conf.experiment_data.get('ROOTDIR', '')}/proj"
3444

3545
Path(project_dir).mkdir(parents=True, exist_ok=True)
36-
# Project root is third parent, ../../../ (zero-indexed).
46+
# Project root is third parent, ../../../.
3747
project_path = Path(__file__).parents[2]
3848
# git clone this project
3949
output = subprocess.check_output(f"git clone file://{project_path} git_project",
4050
cwd=project_dir, shell=True)
4151
assert output is not None
4252
as_conf.load_workflow_commit()
43-
assert "WORKFLOW_COMMIT" in as_conf.experiment_data["AUTOSUBMIT"]
44-
assert len(as_conf.experiment_data["AUTOSUBMIT"]["WORKFLOW_COMMIT"]) > 0
53+
if is_owner:
54+
assert "WORKFLOW_COMMIT" in as_conf.experiment_data["AUTOSUBMIT"]
55+
assert len(as_conf.experiment_data["AUTOSUBMIT"]["WORKFLOW_COMMIT"]) > 0
56+
else:
57+
assert "WORKFLOW_COMMIT" not in as_conf.experiment_data["AUTOSUBMIT"]

test/unit/test_properties.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
3+
import pytest
4+
5+
6+
@pytest.mark.parametrize("is_owner", [True, False])
7+
def test_is_current_real_user_owner(autosubmit_config, mocker, is_owner):
8+
"""Test if the SUDO_USER from the environment matches the owner of the current experiment."""
9+
as_conf = autosubmit_config(
10+
expid='a000',
11+
experiment_data={"ROOTDIR": "/dummy/rootdir", "AS_ENV_CURRENT_USER": "dummy"}
12+
)
13+
mocker.patch("pathlib.Path.owner", return_value="dummy" if is_owner else "otheruser")
14+
assert as_conf.is_current_real_user_owner == is_owner
15+
16+
17+
@pytest.mark.parametrize("is_owner", [True, False])
18+
def test_is_current_logged_user_owner(autosubmit_config, mocker, is_owner):
19+
"""Test if the USER from the environment matches the owner of the current experiment."""
20+
as_conf = autosubmit_config(
21+
expid='a000',
22+
experiment_data={"ROOTDIR": "/dummy/rootdir"}
23+
)
24+
os.environ["USER"] = "dummy" if is_owner else "otheruser"
25+
26+
mocker.patch("pathlib.Path.owner", return_value="dummy")
27+
assert as_conf.is_current_logged_user_owner == is_owner

test/unit/test_save.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import pytest
24
from pathlib import Path
35
from ruamel.yaml import YAML
@@ -17,10 +19,11 @@
1719
], ids=["local_true", "local_false"])
1820
def test_save(autosubmit_config, tmpdir, mocker, data, owner):
1921
data['ROOTDIR'] = tmpdir.strpath
22+
2023
if owner:
21-
data['AS_ENV_CURRENT_USER'] = Path(tmpdir).owner()
24+
os.environ["USER"] = Path(tmpdir).owner()
2225
else:
23-
data['AS_ENV_CURRENT_USER'] = 'whatever'
26+
os.environ["USER"] = 'whatever'
2427
as_conf = autosubmit_config(expid='t000', experiment_data=data)
2528
as_conf.save()
2629
if not owner:

0 commit comments

Comments
 (0)