~ubuntu-branches/ubuntu/karmic/moon/karmic

« back to all changes in this revision

Viewing changes to test/harness/test-runner/DbReport.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Permission is hereby granted, free of charge, to any person obtaining
 
2
// a copy of this software and associated documentation files (the
 
3
// "Software"), to deal in the Software without restriction, including
 
4
// without limitation the rights to use, copy, modify, merge, publish,
 
5
// distribute, sublicense, and/or sell copies of the Software, and to
 
6
// permit persons to whom the Software is furnished to do so, subject to
 
7
// the following conditions:
 
8
//
 
9
// The above copyright notice and this permission notice shall be
 
10
// included in all copies or substantial portions of the Software.
 
11
//
 
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
13
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
14
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
15
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
16
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
17
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
18
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
19
//
 
20
// Copyright (c) 2007-2008 Novell, Inc.
 
21
//
 
22
// Authors:
 
23
//>-Rusty Howell (rhowell@novell.com)
 
24
//
 
25
 
 
26
 
 
27
using System;
 
28
using System.IO;
 
29
using System.Data;
 
30
using System.Collections.Generic;
 
31
using System.Diagnostics;
 
32
using Npgsql;
 
33
 
 
34
 
 
35
namespace MoonlightTests {
 
36
        
 
37
        public class DbReport : IReport {
 
38
                
 
39
                private string connectionString = string.Empty;
 
40
                private IDbConnection dbcon = null;
 
41
                private IDbCommand dbcmd = null;
 
42
                
 
43
                private string runtime;
 
44
                private int runtimeid;
 
45
                private string masters = "masters";
 
46
                
 
47
                private string test_run_dir;
 
48
                private string test_suite = string.Empty;
 
49
                private bool debug = false;
 
50
                
 
51
                public bool HasConnection
 
52
                {
 
53
                        get {return (connectionString != string.Empty);}
 
54
                }
 
55
                
 
56
                public DbReport()
 
57
                {
 
58
 
 
59
                        try {
 
60
                                test_suite = Environment.GetEnvironmentVariable ("MS_TEST_SUITE");
 
61
                        
 
62
                                if ((test_suite != null) && (test_suite.Trim().ToLower() == "true")) {
 
63
                                        test_suite = "ms";
 
64
                                        throw new Exception("DbReport not enabled for MS test suite");// We should not log MS test suite results
 
65
                                }
 
66
                                else {
 
67
                                        test_suite = "moon";
 
68
                                }
 
69
                                StreamReader reader = new StreamReader(".dbconnection.txt");
 
70
                                connectionString = reader.ReadLine(); // Read the first line of the file
 
71
                                reader.Close();
 
72
                                
 
73
                                //Log(string.Format("Connecting to Postgresql server: '{0}'",connectionString));
 
74
                                dbcon =  new NpgsqlConnection(connectionString);
 
75
                                dbcon.Open();
 
76
                                dbcmd = dbcon.CreateCommand();
 
77
                                string query = "select id from testcases;";
 
78
                                
 
79
                                dbcmd.CommandText = query;
 
80
                                IDataReader dbreader = dbcmd.ExecuteReader();
 
81
                                int count = 0;
 
82
                                while(dbreader.Read()) {
 
83
                                        int id = dbreader.GetInt32(0);
 
84
                                        ++count;
 
85
                                }
 
86
                                Log(string.Format("{0} test cases read", count));
 
87
                                dbreader.Close();
 
88
                                dbreader = null;
 
89
                                
 
90
                                Console.WriteLine("\nDbReport enabled\n");
 
91
                        
 
92
                        }
 
93
                        catch(Exception ex) {
 
94
                                Console.WriteLine("\nDbReport disabled\n"); // Use Console.WriteLine() so that this line always appears
 
95
                                WriteHelp();
 
96
                                Log(string.Format("\n{1} {0}",ex.GetType(), ex.Message));
 
97
                                connectionString = string.Empty;
 
98
                        }
 
99
                        
 
100
                }
 
101
                private void WriteHelp()
 
102
                {
 
103
                        
 
104
                        string str = "\nDbReport is disabled because of missing file or invalid connection string\n";
 
105
                        str += "\nCreate file moon/test/.dbconnection.txt with the first line being the connection string to use.";
 
106
                        str += "\nServer=mysqlserver.company.com;Database=db;User ID=user;Password=password;Pooling=false;\n";
 
107
                        
 
108
                        Log(str);
 
109
                                
 
110
                }
 
111
#region IReport members
 
112
                public void BeginRun(TestRun run)
 
113
                {
 
114
                        
 
115
                        if (!HasConnection) {
 
116
                                return;
 
117
                        }
 
118
                                
 
119
                        
 
120
                        string dir = "test-run-data";
 
121
                        string filename = "moonTestSuite.db";
 
122
                        
 
123
                        runtime = run.StartTime.ToString("yyyy-MM-dd HH:mm");//Postgres timestamp format
 
124
                        test_run_dir = Path.Combine(dir,runtime);
 
125
                        
 
126
                        if (!Directory.Exists(test_run_dir)) {
 
127
                                Directory.CreateDirectory(test_run_dir);
 
128
                        }
 
129
                        if (!Directory.Exists(Path.Combine(dir,masters))) {
 
130
                                Directory.CreateDirectory(Path.Combine(dir,masters));
 
131
                        }
 
132
 
 
133
 
 
134
                        Log(string.Format("Runtime: {0}", runtime));
 
135
                        
 
136
                        AddBuild(runtime);
 
137
                }
 
138
                
 
139
                public void EndRun()
 
140
                {
 
141
                }
 
142
                public void Executing(Test test)
 
143
                {
 
144
                }
 
145
                
 
146
                public void AddResult(Test test, TestResult result)
 
147
                {
 
148
                        if (!HasConnection) {
 
149
                                return;
 
150
                        }
 
151
                                
 
152
                        AddTags(test);
 
153
                        string info = string.Empty;
 
154
                        
 
155
                        string testname = test.InputFileName.Split('.')[0];
 
156
                        string masterfile = Path.Combine(masters, Path.GetFileName(test.MasterFile));
 
157
                        string renderfile = Path.Combine(runtime, Path.GetFileName(test.ResultFile));
 
158
                        
 
159
                        string result_file = XmlReport.GetFilePath (test.ResultFile);
 
160
                        string master_file = XmlReport.GetFilePath (test.MasterFile);
 
161
 
 
162
                        //Log("masterfile = " + masterfile);
 
163
                        //Log("renderfile = " + renderfile);
 
164
                        
 
165
                        //Log("result_file = " + result_file);
 
166
                        //Log("master_file = " + master_file);
 
167
                        
 
168
                        
 
169
                        XmlReport.CopyImageToRunDirectory(test_run_dir,result_file);
 
170
                        XmlReport.CopyImageToRunDirectory(Path.Combine("test-run-data",masters), master_file);
 
171
                        
 
172
                        if (masterfile.EndsWith("tif") || masterfile.EndsWith("tiff")) {
 
173
                                masterfile += ".png";
 
174
                                renderfile += ".png";
 
175
                        }
 
176
                        
 
177
                        int internal_id = AddTestCase(test.Id,testname,masterfile,test_suite);
 
178
                        
 
179
                        switch(result) {
 
180
 
 
181
                                case TestResult.Fail:
 
182
                                        info = test.FailedReason;
 
183
                                        break;
 
184
                                case TestResult.Ignore:
 
185
                                        info = test.IgnoreReason;
 
186
                                        break;
 
187
                                case TestResult.KnownFailure:
 
188
                                        info = test.KnownFailureReason;
 
189
                                        break;
 
190
                                default:
 
191
                                        info = string.Empty;
 
192
                                        break;
 
193
                        }
 
194
 
 
195
                        string query = string.Format("INSERT INTO results (internal_id,runtimeid,result,renderfile,info) VALUES ('{0}','{1}','{2}','{3}', '{4}');",internal_id, runtimeid, result.ToString(), renderfile, info);
 
196
                        execnonquery(query);
 
197
 
 
198
                }
 
199
                private int AddTestCase(string id, string testname, string masterfile, string suite)
 
200
                {
 
201
                        int intid = Convert.ToInt32(id);
 
202
                        int internal_id = -1;
 
203
 
 
204
                        string query = string.Format("Select internal_id from testcases where id={0} and suite='{1}'",intid,suite);
 
205
                        
 
206
                        dbcmd.CommandText = query;
 
207
                        IDataReader reader = dbcmd.ExecuteReader();
 
208
                        
 
209
                        // If the test case is not found, add it
 
210
                        if(!reader.Read()) {
 
211
                                reader.Close();
 
212
                                query = string.Format("INSERT INTO testcases (id, suite, name, masterfile) VALUES ({0},'{1}','{2}','{3}');",intid, suite, testname, masterfile);
 
213
                                Log(query);
 
214
                                execnonquery(query);
 
215
                        }
 
216
                        else {
 
217
                                internal_id = reader.GetInt32(0);
 
218
                                Log(string.Format("Testcase exists with internal_id={0}",internal_id));
 
219
                        }
 
220
                        reader.Close();
 
221
                        reader = null;
 
222
 
 
223
                        if (internal_id != -1)
 
224
                                return internal_id;
 
225
 
 
226
                        dbcmd.CommandText = string.Format("SELECT internal_id FROM testcases WHERE id={0} AND suite='{1}';",intid,suite);
 
227
                        reader = dbcmd.ExecuteReader();
 
228
                        reader.Read();
 
229
                        
 
230
                        internal_id = reader.GetInt32(0);
 
231
                        reader.Close();
 
232
                        reader = null;
 
233
 
 
234
 
 
235
                        return internal_id;
 
236
                        
 
237
                }
 
238
#endregion
 
239
                
 
240
                public void Close()
 
241
                {
 
242
                        if (dbcmd != null)
 
243
                                dbcmd.Dispose();
 
244
                        dbcmd = null;
 
245
 
 
246
                        if (dbcon != null)
 
247
                                dbcon.Close();
 
248
                        dbcon = null;
 
249
                }
 
250
 
 
251
                private void AddBuild(string time)
 
252
                {
 
253
                        
 
254
                        string revision = GetSubersionRevision();
 
255
                        string arch = GetArch();
 
256
                        
 
257
                        string query = string.Format("INSERT INTO builds (revision, runtime, arch) VALUES ('{0}','{1}','{2}');",revision,time,arch);
 
258
                        //execnonquery(query);
 
259
                        dbcmd.CommandText = query;
 
260
                        try {
 
261
                                dbcmd.ExecuteNonQuery();
 
262
                        }
 
263
                        catch(Exception ex) {
 
264
                                Log(ex.ToString());
 
265
                                Log(query);
 
266
                        }
 
267
                        IDataReader reader = execreader(string.Format("select id from builds where revision={0} and runtime='{1}' and arch='{2}';",revision,time,arch));
 
268
                        if ((reader != null) && reader.Read())
 
269
                        {
 
270
                                this.runtimeid = reader.GetInt32(0);
 
271
                        }
 
272
                        if (reader != null)
 
273
                                reader.Close();
 
274
                        
 
275
                        
 
276
                                                        
 
277
                }
 
278
                private void AddTags(Test test)
 
279
                {
 
280
                        foreach(string tag in test.Categories)
 
281
                        {
 
282
                                string query = string.Format("SELECT id FROM tags WHERE name = '{0}';",tag.ToLower());
 
283
                                dbcmd.CommandText = query;
 
284
                                IDataReader reader = null;
 
285
                                int tagid = int.MinValue;
 
286
                                try
 
287
                                {
 
288
                                        reader = dbcmd.ExecuteReader();
 
289
                                        reader.Read();
 
290
                                        tagid = reader.GetInt32(0);
 
291
                                        
 
292
                                }
 
293
                                catch(Exception ex) {
 
294
                                        Log("Tag exists:" + tag);
 
295
                                        tagid = int.MinValue;
 
296
                                }
 
297
                                finally {
 
298
                                        if (reader != null)
 
299
                                        {
 
300
                                                reader.Close();
 
301
                                                reader = null;
 
302
                                        }
 
303
                                }
 
304
                                        
 
305
                                if (tagid == int.MinValue) {
 
306
                                        try {
 
307
                                                query = string.Format("INSERT INTO tags (name) VALUES ('{0}');",tag.ToLower());
 
308
                                                execnonquery(query);                                    
 
309
                                                
 
310
                                                query = string.Format("SELECT id FROM tags WHERE name = '{0}';",tag.ToLower());
 
311
                                                dbcmd.CommandText = query;
 
312
                                                reader = dbcmd.ExecuteReader();
 
313
                                                reader.Read();
 
314
                                                tagid = reader.GetInt32(0);
 
315
                                                reader.Close();
 
316
                                        }
 
317
                                        catch (Exception ex) {
 
318
                                                
 
319
                                                Log("ADDTAG " +ex.Message);
 
320
                                        }
 
321
                                        finally{
 
322
                                                if (reader != null) {
 
323
                                                        reader.Close();
 
324
                                                        reader = null;
 
325
                                                }
 
326
                                        }
 
327
                                }
 
328
                                
 
329
                                AddTaggedCase(test.Id,tagid);
 
330
                        }
 
331
                }
 
332
                private void AddTaggedCase(string testid, int tagid)
 
333
                {
 
334
                        string query = string.Format("select testcaseid,tagid from taggedcases where testcaseid={0} and tagid={1};",testid,tagid);
 
335
                        IDataReader reader = execreader(query);
 
336
                        if ((reader != null) && reader.Read()) {
 
337
                                reader.Close();
 
338
                                //Log(string.Format("tagged case EXISTS for testcaseid={0} and tagid={1};",testid,tagid));
 
339
                        }
 
340
                        else {
 
341
                                if (reader != null)
 
342
                                        reader.Close();
 
343
                                query = string.Format("INSERT INTO taggedcases values ({0},'{1}');",tagid, testid);
 
344
                                execnonquery(query);
 
345
                        }
 
346
                        reader = null;
 
347
                                
 
348
                }
 
349
                private void execnonquery(string query)
 
350
                {
 
351
                        try {
 
352
                                dbcmd.CommandText = query;
 
353
                                dbcmd.ExecuteNonQuery();
 
354
                        }
 
355
                        catch(Exception ex) {
 
356
                                Log(string.Format("EXECQUERY {0}: {1}",ex.GetType().ToString(), ex.Message));
 
357
                                Log(query);
 
358
                        }
 
359
                }
 
360
                private IDataReader execreader(string query)
 
361
                {
 
362
                        try {
 
363
                                dbcmd.CommandText = query;
 
364
                                IDataReader reader = dbcmd.ExecuteReader();
 
365
                                return reader;
 
366
                        }
 
367
                        catch(Exception ex) {
 
368
                                Log(string.Format("NONEXEC {0}: {1}",ex.GetType().ToString(), query));
 
369
                                return null;
 
370
                        }
 
371
                }
 
372
 
 
373
                private string GetArch()
 
374
                {
 
375
                        ProcessStartInfo info = new ProcessStartInfo();
 
376
                        info.FileName = "uname";
 
377
                        info.Arguments = "-i";
 
378
                        
 
379
                        info.UseShellExecute = false;
 
380
                        info.RedirectStandardOutput = true;
 
381
                        Process p = new Process();
 
382
                        p.StartInfo = info;
 
383
                        p.Start();
 
384
 
 
385
                        string output = p.StandardOutput.ReadToEnd();
 
386
                        
 
387
                        return output.Trim();
 
388
                }
 
389
                
 
390
                private string GetSubersionRevision()
 
391
                {
 
392
                        
 
393
                        ProcessStartInfo info = new ProcessStartInfo();
 
394
                        info.FileName = "svn";
 
395
                        info.Arguments = "info";
 
396
                        info.UseShellExecute = false;
 
397
                        info.RedirectStandardOutput = true;
 
398
                        Process p = new Process();
 
399
                        p.StartInfo = info;
 
400
                        p.Start();
 
401
 
 
402
                        string revision = string.Empty;
 
403
                        string output = p.StandardOutput.ReadToEnd();
 
404
                        string[] lines = output.Split('\n');
 
405
 
 
406
                        try {
 
407
                        
 
408
                                foreach (string line in lines) {
 
409
                                        if (line.StartsWith("Revision")) {
 
410
                                                revision = line.Split(':')[1].Trim();
 
411
                                        }
 
412
                                }
 
413
                        }
 
414
                        catch (Exception ex) {
 
415
                                
 
416
                                Log("Cannot get revision number via svn info: " + ex.Message);
 
417
                                revision = string.Empty;
 
418
                        }
 
419
 
 
420
                        if (revision == string.Empty) {
 
421
                                revision = Environment.GetEnvironmentVariable ("SVN_REVISION");
 
422
 
 
423
                        }
 
424
                        return revision;
 
425
 
 
426
                }
 
427
                private void Log(string msg)
 
428
                {
 
429
                        if (debug)
 
430
                                Console.WriteLine(msg);
 
431
                }
 
432
        }
 
433
}