From ce633c3d974898ed14489879951af4c7564f1590 Mon Sep 17 00:00:00 2001 From: goei <goei@astron.nl> Date: Tue, 17 Mar 2020 13:19:24 +0100 Subject: [PATCH] TMSS-156: Add base for unittest for TMSSBridge class --- MAC/APL/MainCU/docker/MACBuildDockerfile | 36 +++++ .../MainCU/src/MACScheduler/CMakeLists.txt | 4 +- .../MainCU/src/MACScheduler/MACScheduler.h | 2 +- MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc | 77 +++++---- MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h | 51 +++--- .../src/MACScheduler/test/CMakeLists.txt | 11 ++ .../src/MACScheduler/test/tTMSSBridge.cc | 77 +++++++++ .../src/MACScheduler/test/test_response.json | 153 ++++++++++++++++++ 8 files changed, 358 insertions(+), 53 deletions(-) create mode 100644 MAC/APL/MainCU/docker/MACBuildDockerfile create mode 100644 MAC/APL/MainCU/src/MACScheduler/test/CMakeLists.txt create mode 100644 MAC/APL/MainCU/src/MACScheduler/test/tTMSSBridge.cc create mode 100644 MAC/APL/MainCU/src/MACScheduler/test/test_response.json diff --git a/MAC/APL/MainCU/docker/MACBuildDockerfile b/MAC/APL/MainCU/docker/MACBuildDockerfile new file mode 100644 index 00000000000..f520be4da1b --- /dev/null +++ b/MAC/APL/MainCU/docker/MACBuildDockerfile @@ -0,0 +1,36 @@ +# +# Goal: this dockerfile provides a 'production'-like centos7 system which can be used for building lofar +# MAC MainCU software +# Should be (almost) equivalent to buildhost lcs157 +# +FROM centos:centos7.6.1810 AS builder + +USER root +RUN yum -y groupinstall 'Development Tools' && \ + yum -y install epel-release && \ + yum -y install cmake log4cplus-devel python3 python3-devel python3-pip + +RUN yum install -y cmake gcc-c++ make log4cplus log4cplus-devel python3 python3-libs python3-devel python3-pip \ + boost readline-devel boost-devel binutils-devel boost-python36 boost-python36-devel \ + gettext which openldap-devel npm nodejs git java-11-openjdk + +RUN yum -y install readline-devel boost-python36-devel hdf5-devel blas-devel lapack-devel cfitsio-devel wcslib-devel \ + autogen postgresql-devel cmake3 libpqxx-devel qpid-cpp-server qpid-cpp-client-devel qpid-tools unittest-cpp-devel && \ + pip3 install psycopg2 testing.postgresql lxml mock numpy kombu requests python-dateutil fabric + +RUN echo "Installing Casacore..." && \ + git clone https://github.com/casacore/casacore && \ + mkdir /casacore/build/ && \ + cd /casacore/build/ && \ + cmake -DCMAKE_INSTALL_PREFIX=/opt/casacore -DBUILD_PYTHON3=ON -DBUILD_PYTHON=OFF -DPYTHON_EXECUTABLE=/usr/bin/python3 -DUSE_OPENMP=ON -DUSE_FFTW3=TRUE -DUSE_HDF5=ON -DCMAKE_BUILD_TYPE=Release .. && \ + make && \ + make install + +#RUN echo "Installing Blitz++" && \ +# cd / +# git clone --depth 1 https://github.com/blitzpp/blitz.git && \ +# mkdir -p /blitz/build && \ +# cd /blitz/build && \ +# cmake3 --prefix=/opt/blitz/ .. && \ +# make lib && \ +# make install diff --git a/MAC/APL/MainCU/src/MACScheduler/CMakeLists.txt b/MAC/APL/MainCU/src/MACScheduler/CMakeLists.txt index 146df0f381e..cb416a299ea 100644 --- a/MAC/APL/MainCU/src/MACScheduler/CMakeLists.txt +++ b/MAC/APL/MainCU/src/MACScheduler/CMakeLists.txt @@ -9,7 +9,7 @@ lofar_add_bin_program(MACScheduler lofar_add_bin_program(claimTest claimTest.cc ObsClaimer.cc) - + configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/MACScheduler.conf.in ${CMAKE_CURRENT_BINARY_DIR}/MACScheduler.conf) @@ -17,3 +17,5 @@ configure_file( install(FILES ${CMAKE_CURRENT_BINARY_DIR}/MACScheduler.conf DESTINATION etc) + +add_subdirectory(test) diff --git a/MAC/APL/MainCU/src/MACScheduler/MACScheduler.h b/MAC/APL/MainCU/src/MACScheduler/MACScheduler.h index 33702c4900f..3929e927b18 100644 --- a/MAC/APL/MainCU/src/MACScheduler/MACScheduler.h +++ b/MAC/APL/MainCU/src/MACScheduler/MACScheduler.h @@ -170,7 +170,7 @@ private: OTDB::OTDBconnection* itsOTDBconnection; // connection to the database // TMSS Bridge - TMSSBridge::TMSS* itsTMSSconnection; // connection to TMSS + TMSSBridge* itsTMSSconnection; // connection to TMSS // Cluster to exclude for pipelines. Key is used in the getTreeGroup stored-procedure in OTDB. string itsExclPLcluster; // like !CEP2 or !CEP4 diff --git a/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc b/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc index cfeb5426c7e..b7af4592cce 100644 --- a/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc +++ b/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.cc @@ -1,24 +1,24 @@ -//# TMSSBridge.cc: Implementation of the TMSS Bridge, interface between MAC Scheduler and TMSS -//# -//# Copyright (C) 2004-2012 -//# ASTRON (Netherlands Foundation for Research in Astronomy) -//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, softwaresupport@astron.nl -//# -//# This program is free software; you can redistribute it and/or modify -//# it under the terms of the GNU General Public License as published by -//# the Free Software Foundation; either version 2 of the License, or -//# (at your option) any later version. -//# -//# This program is distributed in the hope that it will be useful, -//# but WITHOUT ANY WARRANTY; without even the implied warranty of -//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -//# GNU General Public License for more details. -//# -//# You should have received a copy of the GNU General Public License -//# along with this program; if not, write to the Free Software -//# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -//# -//# $Id$ +// TMSSBridge.cc: Implementation of the TMSS Bridge, interface between MAC Scheduler and TMSS +// +// Copyright (C) 2020 +// ASTRON (Netherlands Foundation for Research in Astronomy) +// P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, softwaresupport@astron.nl +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// $Id$ #include "TMSSBridge.h" @@ -36,6 +36,9 @@ namespace beast = boost::beast; // from <boost/beast.hpp> namespace http = beast::http; // from <boost/beast/http.hpp> namespace net = boost::asio; // from <boost/asio.hpp> using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp> +namespace pt = boost::property_tree; +using namespace std; + namespace LOFAR { namespace MainCU { @@ -70,19 +73,35 @@ vector<int> TMSSBridge::getSubTaskIDStartingInThreeMinutes(ptime currentTime, st { currentTimeStr = std::string to_iso_extended_string(currentTime); queryStr = "/api/?start_time__lt=" + currentTimeStr + "&cluster__name==" + clusterName; - result = httpGET(itsHost, itsPort, queryStr); + string result = httpGET(itsHost, itsPort, queryStr); + vector<string> urlList = translateHttpResultToSortedUrlList(result); - // How to handle json structure in C++ - // Need to check respons status code of http (200)? - // Get the number of taskIDs like python - // json_response = response.json() - // json_response.get('count')) - // ... and results[idx].items('url) +} -} +// +// Translate the HTTP result to json format and parse the json format +// How to handle json structure in C++? +// Need to check respons status code of http (200)? +// Get the number of taskIDs like python +// json_response = response.json() +// json_response.get('count')) +// ... and results[idx].items('url) +// if multiple found sort on startTime ! +vector<string> urlList = translateHttpResultToSortedUrlList(string result); +{ + // Create a root + pt::ptree root; + // Load json + pt::read_json(result, root); + // Read values + int nbrSubTasksFound = root.get<int>("count", 0); + + string msg = root.get<string>("some.complex.path"); + +} // diff --git a/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h b/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h index d3c72b5c9b8..5db40c10a01 100644 --- a/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h +++ b/MAC/APL/MainCU/src/MACScheduler/TMSSBridge.h @@ -1,30 +1,33 @@ -//# TMSSBridge.h: Implementation of the TMSS Bridge, interface between MAC Scheduler and TMSS -//# -//# Copyright (C) 2008 -//# ASTRON (Netherlands Foundation for Research in Astronomy) -//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, softwaresupport@astron.nl -//# -//# This program is free software; you can redistribute it and/or modify -//# it under the terms of the GNU General Public License as published by -//# the Free Software Foundation; either version 2 of the License, or -//# (at your option) any later version. -//# -//# This program is distributed in the hope that it will be useful, -//# but WITHOUT ANY WARRANTY; without even the implied warranty of -//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -//# GNU General Public License for more details. -//# -//# You should have received a copy of the GNU General Public License -//# along with this program; if not, write to the Free Software -//# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -//# -//# $Id$ +// TMSSBridge.h: Implementation of the TMSS Bridge, interface between MAC Scheduler and TMSS +// +// Copyright (C) 2020 +// ASTRON (Netherlands Foundation for Research in Astronomy) +// P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, softwaresupport@astron.nl +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// $Id$ #ifndef TMSSBRIDGE_H #define TMSSBRIDGE_H namespace LOFAR { - namespace MainCU + namespace MainCU { + +using namespace boost::posix_time::ptime; +using namespace std; class TMSSBridge @@ -35,6 +38,10 @@ public: ~TMSSBridge (); vector<int> getSubTaskIDStartingInThreeMinutes(ptime currentTime, string clusterName); + + // Actually the next method is private, make it public to be able to use in UnitTest++ + vector<string> urlList = translateHttpResultToSortedUrlList(string result); + private: // Copying is not allowed TMSSBridge(const TMSSBridge&); diff --git a/MAC/APL/MainCU/src/MACScheduler/test/CMakeLists.txt b/MAC/APL/MainCU/src/MACScheduler/test/CMakeLists.txt new file mode 100644 index 00000000000..a510830b2a1 --- /dev/null +++ b/MAC/APL/MainCU/src/MACScheduler/test/CMakeLists.txt @@ -0,0 +1,11 @@ +include(LofarCTest) + +lofar_find_package(UnitTest++) + +if(UNITTEST++_FOUND) + lofar_add_test(tTMSSBridge tTMSSBridge.cc) +else() + message("WARNING UnitTest++ not found. Test tTMSSBridge will not be run!") +endif() + + diff --git a/MAC/APL/MainCU/src/MACScheduler/test/tTMSSBridge.cc b/MAC/APL/MainCU/src/MACScheduler/test/tTMSSBridge.cc new file mode 100644 index 00000000000..8db56c8db07 --- /dev/null +++ b/MAC/APL/MainCU/src/MACScheduler/test/tTMSSBridge.cc @@ -0,0 +1,77 @@ +// tTMSSBridge.cc: UnitTester of the TMSS Bridge, interface between MAC Scheduler and TMSS +// +// Copyright (C) 2020 +// ASTRON (Netherlands Foundation for Research in Astronomy) +// P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, softwaresupport@astron.nl +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// $Id$ +#include "UnitTest++/UnitTest++.h" + +// The Subject to Test +#include "../src/TMSSBridge.h" + + +#include <boost/property_tree/ptree.hpp> +#include <boost/property_tree/json_parser.hpp> + + +namespace pt = boost::property_tree; +using namespace std; + +using namespace boost::property_tree; + + +TEST(tmss_translate_http_result_ok) +{ + // check if json read goes well as precondition for the test + ifstream::jsonFile("test_resonse.json"); + if (!jsonFile){ + std::cerr << "Error opening file\n"; + return -1; + } + ptree jsontree; + read_json(jsonFile, jsontree); + + int v0 = jsontree.get<int>("count"); + // check if count is equal to 4 .... otherwise precond test did not match so will make no sense to continue + + TMSSBridge testTMSSBridge = TMSSBridge(); + + + + + // Read file jsson + testResult = testTMSSBridge.translateHttpResultToSortedUrlList(string result); + + // Do some check like CHECK_EQUAL +} + + +TEST(tmss_translate_http_empty_result) +{ + TMSSBridge testTMSSBridge = TMSSBridge(); + + // Read file jsson + testResult = testTMSSBridge.translateHttpResultToSortedUrlList(string result); + + // Do some check like CHECK_EQUAL +} + +int main(int, const char *[]) +{ + return UnitTest::RunAllTests(); +} diff --git a/MAC/APL/MainCU/src/MACScheduler/test/test_response.json b/MAC/APL/MainCU/src/MACScheduler/test/test_response.json new file mode 100644 index 00000000000..dc6e191f40d --- /dev/null +++ b/MAC/APL/MainCU/src/MACScheduler/test/test_response.json @@ -0,0 +1,153 @@ +{ + "count": 4, + "next": null, + "previous": null, + "results": [ + { + "url": "http://127.0.0.1:8000/api/subtask/2000000/", + "tags": [ + "TMSS", + "TESTING" + ], + "created_at": "2020-03-17T07:40:03.631388", + "updated_at": "2020-03-17T07:40:03.631405", + "start_time": "2020-03-17T07:40:03.623354", + "stop_time": "2020-03-17T07:40:03.623355", + "specifications_doc": { + "COBALT": { + "blocksize": 196608, + "correlator": { + "enabled": true, + "phase_centers": [ + { + "index": 0, + "pointing": { + "angle1": 0, + "angle2": 0, + "direction_type": "J2000" + } + } + ], + "channels_per_subband": 64, + "blocks_per_integration": 1, + "integrations_per_block": 1 + }, + "delay_compensation": true, + "bandpass_correction": true + }, + "stations": { + "filter": "HBA_110_190", + "antenna_set": "HBA_DUAL", + "station_list": [ + "CS001", + "CS002" + ], + "analog_pointing": { + "angle1": 45, + "angle2": 20, + "direction_type": "J2000" + }, + "digital_pointings": [ + { + "name": "beam01", + "pointing": { + "angle1": 45, + "angle2": 20, + "direction_type": "J2000" + }, + "subbands": [ + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15 + ] + } + ] + } + }, + "do_cancel": "2020-03-17T07:40:03.625042", + "priority": 1, + "scheduler_input_doc": "{}", + "state": "http://127.0.0.1:8000/api/subtask_state/scheduling/", + "task_blueprint": "http://127.0.0.1:8000/api/task_blueprint/1/", + "specifications_template": "http://127.0.0.1:8000/api/subtask_template/1/", + "schedule_method": "http://127.0.0.1:8000/api/schedule_method/manual/", + "cluster": "http://127.0.0.1:8000/api/cluster/2/" + }, + { + "url": "http://127.0.0.1:8000/api/subtask/2000002/", + "tags": [ + "loaded by fixture", + "TMSS-154", + "manual testing" + ], + "created_at": "2020-02-24T13:19:57", + "updated_at": "2020-02-24T13:19:57", + "start_time": "2020-01-02T00:00:00", + "stop_time": "2020-01-02T12:00:00", + "specifications_doc": 1, + "do_cancel": null, + "priority": 1, + "scheduler_input_doc": 1, + "state": "http://127.0.0.1:8000/api/subtask_state/defined/", + "task_blueprint": null, + "specifications_template": "http://127.0.0.1:8000/api/subtask_template/1/", + "schedule_method": "http://127.0.0.1:8000/api/schedule_method/manual/", + "cluster": "http://127.0.0.1:8000/api/cluster/2/" + }, + { + "url": "http://127.0.0.1:8000/api/subtask/2000003/", + "tags": [ + "loaded by fixture", + "TMSS-154", + "manual testing" + ], + "created_at": "2020-02-24T13:19:57", + "updated_at": "2020-02-24T13:19:57", + "start_time": "2020-01-03T00:00:00", + "stop_time": "2020-01-03T12:00:00", + "specifications_doc": 1, + "do_cancel": null, + "priority": 1, + "scheduler_input_doc": 1, + "state": "http://127.0.0.1:8000/api/subtask_state/defined/", + "task_blueprint": null, + "specifications_template": "http://127.0.0.1:8000/api/subtask_template/1/", + "schedule_method": "http://127.0.0.1:8000/api/schedule_method/manual/", + "cluster": "http://127.0.0.1:8000/api/cluster/3/" + }, + { + "url": "http://127.0.0.1:8000/api/subtask/2000004/", + "tags": [ + "loaded by fixture", + "TMSS-154", + "manual testing" + ], + "created_at": "2020-02-24T13:19:57", + "updated_at": "2020-02-24T13:19:57", + "start_time": "2020-01-04T00:00:00", + "stop_time": "2020-01-04T12:00:00", + "specifications_doc": 1, + "do_cancel": null, + "priority": 1, + "scheduler_input_doc": 1, + "state": "http://127.0.0.1:8000/api/subtask_state/defined/", + "task_blueprint": null, + "specifications_template": "http://127.0.0.1:8000/api/subtask_template/1/", + "schedule_method": "http://127.0.0.1:8000/api/schedule_method/manual/", + "cluster": "http://127.0.0.1:8000/api/cluster/1/" + } + ] +} \ No newline at end of file -- GitLab