]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/atf/atf-report/atf-report.cpp
Merge ATF 0.16 from vendor/atf/dist.
[FreeBSD/FreeBSD.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     size_t m_curtp, m_ntps;
385     std::string m_tcname, m_tpname;
386
387     static
388     std::string
389     attrval(const std::string& str)
390     {
391         return str;
392     }
393
394     static
395     std::string
396     elemval(const std::string& str)
397     {
398         std::string ostr;
399         for (std::string::const_iterator iter = str.begin();
400              iter != str.end(); iter++) {
401             switch (*iter) {
402             case '&': ostr += "&amp;"; break;
403             case '<': ostr += "&lt;"; break;
404             case '>': ostr += "&gt;"; break;
405             default:  ostr += *iter;
406             }
407         }
408         return ostr;
409     }
410
411     void
412     write_info(const std::string& what, const std::string& val)
413     {
414         (*m_os) << "<info class=\"" << what << "\">" << val << "</info>\n";
415     }
416
417     void
418     write_tp_start(const std::string& tp,
419                    size_t ntcs ATF_DEFS_ATTRIBUTE_UNUSED)
420     {
421         (*m_os) << "<tp id=\"" << attrval(tp) << "\">\n";
422     }
423
424     void
425     write_tp_end(struct timeval* tv, const std::string& reason)
426     {
427         if (!reason.empty())
428             (*m_os) << "<failed>" << elemval(reason) << "</failed>\n";
429         (*m_os) << "<tp-time>" << format_tv(tv) << "</tp-time>";
430         (*m_os) << "</tp>\n";
431     }
432
433     void
434     write_tc_start(const std::string& tcname)
435     {
436         (*m_os) << "<tc id=\"" << attrval(tcname) << "\">\n";
437     }
438
439     void
440     write_tc_stdout_line(const std::string& line)
441     {
442         (*m_os) << "<so>" << elemval(line) << "</so>\n";
443     }
444
445     void
446     write_tc_stderr_line(const std::string& line)
447     {
448         (*m_os) << "<se>" << elemval(line) << "</se>\n";
449     }
450
451     void
452     write_tc_end(const std::string& state, struct timeval* tv,
453                  const std::string& reason)
454     {
455         std::string str;
456
457         if (state == "expected_death" || state == "expected_exit" ||
458             state == "expected_failure" || state == "expected_signal" ||
459             state == "expected_timeout") {
460             (*m_os) << "<" << state << ">" << elemval(reason)
461                     << "</" << state << ">\n";
462         } else if (state == "passed") {
463             (*m_os) << "<passed />\n";
464         } else if (state == "failed") {
465             (*m_os) << "<failed>" << elemval(reason) << "</failed>\n";
466         } else if (state == "skipped") {
467             (*m_os) << "<skipped>" << elemval(reason) << "</skipped>\n";
468         } else
469             UNREACHABLE;
470         (*m_os) << "<tc-time>" << format_tv(tv) << "</tc-time>";
471         (*m_os) << "</tc>\n";
472     }
473
474     void
475     write_eof(void)
476     {
477         (*m_os) << "</tests-results>\n";
478     }
479
480 public:
481     xml_writer(const atf::fs::path& p) :
482         m_os(open_outfile(p))
483     {
484         (*m_os) << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
485                 << "<!DOCTYPE tests-results PUBLIC "
486                    "\"-//NetBSD//DTD ATF Tests Results 0.1//EN\" "
487                    "\"http://www.NetBSD.org/XML/atf/tests-results.dtd\">\n\n"
488                    "<tests-results>\n";
489     }
490 };
491
492 // ------------------------------------------------------------------------
493 // The "converter" class.
494 // ------------------------------------------------------------------------
495
496 //!
497 //! \brief A reader that redirects events to multiple writers.
498 //!
499 //! The converter class implements an atf_tps_reader that, for each event
500 //! raised by the parser, redirects it to multiple writers so that they
501 //! can reformat it according to their output rules.
502 //!
503 class converter : public atf::atf_report::atf_tps_reader {
504     typedef std::vector< writer* > outs_vector;
505     outs_vector m_outs;
506
507     void
508     got_info(const std::string& what, const std::string& val)
509     {
510         for (outs_vector::iterator iter = m_outs.begin();
511              iter != m_outs.end(); iter++)
512             (*iter)->write_info(what, val);
513     }
514
515     void
516     got_ntps(size_t ntps)
517     {
518         for (outs_vector::iterator iter = m_outs.begin();
519              iter != m_outs.end(); iter++)
520             (*iter)->write_ntps(ntps);
521     }
522
523     void
524     got_tp_start(const std::string& tp, size_t ntcs)
525     {
526         for (outs_vector::iterator iter = m_outs.begin();
527              iter != m_outs.end(); iter++)
528             (*iter)->write_tp_start(tp, ntcs);
529     }
530
531     void
532     got_tp_end(struct timeval* tv, const std::string& reason)
533     {
534         for (outs_vector::iterator iter = m_outs.begin();
535              iter != m_outs.end(); iter++)
536             (*iter)->write_tp_end(tv, reason);
537     }
538
539     void
540     got_tc_start(const std::string& tcname)
541     {
542         for (outs_vector::iterator iter = m_outs.begin();
543              iter != m_outs.end(); iter++)
544             (*iter)->write_tc_start(tcname);
545     }
546
547     void
548     got_tc_stdout_line(const std::string& line)
549     {
550         for (outs_vector::iterator iter = m_outs.begin();
551              iter != m_outs.end(); iter++)
552             (*iter)->write_tc_stdout_line(line);
553     }
554
555     void
556     got_tc_stderr_line(const std::string& line)
557     {
558         for (outs_vector::iterator iter = m_outs.begin();
559              iter != m_outs.end(); iter++)
560             (*iter)->write_tc_stderr_line(line);
561     }
562
563     void
564     got_tc_end(const std::string& state, struct timeval* tv,
565                const std::string& reason)
566     {
567         for (outs_vector::iterator iter = m_outs.begin();
568              iter != m_outs.end(); iter++)
569             (*iter)->write_tc_end(state, tv, reason);
570     }
571
572     void
573     got_eof(void)
574     {
575         for (outs_vector::iterator iter = m_outs.begin();
576              iter != m_outs.end(); iter++)
577             (*iter)->write_eof();
578     }
579
580 public:
581     converter(std::istream& is) :
582         atf::atf_report::atf_tps_reader(is)
583     {
584     }
585
586     ~converter(void)
587     {
588         for (outs_vector::iterator iter = m_outs.begin();
589              iter != m_outs.end(); iter++)
590             delete *iter;
591     }
592
593     void
594     add_output(const std::string& fmt, const atf::fs::path& p)
595     {
596         if (fmt == "csv") {
597             m_outs.push_back(new csv_writer(p));
598         } else if (fmt == "ticker") {
599             m_outs.push_back(new ticker_writer(p));
600         } else if (fmt == "xml") {
601             m_outs.push_back(new xml_writer(p));
602         } else
603             throw std::runtime_error("Unknown format `" + fmt + "'");
604     }
605 };
606
607 // ------------------------------------------------------------------------
608 // The "atf_report" class.
609 // ------------------------------------------------------------------------
610
611 class atf_report : public atf::application::app {
612     static const char* m_description;
613
614     typedef std::pair< std::string, atf::fs::path > fmt_path_pair;
615     std::vector< fmt_path_pair > m_oflags;
616
617     void process_option(int, const char*);
618     options_set specific_options(void) const;
619
620 public:
621     atf_report(void);
622
623     int main(void);
624 };
625
626 const char* atf_report::m_description =
627     "atf-report is a tool that parses the output of atf-run and "
628     "generates user-friendly reports in multiple different formats.";
629
630 atf_report::atf_report(void) :
631     app(m_description, "atf-report(1)", "atf(7)")
632 {
633 }
634
635 void
636 atf_report::process_option(int ch, const char* arg)
637 {
638     switch (ch) {
639     case 'o':
640         {
641             std::string str(arg);
642             std::string::size_type pos = str.find(':');
643             if (pos == std::string::npos)
644                 throw std::runtime_error("Syntax error in -o option");
645             else {
646                 std::string fmt = str.substr(0, pos);
647                 atf::fs::path path = atf::fs::path(str.substr(pos + 1));
648                 m_oflags.push_back(fmt_path_pair(fmt, path));
649             }
650         }
651         break;
652
653     default:
654         UNREACHABLE;
655     }
656 }
657
658 atf_report::options_set
659 atf_report::specific_options(void)
660     const
661 {
662     using atf::application::option;
663     options_set opts;
664     opts.insert(option('o', "fmt:path", "Adds a new output file; multiple "
665                                         "ones can be specified, and a - "
666                                         "path means stdout"));
667     return opts;
668 }
669
670 int
671 atf_report::main(void)
672 {
673     if (m_argc > 0)
674         throw std::runtime_error("No arguments allowed");
675
676     if (m_oflags.empty())
677         m_oflags.push_back(fmt_path_pair("ticker", atf::fs::path("-")));
678
679     // Look for path duplicates.
680     std::set< atf::fs::path > paths;
681     for (std::vector< fmt_path_pair >::const_iterator iter = m_oflags.begin();
682          iter != m_oflags.end(); iter++) {
683         atf::fs::path p = (*iter).second;
684         if (p == atf::fs::path("/dev/stdout"))
685             p = atf::fs::path("-");
686         if (paths.find(p) != paths.end())
687             throw std::runtime_error("The file `" + p.str() + "' was "
688                                      "specified more than once");
689         paths.insert((*iter).second);
690     }
691
692     // Generate the output files.
693     converter cnv(std::cin);
694     for (std::vector< fmt_path_pair >::const_iterator iter = m_oflags.begin();
695          iter != m_oflags.end(); iter++)
696         cnv.add_output((*iter).first, (*iter).second);
697     cnv.read();
698
699     return EXIT_SUCCESS;
700 }
701
702 int
703 main(int argc, char* const* argv)
704 {
705     return atf_report().run(argc, argv);
706 }