2022-08-11 10:35:00 +00:00
|
|
|
import os
|
|
|
|
from subprocess import Popen
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import requests
|
|
|
|
from tests_app import _PROJECT_ROOT
|
|
|
|
|
|
|
|
from lightning_app.testing.testing import run_app_in_cloud
|
|
|
|
|
|
|
|
|
2022-09-12 14:47:24 +00:00
|
|
|
@pytest.mark.timeout(300)
|
2022-08-11 10:35:00 +00:00
|
|
|
@pytest.mark.cloud
|
|
|
|
def test_commands_and_api_example_cloud() -> None:
|
|
|
|
with run_app_in_cloud(os.path.join(_PROJECT_ROOT, "examples/app_commands_and_api")) as (
|
|
|
|
admin_page,
|
|
|
|
view_page,
|
|
|
|
fetch_logs,
|
|
|
|
_,
|
|
|
|
):
|
|
|
|
# 1: Collect the app_id
|
|
|
|
app_id = admin_page.url.split("/")[-1]
|
|
|
|
|
2022-08-31 19:59:03 +00:00
|
|
|
# 2: Connect to the App
|
2022-09-12 14:47:24 +00:00
|
|
|
Popen(f"python -m lightning connect {app_id} -y", shell=True).wait()
|
2022-08-31 19:59:03 +00:00
|
|
|
|
|
|
|
# 3: Send the first command with the client
|
2022-09-12 14:47:24 +00:00
|
|
|
cmd = "python -m lightning command with client --name=this"
|
2022-08-11 10:35:00 +00:00
|
|
|
Popen(cmd, shell=True).wait()
|
|
|
|
|
2022-08-31 19:59:03 +00:00
|
|
|
# 4: Send the second command without a client
|
2022-09-12 14:47:24 +00:00
|
|
|
cmd = "python -m lightning command without client --name=is"
|
2022-08-11 10:35:00 +00:00
|
|
|
Popen(cmd, shell=True).wait()
|
|
|
|
|
2022-09-12 14:47:24 +00:00
|
|
|
# This prevents some flakyness in the CI. Couldn't reproduce it locally.
|
|
|
|
sleep(5)
|
|
|
|
|
2022-08-31 19:59:03 +00:00
|
|
|
# 5: Send a request to the Rest API directly.
|
2022-08-11 10:35:00 +00:00
|
|
|
base_url = view_page.url.replace("/view", "").replace("/child_flow", "")
|
|
|
|
resp = requests.post(base_url + "/user/command_without_client?name=awesome")
|
|
|
|
assert resp.status_code == 200, resp.json()
|
|
|
|
|
|
|
|
# 5: Validate the logs.
|
|
|
|
has_logs = False
|
|
|
|
while not has_logs:
|
|
|
|
for log in fetch_logs():
|
|
|
|
if "['this', 'is', 'awesome']" in log:
|
|
|
|
has_logs = True
|
|
|
|
sleep(1)
|
2022-08-31 19:59:03 +00:00
|
|
|
|
|
|
|
# 5: Disconnect from the App
|
|
|
|
Popen("lightning disconnect", shell=True).wait()
|