Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example testing on env script #1440

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions smoke_tests/env_testing/run_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
from __future__ import annotations

import os
import sys
import time
from random import random
from uuid import UUID

from globus_compute_sdk import Client, Executor
from globus_sdk.services.auth.errors import AuthAPIError

TUTORIAL_EP_UUID = "4b116d3c-1703-4f8f-9f6f-39921e5864df"
WAIT_CLIENT_SEC = 8


def show_help(err_msg: str = None):
if err_msg:
print(err_msg)
print(
"Usage:\n python3 run_env.py "
+ "(prod|staging|preview|sandbox|test|integration) [optional_func_uuid]\n"
+ "The environment keyword can be abbreviated as long as the first "
"chars uniquely identify it. ie. pro for production, sa for sandbox.\n\n"
"If you specify an optional function UUID, that will be used. Otherwise "
"a new function that takes one argument and echos it as output is "
"registered and used to test.\n\n"
"Note providing existing FN_UUID yields KeyError TBD Fix"
)
sys.exit(1)


def echo_arg_tutorial_ep_testing(s):
return f"Hello Compute user, your argument is {s}"


ENV_UNIQUE = {
"preview": "pre",
"production": "pro",
"sandbox": "sa",
"test": "t",
"staging": "st",
"integration": "i",
}


def get_env(arg: str) -> str | None:
if arg:
arg = arg.lower()
for k, v in ENV_UNIQUE.items():
if arg.startswith(v) and k.startswith(arg):
return k
return None
Comment on lines +36 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, stealing it for my own scripts 👿



if __name__ == "__main__":
fn_id = None
if len(sys.argv) < 2 or len(sys.argv) > 3:
show_help()
elif len(sys.argv) == 3:
try:
fn_id = UUID(sys.argv[2])
except Exception:
show_help(f"Invalid function UUID: {sys.argv[2]}")

env_id = get_env(sys.argv[1])
if env_id is None:
show_help(f"Invalid/non-unique environment: {env_id}")

os.environ["GLOBUS_SDK_ENVIRONMENT"] = env_id
print(f"Testing on GLOBUS_SDK_ENVIRONMENT: ({env_id})")
try:
gcc = Client(environment=env_id)
except AuthAPIError as e:
print(
"Encountered AuthAPIError, you probably need to delete/refresh"
" your token for this environment (delete storage.db is quickest)"
)
print(e)
sys.exit(1)
Comment on lines +56 to +79
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest using argparse here instead


if fn_id is None:
print("Registering echo function...", end="")
fn_id = gcc.register_function(echo_arg_tutorial_ep_testing)
print(f"successfully with FN_UUID: {fn_id}")
else:
print(f"Using provided FN_UUID {fn_id}")

echo_arg = str(random())
print("Submitting task via Client...", end="")
try:
task_id = gcc.run(echo_arg, endpoint_id=TUTORIAL_EP_UUID, function_id=fn_id)
except KeyError:
print("TBD investigate KeyError")
raise
print(f"successfully with TASK_UUID: {task_id}, waiting {WAIT_CLIENT_SEC}s...")
time.sleep(WAIT_CLIENT_SEC)
result = gcc.get_result(task_id)
if echo_arg in result:
print(f"Client run result nominal: {result}")
else:
print(f"ERROR: Client run result was invalid: {result} ")
sys.exit(1)

print("Submitting task via Executor...", end="")
with Executor(client=gcc, endpoint_id=TUTORIAL_EP_UUID) as gce:
fut = gce.submit_to_registered_function(fn_id, args=(echo_arg,))
result = fut.result()
if echo_arg in result:
print(f"result nominal: {result}")
else:
print(f"ERROR: submit result was invalid: {result} ")
sys.exit(1)

print(f"Test finished successfully on ({env_id})!")
Loading