Files
VisualSapfor/src/_VisualDVM/TestingSystem/SAPFOR/SapforTestingPlanner.java

325 lines
14 KiB
Java
Raw Normal View History

2024-10-09 22:21:57 +03:00
package _VisualDVM.TestingSystem.SAPFOR;
2024-10-07 14:22:52 +03:00
import Common.CommonConstants;
import Common.Utils.TextLog;
2024-10-11 00:00:30 +03:00
import Common.Utils.Utils_;
2025-02-18 16:21:20 +03:00
import _VisualDVM.ComponentsServer.Component.Sapfor.Sapfor;
import _VisualDVM.*;
2024-10-09 22:21:57 +03:00
import _VisualDVM.ProjectData.LanguageName;
import _VisualDVM.Repository.EmailMessage;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.TestingSystem.Common.TasksPackageState;
import _VisualDVM.TestingSystem.Common.TestingPlanner;
import _VisualDVM.TestingSystem.SAPFOR.Json.SapforConfiguration_json;
import _VisualDVM.TestingSystem.SAPFOR.Json.SapforTest_json;
import _VisualDVM.TestingSystem.SAPFOR.Json.SapforTestingSet_json;
import _VisualDVM.TestingSystem.SAPFOR.SapforPackage.SapforPackage;
import _VisualDVM.TestingSystem.SAPFOR.SapforTask.ComparisonState;
import _VisualDVM.TestingSystem.SAPFOR.SapforTask.SapforTask;
2024-10-09 22:21:57 +03:00
import _VisualDVM.TestingSystem.SAPFOR.ServerSapfor.ServerSapfor;
import _VisualDVM.TestingSystem.SAPFOR.ServerSapfor.ServerSapforState;
import javafx.util.Pair;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
2024-05-08 00:06:43 +03:00
import java.nio.file.Paths;
import java.util.Date;
public class SapforTestingPlanner extends TestingPlanner<SapforPackage> {
File workspace;
2024-05-07 01:06:47 +03:00
ServerSapfor sapfor;
2024-05-08 00:57:48 +03:00
//--
File repo;
File repo_sapfor;
File repo_sapfor_installation_directory;
int max_version;
int current_version;
//--
File repo_sapfor_assembly; //бинарник собранный в репозитории.
2024-05-08 00:57:48 +03:00
File repo_out;
File repo_err;
//--
public SapforTestingPlanner() {
2024-10-11 00:00:30 +03:00
repo = new File(Utils_.getHomeDirectory(), "Repo");
repo_sapfor = new File(repo, "SAPFOR");
//-
2025-03-13 00:32:20 +03:00
repo_sapfor_installation_directory = new File(repo_sapfor, "install");
repo_out = new File(repo_sapfor_installation_directory, Constants.out_file);
repo_err = new File(repo_sapfor_installation_directory, Constants.err_file);
repo_sapfor_assembly = new File(repo_sapfor_installation_directory, "Sapfor_F");
2024-05-08 00:57:48 +03:00
}
@Override
public String packageDescription() {
return "SAPFOR";
}
@Override
protected ServerCode getActivePackagesCode() {
return ServerCode.GetFirstActiveSapforPackages;
}
@Override
protected ServerCode getCheckIfNeedsKillCode() {
return ServerCode.SapforPackageNeedsKill;
}
@Override
protected TasksPackageState getStateAfterStart() {
return TasksPackageState.RunningExecution;
}
@Override
protected void InitSessionCredentials() {
workspace = testingPackage.getLocalWorkspace();
}
@Override
protected void TestsSynchronize() throws Exception {
testingPackage.readJson();
//--
for (SapforTestingSet_json set_json : testingPackage.package_json.testingSets) {
File setWorkspace = new File(workspace, String.valueOf(set_json.id));
FileUtils.forceMkdir(setWorkspace);
//копирование тестов по конфигурациям.
for (SapforConfiguration_json configuration_json : set_json.configurations) {
//--
File configurationWorkspace = new File(setWorkspace, String.valueOf(configuration_json.id));
FileUtils.forceMkdir(configurationWorkspace);
//--->>>
for (SapforTest_json test_json : set_json.tests) {
File test_root = new File(configurationWorkspace, test_json.description);
2024-10-14 12:54:52 +03:00
Utils_.CheckAndCleanDirectory(test_root);
FileUtils.copyDirectory(new File(Global.TestsDirectory, String.valueOf(test_json.id)), test_root);
}
}
}
}
@Override
protected void PackageWorkspaceCreation() throws Exception {
//копирование визуализатора
File visualiser = new File(workspace, "VisualSapfor.jar");
Files.createSymbolicLink(visualiser.toPath(), new File(Utils_.getHomeDirectory(), Constants.ApplicationFileName).toPath());
//создание настроек
2025-01-18 01:36:02 +03:00
VisualDVMProperties properties = new VisualDVMProperties();
2024-10-08 15:32:39 +03:00
properties.Mode = Mode.Package;
2024-10-11 00:00:30 +03:00
Utils_.jsonToFile(properties, new File(workspace, "properties"));
//подготовка пакетного режима. Запустит его уже очередь.
Utils.createScript(workspace, workspace, "start", "java -jar VisualSapfor.jar");
}
@Override
protected void PackageStart() throws Exception {
File script = new File(workspace, "start");
ProcessBuilder procBuilder = new ProcessBuilder(script.getAbsolutePath());
procBuilder.directory(workspace);
procBuilder.start();
//--->>
File started = new File(workspace, CommonConstants.STARTED);
while (!started.exists()) {
Print("waiting for package start...");
2024-10-11 00:00:30 +03:00
Utils_.sleep(1000);
}
File pid = new File(workspace, "PID");
testingPackage.PID = FileUtils.readFileToString(pid, Charset.defaultCharset());
}
@Override
protected boolean CheckNextState() throws Exception {
boolean progress_changed = false;
boolean state_changed = false;
//--
File workspace = testingPackage.getLocalWorkspace();
//--
File progress = new File(workspace, "progress");
if (progress.exists()) {
String s = FileUtils.readFileToString(progress);
int current_progress = Integer.parseInt(s);
if (current_progress != testingPackage.progress) {
Print("progress changed: " + current_progress);
testingPackage.progress = current_progress;
progress_changed = true;
}
}
//--
File done = new File(workspace, CommonConstants.DONE);
File aborted = new File(workspace, CommonConstants.ABORTED);
if (done.exists()) {
testingPackage.state = TasksPackageState.Analysis;
state_changed = true;
} else if (aborted.exists()) {
testingPackage.state = TasksPackageState.Aborted;
state_changed = true;
}
return progress_changed || state_changed;
}
@Override
protected void DownloadResults() throws Exception {
//не требуется.
}
@Override
protected void AnalyseResults() throws Exception {
//не требуется.
testingPackage.progress = 100;
testingPackage.checkFinishState();
//--для эталона
if (testingPackage.ethalon_id != CommonConstants.Nan) {
TextLog log = new TextLog();
try {
CheckEthalon(log);
} catch (Exception ex) {
ex.printStackTrace();
log.Writeln_("При сравнении с эталоном возникло исключение:");
log.Writeln_(ex.getMessage());
} finally {
if (!log.isEmpty())
testingPackage.description += "\n" + log.text;
}
}
UpdatePackageState();
}
protected void CheckEthalon(TextLog log) throws Exception {
Print("эталон = " + testingPackage.ethalon_id);
SapforPackage ethalon = (SapforPackage) ServerCommand(ServerCode.GetObjectCopyByPK, "", new Pair<>(SapforPackage.class, testingPackage.ethalon_id));
if (ethalon != null) {
Print("найден эталон " + testingPackage.ethalon_id);
ethalon.readJson();
Print("задач в эталоне: " + ethalon.package_json.tasks.size());
Print("задач в пакете: " + testingPackage.package_json.tasks.size());
if (ethalon.canCompare(testingPackage, log)) {
Print("сравнение...");
//--
testingPackage.package_json.getVersionsFiles(testingPackage);
ethalon.package_json.getVersionsFiles(ethalon);
//--
for (SapforTask task1 : ethalon.package_json.tasks) {
SapforTask task2 = testingPackage.package_json.getTaskByKey(task1.getUniqueKey());
task1.checkMatchServer(task2); //тексты сравниваются просто посимвольно.
if (task1.comparisonState.equals(ComparisonState.NotMatch)) {
testingPackage.mismatchesCount++;
}
}
Print("сравнение завершено");
2024-11-29 16:33:46 +03:00
log.Writeln_("Различий с эталоном: " + testingPackage.mismatchesCount);
return;
} else {
Print("сравнение с эталоном невозможно");
2024-11-29 16:33:46 +03:00
log.Writeln_("Cравнение с эталоном невозможно");
}
}
}
@Override
protected void Kill() throws Exception {
File workspace = testingPackage.getLocalWorkspace();
//----
File interrupt_file = new File(workspace, CommonConstants.INTERRUPT);
//----
FileUtils.writeStringToFile(interrupt_file, new Date().toString());
File aborted_file = new File(workspace, CommonConstants.ABORTED);
do {
Print("waiting for interrupt...");
Thread.sleep(1000);
} while (!aborted_file.exists());
Print("coup de grace..");
String kill_command = "killall -SIGKILL " + testingPackage.PID;
Print(kill_command);
Process killer = Runtime.getRuntime().exec(kill_command);
killer.waitFor();
Print("done!");
}
//--
@Override
public void perform() throws Exception {
2024-05-08 00:57:48 +03:00
checkServerSapforsForCompilation();
super.perform();
}
2024-05-08 00:57:48 +03:00
//--------------------
2024-05-07 01:06:47 +03:00
public void getServerSapforForCompilation() throws Exception {
2024-05-08 00:57:48 +03:00
sapfor = (ServerSapfor) ServerCommand(ServerCode.GetSapforForCompilation);
2024-05-07 01:06:47 +03:00
}
2024-05-08 00:06:43 +03:00
void UpdateSapforState(ServerSapforState state_in) throws Exception {
sapfor.state = state_in;
ServerCommand(ServerCode.EditObject, sapfor);
}
2024-05-08 00:57:48 +03:00
void SyncronizeRepository() throws Exception {
2024-05-18 23:31:45 +03:00
UpdateSapforState(ServerSapforState.SAPFORRepositorySynchronization);
Utils.CleanDirectory(repo);
Utils.startScript(repo, repo, "git_clone",
"git clone " + Constants.SAPFOR_REPOSITORY + " " +
2025-03-13 00:32:20 +03:00
Utils_.DQuotes(repo_sapfor) +
" 1>spf_out.txt 2>spf_err.txt\n").waitFor();
2024-05-08 00:57:48 +03:00
}
void CompileSapfor() throws Exception {
2024-05-18 23:31:45 +03:00
UpdateSapforState(ServerSapforState.Compilation);
2024-05-08 00:57:48 +03:00
//-
Utils_.CheckAndCleanDirectory(repo_sapfor_installation_directory);
2024-05-08 00:57:48 +03:00
//--
Utils.startScript(repo_sapfor_installation_directory, repo_sapfor_installation_directory, "build_sapfor",
2025-03-13 00:32:20 +03:00
"cmake .. 1>" + Constants.out_file + " 2>" + Constants.err_file +
2024-05-08 00:57:48 +03:00
"\nmake -j 14 1>>" +
Constants.out_file +
" 2>>" +
Constants.err_file +
"\n").waitFor();
}
//--------------------
public void checkServerSapforsForCompilation() throws Exception {
2024-05-07 01:06:47 +03:00
sapfor = null;
getServerSapforForCompilation();
if (sapfor != null) {
//---
2024-10-07 14:22:52 +03:00
max_version = CommonConstants.Nan;
current_version = CommonConstants.Nan;
//--
2024-05-08 00:57:48 +03:00
SyncronizeRepository();
max_version = (int) ServerCommand(ServerCode.GetMaxSapforVersion);
current_version = Sapfor.readVersionFromCode(Paths.get(repo.getAbsolutePath(),
"/SAPFOR/src/Utils/version.h").toFile());
2024-10-14 15:19:13 +03:00
if (current_version == max_version) {
ServerCommand(ServerCode.DeleteObjectByPK, new Pair(ServerSapfor.class, sapfor.id));
return;
}
2024-05-08 00:57:48 +03:00
//-
2024-10-11 00:00:30 +03:00
File sapforHome = new File(Global.SapforsDirectory, Utils_.getDateName("sapfor"));
//--
sapfor.home_path = sapforHome.getAbsolutePath();
sapfor.languageName = LanguageName.fortran;
sapfor.version = String.valueOf(current_version);
//--
2024-05-08 00:57:48 +03:00
File sapforBin = new File(sapforHome, "Sapfor_F");
2024-05-08 00:06:43 +03:00
File sapforOut = new File(sapforHome, Constants.out_file);
File sapforErr = new File(sapforHome, Constants.err_file);
2024-05-08 00:57:48 +03:00
//-
CompileSapfor();
sapforHome.mkdir();
2024-05-08 00:06:43 +03:00
//--
if (repo_out.exists())
FileUtils.copyFile(repo_out, sapforOut);
if (repo_err.exists())
FileUtils.copyFile(repo_err, sapforErr);
if (repo_sapfor_assembly.exists()) {
FileUtils.copyFile(repo_sapfor_assembly, sapforBin);
2024-05-08 00:06:43 +03:00
sapforBin.setExecutable(true, false);
//--
sapfor.call_command = sapforBin.getAbsolutePath();
sapfor.buildDate = new Date().getTime();
//--
2024-05-08 00:06:43 +03:00
UpdateSapforState(ServerSapforState.Done);
2024-05-17 01:33:39 +03:00
EmailSapforAssembly(current_version, true, sapforOut, sapforErr);
//запуск автоматического тестирования.
ServerCommand(ServerCode.PerformAutoSapforTesting, String.valueOf(sapfor.id), null);
2024-05-08 00:57:48 +03:00
} else {
2024-05-08 00:06:43 +03:00
UpdateSapforState(ServerSapforState.DoneWithErrors);
2024-05-17 01:33:39 +03:00
EmailSapforAssembly(current_version, false, sapforOut, sapforErr);
2024-05-08 00:06:43 +03:00
}
2024-05-07 01:06:47 +03:00
}
}
2024-05-17 01:33:39 +03:00
void EmailSapforAssembly(int version, boolean done, File out, File err) throws Exception {
2024-10-07 14:22:52 +03:00
String version_s = (version == CommonConstants.Nan) ? "?" : String.valueOf(version);
2024-05-17 01:33:39 +03:00
String status = done ? "Успешно" : "С ошибками";
//-
EmailMessage message = new EmailMessage(
"Выполнена сборка системы SAPFOR",
"Версия: " + version_s + "\n" + "Статус: " + status
2024-05-17 01:33:39 +03:00
);
message.addAttachement(out);
message.addAttachement(err);
2024-05-17 01:33:39 +03:00
//-
for (String address : Constants.admins_mails) {
ServerCommand(ServerCode.Email, address, message);
}
2024-05-17 01:33:39 +03:00
}
2023-12-15 18:10:27 +03:00
}