|
| 1 | +import os |
| 2 | +import select |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import threading |
| 6 | +import unittest |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +try: |
| 10 | + import pytest |
| 11 | +except ImportError: |
| 12 | + |
| 13 | + class pytest: |
| 14 | + class _Mark: |
| 15 | + @classmethod |
| 16 | + def __getattr__(cls, item): |
| 17 | + return lambda x: x |
| 18 | + |
| 19 | + mark = _Mark() |
| 20 | + |
| 21 | + |
| 22 | +import probe_check |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.buildonlytest |
| 26 | +class TestStartup(unittest.TestCase): |
| 27 | + def setUp(self): |
| 28 | + self.project_name = "projectName" |
| 29 | + self.notebook_id = "notebookId" |
| 30 | + self.translated_username = "sometranslatedUsername" |
| 31 | + self.origin = "https://origin" |
| 32 | + self.root_dir = Path(__file__).parent |
| 33 | + |
| 34 | + def get_notebook_args_configuration(self) -> str: |
| 35 | + expected_args = " ".join( |
| 36 | + [ |
| 37 | + "--ServerApp.port=8888", |
| 38 | + "--ServerApp.token=''", |
| 39 | + "--ServerApp.password=''", |
| 40 | + f"--ServerApp.base_url=/notebook/{self.project_name}/{self.notebook_id}", |
| 41 | + "--ServerApp.quit_button=False", |
| 42 | + f'--ServerApp.tornado_settings={{"user":"{self.translated_username}","hub_host":"{self.origin}","hub_prefix":"/projects/{self.project_name}"}}', |
| 43 | + ] |
| 44 | + ) |
| 45 | + return expected_args |
| 46 | + |
| 47 | + def get_environment_variables(self) -> dict[str, str]: |
| 48 | + notebook_args = self.get_notebook_args_configuration() |
| 49 | + nb_prefix = f"/notebook/{self.project_name}/{self.notebook_id}" |
| 50 | + return { |
| 51 | + # (outdated?) https://github.com/opendatahub-io/odh-dashboard/blob/2.4.0-release/backend/src/utils/notebookUtils.ts#L284-L293 |
| 52 | + # https://github.com/opendatahub-io/odh-dashboard/blob/1d5a9065c10acc4706b84b06c67f27f16cf6dee7/frontend/src/api/k8s/notebooks.ts#L157-L170 |
| 53 | + "NOTEBOOK_ARGS": notebook_args, |
| 54 | + # NB_PREFIX is set by notebook-controller and codeserver scripting depends on it |
| 55 | + # https://github.com/opendatahub-io/kubeflow/blob/f924a96375988fe3801db883e99ce9ed1ab5939c/components/notebook-controller/controllers/notebook_controller.go#L417 |
| 56 | + "NB_PREFIX": nb_prefix, |
| 57 | + } |
| 58 | + |
| 59 | + def test_codeserver_startup(self): |
| 60 | + env = self.get_environment_variables() |
| 61 | + command = ["/opt/app-root/bin/run-code-server.sh"] |
| 62 | + p = subprocess.Popen( |
| 63 | + command, |
| 64 | + # KFLUXSPRT-5139: https://redhat-internal.slack.com/archives/C04PZ7H0VA8/p1758128206734419 |
| 65 | + stdout=subprocess.PIPE, |
| 66 | + stderr=subprocess.STDOUT, |
| 67 | + env={**os.environ, **env}, |
| 68 | + ) |
| 69 | + thr = threading.Thread(target=pump_stream, args=(p,), daemon=True) |
| 70 | + thr.start() |
| 71 | + |
| 72 | + try: |
| 73 | + ret = probe_check.main(self.project_name, self.notebook_id, "8888") |
| 74 | + self.assertEqual(ret, 0, "Probe check should return 0") |
| 75 | + self.assertEqual(p.poll(), None, "Code server process should still be running") |
| 76 | + finally: |
| 77 | + p.terminate() |
| 78 | + p.wait() |
| 79 | + |
| 80 | + thr.join() |
| 81 | + |
| 82 | + |
| 83 | +def pump_stream(process: subprocess.Popen) -> None: |
| 84 | + """Copy everything from stream -> stdout.""" |
| 85 | + # this may stop pumping even if there's something still buffered, so be it |
| 86 | + while process.returncode is None: |
| 87 | + rl, _, _ = select.select([process.stdout], [], [], 1) |
| 88 | + if rl: |
| 89 | + chunk = process.stdout.read1() |
| 90 | + sys.stdout.buffer.write(chunk) |
| 91 | + sys.stdout.buffer.flush() |
| 92 | + process.stdout.close() |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + unittest.main(verbosity=2) |
0 commit comments