-
Notifications
You must be signed in to change notification settings - Fork 1
/
antithesis_sdk.py
226 lines (190 loc) · 6 KB
/
antithesis_sdk.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import os
import json
"""
Quick wrapper around the fallback SDK
https://antithesis.com/docs/using_antithesis/sdk/fallback/assert.html
"""
class antithesis_fallback_sdk:
def __init__(self):
if "ANTITHESIS_OUTPUT_DIR" not in os.environ:
raise Exception(
"The environment variable ANTITHESIS_OUTPUT_DIR is not defined"
)
self.sdk_location = f"{os.environ['ANTITHESIS_OUTPUT_DIR']}/sdk.jsonl"
self.assertion_types = ["always", "sometimes", "unreachable", "reachable"]
def _get_assertion_evaulate(self) -> dict:
always = {
"hit": True,
"must_hit": True,
"condition": True,
}
sometimes = {
"hit": True,
"must_hit": True,
"condition": True,
}
reachable = {
"hit": True,
"must_hit": True,
"condition": True,
}
unreachable = {
"hit": True,
"must_hit": False,
"condition": False,
}
return {
"always": always,
"sometimes": sometimes,
"reachable": reachable,
"unreachable": unreachable,
}
def _get_assertion_declare(self):
"""
table of metadata for different assertions
"""
always = {
"hit": False,
"must_hit": True,
"assert_type": "always",
"display_type": "Always",
"condition": False,
"details": None,
}
sometimes = {
"hit": False,
"must_hit": True,
"assert_type": "sometimes",
"display_type": "Sometimes",
"condition": False,
"details": None,
}
unreachable = {
"hit": False,
"must_hit": False,
"assert_type": "reachability",
"display_type": "Unreachable",
"condition": False,
"details": None,
}
reachable = {
"hit": False,
"must_hit": True,
"assert_type": "reachability",
"display_type": "Reachable",
"condition": False,
"details": None,
}
return {
"always": always,
"sometimes": sometimes,
"unreachable": unreachable,
"reachable": reachable,
}
def _declare(self, assert_type: str, id: str, message: str) -> dict:
"""
Make the assertion declaration
"""
if assert_type not in self.assertion_types:
raise Exception(
f"The assertion type must be one of {', '.join(self.assertion_types)}"
)
assertions_declare = self._get_assertion_declare()
payload = {"antithesis_assert": assertions_declare[assert_type]}
# Stubbing these out for now
location = {
"class": "",
"function": "",
"file": "",
"begin_line": 0,
"begin_column": 0,
}
payload["antithesis_assert"]["location"] = location
payload["antithesis_assert"]["id"] = id
payload["antithesis_assert"]["message"] = message
return payload
def declare(self, assert_type: str, id: str, message: str) -> None:
payload = self._declare(assert_type, id, message)
self._write_to_json(payload)
def do_assert(
self,
assert_type: str,
id: str,
message: str,
condition: bool,
details: dict | None = None,
) -> None:
"""
Write the assert evaulation to the event sink
"""
# Build the base payload
payload = self._declare(assert_type, id, message)
# Change the values based on evaulation
assert_eval = self._get_assertion_evaulate()[assert_type]
antithesis_assert = payload["antithesis_assert"]
antithesis_assert.update(assert_eval)
payload["antithesis_assert"] = antithesis_assert
payload["antithesis_assert"]["condition"] = condition
if bool(details):
payload["antithesis_assert"]["details"] = details
self._write_to_json(payload)
def always(
self,
declare: bool,
id: str,
message: str,
condition: bool = False,
details: dict | None = None,
):
if declare:
self.declare("always", id, message)
else:
self.do_assert("always", id, message, condition, details)
def sometimes(
self,
declare: bool,
id: str,
message: str,
condition: bool = False,
details: dict | None = None,
):
if declare:
self.declare("sometimes", id, message)
else:
self.do_assert("sometimes", id, message, condition, details)
def reachable(
self,
declare: bool,
id: str,
message: str,
condition: bool = False,
details: dict | None = None,
):
if declare:
self.declare("reachable", id, message)
else:
self.do_assert("reachable", id, message, condition, details)
def unreachable(
self,
declare: bool,
id: str,
message: str,
condition: bool = False,
details: dict | None = None,
):
if declare:
self.declare("unreachable", id, message)
else:
self.do_assert("unreachable", id, message, condition, details)
def _write_to_json(self, assertion_obj: dict) -> None:
"""
Appending the data to json
"""
with open(self.sdk_location, "a") as fh:
fh.write(f"{json.dumps(assertion_obj)}\n")
def get_random_int(self, bytes=1) -> int:
random_bytes = os.urandom(bytes)
return int.from_bytes(random_bytes, byteorder="big")
def setup_complete(self, details: dict = {}) -> None:
message = {"antithesis_setup": {"status": "complete", "details": details}}
self._write_to_json(message)