using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Configuration;
using System.IO;
using System.Diagnostics;
using NetBrain.Common.Utility;
namespace NetBrain_DataSyncController
{
class Program
{
public static DataTable dt = null;
public static string sFullPath = "CheckList.csv";
public static string resultPath = "";
public static string processName = "NetBrain";
static void Main(string[] args)
{
if (int.Parse(args[0]) == (int)ServerType.CheckServer)
{
sFullPath = ConfigurationManager.AppSettings["CheckListPath_CheckServer"];
}
resultPath = ConfigurationManager.AppSettings["CaseResultPath"];
dt = LoadCheckList(sFullPath);
if (dt == null) return;
if (int.Parse(args[0]) == (int)ServerType.SourceServer)
{
Console.WriteLine("RunSouceServer");
Control2SourceServer();
}
else if (int.Parse(args[0]) == (int)ServerType.CheckServer)
{
Control2CheckServer();
}
else if(int.Parse(args[0]) == (int)ServerType.reRunConfig)
{
//reset failed case to NoRun state
ReRunConfig();
}
}
static DataTable LoadCheckList(string sPath)
{
bool isLoadOK = false;
int n = 1;
do
{
try
{
dt = CSVParse.OpenCSV(sPath);
isLoadOK = true;
}
catch (Exception err)
{
Console.WriteLine(err);
}
System.Threading.Thread.Sleep(200);
n++;
} while (isLoadOK == false && n < 30);//wait 6s
return dt;
}
static bool WriteCheckList(DataTable dt, string sPath)
{
bool isWriteOK = false;
int n = 1;
do
{
try
{
CSVParse.SaveCSV(dt, sPath);
isWriteOK = true;
}
catch (Exception err)
{
Console.WriteLine(err);
}
System.Threading.Thread.Sleep(200);
n++;
} while (isWriteOK == false && n < 60);//wait 12s
return isWriteOK;
}
static void Control2SourceServer()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
bool IsStart = false;
int CaseIndex = 0;
int retryCount1 = 0;
int i ;
bool isFinishAll = true;
do
{
dt = LoadCheckList(sFullPath);
if (dt == null) return;
isFinishAll = IsFinishedAll(dt, CheckListTableColumn.BaseState);
if (isFinishAll)
{
Console.WriteLine("All cases have been finished!");
}
for (i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if (int.Parse(dr[(int)CheckListTableColumn.BaseState].ToString()) == (int)TestCaseState.NoRun)
{
Console.WriteLine("Case {0} is running!", dr[(int)CheckListTableColumn.BaseCase].ToString());
if (IsContrainState(TestCaseState.Running, CheckListTableColumn.BaseState) == true)
{
break;
}
else
{
IsStart = true;
dt = LoadCheckList(sFullPath);
if (dt == null) break;
dr = dt.Rows[i];
Console.WriteLine("Case {0} is running!", dr[(int)CheckListTableColumn.BaseCase].ToString());
dr[(int)CheckListTableColumn.BaseState] = (int)TestCaseState.Running;
if (WriteCheckList(dt, sFullPath) == false) break;
CaseIndex = i;
process.StartInfo.FileName = @"C:\Automation\Build\BAT\" + dr[(int)CheckListTableColumn.BaseCase].ToString() + ".bat";
process.Start();
}
}
if (IsStart == true)
{
if (process.HasExited == true)
{
System.Threading.Thread.Sleep(1000);
TestCaseState tcs = IsCasePassed(dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseCase].ToString());
dt = LoadCheckList(sFullPath);
if (dt == null) break;
dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseState] = (int)tcs;
if (tcs == TestCaseState.Fail && !dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseCase].ToString().Contains("Delete"))
{
for (int j = CaseIndex + 1; j < dt.Rows.Count; j++)
{
string failedBase = dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseCase].ToString();
failedBase = failedBase.Replace("_Add", "");
failedBase = failedBase.Replace("_Edit", "");
if (dt.Rows[j][(int)CheckListTableColumn.BaseCase].ToString().Contains(failedBase))
{
dt.Rows[j][(int)CheckListTableColumn.BaseState] = (int)TestCaseState.Fail;
}
}
}
if (WriteCheckList(dt, sFullPath) == false) break;
IsStart = false;
}
}
}
retryCount1 += retryCount1;
System.Threading.Thread.Sleep(1000);
if (retryCount1 > 1000)
{
dt = LoadCheckList(sFullPath);
if (dt == null) break;
dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseState] = (int)TestCaseState.timeout;
if (WriteCheckList(dt, sFullPath) == false) break;
}
} while (isFinishAll == false);
}
static void Control2CheckServer()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
bool IsStart = false;
int CaseIndex = 0;
int retryCount2 = 0;
bool isFinishAll = true;
do
{
dt = LoadCheckList(sFullPath);
if (dt == null) return;
isFinishAll = IsFinishedAll(dt, CheckListTableColumn.SubState);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if (int.Parse(dr[(int)CheckListTableColumn.SubState].ToString()) == (int)TestCaseState.NoRun
&& int.Parse(dr[(int)CheckListTableColumn.BaseState].ToString()) == (int)TestCaseState.Pass)
{
if (IsContrainState(TestCaseState.Running, CheckListTableColumn.SubState) == true)
{
break;
}
else
{
IsStart = true;
dt = LoadCheckList(sFullPath);
if (dt == null) break;
dt.Rows[i][(int)CheckListTableColumn.SubState] = (int)TestCaseState.Running;
if (WriteCheckList(dt, sFullPath) == false) break;
CaseIndex = i;
process.StartInfo.FileName = @"C:\Automation\Build\BAT\" + dr[(int)CheckListTableColumn.SubCase].ToString() + ".bat";
process.Start();
}
}
if (IsStart == true)
{
if (process.HasExited)
{
System.Threading.Thread.Sleep(1000);
TestCaseState tcs = IsCasePassed(dt.Rows[CaseIndex][(int)CheckListTableColumn.SubCase].ToString());
dt = LoadCheckList(sFullPath);
if (dt == null) break;
dt.Rows[CaseIndex][(int)CheckListTableColumn.SubState] = (int)tcs;
if (tcs == TestCaseState.Fail && !dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseCase].ToString().Contains("Delete"))
{
for (int j = i + 1; j < dt.Rows.Count; j++)
{
string failedBase = dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseCase].ToString();
failedBase = failedBase.Replace("_Add_Check", "");
failedBase = failedBase.Replace("_Edit_Check", "");
if (dt.Rows[j][(int)CheckListTableColumn.BaseCase].ToString().Contains(failedBase))
{
if (dt.Rows[j][(int)CheckListTableColumn.BaseCase].ToString().Contains(failedBase))
{
dt.Rows[j][(int)CheckListTableColumn.BaseState] = (int)TestCaseState.Fail;
}
}
else
{
break;
}
}
}
if (WriteCheckList(dt, sFullPath) == false) break;
IsStart = false;
}
}
}
System.Threading.Thread.Sleep(500);
retryCount2 += retryCount2;
System.Threading.Thread.Sleep(1000);
if (retryCount2 > 1000)
{
dt = LoadCheckList(sFullPath);
if (dt == null) break;
dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseState] = (int)TestCaseState.timeout;
if (WriteCheckList(dt, sFullPath) == false) break;
}
} while (isFinishAll == false);
}
static bool IsContrainState(TestCaseState testcasestate, CheckListTableColumn tablecolumn)
{
bool bICS = false;
foreach (DataRow dr in dt.Rows)
{
if ((int.Parse(dr[(int)tablecolumn].ToString()) == (int)testcasestate))
{
bICS = true;
break;
}
}
return bICS;
}
static void ReRunConfig()
{
dt = LoadCheckList(sFullPath);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if (int.Parse(dr[(int)CheckListTableColumn.BaseState].ToString()) != (int)TestCaseState.Pass)
{
dr[(int)CheckListTableColumn.BaseState] = (int)TestCaseState.NoRun;
if (int.Parse(dr[(int)CheckListTableColumn.SubState].ToString()) != (int)TestCaseState.Pass)
{
dr[(int)CheckListTableColumn.SubState] = (int)TestCaseState.NoRun;
}
}
}
}
static TestCaseState IsCasePassed(string strCaseName)
{
FileInfo fi = new FileInfo(resultPath + strCaseName + ".Passed");
FileInfo fi_fail = new FileInfo(resultPath + strCaseName + ".Failed");
TestCaseState tempState = TestCaseState.Fail;
if (!fi.Directory.Exists)
{
fi.Directory.Create();
}
int i = 0;
do
{
if (fi.Exists == true)
{
tempState = TestCaseState.Pass;
break;
}
if (fi.Exists == false)
{
tempState = TestCaseState.Fail;
break;
}
System.Threading.Thread.Sleep(500);
}
while (i++ < 60);//wait 30s
return tempState;
}
static bool IsFinishedAll(DataTable dt, CheckListTableColumn ctl)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if (ctl == CheckListTableColumn.BaseState)
{
if (int.Parse(dr[(int)ctl].ToString()) == (int)TestCaseState.NoRun || int.Parse(dr[(int)ctl].ToString()) == (int)TestCaseState.Running)
return false;
}
else if (ctl == CheckListTableColumn.SubState)
{
if (int.Parse(dr[(int)ctl].ToString()) == (int)TestCaseState.NoRun
&& ((int.Parse(dr[(int)CheckListTableColumn.BaseState].ToString()) == (int)TestCaseState.Pass)
|| int.Parse(dr[(int)CheckListTableColumn.BaseState].ToString()) == (int)TestCaseState.NoRun))
return false;
}
}
return true;
}
static void GenerateBat(string batName)
{
string fileName = @"C:\Automation\Build\BAT\" + batName + ".bat";
Console.WriteLine(fileName);
if (File.Exists(fileName))
{
using (FileStream fs = File.Create(fileName))
{
fs.Close();
}
using (StreamWriter sw = new StreamWriter(fileName))
{
sw.WriteLine(@"@echo off&color 02");
if (Environment.Is64BitOperatingSystem)
{
sw.WriteLine("cd \"C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\IDE\\CommonExtensions\\Microsoft\\TestWindow\\\"");
}
else
{
sw.WriteLine("cd \"C:\\Program Files\\Microsoft Visual Studio 12.0\\Common7\\IDE\\CommonExtensions\\Microsoft\\TestWindow\\\"");
}
sw.WriteLine("vstest.console.exe \"C:\\Automation\\Build\\Netbrain.DataSync.TestCase.dll\" /settings:\"C:\\Automation\\Build\\DataSync.testsettings\" /TestCaseFilter:\"FullyQualifiedName=Netbrain.DataSync.TestCase.Automation."+batName+"\" /logger:trx");
sw.WriteLine("exit");
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Configuration;
using System.IO;
using System.Diagnostics;
using NetBrain.Common.Utility;
namespace NetBrain_DataSyncController
{
class Program
{
public static DataTable dt = null;
public static string sFullPath = "CheckList.csv";
public static string resultPath = "";
public static string processName = "NetBrain";
static void Main(string[] args)
{
if (int.Parse(args[0]) == (int)ServerType.CheckServer)
{
sFullPath = ConfigurationManager.AppSettings["CheckListPath_CheckServer"];
}
resultPath = ConfigurationManager.AppSettings["CaseResultPath"];
dt = LoadCheckList(sFullPath);
if (dt == null) return;
if (int.Parse(args[0]) == (int)ServerType.SourceServer)
{
Console.WriteLine("RunSouceServer");
Control2SourceServer();
}
else if (int.Parse(args[0]) == (int)ServerType.CheckServer)
{
Control2CheckServer();
}
else if(int.Parse(args[0]) == (int)ServerType.reRunConfig)
{
//reset failed case to NoRun state
ReRunConfig();
}
}
static DataTable LoadCheckList(string sPath)
{
bool isLoadOK = false;
int n = 1;
do
{
try
{
dt = CSVParse.OpenCSV(sPath);
isLoadOK = true;
}
catch (Exception err)
{
Console.WriteLine(err);
}
System.Threading.Thread.Sleep(200);
n++;
} while (isLoadOK == false && n < 30);//wait 6s
return dt;
}
static bool WriteCheckList(DataTable dt, string sPath)
{
bool isWriteOK = false;
int n = 1;
do
{
try
{
CSVParse.SaveCSV(dt, sPath);
isWriteOK = true;
}
catch (Exception err)
{
Console.WriteLine(err);
}
System.Threading.Thread.Sleep(200);
n++;
} while (isWriteOK == false && n < 60);//wait 12s
return isWriteOK;
}
static void Control2SourceServer()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
bool IsStart = false;
int CaseIndex = 0;
int retryCount1 = 0;
int i ;
bool isFinishAll = true;
do
{
dt = LoadCheckList(sFullPath);
if (dt == null) return;
isFinishAll = IsFinishedAll(dt, CheckListTableColumn.BaseState);
if (isFinishAll)
{
Console.WriteLine("All cases have been finished!");
}
for (i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if (int.Parse(dr[(int)CheckListTableColumn.BaseState].ToString()) == (int)TestCaseState.NoRun)
{
Console.WriteLine("Case {0} is running!", dr[(int)CheckListTableColumn.BaseCase].ToString());
if (IsContrainState(TestCaseState.Running, CheckListTableColumn.BaseState) == true)
{
break;
}
else
{
IsStart = true;
dt = LoadCheckList(sFullPath);
if (dt == null) break;
dr = dt.Rows[i];
Console.WriteLine("Case {0} is running!", dr[(int)CheckListTableColumn.BaseCase].ToString());
dr[(int)CheckListTableColumn.BaseState] = (int)TestCaseState.Running;
if (WriteCheckList(dt, sFullPath) == false) break;
CaseIndex = i;
process.StartInfo.FileName = @"C:\Automation\Build\BAT\" + dr[(int)CheckListTableColumn.BaseCase].ToString() + ".bat";
process.Start();
}
}
if (IsStart == true)
{
if (process.HasExited == true)
{
System.Threading.Thread.Sleep(1000);
TestCaseState tcs = IsCasePassed(dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseCase].ToString());
dt = LoadCheckList(sFullPath);
if (dt == null) break;
dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseState] = (int)tcs;
if (tcs == TestCaseState.Fail && !dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseCase].ToString().Contains("Delete"))
{
for (int j = CaseIndex + 1; j < dt.Rows.Count; j++)
{
string failedBase = dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseCase].ToString();
failedBase = failedBase.Replace("_Add", "");
failedBase = failedBase.Replace("_Edit", "");
if (dt.Rows[j][(int)CheckListTableColumn.BaseCase].ToString().Contains(failedBase))
{
dt.Rows[j][(int)CheckListTableColumn.BaseState] = (int)TestCaseState.Fail;
}
}
}
if (WriteCheckList(dt, sFullPath) == false) break;
IsStart = false;
}
}
}
retryCount1 += retryCount1;
System.Threading.Thread.Sleep(1000);
if (retryCount1 > 1000)
{
dt = LoadCheckList(sFullPath);
if (dt == null) break;
dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseState] = (int)TestCaseState.timeout;
if (WriteCheckList(dt, sFullPath) == false) break;
}
} while (isFinishAll == false);
}
static void Control2CheckServer()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
bool IsStart = false;
int CaseIndex = 0;
int retryCount2 = 0;
bool isFinishAll = true;
do
{
dt = LoadCheckList(sFullPath);
if (dt == null) return;
isFinishAll = IsFinishedAll(dt, CheckListTableColumn.SubState);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if (int.Parse(dr[(int)CheckListTableColumn.SubState].ToString()) == (int)TestCaseState.NoRun
&& int.Parse(dr[(int)CheckListTableColumn.BaseState].ToString()) == (int)TestCaseState.Pass)
{
if (IsContrainState(TestCaseState.Running, CheckListTableColumn.SubState) == true)
{
break;
}
else
{
IsStart = true;
dt = LoadCheckList(sFullPath);
if (dt == null) break;
dt.Rows[i][(int)CheckListTableColumn.SubState] = (int)TestCaseState.Running;
if (WriteCheckList(dt, sFullPath) == false) break;
CaseIndex = i;
process.StartInfo.FileName = @"C:\Automation\Build\BAT\" + dr[(int)CheckListTableColumn.SubCase].ToString() + ".bat";
process.Start();
}
}
if (IsStart == true)
{
if (process.HasExited)
{
System.Threading.Thread.Sleep(1000);
TestCaseState tcs = IsCasePassed(dt.Rows[CaseIndex][(int)CheckListTableColumn.SubCase].ToString());
dt = LoadCheckList(sFullPath);
if (dt == null) break;
dt.Rows[CaseIndex][(int)CheckListTableColumn.SubState] = (int)tcs;
if (tcs == TestCaseState.Fail && !dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseCase].ToString().Contains("Delete"))
{
for (int j = i + 1; j < dt.Rows.Count; j++)
{
string failedBase = dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseCase].ToString();
failedBase = failedBase.Replace("_Add_Check", "");
failedBase = failedBase.Replace("_Edit_Check", "");
if (dt.Rows[j][(int)CheckListTableColumn.BaseCase].ToString().Contains(failedBase))
{
if (dt.Rows[j][(int)CheckListTableColumn.BaseCase].ToString().Contains(failedBase))
{
dt.Rows[j][(int)CheckListTableColumn.BaseState] = (int)TestCaseState.Fail;
}
}
else
{
break;
}
}
}
if (WriteCheckList(dt, sFullPath) == false) break;
IsStart = false;
}
}
}
System.Threading.Thread.Sleep(500);
retryCount2 += retryCount2;
System.Threading.Thread.Sleep(1000);
if (retryCount2 > 1000)
{
dt = LoadCheckList(sFullPath);
if (dt == null) break;
dt.Rows[CaseIndex][(int)CheckListTableColumn.BaseState] = (int)TestCaseState.timeout;
if (WriteCheckList(dt, sFullPath) == false) break;
}
} while (isFinishAll == false);
}
static bool IsContrainState(TestCaseState testcasestate, CheckListTableColumn tablecolumn)
{
bool bICS = false;
foreach (DataRow dr in dt.Rows)
{
if ((int.Parse(dr[(int)tablecolumn].ToString()) == (int)testcasestate))
{
bICS = true;
break;
}
}
return bICS;
}
static void ReRunConfig()
{
dt = LoadCheckList(sFullPath);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if (int.Parse(dr[(int)CheckListTableColumn.BaseState].ToString()) != (int)TestCaseState.Pass)
{
dr[(int)CheckListTableColumn.BaseState] = (int)TestCaseState.NoRun;
if (int.Parse(dr[(int)CheckListTableColumn.SubState].ToString()) != (int)TestCaseState.Pass)
{
dr[(int)CheckListTableColumn.SubState] = (int)TestCaseState.NoRun;
}
}
}
}
static TestCaseState IsCasePassed(string strCaseName)
{
FileInfo fi = new FileInfo(resultPath + strCaseName + ".Passed");
FileInfo fi_fail = new FileInfo(resultPath + strCaseName + ".Failed");
TestCaseState tempState = TestCaseState.Fail;
if (!fi.Directory.Exists)
{
fi.Directory.Create();
}
int i = 0;
do
{
if (fi.Exists == true)
{
tempState = TestCaseState.Pass;
break;
}
if (fi.Exists == false)
{
tempState = TestCaseState.Fail;
break;
}
System.Threading.Thread.Sleep(500);
}
while (i++ < 60);//wait 30s
return tempState;
}
static bool IsFinishedAll(DataTable dt, CheckListTableColumn ctl)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if (ctl == CheckListTableColumn.BaseState)
{
if (int.Parse(dr[(int)ctl].ToString()) == (int)TestCaseState.NoRun || int.Parse(dr[(int)ctl].ToString()) == (int)TestCaseState.Running)
return false;
}
else if (ctl == CheckListTableColumn.SubState)
{
if (int.Parse(dr[(int)ctl].ToString()) == (int)TestCaseState.NoRun
&& ((int.Parse(dr[(int)CheckListTableColumn.BaseState].ToString()) == (int)TestCaseState.Pass)
|| int.Parse(dr[(int)CheckListTableColumn.BaseState].ToString()) == (int)TestCaseState.NoRun))
return false;
}
}
return true;
}
static void GenerateBat(string batName)
{
string fileName = @"C:\Automation\Build\BAT\" + batName + ".bat";
Console.WriteLine(fileName);
if (File.Exists(fileName))
{
using (FileStream fs = File.Create(fileName))
{
fs.Close();
}
using (StreamWriter sw = new StreamWriter(fileName))
{
sw.WriteLine(@"@echo off&color 02");
if (Environment.Is64BitOperatingSystem)
{
sw.WriteLine("cd \"C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\IDE\\CommonExtensions\\Microsoft\\TestWindow\\\"");
}
else
{
sw.WriteLine("cd \"C:\\Program Files\\Microsoft Visual Studio 12.0\\Common7\\IDE\\CommonExtensions\\Microsoft\\TestWindow\\\"");
}
sw.WriteLine("vstest.console.exe \"C:\\Automation\\Build\\Netbrain.DataSync.TestCase.dll\" /settings:\"C:\\Automation\\Build\\DataSync.testsettings\" /TestCaseFilter:\"FullyQualifiedName=Netbrain.DataSync.TestCase.Automation."+batName+"\" /logger:trx");
sw.WriteLine("exit");
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NetBrain_DataSyncController
{
public enum ServerType
{
SourceServer = 1, CheckServer = 2, reRunConfig = 3
}
public enum TestCaseState
{
NoRun = 0, Running = 1, Pass = 3, Fail = 7, Unknow = 999,timeout=5
}
public enum CheckListTableColumn
{
NO = 0, BaseCase = 1, BaseState = 2, SubCase = 3, SubState = 4, ConfigFile = 5
}
}