forked from FailproofAI/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runtime_validation.py
More file actions
95 lines (73 loc) · 2.75 KB
/
Copy pathtest_runtime_validation.py
File metadata and controls
95 lines (73 loc) · 2.75 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pytest
from pydantic import BaseModel
from exospherehost.runtime import Runtime
from exospherehost.node.BaseNode import BaseNode
class GoodNode(BaseNode):
class Inputs(BaseModel):
name: str
class Outputs(BaseModel):
message: str
class Secrets(BaseModel):
api_key: str
async def execute(self):
return self.Outputs(message=f"hi {self.inputs.name}") # type: ignore
class BadNodeWrongInputsBase(BaseNode):
Inputs = object # not a pydantic BaseModel # type: ignore
class Outputs(BaseModel):
message: str
class Secrets(BaseModel):
token: str
async def execute(self):
return self.Outputs(message="x")
class BadNodeWrongTypes(BaseNode):
class Inputs(BaseModel):
count: int
class Outputs(BaseModel):
ok: bool
class Secrets(BaseModel):
secret: bytes
async def execute(self):
return self.Outputs(ok=True)
def test_runtime_missing_config_raises(monkeypatch):
# Ensure env vars not set
monkeypatch.delenv("EXOSPHERE_STATE_MANAGER_URI", raising=False)
monkeypatch.delenv("EXOSPHERE_API_KEY", raising=False)
with pytest.raises(ValueError):
Runtime(namespace="ns", name="rt", nodes=[GoodNode])
def test_runtime_with_env_ok(monkeypatch):
monkeypatch.setenv("EXOSPHERE_STATE_MANAGER_URI", "http://sm")
monkeypatch.setenv("EXOSPHERE_API_KEY", "k")
rt = Runtime(namespace="ns", name="rt", nodes=[GoodNode])
assert rt is not None
def test_runtime_invalid_params_raises(monkeypatch):
monkeypatch.setenv("EXOSPHERE_STATE_MANAGER_URI", "http://sm")
monkeypatch.setenv("EXOSPHERE_API_KEY", "k")
with pytest.raises(ValueError):
Runtime(namespace="ns", name="rt", nodes=[GoodNode], batch_size=0)
with pytest.raises(ValueError):
Runtime(namespace="ns", name="rt", nodes=[GoodNode], workers=0)
def test_node_validation_errors(monkeypatch):
monkeypatch.setenv("EXOSPHERE_STATE_MANAGER_URI", "http://sm")
monkeypatch.setenv("EXOSPHERE_API_KEY", "k")
with pytest.raises(ValueError) as e:
Runtime(namespace="ns", name="rt", nodes=[BadNodeWrongInputsBase])
assert "Inputs class that inherits" in str(e.value)
with pytest.raises(ValueError) as e2:
Runtime(namespace="ns", name="rt", nodes=[BadNodeWrongTypes])
msg = str(e2.value)
assert "Inputs field" in msg and "Outputs field" in msg and "Secrets field" in msg
def test_duplicate_node_names_raise(monkeypatch):
monkeypatch.setenv("EXOSPHERE_STATE_MANAGER_URI", "http://sm")
monkeypatch.setenv("EXOSPHERE_API_KEY", "k")
class AnotherGood(BaseNode):
class Inputs(BaseModel):
name: str
class Outputs(BaseModel):
message: str
class Secrets(BaseModel):
api_key: str
async def execute(self):
return self.Outputs(message="ok")
AnotherGood.__name__ = "GoodNode" # force duplicate name
with pytest.raises(ValueError):
Runtime(namespace="ns", name="rt", nodes=[GoodNode, AnotherGood])