using System using System Collections Generic using System Diagnostics

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using Yaka.Tests;
namespace TestRunner
{
public class Program
{
private const string AccountPreffix = "TESTBOT";
public const sbyte ActivityExecutionStopped = -1;
public const sbyte ActivityExecutionSucces = 0;
public const sbyte InitializingRealmDataFailed = 1;
public const sbyte TestExecutionFailed = 2;
public const sbyte ActivityFailed = 3;
public const sbyte UnknowActivity = 4;
public const sbyte InvalidParametersCount = 5;
public const sbyte InitDataFileNotFound = 6;
public const sbyte DeserializationInitDataFailed = 7;
public const sbyte InitDataFileOpenFailed = 8;
public const sbyte UnknowError = 127;
private static readonly string _logPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
@".\TestRunnersLog";
private const string lOperationDescription = "Initialization data file loading fail.";
private static Logger _logger;
public static int Main(string[] args)
{
//NOTE: to debug
//Console.ReadKey();
Debugger.Launch();
CreateLogDirectory();
try
{
if (args.Length != 6)
{
//Console.WriteLine("Not enoenough command line parameters. 5 expected but {0} found.", args.Length);
Environment.Exit(InvalidParametersCount);
}
string activityName = args[0];
string testRunnerId = args[1];
string initDataFileName = args[2];
string sessionName = args[3];
string accountIds = args[4];
bool isInitializatonMode = (args[5] == "1");
string logName = string.Format(@"{0}\{1}_{2}.txt", _logPath, sessionName,
testRunnerId);
string crashLogName = string.Format(@"{0}\{1}_crash_{2}.txt", _logPath, sessionName,
testRunnerId);
_logger = new Logger(logName, crashLogName);
Console.SetOut(_logger.InfoWriter);
_logger.Info("Starting test activity {0}", activityName);
#region Загрузка YakaTestInitializationData
ConnectionData testInitData = null;
if (File.Exists(initDataFileName))
{
StreamReader streamReader = null;
try
{
streamReader = File.OpenText(initDataFileName);
}
catch (Exception ex)
{
_logger.Error("{0} File open exception: {1}", lOperationDescription, ex.Message);
Environment.Exit(InitDataFileOpenFailed);
}
BinaryFormatter serializer = new BinaryFormatter();
try
{
testInitData = (ConnectionData)serializer.Deserialize(streamReader.BaseStream);
}
catch (Exception ex)
{
_logger.Error("{0} Error on deserializing YakaTestInitializationData: {1}",
lOperationDescription, ex);
Environment.Exit(DeserializationInitDataFailed);
}
}
else
{
_logger.Error("{0} File <{1}> not found.", lOperationDescription, initDataFileName);
Environment.Exit(InitDataFileNotFound);
}
#endregion
try
{
IPerformanceTestActivity activity = LoadActivity(activityName, testInitData, accountIds,
testRunnerId);
if (isInitializatonMode)
{
try
{
activity.InitRealmData();
Environment.Exit(ActivityExecutionSucces);
}
catch (Exception ex)
{
_logger.Error("{0}", ex);
Environment.Exit(InitializingRealmDataFailed);
}
}
else
{
try
{
activity.RunTest();
}
catch (Exception ex)
{
_logger.Error("{0}", ex);
Environment.Exit(TestExecutionFailed);
}
}
if (activity.IsPassed())
{
Environment.Exit(ActivityExecutionSucces);
}
else
{
Environment.Exit(ActivityFailed);
}
}
catch(Exception)
{
_logger.Error("Unknown activity '{0}'. Check spelling or verify that activity "
+ "implements ITestActivity or ITestSessionActivity.",
activityName);
Environment.Exit(UnknowActivity);
}
Environment.Exit(ActivityExecutionSucces);
return ActivityExecutionSucces;
}
catch (Exception ex)
{
_logger.Error("{0}", ex);
Environment.Exit(UnknowError);
return UnknowError;
}
}
private static IPerformanceTestActivity LoadActivity(string activityName, ConnectionData connectionData, string accountsId, string testRunnerId)
{
Assembly assembly =
Assembly.LoadFrom(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\PerformanceTests.dll");
IPerformanceTestActivity activity;
Type activityType = Array.Find(assembly.GetTypes(), t => t.Name == activityName);
if (activityType != null)
{
string[] accounts = GetAccounts(accountsId);
activity = (IPerformanceTestActivity)Activator.CreateInstance(activityType,
connectionData.EntryPointHost,
connectionData.EntryPointPort,
accounts, testRunnerId);
}
else
{
throw new TypeLoadException(string.Format("Failed to create instance of type - {0}", activityType.Name));
}
return activity;
}
private static string[] GetAccounts(string accountIds)
{
List<string> accounts = new List<string>();
int[] ids = DecodeAccountsIds(accountIds);
for (int i = 0; i < ids.Length; i++)
{
accounts.Add(string.Format("{0}{1}", AccountPreffix, ids[i]));
}
return accounts.ToArray();
}
private static int[] DecodeAccountsIds(string encodedString)
{
string[] ids = encodedString.Split(' ');
List<int> result = new List<int>();
foreach (string id in ids)
{
int accountId = Int32.Parse(id);
result.Add(accountId);
}
return result.ToArray();
}
private static void CreateLogDirectory()
{
try
{
Directory.CreateDirectory(_logPath);
}
catch (Exception)
{
Environment.Exit(1);
}
}
}
}