File size: 4,718 Bytes
71ad4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
Test script to verify HuggingFace deployment configuration
This script checks the common issues that might occur in a Docker deployment.
"""

import os
import sys
import tempfile
import importlib.util
import requests

def check_tmp_writable():
    """Check if /tmp directory is writable"""
    print("Checking if /tmp directory is writable...")
    try:
        with tempfile.NamedTemporaryFile(dir='/tmp', prefix='enflow_test_') as tmp:
            tmp.write(b'test')
            print(f"βœ… Successfully wrote to temp file: {tmp.name}")
        print("βœ… /tmp is writable")
        return True
    except Exception as e:
        print(f"❌ Could not write to /tmp: {str(e)}")
        return False

def check_tmp_subdirs():
    """Check if we can create subdirectories in /tmp"""
    print("\nChecking if we can create subdirectories in /tmp...")
    try:
        test_dir = '/tmp/enflow_test_dir'
        if os.path.exists(test_dir):
            os.rmdir(test_dir)
        os.makedirs(test_dir)
        print(f"βœ… Successfully created directory: {test_dir}")
        
        # Try to write a file in the new directory
        test_file = os.path.join(test_dir, 'test.txt')
        with open(test_file, 'w') as f:
            f.write('test')
        print(f"βœ… Successfully wrote to file in test directory: {test_file}")
        
        # Clean up
        os.remove(test_file)
        os.rmdir(test_dir)
        print("βœ… Successfully cleaned up test directory")
        return True
    except Exception as e:
        print(f"❌ Could not create/write to subdirectory in /tmp: {str(e)}")
        return False

def check_env_variables():
    """Check if environment variables are accessible"""
    print("\nChecking environment variables...")
    required_vars = [
        'MONGO_URI', 
        'JWT_SECRET', 
        'CLOUDINARY_CLOUD_NAME', 
        'CLOUDINARY_API_KEY', 
        'CLOUDINARY_API_SECRET', 
        'OPENAI_API_KEY'
    ]
    
    all_present = True
    for var in required_vars:
        if os.environ.get(var):
            print(f"βœ… {var} is set")
        else:
            print(f"❌ {var} is not set")
            all_present = False
    
    return all_present

def check_flask_app():
    """Check if Flask app can be imported"""
    print("\nChecking if Flask app can be imported...")
    try:
        # Add current directory to sys.path if needed
        if os.getcwd() not in sys.path:
            sys.path.append(os.getcwd())
            
        # Try importing the app module
        spec = importlib.util.find_spec('app')
        if spec is None:
            print("❌ Could not find app module")
            return False
            
        from app import app
        print("βœ… Flask app imported successfully")
        return True
    except Exception as e:
        print(f"❌ Could not import Flask app: {str(e)}")
        return False

def check_test_endpoint():
    """Try to start the Flask app and test the endpoint"""
    print("\nChecking if the test endpoint works...")
    
    # This is a simplified test - in a real scenario, you might want to
    # start the Flask app in a separate process and then make the request
    
    try:
        # Check if the app is already running on localhost
        response = requests.get('http://localhost:5000/api/test', timeout=2)
        if response.status_code == 200:
            print(f"βœ… Test endpoint returned: {response.json()}")
            return True
    except requests.exceptions.RequestException:
        print("ℹ️ App not currently running on localhost, skipping endpoint test")
    
    print("ℹ️ To fully test the endpoints, start the app separately with 'python app.py'")
    return True  # Return True even if we skipped the test to avoid failing the overall check

def main():
    """Run all checks"""
    print("=== Enflow HuggingFace Deployment Test ===\n")
    
    # Run checks
    tmp_writable = check_tmp_writable()
    tmp_subdirs = check_tmp_subdirs()
    env_vars = check_env_variables()
    flask_app = check_flask_app()
    test_endpoint = check_test_endpoint()
    
    # Summary
    print("\n=== Test Summary ===")
    print(f"βœ… /tmp writable: {tmp_writable}")
    print(f"βœ… Can create subdirs in /tmp: {tmp_subdirs}")
    print(f"βœ… Environment variables: {env_vars}")
    print(f"βœ… Flask app importable: {flask_app}")
    print(f"βœ… Test endpoint check: {test_endpoint}")
    
    if all([tmp_writable, tmp_subdirs, env_vars, flask_app]):
        print("\nβœ… All tests passed! The app should work on HuggingFace.")
    else:
        print("\n❌ Some tests failed. Please fix the issues before deploying.")
        sys.exit(1)

if __name__ == "__main__":
    main()