Path: blob/main/contrib/kyua/drivers/list_tests_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/list_tests.hpp"2930extern "C" {31#include <sys/stat.h>3233#include <unistd.h>34}3536#include <map>37#include <set>38#include <string>3940#include <atf-c++.hpp>4142#include "cli/cmd_list.hpp"43#include "cli/common.ipp"44#include "engine/atf.hpp"45#include "engine/config.hpp"46#include "engine/exceptions.hpp"47#include "engine/filters.hpp"48#include "engine/scheduler.hpp"49#include "model/metadata.hpp"50#include "model/test_case.hpp"51#include "model/test_program.hpp"52#include "utils/config/tree.ipp"53#include "utils/env.hpp"54#include "utils/format/macros.hpp"55#include "utils/optional.ipp"56#include "utils/test_utils.ipp"5758namespace config = utils::config;59namespace fs = utils::fs;60namespace scheduler = engine::scheduler;6162using utils::none;63using utils::optional;646566namespace {676869/// Gets the path to the helpers for this test program.70///71/// \param test_case A pointer to the currently running test case.72///73/// \return The path to the helpers binary.74static fs::path75helpers(const atf::tests::tc* test_case)76{77return fs::path(test_case->get_config_var("srcdir")) /78"list_tests_helpers";79}808182/// Hooks to capture the incremental listing of test cases.83class capture_hooks : public drivers::list_tests::base_hooks {84public:85/// Set of the listed test cases in a program:test_case form.86std::set< std::string > test_cases;8788/// Set of the listed test cases in a program:test_case form.89std::map< std::string, model::metadata > metadatas;9091/// Called when a test case is identified in a test suite.92///93/// \param test_program The test program containing the test case.94/// \param test_case_name The name of the located test case.95virtual void96got_test_case(const model::test_program& test_program,97const std::string& test_case_name)98{99const std::string ident = F("%s:%s") %100test_program.relative_path() % test_case_name;101test_cases.insert(ident);102103metadatas.insert(std::map< std::string, model::metadata >::value_type(104ident, test_program.find(test_case_name).get_metadata()));105}106};107108109/// Creates a mock test suite.110///111/// \param tc Pointer to the caller test case; needed to obtain the srcdir112/// variable of the caller.113/// \param source_root Basename of the directory that will contain the114/// Kyuafiles.115/// \param build_root Basename of the directory that will contain the test116/// programs. May or may not be the same as source_root.117static void118create_helpers(const atf::tests::tc* tc, const fs::path& source_root,119const fs::path& build_root)120{121ATF_REQUIRE(::mkdir(source_root.c_str(), 0755) != -1);122ATF_REQUIRE(::mkdir((source_root / "dir").c_str(), 0755) != -1);123if (source_root != build_root) {124ATF_REQUIRE(::mkdir(build_root.c_str(), 0755) != -1);125ATF_REQUIRE(::mkdir((build_root / "dir").c_str(), 0755) != -1);126}127ATF_REQUIRE(::symlink(helpers(tc).c_str(),128(build_root / "dir/program").c_str()) != -1);129130atf::utils::create_file(131(source_root / "Kyuafile").str(),132"syntax(2)\n"133"include('dir/Kyuafile')\n");134135atf::utils::create_file(136(source_root / "dir/Kyuafile").str(),137"syntax(2)\n"138"atf_test_program{name='program', test_suite='suite-name'}\n");139}140141142/// Runs the mock test suite.143///144/// \param source_root Path to the directory that contains the Kyuafiles.145/// \param build_root If not none, path to the directory that contains the test146/// programs.147/// \param hooks The hooks to use during the listing.148/// \param filter_program If not null, the filter on the test program name.149/// \param filter_test_case If not null, the filter on the test case name.150/// \param the_variable If not null, the value to pass to the test program as151/// its "the-variable" configuration property.152///153/// \return The result data of the driver.154static drivers::list_tests::result155run_helpers(const fs::path& source_root,156const optional< fs::path > build_root,157drivers::list_tests::base_hooks& hooks,158const char* filter_program = NULL,159const char* filter_test_case = NULL,160const char* the_variable = NULL)161{162std::set< engine::test_filter > filters;163if (filter_program != NULL && filter_test_case != NULL)164filters.insert(engine::test_filter(fs::path(filter_program),165filter_test_case));166167config::tree user_config = engine::empty_config();168if (the_variable != NULL) {169user_config.set_string("test_suites.suite-name.the-variable",170the_variable);171}172173return drivers::list_tests::drive(source_root / "Kyuafile", build_root,174filters, user_config, hooks);175}176177178} // anonymous namespace179180181ATF_TEST_CASE_WITHOUT_HEAD(one_test_case);182ATF_TEST_CASE_BODY(one_test_case)183{184utils::setenv("TESTS", "some_properties");185capture_hooks hooks;186create_helpers(this, fs::path("root"), fs::path("root"));187run_helpers(fs::path("root"), none, hooks);188189std::set< std::string > exp_test_cases;190exp_test_cases.insert("dir/program:some_properties");191ATF_REQUIRE(exp_test_cases == hooks.test_cases);192}193194195ATF_TEST_CASE_WITHOUT_HEAD(many_test_cases);196ATF_TEST_CASE_BODY(many_test_cases)197{198utils::setenv("TESTS", "no_properties some_properties");199capture_hooks hooks;200create_helpers(this, fs::path("root"), fs::path("root"));201run_helpers(fs::path("root"), none, hooks);202203std::set< std::string > exp_test_cases;204exp_test_cases.insert("dir/program:no_properties");205exp_test_cases.insert("dir/program:some_properties");206ATF_REQUIRE(exp_test_cases == hooks.test_cases);207}208209210ATF_TEST_CASE_WITHOUT_HEAD(filter_match);211ATF_TEST_CASE_BODY(filter_match)212{213utils::setenv("TESTS", "no_properties some_properties");214capture_hooks hooks;215create_helpers(this, fs::path("root"), fs::path("root"));216run_helpers(fs::path("root"), none, hooks, "dir/program",217"some_properties");218219std::set< std::string > exp_test_cases;220exp_test_cases.insert("dir/program:some_properties");221ATF_REQUIRE(exp_test_cases == hooks.test_cases);222}223224225ATF_TEST_CASE_WITHOUT_HEAD(build_root);226ATF_TEST_CASE_BODY(build_root)227{228utils::setenv("TESTS", "no_properties some_properties");229capture_hooks hooks;230create_helpers(this, fs::path("source"), fs::path("build"));231run_helpers(fs::path("source"), utils::make_optional(fs::path("build")),232hooks);233234std::set< std::string > exp_test_cases;235exp_test_cases.insert("dir/program:no_properties");236exp_test_cases.insert("dir/program:some_properties");237ATF_REQUIRE(exp_test_cases == hooks.test_cases);238}239240241ATF_TEST_CASE_WITHOUT_HEAD(config_in_head);242ATF_TEST_CASE_BODY(config_in_head)243{244utils::setenv("TESTS", "config_in_head");245capture_hooks hooks;246create_helpers(this, fs::path("source"), fs::path("build"));247run_helpers(fs::path("source"), utils::make_optional(fs::path("build")),248hooks, NULL, NULL, "magic value");249250std::set< std::string > exp_test_cases;251exp_test_cases.insert("dir/program:config_in_head");252ATF_REQUIRE(exp_test_cases == hooks.test_cases);253254const model::metadata& metadata = hooks.metadatas.find(255"dir/program:config_in_head")->second;256ATF_REQUIRE_EQ("the-variable is magic value", metadata.description());257}258259260ATF_TEST_CASE_WITHOUT_HEAD(crash);261ATF_TEST_CASE_BODY(crash)262{263utils::setenv("TESTS", "crash_list some_properties");264capture_hooks hooks;265create_helpers(this, fs::path("root"), fs::path("root"));266run_helpers(fs::path("root"), none, hooks, "dir/program");267268std::set< std::string > exp_test_cases;269exp_test_cases.insert("dir/program:__test_cases_list__");270ATF_REQUIRE(exp_test_cases == hooks.test_cases);271}272273274ATF_INIT_TEST_CASES(tcs)275{276scheduler::register_interface(277"atf", std::shared_ptr< scheduler::interface >(278new engine::atf_interface()));279280ATF_ADD_TEST_CASE(tcs, one_test_case);281ATF_ADD_TEST_CASE(tcs, many_test_cases);282ATF_ADD_TEST_CASE(tcs, filter_match);283ATF_ADD_TEST_CASE(tcs, build_root);284ATF_ADD_TEST_CASE(tcs, config_in_head);285ATF_ADD_TEST_CASE(tcs, crash);286}287288289