11#!/usr/bin/env python
22import os
33import pytest
4- import shutil
4+ import shutil
55import subprocess
66import sys
77
88from utils import Logger
99
10- class RunTests ():
1110
12- # Grab the folders for the llmware and llmware-packaging repos. They are assumed to live in the same parent folder
11+ class RunTests :
12+ # Initialize and grab the folder paths for the llmware and llmware-packaging repos
1313 def __init__ (self ):
1414 tests_folder = os .path .dirname (os .path .realpath (__file__ ))
15- self .repo_root = os .path .join (tests_folder , ".." )
16-
17- # Ensure the latest/greatest llmware module is installed locally
15+ self .repo_root = os .path .abspath (os .path .join (tests_folder , ".." ))
16+ self .logger = Logger ()
17+
18+ # Ensure the latest llmware module is installed locally
1819 def update_llmware_install (self ):
19- self .run_command ("pip uninstall llmware -y" , self .repo_root )
20- self .run_command ("pip install ." , self .repo_root )
20+ self .logger .log ("Updating llmware installation..." )
21+ try :
22+ self .run_command ("pip uninstall llmware -y" , self .repo_root )
23+ self .run_command ("pip install ." , self .repo_root )
24+ except Exception as e :
25+ self .logger .log (f"Error updating llmware: { e } " , level = "error" )
26+ sys .exit (1 )
2127
22- # Try to start from as clean an environment as possible
28+ # Clean the environment to start from a fresh state
2329 def clean_the_environment (self ):
24- Logger ().log ("Cleaning the environment..." )
30+ self .logger .log ("Cleaning the environment..." )
31+
32+ # Remove the default llmware data folders
33+ self .remove_folder (os .path .join (os .environ ["HOME" ], "llmware_data" ))
34+ self .remove_folder (os .path .join (os .environ ["HOME" ], "llmware_data_custom" ))
2535
26- # Remove the default data folder: $HOME/llmware_data
27- llmware_data_dir = os .path .join (os .environ ["HOME" ],"llmware_data" )
28- llmware_data_custom_dir = os .path .join (os .environ ["HOME" ],"llmware_data_custom" )
29- Logger ().log (f"Deleting { llmware_data_dir } " )
30- if os .path .exists (llmware_data_dir ):
31- shutil .rmtree (llmware_data_dir )
36+ # Reset MongoDB
37+ try :
38+ self .logger .log ("Resetting MongoDB (dropping the 'llmware' database)..." )
39+ from llmware .resources import MongoDBManager
40+ MongoDBManager ().client .drop_database ("llmware" )
41+ except Exception as e :
42+ self .logger .log (f"Error resetting MongoDB: { e } " , level = "error" )
43+ sys .exit (1 )
3244
33- if os .path .exists (llmware_data_custom_dir ):
34- shutil .rmtree (llmware_data_custom_dir )
35-
36- # Reset Mongo
37- Logger ().log ("Resetting Mongo (deleting the llmware db)" )
38- from llmware .resources import MongoDBManager
39- MongoDBManager ().client .drop_database ("llmware" )
45+ # Reset Milvus collections
46+ try :
47+ self .logger .log ("Resetting Milvus (dropping all collections)..." )
48+ from llmware .configs import LLMWareConfig
49+ from pymilvus import connections , utility
50+ connections .connect ("default" , host = os .environ .get ('MILVUS_HOST' , 'localhost' ), port = 19530 )
51+ for collection in utility .list_collections ():
52+ utility .drop_collection (collection )
53+ self .logger .log ("All Milvus collections dropped successfully." )
54+ except Exception as e :
55+ self .logger .log (f"Error resetting Milvus: { e } " , level = "error" )
56+ sys .exit (1 )
4057
41- # Reset Milvus
42- Logger ().log ("Resetting Milvus (deleting all collections)" )
43- from llmware .configs import LLMWareConfig
44- from pymilvus import connections , utility , FieldSchema , CollectionSchema , DataType , Collection
45- connections .connect ("default" , host = os .environ .get ('MILVUS_HOST' ,'localhost' ) , port = 19530 )
46- for collection in utility .list_collections ():
47- utility .drop_collection (collection )
58+ # Helper function to remove a folder if it exists
59+ def remove_folder (self , folder_path ):
60+ if os .path .exists (folder_path ):
61+ try :
62+ self .logger .log (f"Removing folder: { folder_path } " )
63+ shutil .rmtree (folder_path )
64+ except Exception as e :
65+ self .logger .log (f"Error removing folder { folder_path } : { e } " , level = "error" )
66+ else :
67+ self .logger .log (f"Folder not found: { folder_path } " )
4868
49- # Run a system command in the given dir , wait for it to complete and print the output
69+ # Run a system command in the given directory , wait for it to complete, and log the output
5070 def run_command (self , command , working_dir ):
51- Logger () .log (f"Running command '{ command } ' in '{ working_dir } '..." )
71+ self . logger .log (f"Running command '{ command } ' in '{ working_dir } '..." )
5272 command_array = command .split (" " )
53- command_output = ""
54- p = subprocess .Popen (command_array , cwd = working_dir , stdout = subprocess .PIPE )
55- for line in p .stdout :
56- output_line = line .decode ('utf8' )
57- command_output += output_line
58- Logger ().log (output_line )
59- p .wait ()
60- Logger ().log ("" )
61- return command_output
73+ try :
74+ process = subprocess .Popen (command_array , cwd = working_dir , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
75+ stdout , stderr = process .communicate ()
76+ if stdout :
77+ self .logger .log (stdout .decode ('utf-8' ))
78+ if stderr :
79+ self .logger .log (stderr .decode ('utf-8' ), level = "error" )
80+ if process .returncode != 0 :
81+ raise subprocess .CalledProcessError (process .returncode , command )
82+ except subprocess .CalledProcessError as e :
83+ self .logger .log (f"Command '{ command } ' failed with error: { e } " , level = "error" )
84+ sys .exit (1 )
85+ except Exception as e :
86+ self .logger .log (f"An unexpected error occurred while running '{ command } ': { e } " , level = "error" )
87+ sys .exit (1 )
88+
89+
90+ if __name__ == "__main__" :
91+ test_runner = RunTests ()
92+
93+ # Update and clean environment before running tests
94+ test_runner .update_llmware_install ()
95+ test_runner .clean_the_environment ()
6296
63- test_runner = RunTests ()
64- test_runner .update_llmware_install ()
65- test_runner .clean_the_environment ()
66- pytest .main (sys .argv [1 :])
97+ # Run the tests with pytest
98+ try :
99+ pytest .main (sys .argv [1 :])
100+ except Exception as e :
101+ test_runner .logger .log (f"Error running tests: { e } " , level = "error" )
102+ sys .exit (1 )
0 commit comments