Path: blob/main/contrib/kyua/engine/execenv/execenv.hpp
48081 views
// Copyright 2023 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/// \file engine/execenv/execenv.hpp29/// Execution environment subsystem interface.3031#if !defined(ENGINE_EXECENV_EXECENV_HPP)32#define ENGINE_EXECENV_EXECENV_HPP3334#include "model/test_program.hpp"35#include "utils/optional.ipp"36#include "utils/process/operations_fwd.hpp"3738using utils::process::args_vector;39using utils::optional;4041namespace engine {42namespace execenv {434445extern const char* default_execenv_name;464748/// Abstract interface of an execution environment.49class interface {50protected:51const model::test_program& _test_program;52const std::string& _test_case_name;5354public:55/// Constructor.56///57/// \param program The test program.58/// \param test_case_name Name of the test case.59interface(const model::test_program& test_program,60const std::string& test_case_name) :61_test_program(test_program),62_test_case_name(test_case_name)63{}6465/// Destructor.66virtual ~interface() {}6768/// Initializes execution environment.69///70/// It's expected to be called inside a fork which runs71/// scheduler::interface::exec_test(), so we can fail a test fast if its72/// execution environment setup fails, and test execution could use the73/// configured proc environment, if expected.74virtual void init() const = 0;7576/// Cleanups or removes execution environment.77///78/// It's expected to be called inside a fork for execenv cleanup.79virtual void cleanup() const = 0;8081/// Executes a test within the execution environment.82///83/// It's expected to be called inside a fork which runs84/// scheduler::interface::exec_test() or exec_cleanup().85///86/// \param args The arguments to pass to the binary.87virtual void exec(const args_vector& args) const UTILS_NORETURN = 0;88};899091/// Abstract interface of an execution environment manager.92class manager {93public:94/// Destructor.95virtual ~manager() {}9697/// Returns name of an execution environment.98virtual const std::string& name() const = 0;99100/// Returns whether this execution environment is actually supported.101///102/// It can be compile time and/or runtime check.103virtual bool is_supported() const = 0;104105/// Returns execution environment for a test.106///107/// It checks if the given test is designed for this execution environment.108///109/// \param program The test program.110/// \param test_case_name Name of the test case.111///112/// \return An execenv object if the test conforms, or none.113virtual std::unique_ptr< interface > probe(114const model::test_program& test_program,115const std::string& test_case_name) const = 0;116117// TODO: execenv related extra metadata could be provided by a manager118// not to know how exactly and where it should be added to the kyua119};120121122/// Registers an execution environment.123///124/// \param manager Execution environment manager.125void register_execenv(const std::shared_ptr< manager > manager);126127128/// Returns list of registered execenv managers, except default host one.129///130/// \return A vector of pointers to execenv managers.131const std::vector< std::shared_ptr< manager> > execenvs();132133134/// Returns execution environment for a test case.135///136/// \param program The test program.137/// \param test_case_name Name of the test case.138///139/// \return An execution environment of a test.140std::unique_ptr< execenv::interface > get(141const model::test_program& test_program,142const std::string& test_case_name);143144145} // namespace execenv146} // namespace engine147148#endif // !defined(ENGINE_EXECENV_EXECENV_HPP)149150151