-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_docker_setup.py
More file actions
146 lines (126 loc) · 4.63 KB
/
test_docker_setup.py
File metadata and controls
146 lines (126 loc) · 4.63 KB
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
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
"""
Test script to validate Docker containerization setup for Diamond Price Predictor.
This script checks if the Docker containers can be built and basic configurations are correct.
"""
import subprocess
import sys
import os
import yaml
import json
def run_command(command, description=""):
"""Run a shell command and return the result."""
try:
print(f"Running: {description or command}")
result = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=60)
if result.returncode == 0:
print(f"[PASS] Success: {description or command}")
return True, result.stdout
else:
print(f"[FAIL] Failed: {description or command}")
print(f"Error: {result.stderr}")
return False, result.stderr
except subprocess.TimeoutExpired:
print(f"[FAIL] Timeout: {description or command}")
return False, "Command timed out"
except Exception as e:
print(f"[FAIL] Exception: {description or command} - {str(e)}")
return False, str(e)
def test_docker_compose_config():
"""Test docker-compose configuration syntax."""
print("\n=== Testing docker-compose configuration ===")
success, output = run_command("docker-compose config", "Validating docker-compose.yml syntax")
if success:
print("[PASS] docker-compose.yml is valid")
return True
return False
def test_dockerfiles_exist():
"""Check if required Dockerfiles exist."""
print("\n=== Testing Dockerfile existence ===")
required_files = [
"Dockerfile.flask",
"Dockerfile.streamlit",
".dockerignore"
]
all_exist = True
for file in required_files:
if os.path.exists(file):
print(f"[PASS] {file} exists")
else:
print(f"[FAIL] {file} missing")
all_exist = False
return all_exist
def test_requirements_file():
"""Check if requirements.txt exists and contains necessary packages."""
print("\n=== Testing requirements.txt ===")
if not os.path.exists("requirements.txt"):
print("✗ requirements.txt missing")
return False
required_packages = ["flask", "streamlit", "gunicorn", "pandas", "numpy", "scikit-learn"]
with open("requirements.txt", "r") as f:
content = f.read().lower()
missing_packages = []
for package in required_packages:
if package not in content:
missing_packages.append(package)
if missing_packages:
print(f"[FAIL] Missing packages: {', '.join(missing_packages)}")
return False
else:
print("[PASS] All required packages found in requirements.txt")
return True
def test_directory_structure():
"""Check if required directories exist or will be created."""
print("\n=== Testing directory structure ===")
required_dirs = ["artifacts", "logs"]
for dir_name in required_dirs:
if os.path.exists(dir_name):
print(f"[PASS] {dir_name}/ directory exists")
else:
print(f"[INFO] {dir_name}/ directory will be created by Docker volumes")
return True
def test_yaml_syntax():
"""Test YAML file syntax."""
print("\n=== Testing YAML syntax ===")
try:
with open("docker-compose.yml", "r") as f:
yaml.safe_load(f)
print("[PASS] docker-compose.yml has valid YAML syntax")
return True
except yaml.YAMLError as e:
print(f"[FAIL] docker-compose.yml has invalid YAML syntax: {e}")
return False
except FileNotFoundError:
print("[FAIL] docker-compose.yml not found")
return False
def main():
"""Run all containerization tests."""
print("Diamond Price Predictor - Docker Containerization Tests")
print("=" * 60)
tests = [
test_dockerfiles_exist,
test_requirements_file,
test_directory_structure,
test_yaml_syntax,
test_docker_compose_config,
]
results = []
for test in tests:
try:
result = test()
results.append(result)
except Exception as e:
print(f"[FAIL] Test {test.__name__} failed with exception: {e}")
results.append(False)
# Summary
print(f"\n{'=' * 60}")
print(f"Test Summary: {sum(results)}/{len(results)} tests passed")
if all(results):
print("[PASS] All containerization tests passed!")
print("[INFO] Docker setup is ready for deployment")
return 0
else:
print("[FAIL] Some tests failed. Please fix the issues before proceeding.")
return 1
if __name__ == "__main__":
sys.exit(main())