Skip to content

Commit

Permalink
Add import testing to detect runtime misconfigurations
Browse files Browse the repository at this point in the history
  • Loading branch information
nusantara-self committed Feb 10, 2025
1 parent 306d28d commit a7f003b
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from os.path import isfile, join, basename
import tempfile
from docker.errors import BuildError

import subprocess
import textwrap
import re

class Registry:
def __init__(self, client, registry_string, default_registry):
Expand Down Expand Up @@ -35,6 +37,56 @@ def login(self):
def last_build_commit(self, namespace, repo, tag):
return None

def test_imports(self, image_tag, command, worker_name):
print("\nπŸ” Testing Python imports in built image...")
test_code = textwrap.dedent(f'''
import os, sys, re, os.path as osp
entrypoint_full = "{command}"
# Use the worker folder as a fallback if the directory from the command isn't found
fallback_dir = "{worker_name}"
if "/" in entrypoint_full:
dir_part = osp.dirname(entrypoint_full)
file_part = osp.basename(entrypoint_full)
# if the directory part does not exist, try fallback
if not os.path.isdir(dir_part) and os.path.isdir(fallback_dir):
dir_part = fallback_dir
os.chdir(osp.join(os.getcwd(), dir_part))
entrypoint = file_part
else:
entrypoint = entrypoint_full
if not os.path.exists(entrypoint):
print("❌ ERROR: {{}} not found inside the container.".format(entrypoint))
sys.exit(1)
with open(entrypoint, 'r', encoding='utf-8') as f:
content = f.readlines()
imports = [line.strip() for line in content if re.match(r'^\\s*(import|from)\\s+[a-zA-Z0-9_.]+', line)]
print("πŸ” Checking Python imports from", entrypoint)
for imp in imports:
try:
exec(imp)
print("βœ…", imp, "- SUCCESS")
except Exception as e:
print("❌", imp, "- FAILED:", e)
sys.exit(1)
print("βœ… All imports tested successfully!")
''')
try:
result = subprocess.run(
["docker", "run", "--rm", "--entrypoint", "python", image_tag, "-c", test_code],
capture_output=True, text=True
)
print(result.stdout)
if result.returncode != 0:
print("⚠️ Import testing FAILED with exit code", result.returncode)
else:
print("βœ… Import testing succeeded")
except Exception as e:
print("Error during import testing:", e)

def build_docker(self, namespace, base_path, worker_path, flavor, git_commit_sha):
worker_name = basename(worker_path)

Expand Down Expand Up @@ -98,7 +150,13 @@ def build(dockerfile):
f.flush()
try:
build(f.name)
image_tag = f"{namespace}/{flavor['repo']}"
print(f"Build succeeded for worker {worker_name} using base image {base}.")
# Test the imports before pushing.
try:
self.test_imports(image_tag, flavor['command'], worker_name)
except Exception as e:
print("Import testing encountered an error:", e)
return # Build succeeded; exit the function
except BuildError as be:
print(f"BuildError encountered with base image {base} for worker {worker_name}.")
Expand Down

0 comments on commit a7f003b

Please sign in to comment.