import os
import shutil
import glob
from unittest import TestCase
from msserviceprofiler.msguard.security import open_s
def check_csv_no_empty_start(csv_file):
with open_s(csv_file, 'r', encoding='utf-8') as f:
next(f)
for _, line in enumerate(f, start=2):
stripped_line = line.strip()
if stripped_line.startswith(','):
return False
return True
def get_modelevalstate_path():
file_path = os.path.abspath(__file__)
dir_path = os.path.dirname(file_path)
target_path = os.path.abspath(os.path.join(dir_path, "../../../msserviceprofiler/modelevalstate"))
return target_path
class TestTrainCmd(TestCase):
COMMAND_SUCCESS = 0
MODELEVALSTATE_DIR = get_modelevalstate_path()
RESULT_DIR = os.path.join(MODELEVALSTATE_DIR, "result")
def setUp(self):
if os.path.exists(self.RESULT_DIR):
shutil.rmtree(self.RESULT_DIR)
def tearDown(self):
if os.path.exists(self.RESULT_DIR):
shutil.rmtree(self.RESULT_DIR)
def test_compare_ms_service_profiler_data(self):
train_cmd = "cd " + self.MODELEVALSTATE_DIR + " && msserviceprofiler optimizer"
self.assertEqual(os.system(train_cmd), self.COMMAND_SUCCESS)
self.assertTrue(os.path.exists(self.RESULT_DIR))
pattern = os.path.join(self.RESULT_DIR, "store", "data_storage_*.csv")
matched_files = glob.glob(pattern)
self.assertEqual(len(matched_files), 1)
csv_file = matched_files[0]
result = check_csv_no_empty_start(csv_file)
self.assertTrue(result)