Skip to content

Commit 8d14efa

Browse files
authored
Fix the smoke test so it take the server URL from the config.toml (#3227)
# Description of Changes Closes https://github.com/clockworklabs/SpacetimeDBPrivate/issues/1998#event-19488677475 Now the smoke test parse the `config.toml` for the correct address for connecting. # API and ABI breaking changes <!-- If this is an API or ABI breaking change, please apply the corresponding GitHub label. --> # Expected complexity level and risk 1 # Testing - [x] Run the smoke both on public / private
1 parent adbfe3b commit 8d14efa

2 files changed

Lines changed: 39 additions & 25 deletions

File tree

smoketests/__init__.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,7 @@ def run():
270270
# and **not raise any exceptions to the caller**.
271271
return ReturnThread(run).join
272272

273-
# Make an HTTP call with `method` to `path`.
274-
#
275-
# If the response is 200, return the body.
276-
# Otherwise, throw an `Exception` constructed with two arguments, the response object and the body.
277-
def api_call(self, method, path, body = None, headers = {}):
273+
def get_server_address(self):
278274
with open(self.config_path, "rb") as f:
279275
config = tomllib.load(f)
280276
token = config['spacetimedb_token']
@@ -284,24 +280,35 @@ def api_call(self, method, path, body = None, headers = {}):
284280
raise Exception(f"Unable to find server in config with nickname {server_name}")
285281
host = server_config['host']
286282
protocol = server_config['protocol']
287-
conn = None
288-
if protocol == "http":
289-
conn = http.client.HTTPConnection(host)
290-
elif protocol == "https":
291-
conn = http.client.HTTPSConnection(host)
292-
else:
293-
raise Exception(f"Unknown protocol: {protocol}")
294-
auth = {"Authorization": f'Bearer {token}'}
295-
headers.update(auth)
296-
log_cmd([method, path])
297-
conn.request(method, path, body, headers)
298-
resp = conn.getresponse()
299-
body = resp.read()
300-
logging.debug(f"{resp.status} {body}")
301-
if resp.status != 200:
302-
raise Exception(resp, body)
303-
return body
304283

284+
return dict(host=host, protocol=protocol, token=token)
285+
286+
# Make an HTTP call with `method` to `path`.
287+
#
288+
# If the response is 200, return the body.
289+
# Otherwise, throw an `Exception` constructed with two arguments, the response object and the body.
290+
def api_call(self, method, path, body=None, headers={}):
291+
server = self.get_server_address()
292+
host = server["host"]
293+
protocol = server["protocol"]
294+
token = server["token"]
295+
conn = None
296+
if protocol == "http":
297+
conn = http.client.HTTPConnection(host)
298+
elif protocol == "https":
299+
conn = http.client.HTTPSConnection(host)
300+
else:
301+
raise Exception(f"Unknown protocol: {protocol}")
302+
auth = {"Authorization": f'Bearer {token}'}
303+
headers.update(auth)
304+
log_cmd([method, path])
305+
conn.request(method, path, body, headers)
306+
resp = conn.getresponse()
307+
body = resp.read()
308+
logging.debug(f"{resp.status} {body}")
309+
if resp.status != 200:
310+
raise Exception(resp, body)
311+
return body
305312

306313
@classmethod
307314
def write_module_code(cls, module_code):

smoketests/tests/quickstart.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ def _publish(self) -> Path:
8181
base_path = Path(self.enterClassContext(tempfile.TemporaryDirectory()))
8282
server_path = base_path / "server"
8383
self.project_path = server_path
84-
self.config_path = server_path / "config.toml"
8584

8685
self.generate_server(server_path)
8786
self.publish_module(f"quickstart-chat-{self.lang}", capture_stderr=True, clear=True)
@@ -125,6 +124,10 @@ def _test_quickstart(self):
125124
for src, dst in self.replacements.items():
126125
main = main.replace(src, dst)
127126
main += "\n" + self.extra_code
127+
server = self.get_server_address()
128+
host = server["host"]
129+
protocol = server["protocol"]
130+
main = main.replace("http://localhost:3000", f"{protocol}://{host}")
128131
_write_file(client_path / self.client_file, main)
129132

130133
self.check("", client_path, self.connected_str)
@@ -142,9 +145,11 @@ class Rust(BaseQuickstart):
142145
run_cmd = ["cargo", "run"]
143146
build_cmd = ["cargo", "build"]
144147

145-
# Replace the interactive user input to allow direct testing
146148
replacements = {
147-
"user_input_loop(&ctx)": "user_input_direct(&ctx)"
149+
# Replace the interactive user input to allow direct testing
150+
"user_input_loop(&ctx)": "user_input_direct(&ctx)",
151+
# Don't cache the token, because it will cause the test to fail if we run against a non-default server (because we don't cache the corresponding signing keypair)
152+
".with_token(creds_store()": "//.with_token(creds_store()"
148153
}
149154
extra_code = """
150155
fn user_input_direct(ctx: &DbConnection) {
@@ -194,6 +199,8 @@ class CSharp(BaseQuickstart):
194199
"InputLoop();": "UserInputDirect();",
195200
".OnConnect(OnConnected)": ".OnConnect(OnConnectedSignal)",
196201
".OnConnectError(OnConnectError)": ".OnConnectError(OnConnectErrorSignal)",
202+
# Don't cache the token, because it will cause the test to fail if we run against a non-default server (because we don't cache the corresponding signing keypair)
203+
".WithToken(AuthToken.Token)": "//.WithToken(AuthToken.Token)",
197204
"Main();": "" # To put the main function at the end so it can see the new functions
198205
}
199206
# So we can wait for the connection to be established...

0 commit comments

Comments
 (0)