Files
nomad_mcp/test_job_registration.py
2025-02-26 15:25:39 +07:00

100 lines
2.9 KiB
Python

#!/usr/bin/env python
"""
Test script to verify job registration with explicit namespace.
"""
import os
import sys
import uuid
from dotenv import load_dotenv
from app.services.nomad_client import NomadService
# Load environment variables from .env file
load_dotenv()
def get_test_job_spec(job_id):
"""Create a simple test job specification."""
return {
"ID": job_id,
"Name": job_id,
"Type": "service",
"Datacenters": ["jm"],
"Namespace": "development",
"Priority": 50,
"TaskGroups": [
{
"Name": "app",
"Count": 1,
"Tasks": [
{
"Name": "nginx",
"Driver": "docker",
"Config": {
"image": "nginx:latest",
"ports": ["http"],
},
"Resources": {
"CPU": 100,
"MemoryMB": 128
}
}
],
"Networks": [
{
"DynamicPorts": [
{
"Label": "http",
"Value": 0,
"To": 80
}
]
}
]
}
]
}
def main():
print("Testing Nomad job registration...")
# Check if NOMAD_ADDR is configured
nomad_addr = os.getenv("NOMAD_ADDR")
if not nomad_addr:
print("Error: NOMAD_ADDR is not configured in .env file.")
sys.exit(1)
print(f"Connecting to Nomad at: {nomad_addr}")
try:
# Initialize the Nomad service
nomad_service = NomadService()
# Create a unique job ID for testing
job_id = f"test-job-{uuid.uuid4().hex[:8]}"
print(f"Created test job ID: {job_id}")
# Create job specification
job_spec = get_test_job_spec(job_id)
print("Created job specification with explicit namespace: development")
# Start the job
print(f"Attempting to start job {job_id}...")
start_response = nomad_service.start_job(job_spec)
print(f"Job start response: {start_response}")
print(f"Job {job_id} started successfully!")
# Clean up - stop the job
print(f"Stopping job {job_id}...")
stop_response = nomad_service.stop_job(job_id, purge=True)
print(f"Job stop response: {stop_response}")
print(f"Job {job_id} stopped and purged successfully!")
print("\nNomad job registration test completed successfully.")
except Exception as e:
print(f"Error during job registration test: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
main()