summaryrefslogtreecommitdiffhomepage
path: root/test/unit/applications/lang/wasm_component.py
blob: a6c8dd140fc5c32d65eb548d977c1e6597b5f0d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from pathlib import Path
import shutil
import subprocess
from urllib.parse import quote

from unit.applications.proto import ApplicationProto
from unit.option import option


class ApplicationWasmComponent(ApplicationProto):
    @staticmethod
    def prepare_env(script):
        try:
            subprocess.check_output(
                ['cargo', 'component', '--help'],
                stderr=subprocess.STDOUT,
            )
        except (subprocess.CalledProcessError, FileNotFoundError):
            return None

        temp_dir = Path(f'{option.temp_dir}/wasm_component/')

        if not temp_dir.exists():
            temp_dir.mkdir()

        app_path = f'{temp_dir}/{script}'

        shutil.copytree(f'{option.test_dir}/wasm_component/{script}', app_path)

        try:
            output = subprocess.check_output(
                ['cargo', 'component', 'build', '--release'],
                cwd=app_path,
                stderr=subprocess.STDOUT,
            )
        except KeyboardInterrupt:
            raise

        except subprocess.CalledProcessError:
            return None

        return output

    def load(self, script, **kwargs):
        self.prepare_env(script)

        component_path = f'{option.temp_dir}/wasm_component/{script}/target/wasm32-wasip1/release/test_wasi_component.wasm'

        self._load_conf(
            {
                "listeners": {
                    "*:8080": {"pass": f"applications/{quote(script, '')}"}
                },
                "applications": {
                    script: {
                        "type": "wasm-wasi-component",
                        "processes": {"spare": 0},
                        "component": component_path,
                    }
                },
            },
            **kwargs,
        )