/ tests / conftest.py
conftest.py
  1  """Common fixtures."""
  2  
  3  from __future__ import annotations
  4  
  5  import pathlib
  6  import typing
  7  from typing import Callable
  8  
  9  import aiida
 10  import aiida.orm
 11  import pytest
 12  
 13  import hpclb
 14  
 15  pytest_plugins = ["aiida.tools.pytest_fixtures"]
 16  
 17  
 18  def pytest_collection_modifyitems(
 19      session: pytest.Session,  # noqa: ARG001  # signature determined by pytest
 20      config: pytest.Config,
 21      items: list[pytest.Item],
 22  ) -> None:
 23      """Skip tests that require the f7t cluster container fleet by default."""
 24      if not config.getoption("-m"):
 25          skip_me = pytest.mark.skip(reason="use `-m cluster` to run this test")
 26          for item in items:
 27              if "cluster" in item.keywords:
 28                  item.add_marker(skip_me)
 29  
 30  
 31  @pytest.fixture
 32  def example_code(
 33      aiida_computer_local: typing.Callable[[], aiida.orm.Computer],
 34      aiida_code_installed: typing.Callable[..., aiida.orm.InstalledCode],
 35  ) -> aiida.orm.InstalledCode:
 36      """Create an mock code."""
 37      code = aiida_code_installed(
 38          default_calc_job_plugin="hpclb.generic", computer=aiida_computer_local()
 39      )
 40      code.store()
 41      return code
 42  
 43  
 44  @pytest.fixture
 45  def example_targetdir(tmp_path: pathlib.Path) -> aiida.orm.JsonableData:
 46      """Construct an example targetdir data node (wrapped in JsonableData)."""
 47      staging = tmp_path
 48      config_file = staging / "foo.config"
 49      config_file.write_text("a = 1\nb = 2\n")
 50      return aiida.orm.JsonableData(
 51          hpclb.aiida.data.TargetDir(
 52              name="root",
 53              subdirs=[
 54                  hpclb.aiida.data.TargetDir(
 55                      name="config",
 56                      upload=[
 57                          hpclb.aiida.data.UploadFile(
 58                              source=config_file,
 59                              input_label="config_file",
 60                              tgt_name="input.config",
 61                          )
 62                      ],
 63                  ),
 64                  hpclb.aiida.data.TargetDir(name="out-files"),
 65              ],
 66              remote=[
 67                  hpclb.aiida.data.RemotePath(
 68                      src_path=pathlib.Path("/some/absolute/path/somefile.xml"),
 69                      tgt_name="data.xml",
 70                      copy=True,
 71                  ),
 72                  hpclb.aiida.data.RemotePath(
 73                      src_path=pathlib.Path("/some/path/to/dir"),
 74                      tgt_name="datadir",
 75                      copy=False,
 76                  ),
 77              ],
 78          )
 79      )
 80  
 81  
 82  @pytest.fixture
 83  def cluster(aiida_computer: Callable[..., aiida.orm.Computer]) -> aiida.orm.Computer:
 84      """Set up the f7t test cluster computer."""
 85      comp = aiida_computer(
 86          label="testcluster",
 87          hostname="localhost",
 88          transport_type="firecrest",
 89          scheduler_type="firecrest",
 90          # Yes, the following are hardcoded credentials. No, they are not dangerous.
 91          # They are for the test cluster containers, not for a real system.
 92          configuration_kwargs={
 93              "url": "http://localhost:8000",
 94              "token_uri": "http://localhost:8080/auth/realms/kcrealm/protocol/openid-connect/token",
 95              "client_id": "firecrest-test-client",
 96              "client_secret": "wZVHVIEd9dkJDh9hMKc6DTvkqXxnDttk",
 97              "compute_resource": "cluster-slurm-api",
 98              "billing_account": "myproject",
 99              "temp_directory": "/home/fireuser/f7temp",
100              "max_io_allowed": 8,
101              "checksum_check": False,
102          },
103      )
104      dummy = pathlib.Path(__file__).parent / "data" / "dummy.sh"
105      transport = comp.get_transport()
106      transport.put(localpath=dummy, remotepath=pathlib.Path("/home/fireuser/dummy.sh"))
107      transport.chmod(path=pathlib.Path("/home/fireuser/dummy.sh"), mode=755)
108      return comp
109  
110  
111  @pytest.fixture
112  def cluster_dummy(
113      cluster: aiida.orm.Computer,
114      aiida_code_installed: Callable[..., aiida.orm.InstalledCode],
115  ) -> aiida.orm.InstalledCode:
116      """Set up the dummy code on the test cluster."""
117      return aiida_code_installed(
118          label="dummy",
119          default_calc_job_plugin="hpclb.generic",
120          computer=cluster,
121          filepath_executable="/home/fireuser/dummy.sh",
122          with_mpi=True,
123      )