From 9ece05e3b31d5b884a9e266e63b2eabc6851c85e Mon Sep 17 00:00:00 2001 From: akshaj000 Date: Mon, 23 Oct 2023 11:22:56 +0530 Subject: [PATCH] Auto setup server for tests --- tests/api/test_genai_server/__init__.py | 59 +++++++++++++++++++ tests/api/test_genai_server/setup/__init__.py | 1 + tests/api/test_genai_server/setup/main.py | 7 +++ tests/api/test_genai_server/setup/server.conf | 3 + .../test_genai_server/setup/stack_config.json | 46 +++++++++++++++ tests/api/test_genai_server/test_etl.py | 8 ++- .../test_retrieve_from_vector_db.py | 14 ++--- tests/api/test_genai_server/test_retriever.py | 8 ++- tests/api/test_genai_server/test_session.py | 9 +-- tests/api/test_genai_server/test_vectordb.py | 8 ++- 10 files changed, 143 insertions(+), 20 deletions(-) create mode 100644 tests/api/test_genai_server/setup/__init__.py create mode 100644 tests/api/test_genai_server/setup/main.py create mode 100644 tests/api/test_genai_server/setup/server.conf create mode 100644 tests/api/test_genai_server/setup/stack_config.json diff --git a/tests/api/test_genai_server/__init__.py b/tests/api/test_genai_server/__init__.py index e69de29b..288f4f81 100644 --- a/tests/api/test_genai_server/__init__.py +++ b/tests/api/test_genai_server/__init__.py @@ -0,0 +1,59 @@ +import unittest +import threading +import time +import os +import signal +import socket +import psutil +from setup import stack + + +def run_server(port): + stack.run_server(host="127.0.0.1", port=port) + + +def find_free_port(initial_port): + port = initial_port + while True: + try: + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind(("127.0.0.1", port)) + return port + except OSError: + port += 1 + + +def start_server(): + free_port = find_free_port(5600) + server_thread = threading.Thread(target=run_server, args=(free_port,)) + server_thread.start() + time.sleep(6) + return free_port + + +def stop_server(port): + try: + for conn in psutil.net_connections(): + if conn.laddr.port == port: + os.kill(conn.pid, signal.SIGTERM) + break + except (ProcessLookupError, psutil.NoSuchProcess): + pass + time.sleep(2) + + +class TestCaseServer(unittest.TestCase): + + host = None + server_port = None + + @classmethod + def setUpClass(cls) -> None: + cls.server_port = start_server() + cls.host = "127.0.0.1" + cls.base_url = f"http://{cls.host}:{cls.server_port}/api/" + + @classmethod + def tearDownClass(cls) -> None: + stop_server(cls.server_port) + diff --git a/tests/api/test_genai_server/setup/__init__.py b/tests/api/test_genai_server/setup/__init__.py new file mode 100644 index 00000000..30e46aeb --- /dev/null +++ b/tests/api/test_genai_server/setup/__init__.py @@ -0,0 +1 @@ +from .main import stack diff --git a/tests/api/test_genai_server/setup/main.py b/tests/api/test_genai_server/setup/main.py new file mode 100644 index 00000000..850a2225 --- /dev/null +++ b/tests/api/test_genai_server/setup/main.py @@ -0,0 +1,7 @@ +from genai_stack.genai_server.settings.config import read_configurations +from genai_stack.genai_server.utils import get_current_stack +import pathlib + +path = f"{str(pathlib.Path().resolve())}/setup" +stack_configurations = read_configurations(path)[1] +stack = get_current_stack(config=stack_configurations) diff --git a/tests/api/test_genai_server/setup/server.conf b/tests/api/test_genai_server/setup/server.conf new file mode 100644 index 00000000..35a1cec4 --- /dev/null +++ b/tests/api/test_genai_server/setup/server.conf @@ -0,0 +1,3 @@ +[database] +database_name = db +database_driver = sqlite \ No newline at end of file diff --git a/tests/api/test_genai_server/setup/stack_config.json b/tests/api/test_genai_server/setup/stack_config.json new file mode 100644 index 00000000..801e1bb2 --- /dev/null +++ b/tests/api/test_genai_server/setup/stack_config.json @@ -0,0 +1,46 @@ +{ + "components":{ + "vectordb":{ + "name":"chromadb", + "config":{ + } + }, + "memory":{ + "name":"langchain", + "config":{ + + } + }, + "llm_cache":{ + "name":"cache", + "config":{ + + } + }, + "model":{ + "name":"gpt3.5", + "config":{ + "parameters":{"openai_api_key": "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"} + } + }, + "embedding":{ + "name":"langchain", + "config":{ + "name":"HuggingFaceEmbeddings", + "fields":{ + "model_name": "sentence-transformers/all-mpnet-base-v2", + "model_kwargs": {"device": "cpu"}, + "encode_kwargs": {"normalize_embeddings": false} + } + } + }, + "prompt_engine":{ + "name": "engine", + "config": {} + }, + "retriever": { + "name": "langchain", + "config": {} + } + } +} diff --git a/tests/api/test_genai_server/test_etl.py b/tests/api/test_genai_server/test_etl.py index 2e435da9..e049b4f5 100644 --- a/tests/api/test_genai_server/test_etl.py +++ b/tests/api/test_genai_server/test_etl.py @@ -2,14 +2,16 @@ """Tests for `genai_server`.""" -import unittest import requests +from tests.api.test_genai_server import TestCaseServer -class TestETLServerAPIs(unittest.TestCase): + +class TestETLServerAPIs(TestCaseServer): def setUp(self) -> None: - self.base_url = "http://127.0.0.1:8080/api/etl" + super().setUp() + self.base_url = self.base_url + "etl" def test_submit_job(self): response = requests.get( diff --git a/tests/api/test_genai_server/test_retrieve_from_vector_db.py b/tests/api/test_genai_server/test_retrieve_from_vector_db.py index 565f7457..0da10783 100644 --- a/tests/api/test_genai_server/test_retrieve_from_vector_db.py +++ b/tests/api/test_genai_server/test_retrieve_from_vector_db.py @@ -2,15 +2,15 @@ """Tests for `genai_server`.""" import json -import unittest import requests +from tests.api.test_genai_server import TestCaseServer -class TestRetrieverDataFromDBAPIs(unittest.TestCase): - def setUp(self) -> None: - self.base_url = "http://127.0.0.1:5000/api" +class TestRetrieverDataFromDBAPIs(TestCaseServer): + def setUp(self) -> None: + super().setUp() self.data = [ { "id": self.create_session(), @@ -42,7 +42,7 @@ def setUp(self) -> None: ] def create_session(self): - response = requests.post(url=self.base_url + "/session") + response = requests.post(url=self.base_url + "session") assert response.status_code == 200 assert response.json() data = response.json() @@ -51,7 +51,7 @@ def create_session(self): def add_document_to_session(self, session_id, data): response = requests.post( - url=self.base_url + "/vectordb/add-documents", + url=self.base_url + "vectordb/add-documents", data=json.dumps( { "session_id": session_id, @@ -63,7 +63,7 @@ def add_document_to_session(self, session_id, data): def search_for_doc(self, session_id, query): response = requests.get( - url=self.base_url + "/retriever/retrieve", + url=self.base_url + "retriever/retrieve", params={"session_id": session_id, "query": query}, ) assert response.status_code == 200 diff --git a/tests/api/test_genai_server/test_retriever.py b/tests/api/test_genai_server/test_retriever.py index 2325a4ee..14ba94d4 100644 --- a/tests/api/test_genai_server/test_retriever.py +++ b/tests/api/test_genai_server/test_retriever.py @@ -2,14 +2,16 @@ """Tests for `genai_server`.""" -import unittest import requests +from tests.api.test_genai_server import TestCaseServer -class TestRetrieverServerAPIs(unittest.TestCase): + +class TestRetrieverServerAPIs(TestCaseServer): def setUp(self) -> None: - self.base_url = "http://127.0.0.1:5000/api/retriever" + super().setUp() + self.base_url = self.base_url + "retriever" def test_retrieve(self): response = requests.get( diff --git a/tests/api/test_genai_server/test_session.py b/tests/api/test_genai_server/test_session.py index d57692d5..7eb8233e 100644 --- a/tests/api/test_genai_server/test_session.py +++ b/tests/api/test_genai_server/test_session.py @@ -2,18 +2,19 @@ """Tests for `genai_server`.""" -import unittest import requests +from tests.api.test_genai_server import TestCaseServer -class TestSessionServerAPIs(unittest.TestCase): + +class TestSessionServerAPIs(TestCaseServer): def setUp(self) -> None: - self.base_url = "http://127.0.0.1:5000/api/session" + super().setUp() + self.base_url = self.base_url + "session" def test_create_session(self): response = requests.post(url=self.base_url) - print(response.json()) assert response.status_code == 200 def test_sessions_list(self): diff --git a/tests/api/test_genai_server/test_vectordb.py b/tests/api/test_genai_server/test_vectordb.py index 5adc25bd..345966ca 100644 --- a/tests/api/test_genai_server/test_vectordb.py +++ b/tests/api/test_genai_server/test_vectordb.py @@ -2,14 +2,16 @@ """Tests for `genai_server`.""" import json -import unittest import requests +from tests.api.test_genai_server import TestCaseServer -class TestVectorDBServerAPIs(unittest.TestCase): + +class TestVectorDBServerAPIs(TestCaseServer): def setUp(self) -> None: - self.base_url = "http://127.0.0.1:5000/api/vectordb" + super().setUp() + self.base_url = self.base_url + "vectordb" def test_add_document(self): response = requests.post(