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