Blame opae-libs/tests/pyopae/test_pyopae.cpp

Packit 534379
// Copyright(c) 2017-2018, Intel Corporation
Packit 534379
//
Packit 534379
// Redistribution  and  use  in source  and  binary  forms,  with  or  without
Packit 534379
// modification, are permitted provided that the following conditions are met:
Packit 534379
//
Packit 534379
// * Redistributions of  source code  must retain the  above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer.
Packit 534379
// * Redistributions in binary form must reproduce the above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer in the documentation
Packit 534379
//   and/or other materials provided with the distribution.
Packit 534379
// * Neither the name  of Intel Corporation  nor the names of its contributors
Packit 534379
//   may be used to  endorse or promote  products derived  from this  software
Packit 534379
//   without specific prior written permission.
Packit 534379
//
Packit 534379
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 534379
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO,  THE
Packit 534379
// IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 534379
// ARE DISCLAIMED.  IN NO EVENT  SHALL THE COPYRIGHT OWNER  OR CONTRIBUTORS BE
Packit 534379
// LIABLE  FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
Packit 534379
// CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT  OF
Packit 534379
// SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
Packit 534379
// INTERRUPTION)  HOWEVER CAUSED  AND ON ANY THEORY  OF LIABILITY,  WHETHER IN
Packit 534379
// CONTRACT,  STRICT LIABILITY,  OR TORT  (INCLUDING NEGLIGENCE  OR OTHERWISE)
Packit 534379
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
Packit 534379
// POSSIBILITY OF SUCH DAMAGE.
Packit 534379
#include <Python.h>
Packit 534379
Packit 534379
#include <pybind11/embed.h>
Packit 534379
#include <pybind11/pybind11.h>
Packit 534379
#include <pybind11/stl.h>
Packit 534379
#include <fstream>
Packit 534379
#include <iostream>
Packit 534379
#include "mock/test_system.h"
Packit 534379
#include "platform/fpga_hw.h"
Packit 534379
Packit 534379
namespace py = pybind11;
Packit 534379
using namespace opae::testing;
Packit 534379
Packit 534379
PYBIND11_EMBEDDED_MODULE(mopae, m) {
Packit 534379
  m.doc() = "Open Programmable Acceleration Engine";
Packit 534379
}
Packit 534379
Packit 534379
PYBIND11_EMBEDDED_MODULE(mockopae, m) {
Packit 534379
  py::class_<test_platform> pytp(m, "test_platform");
Packit 534379
  pytp.def_static("platforms", &test_platform::platforms)
Packit 534379
      .def_static("get", &test_platform::get)
Packit 534379
      .def_static("exists", &test_platform::exists)
Packit 534379
      .def("is_mock", [](test_platform &p) { return p.mock_sysfs != nullptr; })
Packit 534379
      .def_property_readonly("devices",
Packit 534379
                             [](test_platform &p) { return p.devices; });
Packit 534379
  ;
Packit 534379
Packit 534379
  py::class_<test_device> pytd(m, "test_device");
Packit 534379
  pytd.def_property_readonly("afu_guid",
Packit 534379
                             [](test_device &td) { return td.afu_guid; })
Packit 534379
      .def_property_readonly("fme_guid",
Packit 534379
                             [](test_device &td) { return td.fme_guid; });
Packit 534379
  py::class_<test_system> pyts(m, "test_system");
Packit 534379
Packit 534379
  pyts.def(py::init(&test_system::instance))
Packit 534379
      .def("initialize", &test_system::initialize)
Packit 534379
      .def("finalize", &test_system::finalize)
Packit 534379
      .def("prepare_sysfs", &test_system::prepare_syfs)
Packit 534379
      .def("remove_sysfs", &test_system::remove_sysfs);
Packit 534379
}
Packit 534379
Packit 534379
int run_unittest(const char *testpy, py::module pymain) {
Packit 534379
  auto globals = py::globals();
Packit 534379
  auto mock = py::module::import("mockopae");
Packit 534379
  auto unit = py::module::import("unittest");
Packit 534379
  auto scope = py::dict(pymain.attr("__dict__"));
Packit 534379
  globals["mockopae"] = mock;
Packit 534379
  globals["unittest"] = unit;
Packit 534379
  try {
Packit 534379
    py::eval_file(testpy, scope);
Packit 534379
    auto suite = unit.attr("TestLoader")().attr("loadTestsFromModule")(pymain);
Packit 534379
    py::dict kwargs;
Packit 534379
    kwargs["verbosity"] = 2;
Packit 534379
    auto runner = unit.attr("TextTestRunner")(**kwargs);
Packit 534379
    auto result = runner.attr("run")(suite);
Packit 534379
    return result.attr("wasSuccessful")().cast<bool>() ? 0 : 1;
Packit 534379
  } catch (py::error_already_set &ex) {
Packit 534379
    test_system::instance()->finalize();
Packit 534379
    std::cerr << "error executing: " << testpy << " - " << ex.what() << "\n";
Packit 534379
    ex.restore();
Packit 534379
  }
Packit 534379
  return EXIT_FAILURE;
Packit 534379
}
Packit 534379
Packit 534379
int main(int argc, char *argv[]) {
Packit 534379
  py::scoped_interpreter guard{};
Packit 534379
  auto locals = py::dict();
Packit 534379
  auto globals = py::globals();
Packit 534379
  auto mopae = py::module::import("mopae");
Packit 534379
  auto _opae = py::module::import("_opae");
Packit 534379
  mopae.attr("fpga") = _opae;
Packit 534379
  globals["opae"] = mopae;
Packit 534379
  if (argc > 1) {
Packit 534379
    auto pymain = py::module::import("__main__");
Packit 534379
    if (argc > 2 && std::string(argv[1]) == "test") {
Packit 534379
      return run_unittest(argv[2], pymain);
Packit 534379
    }
Packit 534379
    py::list pyargv;
Packit 534379
    auto sys = py::module::import("sys");
Packit 534379
    for (int i = 1; i < argc; ++i) {
Packit 534379
      pyargv.append(argv[i]);
Packit 534379
    }
Packit 534379
    sys.attr("argv") = pyargv;
Packit 534379
    try {
Packit 534379
      py::eval_file(argv[1], pymain.attr("__dict__"));
Packit 534379
    } catch (py::error_already_set &pyerr) {
Packit 534379
      if (!pyerr.matches(PyExc_SystemExit)) {
Packit 534379
        pyerr.restore();
Packit 534379
      }
Packit 534379
    }
Packit 534379
  }
Packit 534379
  return 0;
Packit 534379
}