Path: blob/main/contrib/kyua/drivers/scan_results_test.cpp
39478 views
// Copyright 2011 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "drivers/scan_results.hpp"2930#include <set>3132#include <atf-c++.hpp>3334#include "engine/filters.hpp"35#include "model/context.hpp"36#include "model/metadata.hpp"37#include "model/test_case.hpp"38#include "model/test_program.hpp"39#include "model/test_result.hpp"40#include "store/exceptions.hpp"41#include "store/read_transaction.hpp"42#include "store/write_backend.hpp"43#include "store/write_transaction.hpp"44#include "utils/datetime.hpp"45#include "utils/format/containers.ipp"46#include "utils/format/macros.hpp"47#include "utils/optional.ipp"48#include "utils/sanity.hpp"4950namespace datetime = utils::datetime;51namespace fs = utils::fs;5253using utils::none;54using utils::optional;555657namespace {585960/// Records the callback values for futher investigation.61class capture_hooks : public drivers::scan_results::base_hooks {62public:63/// Whether begin() was called or not.64bool _begin_called;6566/// The captured driver result, if any.67optional< drivers::scan_results::result > _end_result;6869/// The captured context, if any.70optional< model::context > _context;7172/// The captured results, flattened as "program:test_case:result".73std::set< std::string > _results;7475/// Constructor.76capture_hooks(void) :77_begin_called(false)78{79}8081/// Callback executed before any operation is performed.82void83begin(void)84{85_begin_called = true;86}8788/// Callback executed after all operations are performed.89///90/// \param r A structure with all results computed by this driver. Note91/// that this is also returned by the drive operation.92void93end(const drivers::scan_results::result& r)94{95PRE(!_end_result);96_end_result = r;97}9899/// Callback executed when the context is loaded.100///101/// \param context The context loaded from the database.102void got_context(const model::context& context)103{104PRE(!_context);105_context = context;106}107108/// Callback executed when a test results is found.109///110/// \param iter Container for the test result's data.111void got_result(store::results_iterator& iter)112{113const char* type;114switch (iter.result().type()) {115case model::test_result_passed: type = "passed"; break;116case model::test_result_skipped: type = "skipped"; break;117default:118UNREACHABLE_MSG("Formatting unimplemented");119}120const datetime::delta duration = iter.end_time() - iter.start_time();121_results.insert(F("%s:%s:%s:%s:%s:%s") %122iter.test_program()->absolute_path() %123iter.test_case_name() % type % iter.result().reason() %124duration.seconds % duration.useconds);125}126};127128129/// Populates a results file.130///131/// It is not OK to call this function multiple times on the same file.132///133/// \param db_name The database to update.134/// \param count The number of "elements" to insert into the results file.135/// Determines the number of test programs and the number of test cases136/// each has. Can be used to determine from the caller which particular137/// results file has been loaded.138static void139populate_results_file(const char* db_name, const int count)140{141store::write_backend backend = store::write_backend::open_rw(142fs::path(db_name));143144store::write_transaction tx = backend.start_write();145146std::map< std::string, std::string > env;147for (int i = 0; i < count; i++)148env[F("VAR%s") % i] = F("Value %s") % i;149const model::context context(fs::path("/root"), env);150tx.put_context(context);151152for (int i = 0; i < count; i++) {153model::test_program_builder test_program_builder(154"fake", fs::path(F("dir/prog_%s") % i), fs::path("/root"),155F("suite_%s") % i);156for (int j = 0; j < count; j++) {157test_program_builder.add_test_case(F("case_%s") % j);158}159const model::test_program test_program = test_program_builder.build();160const int64_t tp_id = tx.put_test_program(test_program);161162for (int j = 0; j < count; j++) {163const model::test_result result(model::test_result_skipped,164F("Count %s") % j);165const int64_t tc_id = tx.put_test_case(test_program,166F("case_%s") % j, tp_id);167const datetime::timestamp start =168datetime::timestamp::from_microseconds(1000010);169const datetime::timestamp end =170datetime::timestamp::from_microseconds(5000020 + i + j);171tx.put_result(result, tc_id, start, end);172}173}174175tx.commit();176}177178179} // anonymous namespace180181182ATF_TEST_CASE_WITHOUT_HEAD(ok__all);183ATF_TEST_CASE_BODY(ok__all)184{185populate_results_file("test.db", 2);186187capture_hooks hooks;188const drivers::scan_results::result result = drivers::scan_results::drive(189fs::path("test.db"), std::set< engine::test_filter >(), hooks);190ATF_REQUIRE(result.unused_filters.empty());191ATF_REQUIRE(hooks._begin_called);192ATF_REQUIRE(hooks._end_result);193194std::map< std::string, std::string > env;195env["VAR0"] = "Value 0";196env["VAR1"] = "Value 1";197const model::context context(fs::path("/root"), env);198ATF_REQUIRE(context == hooks._context.get());199200std::set< std::string > results;201results.insert("/root/dir/prog_0:case_0:skipped:Count 0:4:10");202results.insert("/root/dir/prog_0:case_1:skipped:Count 1:4:11");203results.insert("/root/dir/prog_1:case_0:skipped:Count 0:4:11");204results.insert("/root/dir/prog_1:case_1:skipped:Count 1:4:12");205ATF_REQUIRE_EQ(results, hooks._results);206}207208209ATF_TEST_CASE_WITHOUT_HEAD(ok__filters);210ATF_TEST_CASE_BODY(ok__filters)211{212populate_results_file("test.db", 3);213214std::set< engine::test_filter > filters;215filters.insert(engine::test_filter(fs::path("dir/prog_1"), ""));216filters.insert(engine::test_filter(fs::path("dir/prog_1"), "no"));217filters.insert(engine::test_filter(fs::path("dir/prog_2"), "case_1"));218filters.insert(engine::test_filter(fs::path("dir/prog_3"), ""));219220capture_hooks hooks;221const drivers::scan_results::result result = drivers::scan_results::drive(222fs::path("test.db"), filters, hooks);223ATF_REQUIRE(hooks._begin_called);224ATF_REQUIRE(hooks._end_result);225226std::set< engine::test_filter > unused_filters;227unused_filters.insert(engine::test_filter(fs::path("dir/prog_1"), "no"));228unused_filters.insert(engine::test_filter(fs::path("dir/prog_3"), ""));229ATF_REQUIRE_EQ(unused_filters, result.unused_filters);230231std::set< std::string > results;232results.insert("/root/dir/prog_1:case_0:skipped:Count 0:4:11");233results.insert("/root/dir/prog_1:case_1:skipped:Count 1:4:12");234results.insert("/root/dir/prog_1:case_2:skipped:Count 2:4:13");235results.insert("/root/dir/prog_2:case_1:skipped:Count 1:4:13");236ATF_REQUIRE_EQ(results, hooks._results);237}238239240ATF_TEST_CASE_WITHOUT_HEAD(missing_db);241ATF_TEST_CASE_BODY(missing_db)242{243capture_hooks hooks;244ATF_REQUIRE_THROW(245store::error,246drivers::scan_results::drive(fs::path("test.db"),247std::set< engine::test_filter >(),248hooks));249}250251252ATF_INIT_TEST_CASES(tcs)253{254ATF_ADD_TEST_CASE(tcs, ok__all);255ATF_ADD_TEST_CASE(tcs, ok__filters);256ATF_ADD_TEST_CASE(tcs, missing_db);257}258259260