forked from hpsa/hpe-application-automation-tools-plugin
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathMtbxManager.cs
268 lines (233 loc) · 11.5 KB
/
MtbxManager.cs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Certain versions of software accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company.
* This software was acquired by Micro Focus on September 1, 2017, and is now offered by OpenText.
* Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.
* __________________________________________________________________
* MIT License
*
* Copyright 2012-2024 Open Text
*
* The only warranties for products and services of Open Text and
* its affiliates and licensors ("Open Text") are as may be set forth
* in the express warranty statements accompanying such products and services.
* Nothing herein should be construed as constituting an additional warranty.
* Open Text shall not be liable for technical or editorial errors or
* omissions contained herein. The information contained herein is subject
* to change without notice.
*
* Except as specifically indicated otherwise, this document contains
* confidential information and a valid license is required for possession,
* use or copying. If this work is provided to the U.S. Government,
* consistent with FAR 12.211 and 12.212, Commercial Computer Software,
* Computer Software Documentation, and Technical Data for Commercial Items are
* licensed to the U.S. Government under vendor's standard commercial license.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ___________________________________________________________________
*/
using HpToolsLauncher.Properties;
using HpToolsLauncher.TestRunners;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
using System.Xml.Schema;
namespace HpToolsLauncher
{
public class MtbxManager
{
//the xml format of an mtbx file below:
/*
<Mtbx>
<Test Name="test1" path="${workspace}\test1">
<Parameter Name="mee" Value="12" Type="Integer"/>
<Parameter Name="mee1" Value="12.0" Type="Double"/>
<Parameter Name="mee2" Value="abc" Type="String"/>
<Parameter name="ParamBoolean" type="boolean" value="False"/>
<DataTable path="c:\tables\my_data_table.xls"/>
<Iterations mode="rngIterations|rngAll|oneIteration" start="2" end="3"/>
</Test>
<Test Name="test2" path="${workspace}\test2">
<Parameter Name="mee" Value="12" Type="Integer"/>
<Parameter Name="mee1" Value="12.0" Type="Double"/>
<Parameter Name="mee2" Value="abc" Type="String"/>
<Parameter name="ParamBoolean" type="boolean" value="False"/>
</Test>
</Mtbx>
*/
public static List<TestInfo> LoadMtbx(string mtbxContent, string testGroup)
{
return LoadMtbx(mtbxContent, null, testGroup);
}
public static List<TestInfo> Parse(string mtbxFileName)
{
string xmlContent = File.ReadAllText(mtbxFileName);
return Parse(xmlContent, null, mtbxFileName);
}
private static XAttribute GetAttribute(XElement x, XName attributeName)
{
return x.Attributes().FirstOrDefault(a => a.Name.Namespace == attributeName.Namespace
&& string.Equals(a.Name.LocalName, attributeName.LocalName, StringComparison.OrdinalIgnoreCase));
}
private static XElement GetElement(XElement x, XName eName)
{
return x.Elements().FirstOrDefault(a => a.Name.Namespace == eName.Namespace
&& string.Equals(a.Name.LocalName, eName.LocalName, StringComparison.OrdinalIgnoreCase));
}
private static IEnumerable<XElement> GetElements(XElement x, XName eName)
{
return x.Elements().Where(a => a.Name.Namespace == eName.Namespace
&& string.Equals(a.Name.LocalName, eName.LocalName, StringComparison.OrdinalIgnoreCase));
}
public static List<TestInfo> Parse(string mtbxFileName, Dictionary<string, string> jenkinsEnvVars, string testGroupName)
{
return LoadMtbx(File.ReadAllText(mtbxFileName), jenkinsEnvVars, testGroupName);
}
private static string ReplaceString(string str, string oldValue, string newValue, StringComparison comparison)
{
StringBuilder sb = new StringBuilder();
int previousIndex = 0;
int index = str.IndexOf(oldValue, comparison);
while (index != -1)
{
sb.Append(str.Substring(previousIndex, index - previousIndex));
sb.Append(newValue);
index += oldValue.Length;
previousIndex = index;
index = str.IndexOf(oldValue, index, comparison);
}
sb.Append(str.Substring(previousIndex));
return sb.ToString();
}
public static List<TestInfo> LoadMtbx(string xmlContent, Dictionary<string, string> jankinsEnvVars, string testGroupName)
{
var localEnv = Environment.GetEnvironmentVariables();
foreach (string varName in localEnv.Keys)
{
string value = (string)localEnv[varName];
xmlContent = ReplaceString(xmlContent, "%" + varName + "%", value, StringComparison.OrdinalIgnoreCase);
xmlContent = ReplaceString(xmlContent, "${" + varName + "}", value, StringComparison.OrdinalIgnoreCase);
}
if (jankinsEnvVars != null)
{
foreach (string varName in jankinsEnvVars.Keys)
{
string value = jankinsEnvVars[varName];
xmlContent = ReplaceString(xmlContent, "%" + varName + "%", value, StringComparison.OrdinalIgnoreCase);
xmlContent = ReplaceString(xmlContent, "${" + varName + "}", value, StringComparison.OrdinalIgnoreCase);
}
}
List<TestInfo> retval = new List<TestInfo>();
XDocument doc = XDocument.Parse(xmlContent);
XmlSchemaSet schemas = new XmlSchemaSet();
var assembly = Assembly.GetExecutingAssembly();
var schemaStream = assembly.GetManifestResourceStream("HpToolsLauncher.MtbxSchema.xsd");
XmlSchema schema = XmlSchema.Read(schemaStream, null);
schemas.Add(schema);
string validationMessages = "";
doc.Validate(schemas, (o, e) =>
{
validationMessages += e.Message + Environment.NewLine;
});
if (!string.IsNullOrWhiteSpace(validationMessages))
ConsoleWriter.WriteLine("mtbx schema validation errors: " + validationMessages);
try
{
var root = doc.Root;
foreach (var test in GetElements(root, "Test"))
{
string path = GetAttribute(test, "path").Value;
if (!Directory.Exists(path))
{
string line = string.Format(Resources.GeneralFileNotFound, path);
ConsoleWriter.WriteLine(line);
ConsoleWriter.ErrorSummaryLines.Add(line);
Launcher.ExitCode = Launcher.ExitCodeEnum.Failed;
continue;
}
XAttribute xname = GetAttribute(test, "name");
string name = "Unnamed Test";
if (xname != null && xname.Value != "")
{
name = xname.Value;
}
// optional report path attribute
XAttribute xReportPath = GetAttribute(test, "reportPath");
string reportPath = null;
if (xReportPath != null)
{
reportPath = xReportPath.Value;
}
TestInfo testInfo = new TestInfo(path, name, testGroupName)
{
ReportPath = reportPath
};
HashSet<string> paramNames = new HashSet<string>();
foreach (var param in GetElements(test, "Parameter"))
{
string pname = GetAttribute(param, "name").Value;
string pval = GetAttribute(param, "value").Value;
XAttribute attrType = GetAttribute(param, "type");
XAttribute attrSource = GetAttribute(param, "source");
string ptype = "string";
string source = null;
if (attrType != null)
ptype = attrType.Value;
if (attrSource != null)
source = attrSource.Value;
var testParam = new TestParameterInfo() { Name = pname, Type = ptype, Value = pval, Source = source };
if (!paramNames.Contains(testParam.Name))
{
paramNames.Add(testParam.Name);
testInfo.Params.Add(testParam);
}
else
{
string line = string.Format(Resources.GeneralDuplicateParameterWarning, pname, path);
ConsoleWriter.WriteLine(line);
}
}
XElement dataTable = GetElement(test, "DataTable");
if (dataTable != null)
{
testInfo.DataTablePath = GetAttribute(dataTable, "path").Value;
}
XElement iterations = GetElement(test, "Iterations");
if (iterations != null)
{
IterationInfo ii = new IterationInfo();
XAttribute modeAttr = GetAttribute(iterations, "mode");
if (modeAttr != null)
{
ii.IterationMode = modeAttr.Value;
}
XAttribute startAttr = GetAttribute(iterations, "start");
if (startAttr != null)
{
ii.StartIteration = startAttr.Value;
}
XAttribute endAttr = GetAttribute(iterations, "end");
if (endAttr != null)
{
ii.EndIteration = endAttr.Value;
}
testInfo.IterationInfo = ii;
}
retval.Add(testInfo);
}
}
catch (Exception ex)
{
ConsoleWriter.WriteException("Problem while parsing Mtbx file", ex);
}
return retval;
}
}
}