Blame engine/atf.cpp

Packit 209faa
// Copyright 2014 The Kyua Authors.
Packit 209faa
// All rights reserved.
Packit 209faa
//
Packit 209faa
// Redistribution and use in source and binary forms, with or without
Packit 209faa
// modification, are permitted provided that the following conditions are
Packit 209faa
// met:
Packit 209faa
//
Packit 209faa
// * Redistributions of source code must retain the above copyright
Packit 209faa
//   notice, this list of conditions and the following disclaimer.
Packit 209faa
// * Redistributions in binary form must reproduce the above copyright
Packit 209faa
//   notice, this list of conditions and the following disclaimer in the
Packit 209faa
//   documentation and/or other materials provided with the distribution.
Packit 209faa
// * Neither the name of Google Inc. nor the names of its contributors
Packit 209faa
//   may be used to endorse or promote products derived from this software
Packit 209faa
//   without specific prior written permission.
Packit 209faa
//
Packit 209faa
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 209faa
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 209faa
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 209faa
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 209faa
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 209faa
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 209faa
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 209faa
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 209faa
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 209faa
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 209faa
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 209faa
Packit 209faa
#include "engine/atf.hpp"
Packit 209faa
Packit 209faa
extern "C" {
Packit 209faa
#include <unistd.h>
Packit 209faa
}
Packit 209faa
Packit 209faa
#include <cerrno>
Packit 209faa
#include <cstdlib>
Packit 209faa
#include <fstream>
Packit 209faa
Packit 209faa
#include "engine/atf_list.hpp"
Packit 209faa
#include "engine/atf_result.hpp"
Packit 209faa
#include "engine/exceptions.hpp"
Packit 209faa
#include "model/test_case.hpp"
Packit 209faa
#include "model/test_program.hpp"
Packit 209faa
#include "model/test_result.hpp"
Packit 209faa
#include "utils/defs.hpp"
Packit 209faa
#include "utils/env.hpp"
Packit 209faa
#include "utils/format/macros.hpp"
Packit 209faa
#include "utils/fs/path.hpp"
Packit 209faa
#include "utils/logging/macros.hpp"
Packit 209faa
#include "utils/optional.ipp"
Packit 209faa
#include "utils/process/exceptions.hpp"
Packit 209faa
#include "utils/process/operations.hpp"
Packit 209faa
#include "utils/process/status.hpp"
Packit 209faa
#include "utils/stream.hpp"
Packit 209faa
Packit 209faa
namespace config = utils::config;
Packit 209faa
namespace fs = utils::fs;
Packit 209faa
namespace process = utils::process;
Packit 209faa
Packit 209faa
using utils::optional;
Packit 209faa
Packit 209faa
Packit 209faa
namespace {
Packit 209faa
Packit 209faa
Packit 209faa
/// Basename of the file containing the result written by the ATF test case.
Packit 209faa
static const char* result_name = "result.atf";
Packit 209faa
Packit 209faa
Packit 209faa
/// Magic numbers returned by exec_list when exec(2) fails.
Packit 209faa
enum list_exit_code {
Packit 209faa
    exit_eacces = 90,
Packit 209faa
    exit_enoent,
Packit 209faa
    exit_enoexec,
Packit 209faa
};
Packit 209faa
Packit 209faa
Packit 209faa
}  // anonymous namespace
Packit 209faa
Packit 209faa
Packit 209faa
/// Executes a test program's list operation.
Packit 209faa
///
Packit 209faa
/// This method is intended to be called within a subprocess and is expected
Packit 209faa
/// to terminate execution either by exec(2)ing the test program or by
Packit 209faa
/// exiting with a failure.
Packit 209faa
///
Packit 209faa
/// \param test_program The test program to execute.
Packit 209faa
/// \param vars User-provided variables to pass to the test program.
Packit 209faa
void
Packit 209faa
engine::atf_interface::exec_list(const model::test_program& test_program,
Packit 209faa
                                 const config::properties_map& vars) const
Packit 209faa
{
Packit 209faa
    utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
Packit 209faa
Packit 209faa
    process::args_vector args;
Packit 209faa
    for (config::properties_map::const_iterator iter = vars.begin();
Packit 209faa
         iter != vars.end(); ++iter) {
Packit 209faa
        args.push_back(F("-v%s=%s") % (*iter).first % (*iter).second);
Packit 209faa
    }
Packit 209faa
Packit 209faa
    args.push_back("-l");
Packit 209faa
    try {
Packit 209faa
        process::exec_unsafe(test_program.absolute_path(), args);
Packit 209faa
    } catch (const process::system_error& e) {
Packit 209faa
        if (e.original_errno() == EACCES)
Packit 209faa
            ::_exit(exit_eacces);
Packit 209faa
        else if (e.original_errno() == ENOENT)
Packit 209faa
            ::_exit(exit_enoent);
Packit 209faa
        else if (e.original_errno() == ENOEXEC)
Packit 209faa
            ::_exit(exit_enoexec);
Packit 209faa
        throw;
Packit 209faa
    }
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Computes the test cases list of a test program.
Packit 209faa
///
Packit 209faa
/// \param status The termination status of the subprocess used to execute
Packit 209faa
///     the exec_test() method or none if the test timed out.
Packit 209faa
/// \param stdout_path Path to the file containing the stdout of the test.
Packit 209faa
/// \param stderr_path Path to the file containing the stderr of the test.
Packit 209faa
///
Packit 209faa
/// \return A list of test cases.
Packit 209faa
///
Packit 209faa
/// \throw error If there is a problem parsing the test case list.
Packit 209faa
model::test_cases_map
Packit 209faa
engine::atf_interface::parse_list(const optional< process::status >& status,
Packit 209faa
                                  const fs::path& stdout_path,
Packit 209faa
                                  const fs::path& stderr_path) const
Packit 209faa
{
Packit 209faa
    const std::string stderr_contents = utils::read_file(stderr_path);
Packit 209faa
    if (!stderr_contents.empty())
Packit 209faa
        LW("Test case list wrote to stderr: " + stderr_contents);
Packit 209faa
Packit 209faa
    if (!status)
Packit 209faa
        throw engine::error("Test case list timed out");
Packit 209faa
    if (status.get().exited()) {
Packit 209faa
        const int exitstatus = status.get().exitstatus();
Packit 209faa
        if (exitstatus == EXIT_SUCCESS) {
Packit 209faa
            // Nothing to do; fall through.
Packit 209faa
        } else if (exitstatus == exit_eacces) {
Packit 209faa
            throw engine::error("Permission denied to run test program");
Packit 209faa
        } else if (exitstatus == exit_enoent) {
Packit 209faa
            throw engine::error("Cannot find test program");
Packit 209faa
        } else if (exitstatus == exit_enoexec) {
Packit 209faa
            throw engine::error("Invalid test program format");
Packit 209faa
        } else {
Packit 209faa
            throw engine::error("Test program did not exit cleanly");
Packit 209faa
        }
Packit 209faa
    } else {
Packit 209faa
        throw engine::error("Test program received signal");
Packit 209faa
    }
Packit 209faa
Packit 209faa
    std::ifstream input(stdout_path.c_str());
Packit 209faa
    if (!input)
Packit 209faa
        throw engine::load_error(stdout_path, "Cannot open file for read");
Packit 209faa
    const model::test_cases_map test_cases = parse_atf_list(input);
Packit 209faa
Packit 209faa
    if (!stderr_contents.empty())
Packit 209faa
        throw engine::error("Test case list wrote to stderr");
Packit 209faa
Packit 209faa
    return test_cases;
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Executes a test case of the test program.
Packit 209faa
///
Packit 209faa
/// This method is intended to be called within a subprocess and is expected
Packit 209faa
/// to terminate execution either by exec(2)ing the test program or by
Packit 209faa
/// exiting with a failure.
Packit 209faa
///
Packit 209faa
/// \param test_program The test program to execute.
Packit 209faa
/// \param test_case_name Name of the test case to invoke.
Packit 209faa
/// \param vars User-provided variables to pass to the test program.
Packit 209faa
/// \param control_directory Directory where the interface may place control
Packit 209faa
///     files.
Packit 209faa
void
Packit 209faa
engine::atf_interface::exec_test(const model::test_program& test_program,
Packit 209faa
                                 const std::string& test_case_name,
Packit 209faa
                                 const config::properties_map& vars,
Packit 209faa
                                 const fs::path& control_directory) const
Packit 209faa
{
Packit 209faa
    utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
Packit 209faa
Packit 209faa
    process::args_vector args;
Packit 209faa
    for (config::properties_map::const_iterator iter = vars.begin();
Packit 209faa
         iter != vars.end(); ++iter) {
Packit 209faa
        args.push_back(F("-v%s=%s") % (*iter).first % (*iter).second);
Packit 209faa
    }
Packit 209faa
Packit 209faa
    args.push_back(F("-r%s") % (control_directory / result_name));
Packit 209faa
    args.push_back(test_case_name);
Packit 209faa
    process::exec(test_program.absolute_path(), args);
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Executes a test cleanup routine of the test program.
Packit 209faa
///
Packit 209faa
/// This method is intended to be called within a subprocess and is expected
Packit 209faa
/// to terminate execution either by exec(2)ing the test program or by
Packit 209faa
/// exiting with a failure.
Packit 209faa
///
Packit 209faa
/// \param test_program The test program to execute.
Packit 209faa
/// \param test_case_name Name of the test case to invoke.
Packit 209faa
/// \param vars User-provided variables to pass to the test program.
Packit 209faa
/// \param unused_control_directory Directory where the interface may place
Packit 209faa
///     control files.
Packit 209faa
void
Packit 209faa
engine::atf_interface::exec_cleanup(
Packit 209faa
    const model::test_program& test_program,
Packit 209faa
    const std::string& test_case_name,
Packit 209faa
    const config::properties_map& vars,
Packit 209faa
    const fs::path& UTILS_UNUSED_PARAM(control_directory)) const
Packit 209faa
{
Packit 209faa
    utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
Packit 209faa
Packit 209faa
    process::args_vector args;
Packit 209faa
    for (config::properties_map::const_iterator iter = vars.begin();
Packit 209faa
         iter != vars.end(); ++iter) {
Packit 209faa
        args.push_back(F("-v%s=%s") % (*iter).first % (*iter).second);
Packit 209faa
    }
Packit 209faa
Packit 209faa
    args.push_back(F("%s:cleanup") % test_case_name);
Packit 209faa
    process::exec(test_program.absolute_path(), args);
Packit 209faa
}
Packit 209faa
Packit 209faa
Packit 209faa
/// Computes the result of a test case based on its termination status.
Packit 209faa
///
Packit 209faa
/// \param status The termination status of the subprocess used to execute
Packit 209faa
///     the exec_test() method or none if the test timed out.
Packit 209faa
/// \param control_directory Directory where the interface may have placed
Packit 209faa
///     control files.
Packit 209faa
/// \param unused_stdout_path Path to the file containing the stdout of the
Packit 209faa
///     test.
Packit 209faa
/// \param unused_stderr_path Path to the file containing the stderr of the
Packit 209faa
///     test.
Packit 209faa
///
Packit 209faa
/// \return A test result.
Packit 209faa
model::test_result
Packit 209faa
engine::atf_interface::compute_result(
Packit 209faa
    const optional< process::status >& status,
Packit 209faa
    const fs::path& control_directory,
Packit 209faa
    const fs::path& UTILS_UNUSED_PARAM(stdout_path),
Packit 209faa
    const fs::path& UTILS_UNUSED_PARAM(stderr_path)) const
Packit 209faa
{
Packit 209faa
    return calculate_atf_result(status, control_directory / result_name);
Packit 209faa
}