2022-07-29 14:44:52 +00:00
|
|
|
import os
|
2022-11-24 17:21:32 +00:00
|
|
|
from unittest import mock
|
2022-07-29 14:44:52 +00:00
|
|
|
|
2022-11-04 17:41:59 +00:00
|
|
|
import pytest
|
2023-01-12 15:13:52 +00:00
|
|
|
from integrations_app.public import _PATH_EXAMPLES
|
2022-12-16 09:49:17 +00:00
|
|
|
from lightning_utilities.core.imports import package_available
|
2022-07-29 14:44:52 +00:00
|
|
|
|
2023-02-01 11:07:00 +00:00
|
|
|
from lightning.app.testing.helpers import _RunIf
|
|
|
|
from lightning.app.testing.testing import application_testing, LightningTestApp
|
2022-07-29 14:44:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LightningTestMultiNodeApp(LightningTestApp):
|
|
|
|
def on_before_run_once(self):
|
|
|
|
res = super().on_before_run_once()
|
2022-11-04 17:41:59 +00:00
|
|
|
if self.works and all(w.has_stopped for w in self.works):
|
2022-12-16 09:49:17 +00:00
|
|
|
assert len(self.works) == 2
|
2022-07-29 14:44:52 +00:00
|
|
|
return True
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2022-12-16 09:49:17 +00:00
|
|
|
# for the skip to work, the package needs to be installed without editable mode
|
|
|
|
_SKIP_LIGHTNING_UNAVAILABLE = pytest.mark.skipif(not package_available("lightning"), reason="script requires lightning")
|
2022-11-04 17:41:59 +00:00
|
|
|
|
|
|
|
|
2022-11-07 09:36:41 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"app_name",
|
|
|
|
[
|
2022-11-08 12:55:31 +00:00
|
|
|
"train_pytorch.py",
|
|
|
|
"train_any.py",
|
|
|
|
"train_pytorch_spawn.py",
|
2023-01-04 15:57:18 +00:00
|
|
|
pytest.param("train_fabric.py", marks=_SKIP_LIGHTNING_UNAVAILABLE),
|
2022-12-16 09:49:17 +00:00
|
|
|
pytest.param("train_lt_script.py", marks=_SKIP_LIGHTNING_UNAVAILABLE),
|
|
|
|
pytest.param("train_lt.py", marks=_SKIP_LIGHTNING_UNAVAILABLE),
|
2022-11-07 09:36:41 +00:00
|
|
|
],
|
|
|
|
)
|
2022-12-16 09:49:17 +00:00
|
|
|
@_RunIf(skip_windows=True) # flaky
|
2023-02-01 11:07:00 +00:00
|
|
|
@mock.patch("lightning.app.components.multi_node.base.is_running_in_cloud", return_value=True)
|
2022-11-24 17:21:32 +00:00
|
|
|
def test_multi_node_examples(_, app_name, monkeypatch):
|
2022-12-16 09:49:17 +00:00
|
|
|
# note: this test will fail locally:
|
2023-02-01 11:07:00 +00:00
|
|
|
# * if you installed `lightning.app`, then the examples need to be
|
|
|
|
# rewritten to use `lightning.app` imports (CI does this)
|
2022-12-16 09:49:17 +00:00
|
|
|
# * if you installed `lightning`, then the imports in this file and mocks
|
|
|
|
# need to be changed to use `lightning`.
|
2022-11-22 19:04:29 +00:00
|
|
|
monkeypatch.chdir(os.path.join(_PATH_EXAMPLES, "app_multi_node"))
|
2022-12-13 13:13:51 +00:00
|
|
|
command_line = [app_name, "--blocking", "False", "--open-ui", "False", "--setup"]
|
2022-12-16 09:49:17 +00:00
|
|
|
result = application_testing(LightningTestMultiNodeApp, command_line)
|
2022-11-04 17:41:59 +00:00
|
|
|
assert result.exit_code == 0
|