]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/atf/atf-report/atf-report.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / atf / atf-report / atf-report.cpp
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 // 1. Redistributions of source code must retain the above copyright
11 //    notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 //    notice, this list of conditions and the following disclaimer in the
14 //    documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29
30 extern "C" {
31 #include <sys/time.h>
32 }
33
34 #include <cstdlib>
35 #include <fstream>
36 #include <iomanip>
37 #include <iostream>
38 #include <memory>
39 #include <sstream>
40 #include <utility>
41 #include <vector>
42
43 #include "atf-c/defs.h"
44
45 #include "atf-c++/detail/application.hpp"
46 #include "atf-c++/detail/fs.hpp"
47 #include "atf-c++/detail/sanity.hpp"
48 #include "atf-c++/detail/text.hpp"
49 #include "atf-c++/detail/ui.hpp"
50
51 #include "reader.hpp"
52
53 typedef std::auto_ptr< std::ostream > ostream_ptr;
54
55 static ostream_ptr
56 open_outfile(const atf::fs::path& path)
57 {
58     ostream_ptr osp;
59     if (path.str() == "-")
60         osp = ostream_ptr(new std::ofstream("/dev/stdout"));
61     else
62         osp = ostream_ptr(new std::ofstream(path.c_str()));
63     if (!(*osp))
64         throw std::runtime_error("Could not create file " + path.str());
65     return osp;
66 }
67
68 static std::string
69 format_tv(struct timeval* tv)
70 {
71     std::ostringstream output;
72     output << static_cast< long >(tv->tv_sec) << '.'
73            << std::setfill('0') << std::setw(6)
74            << static_cast< long >(tv->tv_usec);
75     return output.str();
76 }
77
78 // ------------------------------------------------------------------------
79 // The "writer" interface.
80 // ------------------------------------------------------------------------
81
82 //!
83 //! \brief A base class that defines an output format.
84 //!
85 //! The writer base class defines a generic interface to output formats.
86 //! This is meant to be subclassed, and each subclass can redefine any
87 //! method to format the information as it wishes.
88 //!
89 //! This class is not tied to a output stream nor a file because, depending
90 //! on the output format, we will want to write to a single file or to
91 //! multiple ones.
92 //!
93 class writer {
94 public:
95     writer(void) {}
96     virtual ~writer(void) {}
97
98     virtual void write_info(const std::string&, const std::string&) {}
99     virtual void write_ntps(size_t) {}
100     virtual void write_tp_start(const std::string&, size_t) {}
101     virtual void write_tp_end(struct timeval*, const std::string&) {}
102     virtual void write_tc_start(const std::string&) {}
103     virtual void write_tc_stdout_line(const std::string&) {}
104     virtual void write_tc_stderr_line(const std::string&) {}
105     virtual void write_tc_end(const std::string&, struct timeval*,
106                               const std::string&) {}
107     virtual void write_eof(void) {}
108 };
109
110 // ------------------------------------------------------------------------
111 // The "csv_writer" class.
112 // ------------------------------------------------------------------------
113
114 //!
115 //! \brief A very simple plain-text output format.
116 //!
117 //! The csv_writer class implements a very simple plain-text output
118 //! format that summarizes the results of each executed test case.  The
119 //! results are meant to be easily parseable by third-party tools, hence
120 //! they are formatted as a CSV file.
121 //!
122 class csv_writer : public writer {
123     ostream_ptr m_os;
124     bool m_failed;
125
126     std::string m_tpname;
127     std::string m_tcname;
128
129 public:
130     csv_writer(const atf::fs::path& p) :
131         m_os(open_outfile(p))
132     {
133     }
134
135     virtual
136     void
137     write_tp_start(const std::string& name,
138                    size_t ntcs ATF_DEFS_ATTRIBUTE_UNUSED)
139     {
140         m_tpname = name;
141         m_failed = false;
142     }
143
144     virtual
145     void
146     write_tp_end(struct timeval* tv, const std::string& reason)
147     {
148         const std::string timestamp = format_tv(tv);
149
150         if (!reason.empty())
151             (*m_os) << "tp, " << timestamp << ", " << m_tpname << ", bogus, "
152                     << reason << "\n";
153         else if (m_failed)
154             (*m_os) << "tp, " << timestamp << ", "<< m_tpname << ", failed\n";
155         else
156             (*m_os) << "tp, " << timestamp << ", "<< m_tpname << ", passed\n";
157     }
158
159     virtual
160     void
161     write_tc_start(const std::string& name)
162     {
163         m_tcname = name;
164     }
165
166     virtual
167     void
168     write_tc_end(const std::string& state, struct timeval* tv,
169                  const std::string& reason)
170     {
171         std::string str = m_tpname + ", " + m_tcname + ", " + state;
172         if (!reason.empty())
173             str += ", " + reason;
174         (*m_os) << "tc, " << format_tv(tv) << ", " << str << "\n";
175
176         if (state == "failed")
177             m_failed = true;
178     }
179 };
180
181 // ------------------------------------------------------------------------
182 // The "ticker_writer" class.
183 // ------------------------------------------------------------------------
184
185 //!
186 //! \brief A console-friendly output format.
187 //!
188 //! The ticker_writer class implements a formatter that is user-friendly
189 //! in the sense that it shows the execution of test cases in an easy to
190 //! read format.  It is not meant to be parseable and its format can
191 //! freely change across releases.
192 //!
193 class ticker_writer : public writer {
194     ostream_ptr m_os;
195
196     size_t m_curtp, m_ntps;
197     size_t m_tcs_passed, m_tcs_failed, m_tcs_skipped, m_tcs_expected_failures;
198     std::string m_tcname, m_tpname;
199     std::vector< std::string > m_failed_tcs;
200     std::map< std::string, std::string > m_expected_failures_tcs;
201     std::vector< std::string > m_failed_tps;
202
203     void
204     write_info(const std::string& what, const std::string& val)
205     {
206         if (what == "tests.root") {
207             (*m_os) << "Tests root: " << val << "\n\n";
208         }
209     }
210
211     void
212     write_ntps(size_t ntps)
213     {
214         m_curtp = 1;
215         m_tcs_passed = 0;
216         m_tcs_failed = 0;
217         m_tcs_skipped = 0;
218         m_tcs_expected_failures = 0;
219         m_ntps = ntps;
220     }
221
222     void
223     write_tp_start(const std::string& tp, size_t ntcs)
224     {
225         using atf::text::to_string;
226         using atf::ui::format_text;
227
228         m_tpname = tp;
229
230         (*m_os) << format_text(tp + " (" + to_string(m_curtp) +
231                                "/" + to_string(m_ntps) + "): " +
232                                to_string(ntcs) + " test cases")
233                 << "\n";
234         (*m_os).flush();
235     }
236
237     void
238     write_tp_end(struct timeval* tv, const std::string& reason)
239     {
240         using atf::ui::format_text_with_tag;
241
242         m_curtp++;
243
244         if (!reason.empty()) {
245             (*m_os) << format_text_with_tag("BOGUS TEST PROGRAM: Cannot "
246                                             "trust its results because "
247                                             "of `" + reason + "'",
248                                             m_tpname + ": ", false)
249                     << "\n";
250             m_failed_tps.push_back(m_tpname);
251         }
252         (*m_os) << "[" << format_tv(tv) << "s]\n\n";
253         (*m_os).flush();
254
255         m_tpname.clear();
256     }
257
258     void
259     write_tc_start(const std::string& tcname)
260     {
261         m_tcname = tcname;
262
263         (*m_os) << "    " + tcname + ": ";
264         (*m_os).flush();
265     }
266
267     void
268     write_tc_end(const std::string& state, struct timeval* tv,
269                  const std::string& reason)
270     {
271         std::string str;
272
273         (*m_os) << "[" << format_tv(tv) << "s] ";
274
275         if (state == "expected_death" || state == "expected_exit" ||
276             state == "expected_failure" || state == "expected_signal" ||
277             state == "expected_timeout") {
278             str = "Expected failure: " + reason;
279             m_tcs_expected_failures++;
280             m_expected_failures_tcs[m_tpname + ":" + m_tcname] = reason;
281         } else if (state == "failed") {
282             str = "Failed: " + reason;
283             m_tcs_failed++;
284             m_failed_tcs.push_back(m_tpname + ":" + m_tcname);
285         } else if (state == "passed") {
286             str = "Passed.";
287             m_tcs_passed++;
288         } else if (state == "skipped") {
289             str = "Skipped: " + reason;
290             m_tcs_skipped++;
291         } else
292             UNREACHABLE;
293
294         // XXX Wrap text.  format_text_with_tag does not currently allow
295         // to specify the current column, which is needed because we have
296         // already printed the tc's name.
297         (*m_os) << str << '\n';
298
299         m_tcname = "";
300     }
301
302     static void
303     write_expected_failures(const std::map< std::string, std::string >& xfails,
304                             std::ostream& os)
305     {
306         using atf::ui::format_text;
307         using atf::ui::format_text_with_tag;
308
309         os << format_text("Test cases for known bugs:") << "\n";
310
311         for (std::map< std::string, std::string >::const_iterator iter =
312              xfails.begin(); iter != xfails.end(); iter++) {
313             const std::string& name = (*iter).first;
314             const std::string& reason = (*iter).second;
315
316             os << format_text_with_tag(reason, "    " + name + ": ", false)
317                << "\n";
318         }
319     }
320
321     void
322     write_eof(void)
323     {
324         using atf::text::join;
325         using atf::text::to_string;
326         using atf::ui::format_text;
327         using atf::ui::format_text_with_tag;
328
329         if (!m_failed_tps.empty()) {
330             (*m_os) << format_text("Failed (bogus) test programs:")
331                     << "\n";
332             (*m_os) << format_text_with_tag(join(m_failed_tps, ", "),
333                                             "    ", false) << "\n\n";
334         }
335
336         if (!m_expected_failures_tcs.empty()) {
337             write_expected_failures(m_expected_failures_tcs, *m_os);
338             (*m_os) << "\n";
339         }
340
341         if (!m_failed_tcs.empty()) {
342             (*m_os) << format_text("Failed test cases:") << "\n";
343             (*m_os) << format_text_with_tag(join(m_failed_tcs, ", "),
344                                             "    ", false) << "\n\n";
345         }
346
347         (*m_os) << format_text("Summary for " + to_string(m_ntps) +
348                                " test programs:") << "\n";
349         (*m_os) << format_text_with_tag(to_string(m_tcs_passed) +
350                                         " passed test cases.",
351                                         "    ", false) << "\n";
352         (*m_os) << format_text_with_tag(to_string(m_tcs_failed) +
353                                         " failed test cases.",
354                                         "    ", false) << "\n";
355         (*m_os) << format_text_with_tag(to_string(m_tcs_expected_failures) +
356                                         " expected failed test cases.",
357                                         "    ", false) << "\n";
358         (*m_os) << format_text_with_tag(to_string(m_tcs_skipped) +
359                                         " skipped test cases.",
360                                         "    ", false) << "\n";
361     }
362
363 public:
364     ticker_writer(const atf::fs::path& p) :
365         m_os(open_outfile(p))
366     {
367     }
368 };
369
370 // ------------------------------------------------------------------------
371 // The "xml" class.
372 // ------------------------------------------------------------------------
373
374 //!
375 //! \brief A single-file XML output format.
376 //!
377 //! The xml_writer class implements a formatter that prints the results
378 //! of test cases in an XML format easily parseable later on by other
379 //! utilities.
380 //!
381 class xml_writer : public writer {
382     ostream_ptr m_os;
383
384     std::string m_tcname, m_tpname;
385
386     static
387     std::string
388     attrval(const std::string& str)
389     {
390         return str;
391     }
392
393     static
394     std::string
395     elemval(const std::string& str)
396     {
397         std::string ostr;
398         for (std::string::const_iterator iter = str.begin();
399              iter != str.end(); iter++) {
400             switch (*iter) {
401             case '&': ostr += "&amp;"; break;
402             case '<': ostr += "&lt;"; break;
403             case '>': ostr += "&gt;"; break;
404             default:  ostr += *iter;
405             }
406         }
407         return ostr;
408     }
409
410     void
411     write_info(const std::string& what, const std::string& val)
412     {
413         (*m_os) << "<info class=\"" << what << "\">" << val << "</info>\n";
414     }
415
416     void
417     write_tp_start(const std::string& tp,
418                    size_t ntcs ATF_DEFS_ATTRIBUTE_UNUSED)
419     {
420         (*m_os) << "<tp id=\"" << attrval(tp) << "\">\n";
421     }
422
423     void
424     write_tp_end(struct timeval* tv, const std::string& reason)
425     {
426         if (!reason.empty())
427             (*m_os) << "<failed>" << elemval(reason) << "</failed>\n";
428         (*m_os) << "<tp-time>" << format_tv(tv) << "</tp-time>";
429         (*m_os) << "</tp>\n";
430     }
431
432     void
433     write_tc_start(const std::string& tcname)
434     {
435         (*m_os) << "<tc id=\"" << attrval(tcname) << "\">\n";
436     }
437
438     void
439     write_tc_stdout_line(const std::string& line)
440     {
441         (*m_os) << "<so>" << elemval(line) << "</so>\n";
442     }
443
444     void
445     write_tc_stderr_line(const std::string& line)
446     {
447         (*m_os) << "<se>" << elemval(line) << "</se>\n";
448     }
449
450     void
451     write_tc_end(const std::string& state, struct timeval* tv,
452                  const std::string& reason)
453     {
454         std::string str;
455
456         if (state == "expected_death" || state == "expected_exit" ||
457             state == "expected_failure" || state == "expected_signal" ||
458             state == "expected_timeout") {
459             (*m_os) << "<" << state << ">" << elemval(reason)
460                     << "</" << state << ">\n";
461         } else if (state == "passed") {
462             (*m_os) << "<passed />\n";
463         } else if (state == "failed") {
464             (*m_os) << "<failed>" << elemval(reason) << "</failed>\n";
465         } else if (state == "skipped") {
466             (*m_os) << "<skipped>" << elemval(reason) << "</skipped>\n";
467         } else
468             UNREACHABLE;
469         (*m_os) << "<tc-time>" << format_tv(tv) << "</tc-time>";
470         (*m_os) << "</tc>\n";
471     }
472
473     void
474     write_eof(void)
475     {
476         (*m_os) << "</tests-results>\n";
477     }
478
479 public:
480     xml_writer(const atf::fs::path& p) :
481         m_os(open_outfile(p))
482     {
483         (*m_os) << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
484                 << "<!DOCTYPE tests-results PUBLIC "
485                    "\"-//NetBSD//DTD ATF Tests Results 0.1//EN\" "
486                    "\"http://www.NetBSD.org/XML/atf/tests-results.dtd\">\n\n"
487                    "<tests-results>\n";
488     }
489 };
490
491 // ------------------------------------------------------------------------
492 // The "converter" class.
493 // ------------------------------------------------------------------------
494
495 //!
496 //! \brief A reader that redirects events to multiple writers.
497 //!
498 //! The converter class implements an atf_tps_reader that, for each event
499 //! raised by the parser, redirects it to multiple writers so that they
500 //! can reformat it according to their output rules.
501 //!
502 class converter : public atf::atf_report::atf_tps_reader {
503     typedef std::vector< writer* > outs_vector;
504     outs_vector m_outs;
505
506     void
507     got_info(const std::string& what, const std::string& val)
508     {
509         for (outs_vector::iterator iter = m_outs.begin();
510              iter != m_outs.end(); iter++)
511             (*iter)->write_info(what, val);
512     }
513
514     void
515     got_ntps(size_t ntps)
516     {
517         for (outs_vector::iterator iter = m_outs.begin();
518              iter != m_outs.end(); iter++)
519             (*iter)->write_ntps(ntps);
520     }
521
522     void
523     got_tp_start(const std::string& tp, size_t ntcs)
524     {
525         for (outs_vector::iterator iter = m_outs.begin();
526              iter != m_outs.end(); iter++)
527             (*iter)->write_tp_start(tp, ntcs);
528     }
529
530     void
531     got_tp_end(struct timeval* tv, const std::string& reason)
532     {
533         for (outs_vector::iterator iter = m_outs.begin();
534              iter != m_outs.end(); iter++)
535             (*iter)->write_tp_end(tv, reason);
536     }
537
538     void
539     got_tc_start(const std::string& tcname)
540     {
541         for (outs_vector::iterator iter = m_outs.begin();
542              iter != m_outs.end(); iter++)
543             (*iter)->write_tc_start(tcname);
544     }
545
546     void
547     got_tc_stdout_line(const std::string& line)
548     {
549         for (outs_vector::iterator iter = m_outs.begin();
550              iter != m_outs.end(); iter++)
551             (*iter)->write_tc_stdout_line(line);
552     }
553
554     void
555     got_tc_stderr_line(const std::string& line)
556     {
557         for (outs_vector::iterator iter = m_outs.begin();
558              iter != m_outs.end(); iter++)
559             (*iter)->write_tc_stderr_line(line);
560     }
561
562     void
563     got_tc_end(const std::string& state, struct timeval* tv,
564                const std::string& reason)
565     {
566         for (outs_vector::iterator iter = m_outs.begin();
567              iter != m_outs.end(); iter++)
568             (*iter)->write_tc_end(state, tv, reason);
569     }
570
571     void
572     got_eof(void)
573     {
574         for (outs_vector::iterator iter = m_outs.begin();
575              iter != m_outs.end(); iter++)
576             (*iter)->write_eof();
577     }
578
579 public:
580     converter(std::istream& is) :
581         atf::atf_report::atf_tps_reader(is)
582     {
583     }
584
585     ~converter(void)
586     {
587         for (outs_vector::iterator iter = m_outs.begin();
588              iter != m_outs.end(); iter++)
589             delete *iter;
590     }
591
592     void
593     add_output(const std::string& fmt, const atf::fs::path& p)
594     {
595         if (fmt == "csv") {
596             m_outs.push_back(new csv_writer(p));
597         } else if (fmt == "ticker") {
598             m_outs.push_back(new ticker_writer(p));
599         } else if (fmt == "xml") {
600             m_outs.push_back(new xml_writer(p));
601         } else
602             throw std::runtime_error("Unknown format `" + fmt + "'");
603     }
604 };
605
606 // ------------------------------------------------------------------------
607 // The "atf_report" class.
608 // ------------------------------------------------------------------------
609
610 class atf_report : public atf::application::app {
611     static const char* m_description;
612
613     typedef std::pair< std::string, atf::fs::path > fmt_path_pair;
614     std::vector< fmt_path_pair > m_oflags;
615
616     void process_option(int, const char*);
617     options_set specific_options(void) const;
618
619 public:
620     atf_report(void);
621
622     int main(void);
623 };
624
625 const char* atf_report::m_description =
626     "atf-report is a tool that parses the output of atf-run and "
627     "generates user-friendly reports in multiple different formats.";
628
629 atf_report::atf_report(void) :
630     app(m_description, "atf-report(1)", "atf(7)")
631 {
632 }
633
634 void
635 atf_report::process_option(int ch, const char* arg)
636 {
637     switch (ch) {
638     case 'o':
639         {
640             std::string str(arg);
641             std::string::size_type pos = str.find(':');
642             if (pos == std::string::npos)
643                 throw std::runtime_error("Syntax error in -o option");
644             else {
645                 std::string fmt = str.substr(0, pos);
646                 atf::fs::path path = atf::fs::path(str.substr(pos + 1));
647                 m_oflags.push_back(fmt_path_pair(fmt, path));
648             }
649         }
650         break;
651
652     default:
653         UNREACHABLE;
654     }
655 }
656
657 atf_report::options_set
658 atf_report::specific_options(void)
659     const
660 {
661     using atf::application::option;
662     options_set opts;
663     opts.insert(option('o', "fmt:path", "Adds a new output file; multiple "
664                                         "ones can be specified, and a - "
665                                         "path means stdout"));
666     return opts;
667 }
668
669 int
670 atf_report::main(void)
671 {
672     if (m_argc > 0)
673         throw std::runtime_error("No arguments allowed");
674
675     if (m_oflags.empty())
676         m_oflags.push_back(fmt_path_pair("ticker", atf::fs::path("-")));
677
678     // Look for path duplicates.
679     std::set< atf::fs::path > paths;
680     for (std::vector< fmt_path_pair >::const_iterator iter = m_oflags.begin();
681          iter != m_oflags.end(); iter++) {
682         atf::fs::path p = (*iter).second;
683         if (p == atf::fs::path("/dev/stdout"))
684             p = atf::fs::path("-");
685         if (paths.find(p) != paths.end())
686             throw std::runtime_error("The file `" + p.str() + "' was "
687                                      "specified more than once");
688         paths.insert((*iter).second);
689     }
690
691     // Generate the output files.
692     converter cnv(std::cin);
693     for (std::vector< fmt_path_pair >::const_iterator iter = m_oflags.begin();
694          iter != m_oflags.end(); iter++)
695         cnv.add_output((*iter).first, (*iter).second);
696     cnv.read();
697
698     return EXIT_SUCCESS;
699 }
700
701 int
702 main(int argc, char* const* argv)
703 {
704     return atf_report().run(argc, argv);
705 }