[Feat] Change data path into /data
This commit is contained in:
12
data/services.json
Normal file
12
data/services.json
Normal file
@ -0,0 +1,12 @@
|
||||
[
|
||||
{
|
||||
"tag": "sys",
|
||||
"name": "nginx",
|
||||
"path": null
|
||||
},
|
||||
{
|
||||
"tag": "docker",
|
||||
"name": "homepage",
|
||||
"path": "/home/virtualguard/projects/webservices/homepage"
|
||||
}
|
||||
]
|
5
main.py
5
main.py
@ -2,6 +2,7 @@
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from src.manager import ServiceFactory, ServiceRepository, Manager
|
||||
from src.services import DockerServiceStrategy, SystemServiceStrategy
|
||||
|
||||
@ -25,7 +26,9 @@ def main():
|
||||
args = parser.parse_args()
|
||||
|
||||
# 初始化仓库和管理器
|
||||
repo = ServiceRepository("services.json")
|
||||
# 确保 data 目录存在
|
||||
os.makedirs("data", exist_ok=True)
|
||||
repo = ServiceRepository("data/services.json")
|
||||
manager = Manager(repo)
|
||||
|
||||
if args.command == "register":
|
||||
|
@ -1,7 +0,0 @@
|
||||
[
|
||||
{
|
||||
"tag": "sys",
|
||||
"name": "nginx",
|
||||
"path": null
|
||||
}
|
||||
]
|
@ -59,8 +59,12 @@ class ServiceRepository:
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, file_path: str = '../data/services.json'):
|
||||
def __init__(self, file_path: str = 'data/services.json'):
|
||||
self.file_path = file_path
|
||||
# 确保目录存在
|
||||
dir_path = os.path.dirname(file_path)
|
||||
if dir_path:
|
||||
os.makedirs(dir_path, exist_ok=True)
|
||||
|
||||
def save(self, service: Service) -> None:
|
||||
"""Save a service to the repository.
|
||||
|
4
test.sh
Executable file
4
test.sh
Executable file
@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
set -ue
|
||||
|
||||
source .venv/bin/activate && python -m unittest discover -s test
|
@ -22,7 +22,10 @@ class TestServiceFactory(unittest.TestCase):
|
||||
class TestServiceRepository(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp_dir = tempfile.TemporaryDirectory()
|
||||
self.repo_path = os.path.join(self.temp_dir.name, "services.json")
|
||||
# 在临时目录下创建 data 子目录
|
||||
data_dir = os.path.join(self.temp_dir.name, "data")
|
||||
os.makedirs(data_dir, exist_ok=True)
|
||||
self.repo_path = os.path.join(data_dir, "services.json")
|
||||
self.repo = ServiceRepository(self.repo_path)
|
||||
|
||||
def tearDown(self):
|
||||
|
@ -126,25 +126,21 @@ class TestServiceOperation(unittest.TestCase):
|
||||
self.assertEqual(result, 0)
|
||||
mock_warning.assert_called()
|
||||
|
||||
@patch("src.services.get_operation", return_value=0)
|
||||
@patch("src.services.SystemServiceStrategy.execute")
|
||||
@patch("src.services.SystemServiceStrategy.generate_command", return_value=["sudo", "systemctl", "stop", "nginx"])
|
||||
@patch("src.services.SystemServiceStrategy.execute")
|
||||
@patch("src.services.logger.info")
|
||||
@patch("src.services.SystemServiceStrategy.execute")
|
||||
@patch("src.services.SystemServiceStrategy.generate_command")
|
||||
@patch("src.services.get_operation", return_value=0)
|
||||
def test_service_operation(self, mock_get_operation, mock_generate, mock_execute, mock_info):
|
||||
@patch("src.services.logger.info")
|
||||
def test_service_operation(self, mock_info, mock_get_operation, mock_generate, mock_execute):
|
||||
"""测试服务操作流程"""
|
||||
service = Service(tag="sys", name="nginx")
|
||||
service.service_operation()
|
||||
|
||||
# 修复:generate_command 需要 self 参数
|
||||
mock_generate.assert_called_with(0, "nginx", None)
|
||||
# 验证调用
|
||||
mock_get_operation.assert_called_once()
|
||||
mock_generate.assert_called_with(0, "nginx")
|
||||
mock_execute.assert_called_with(["sudo", "systemctl", "stop", "nginx"])
|
||||
mock_info.assert_any_call("Executing: sudo systemctl stop nginx")
|
||||
mock_info.assert_any_call("Service nginx operation completed")
|
||||
mock_info.assert_any_call("Executing: sudo systemctl stop nginx")
|
||||
mock_info.assert_any_call("Service nginx operation completed")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Reference in New Issue
Block a user