Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F#2051006 Controller Service - Jenkins plugin integration #628

Draft
wants to merge 10 commits into
base: latest
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HpToolsLauncher/HpToolsLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="ConsoleQuickEdit.cs" />
<Compile Include="ElevatedProcess.cs" />
<Compile Include="IProcessAdapter.cs" />
<Compile Include="TestRunners\ServiceTestRunner.cs" />
<Compile Include="Utils\Encoder.cs" />
<Compile Include="Utils\Encrypter.cs" />
<Compile Include="Launcher.cs" />
Expand Down
30 changes: 30 additions & 0 deletions HpToolsLauncher/Launcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,36 @@ private IAssetRunner CreateRunner(bool isFirstRun, List<TestInfo> reruntests = n
{
case TestStorageType.AlmLabManagement:

case TestStorageType.LoadRunner:
{
int timeout = 0;
if (_ciParams.ContainsKey("fsTimeout"))
{
int parsedTimeout;
if (int.TryParse(_ciParams["fsTimeout"], out parsedTimeout) && parsedTimeout != -1)
timeout = parsedTimeout;
}

string resultsDirectory = _ciParams["fsReportPath"].ToString();
if (!Directory.Exists(resultsDirectory))
{
Console.WriteLine("The provided scenario folder path {0} does not exist.", resultsDirectory);
Environment.Exit((int)ExitCodeEnum.Failed);
}
string testPath = _ciParams["Test1"].ToString();
if (!File.Exists(testPath))
{
Console.WriteLine("The provided scenario folder path {0} does not exist.", testPath);
Environment.Exit((int)ExitCodeEnum.Failed);
}

ServiceTestRunner svc = new ServiceTestRunner();
bool result = svc.RunServiceTest(testPath, resultsDirectory, timeout);

Environment.Exit(result ? (int)ExitCodeEnum.Passed : (int)ExitCodeEnum.Failed);
break;
}

case TestStorageType.Alm:
{
//check that all required parameters exist
Expand Down
211 changes: 211 additions & 0 deletions HpToolsLauncher/TestRunners/ServiceTestRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace HpToolsLauncher.TestRunners
{
public class ServiceTestRunner
{
//
public string binPath = Environment.ExpandEnvironmentVariables("%LR_PATH%bin");
public string[] processesToKill = new string[] { "wlrun", "lrun_svc", "CmdServiceClient" };
public int _timeout;

public bool RunServiceTest(string testPath, string resultsDirectory, int timeout)
{
_timeout = timeout * 1000;
Cleanup(processesToKill);
LogMessage("Cleanup complete.");

//Start lrun_svc.exe and CmdServiceClient.exe
StartLrunSvc();
LogMessage("lrun_svc.exe is running");
Process client = StartClient();
LogMessage("CmdServiceClient.exe is running");

//Set the load test data to the given .lrs
string command = "setLoadTestData " + testPath;
Write(client, command);
LogMessage("Command given:",command);

string result = Read(client);
LogMessage("Client response:", result);
if ((result.Contains("failed") || (result.Contains("empty"))))
{
Cleanup(processesToKill);
return false;
}


//Set the results folder directory
string command1 ="setResultsDirectory " + resultsDirectory;
Write(client, command1);
LogMessage("Command given:", command1);

result = Read(client);
LogMessage("Client response:",result);
if ((result.Contains("failed") || (result.Contains("empty"))))
{
Cleanup(processesToKill);
return false;
}

//Start the load test
Write(client,"startLoadTest");
LogMessage("Command given: startLoadTest");

result = Read(client);
LogMessage("Client response:",result);
if (result.Contains("failed"))
{
Cleanup(processesToKill);
return false;
}
LogMessage("The test has started, waiting for the test to end.");
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var startTime= DateTime.Now;

//Get the result
result = "";
while (!result.Contains("Ended")) //wait for the test to end
{
Write(client, "getServiceState"); //ready, collating, running, ended
result = Read(client);
if ((result.Contains("failed") || (result.Contains("empty"))))
{
LogMessage(result);
Cleanup(processesToKill);
return false;
}
Thread.Sleep(1);
}

stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
LogMessage("Test completed in", elapsedTime);
Dispose(client);
Cleanup(processesToKill);
return true;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid too many return statements within this method.

}

#region Process utilities
public void StartLrunSvc()
{
LogMessage("Starting lrun_svc.exe...");
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = binPath,
FileName = "lrun_svc.exe",
};
var proc = new Process
{
StartInfo = startInfo
};
proc.Start();
System.Threading.Thread.Sleep(5000);
}

public Process StartClient()
{
var startInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = binPath + @"\CmdServiceClient.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false
};

var proc = new Process
{
StartInfo = startInfo
};
proc.Start();
return proc;
}

public void Cleanup(string[] processes)
{
foreach (string process in processes)
{
Process[] workers = Process.GetProcessesByName(process);
foreach (Process worker in workers)
{
try
{
worker.Kill();
worker.WaitForExit();
worker.Dispose();
}
catch (UnauthorizedAccessException)
{
continue;
}

}
}

}

public void LogMessage(string message)
{
Console.WriteLine("[{0}] {1}", DateTime.Now.ToString("h:mm:ss tt"), message);
}

public void LogMessage(string message, string extraInfo)
{
Console.WriteLine("[{0}] {1} {2}", DateTime.Now.ToString("h:mm:ss tt"), message, extraInfo);
}
#endregion

#region Client communication
public void Write(Process client, string command)
{
StreamWriter writer = client.StandardInput;
writer.WriteLine(command);
writer.Flush();
}
public void Write(Process client, string command, int timeout)
{
StreamWriter writer = client.StandardInput;
writer.WriteLine(command);
//System.Threading.Thread.Sleep(timeout);
writer.Flush();
}
public string ReadLine(Process client)
{

StreamReader reader = client.StandardOutput;
string result = "";
do
{
int output = reader.Read();
result += (char)output;
}
while (reader.Peek() > -1);
reader.DiscardBufferedData();
return result;
}
public void Dispose(Process client)
{
client.StandardInput.Close();
client.StandardOutput.Close();
client.Dispose();
}
public string Read(Process client)
{
string result = ReadLine(client);
while (result.Length < 3)
{
result =ReadLine(client);
System.Threading.Thread.Sleep(5);
}
return result;
}
}
#endregion
}
2 changes: 1 addition & 1 deletion HpToolsLauncher/app.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" /></startup>
<startup><supportedRuntime version="v4.0"/></startup>
<runtime>
<enforceFIPSPolicy enabled="false"/>
</runtime>
Expand Down
Loading