| """Cloud-init `user_data` generator — pure-string tests.""" |
|
|
| from __future__ import annotations |
|
|
| import pytest |
| import yaml |
|
|
| from mindxtrain.deploy.cloud_init import BOOTSTRAP_SENTINEL, render |
|
|
|
|
| def test_render_default_is_valid_yaml() -> None: |
| text = render() |
| |
| assert text.startswith("#cloud-config\n") |
| |
| body = yaml.safe_load(text) |
| assert "runcmd" in body |
| assert "packages" in body |
| assert "git" in body["packages"] |
| assert "podman" in body["packages"] |
|
|
|
|
| def test_render_sets_bootstrap_sentinel() -> None: |
| text = render() |
| assert BOOTSTRAP_SENTINEL == "/workspace/mindxtrain/.bootstrap-done" |
| assert BOOTSTRAP_SENTINEL in text |
| |
| assert f"touch {BOOTSTRAP_SENTINEL}" in text |
|
|
|
|
| def test_render_clones_specified_repo_and_branch() -> None: |
| text = render(repo="someowner/somerepo", branch="dev") |
| assert "https://github.com/someowner/somerepo.git" in text |
| assert "--branch dev" in text |
|
|
|
|
| def test_render_run_bench_false_omits_mindxtrain_bench() -> None: |
| text = render(run_bench=False) |
| assert "mindxtrain bench" not in text |
| |
| assert "pip install -e .[" in text |
|
|
|
|
| @pytest.mark.parametrize( |
| "field,bad", |
| [ |
| ("repo", "evil; rm -rf /"), |
| ("repo", "a b/c"), |
| ("branch", "main$(ls)"), |
| ("container", "a`b`c"), |
| ("extras", "ml,eval | rm -rf /"), |
| ("remote_path", "/tmp/$(whoami)"), |
| ], |
| ) |
| def test_render_rejects_shell_injectable_inputs(field: str, bad: str) -> None: |
| kwargs = {field: bad} |
| with pytest.raises(ValueError, match="cloud-init"): |
| render(**kwargs) |
|
|
|
|
| def test_render_with_safe_special_chars() -> None: |
| |
| text = render(container="rocm/primus:v26.2", repo="org-name/repo.name") |
| assert "rocm/primus:v26.2" in text |
| assert "org-name/repo.name" in text |
|
|