|  | 
|  | 1 | +import tempfile | 
|  | 2 | +from pathlib import Path | 
|  | 3 | +from unittest import mock | 
|  | 4 | +from unittest.mock import call | 
|  | 5 | + | 
| 1 | 6 | import pytest | 
| 2 | 7 | from aiohttp import web | 
| 3 | 8 | 
 | 
| 4 | 9 | from aleph.vm.conf import settings | 
| 5 | 10 | from aleph.vm.orchestrator.supervisor import setup_webapp | 
|  | 11 | +from aleph.vm.sevclient import SevClient | 
| 6 | 12 | 
 | 
| 7 | 13 | 
 | 
| 8 | 14 | @pytest.mark.asyncio | 
| @@ -121,3 +127,47 @@ def get_persistent_executions(self): | 
| 121 | 127 |     ) | 
| 122 | 128 |     assert response.status == 200 | 
| 123 | 129 |     assert await response.json() == {"success": True, "successful": [], "failing": [], "errors": {}} | 
|  | 130 | + | 
|  | 131 | + | 
|  | 132 | +@pytest.mark.asyncio | 
|  | 133 | +async def test_about_certificates_missing_setting(aiohttp_client): | 
|  | 134 | +    """Test that the certificates system endpoint returns an error if the setting isn't enabled""" | 
|  | 135 | +    settings.ENABLE_CONFIDENTIAL_COMPUTING = False | 
|  | 136 | + | 
|  | 137 | +    app = setup_webapp() | 
|  | 138 | +    app["sev_client"] = SevClient(Path().resolve()) | 
|  | 139 | +    client = await aiohttp_client(app) | 
|  | 140 | +    response: web.Response = await client.get("/about/certificates") | 
|  | 141 | +    assert response.status == 400 | 
|  | 142 | +    assert await response.text() == "400: Confidential computing setting not enabled on that server" | 
|  | 143 | + | 
|  | 144 | + | 
|  | 145 | +@pytest.mark.asyncio | 
|  | 146 | +async def test_about_certificates(aiohttp_client): | 
|  | 147 | +    """Test that the certificates system endpoint responds. No auth needed""" | 
|  | 148 | + | 
|  | 149 | +    settings.ENABLE_QEMU_SUPPORT = True | 
|  | 150 | +    settings.ENABLE_CONFIDENTIAL_COMPUTING = True | 
|  | 151 | +    settings.setup() | 
|  | 152 | + | 
|  | 153 | +    with mock.patch( | 
|  | 154 | +        "pathlib.Path.is_file", | 
|  | 155 | +        return_value=False, | 
|  | 156 | +    ) as is_file_mock: | 
|  | 157 | +        with mock.patch( | 
|  | 158 | +            "aleph.vm.sevclient.run_in_subprocess", | 
|  | 159 | +            return_value=True, | 
|  | 160 | +        ) as export_mock: | 
|  | 161 | +            with tempfile.TemporaryDirectory() as tmp_dir: | 
|  | 162 | +                app = setup_webapp() | 
|  | 163 | +                sev_client = SevClient(Path(tmp_dir)) | 
|  | 164 | +                app["sev_client"] = sev_client | 
|  | 165 | +                # Create mock file to return it | 
|  | 166 | +                Path(sev_client.certificates_archive).touch(exist_ok=True) | 
|  | 167 | + | 
|  | 168 | +                client = await aiohttp_client(app) | 
|  | 169 | +                response: web.Response = await client.get("/about/certificates") | 
|  | 170 | +                assert response.status == 200 | 
|  | 171 | +                is_file_mock.assert_has_calls([call(), call()]) | 
|  | 172 | +                certificates_expected_dir = sev_client.certificates_archive | 
|  | 173 | +                export_mock.assert_called_once_with(["sevctl", "export", str(certificates_expected_dir)], check=True) | 
0 commit comments